-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Satendra kumar
committed
Aug 16, 2015
0 parents
commit bac4788
Showing
17 changed files
with
510 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
project/boot/ | ||
project/plugins/project/ | ||
project/plugins/target/ | ||
project/target/ | ||
target/ | ||
project/boot/* | ||
project/plugins/project/* | ||
project/plugins/target/* | ||
project/target/* | ||
target/* | ||
.idea | ||
.idea/* | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.classpath | ||
.project | ||
.scala_dependencies | ||
.target/ | ||
.cache | ||
logs/* | ||
.settings/ | ||
/bin/ | ||
.cache-main | ||
.cache-tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
name := "slick-for-production" | ||
|
||
scalaVersion := "2.11.7" | ||
|
||
|
||
|
||
libraryDependencies ++= Seq( | ||
"mysql" % "mysql-connector-java" % "5.1.36", | ||
"ch.qos.logback" % "logback-classic" % "1.1.3", | ||
"com.typesafe.slick" %% "slick" % "3.0.1", | ||
"org.scalatest" %% "scalatest" % "2.2.5" % "test", | ||
"com.h2database" % "h2" % "1.4.187" % "test" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=0.13.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "3.0.0" ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
mysql = { | ||
dataSourceClass="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" | ||
properties { | ||
user="root" | ||
password="root" | ||
databaseName="bank_db" | ||
serverName="localhost" | ||
} | ||
numThreads=10 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
|
||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%highlight([%level]) - [%date] - [%logger] %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
<prudent>true</prudent> | ||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | ||
<fileNamePattern>log-%d{yyyy-MM-dd}.log</fileNamePattern> | ||
</rollingPolicy> | ||
<append>true</append> | ||
<encoder> | ||
<pattern>%highlight([%level]) - [%date] - [%logger] %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="info"> | ||
<appender-ref ref="CONSOLE" /> | ||
<appender-ref ref="FILE" /> | ||
</root> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.knol.db.connection | ||
|
||
import scala.slick.driver.JdbcProfile | ||
|
||
trait DBComponent { | ||
|
||
val driver: JdbcProfile | ||
|
||
import driver.api._ | ||
|
||
val db: Database | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/scala/com/knol/db/connection/MySqlDBComponent.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.knol.db.connection | ||
|
||
import slick.driver.MySQLDriver | ||
|
||
trait MySqlDBComponent extends DBComponent { | ||
|
||
val driver = MySQLDriver | ||
|
||
import driver.api._ | ||
|
||
val db: Database = MySqlDB.connectionPool | ||
|
||
} | ||
|
||
private[connection] object MySqlDB { | ||
|
||
import slick.driver.MySQLDriver.api._ | ||
|
||
val connectionPool = Database.forConfig("dbconf") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.knol.db.repo | ||
|
||
import com.knol.db.connection.DBComponent | ||
import scala.concurrent.Future | ||
|
||
trait BankInfoRepository extends BankInfoTable { this: DBComponent => | ||
|
||
import driver.api._ | ||
|
||
def create(bankInfo: BankInfo): Future[Int] = db.run { brandTableInfoAutoInc += bankInfo } | ||
|
||
def update(bankInfo: BankInfo): Future[Int] = db.run { brandInfoTableQuery.filter(_.id === bankInfo.id.get).update(bankInfo) } | ||
|
||
def getById(id: Int): Future[Option[BankInfo]] = db.run { brandInfoTableQuery.filter(_.id === id).result.headOption } | ||
|
||
def getAll(): Future[List[BankInfo]] = db.run { brandInfoTableQuery.to[List].result } | ||
|
||
def delete(id: Int): Future[Int] = db.run { brandInfoTableQuery.filter(_.id === id).delete } | ||
|
||
/** | ||
* Get bank and info using foreign key relationship | ||
*/ | ||
def getBankWithInfo(): Future[List[(Bank, BankInfo)]] = | ||
db.run { | ||
(for { | ||
info <- brandInfoTableQuery | ||
bank <- info.bank | ||
} yield (bank, info)).to[List].result | ||
} | ||
|
||
/** | ||
* Get all bank and their info.It is possible some bank do not have their product | ||
*/ | ||
def getAllBankWithInfo(): Future[List[(Bank, Option[BankInfo])]] = | ||
db.run { | ||
brandTableQuery.joinLeft(brandInfoTableQuery).on(_.id === _.bankId).to[List].result | ||
} | ||
} | ||
|
||
private[repo] trait BankInfoTable extends BankTable { this: DBComponent => | ||
|
||
import driver.api._ | ||
|
||
private[BankInfoTable] class BankInfoTable(tag: Tag) extends Table[BankInfo](tag, "bankinfo") { | ||
val id = column[Int]("id", O.PrimaryKey, O.AutoInc) | ||
val owner = column[String]("owner") | ||
val bankId = column[Int]("bank_id") | ||
val branches = column[Int]("branches") | ||
def bank = foreignKey("bank_product_fk", bankId, brandTableQuery)(_.id) | ||
def * = (owner, branches, bankId, id.?) <> (BankInfo.tupled, BankInfo.unapply) | ||
|
||
} | ||
|
||
protected val brandInfoTableQuery = TableQuery[BankInfoTable] | ||
|
||
protected def brandTableInfoAutoInc = brandInfoTableQuery returning brandInfoTableQuery.map(_.id) | ||
|
||
} | ||
|
||
case class BankInfo(owner: String, branches: Int, bankId: Int, id: Option[Int] = None) |
60 changes: 60 additions & 0 deletions
60
src/main/scala/com/knol/db/repo/BankProductRepository.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.knol.db.repo | ||
|
||
import com.knol.db.connection.DBComponent | ||
import scala.concurrent.Future | ||
|
||
trait BankProductRepository extends BankProductTable { this: DBComponent => | ||
|
||
import driver.api._ | ||
|
||
def create(bankProduct: BankProduct): Future[Int] = db.run { brandProductTableAutoInc += bankProduct } | ||
|
||
def update(bankProduct: BankProduct): Future[Int] = db.run { brandProductTableQuery.filter(_.id === bankProduct.id.get).update(bankProduct) } | ||
|
||
def getById(id: Int): Future[Option[BankProduct]] = db.run { brandProductTableQuery.filter(_.id === id).result.headOption } | ||
|
||
def getAll(): Future[List[BankProduct]] = db.run { brandProductTableQuery.to[List].result } | ||
|
||
def delete(id: Int): Future[Int] = db.run { brandProductTableQuery.filter(_.id === id).delete } | ||
|
||
/** | ||
* Get bank and product using foreign key relationship | ||
*/ | ||
def getBankWithProduct(): Future[List[(Bank, BankProduct)]] = | ||
db.run { | ||
(for { | ||
product <- brandProductTableQuery | ||
bank <- product.bank | ||
} yield (bank, product)).to[List].result | ||
} | ||
|
||
/** | ||
* Get all bank and their product.It is possible some bank do not have their product | ||
*/ | ||
def getAllBankWithProduct(): Future[List[(Bank, Option[BankProduct])]] = | ||
db.run { | ||
brandTableQuery.joinLeft(brandProductTableQuery).on(_.id === _.bankId).to[List].result | ||
} | ||
|
||
} | ||
|
||
private[repo] trait BankProductTable extends BankTable { this: DBComponent => | ||
|
||
import driver.api._ | ||
|
||
private[BankProductTable] class BankProductTable(tag: Tag) extends Table[BankProduct](tag, "bankproduct") { | ||
val id = column[Int]("id", O.PrimaryKey, O.AutoInc) | ||
val name = column[String]("name") | ||
val bankId = column[Int]("bank_id") | ||
def bank = foreignKey("bank_product_fk", bankId, brandTableQuery)(_.id) | ||
def * = (name, bankId, id.?) <> (BankProduct.tupled, BankProduct.unapply) | ||
|
||
} | ||
|
||
protected val brandProductTableQuery = TableQuery[BankProductTable] | ||
|
||
protected def brandProductTableAutoInc = brandProductTableQuery returning brandProductTableQuery.map(_.id) | ||
|
||
} | ||
|
||
case class BankProduct(name: String, bankId: Int, id: Option[Int] = None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.knol.db.repo | ||
|
||
import com.knol.db.connection.DBComponent | ||
import scala.concurrent.Future | ||
import com.knol.db.connection.MySqlDBComponent | ||
|
||
trait BankRepository extends BankTable { this: DBComponent => | ||
|
||
import driver.api._ | ||
|
||
/** | ||
* @param bank | ||
* create new bank | ||
*/ | ||
def create(bank: Bank): Future[Int] = db.run { brandTableAutoInc += bank } | ||
|
||
/** | ||
* @param bank | ||
* update existing bank | ||
*/ | ||
def update(bank: Bank): Future[Int] = db.run { brandTableQuery.filter(_.id === bank.id.get).update(bank) } | ||
|
||
/** | ||
* @param id | ||
* Get bank by id | ||
*/ | ||
def getById(id: Int): Future[Option[Bank]] = db.run { brandTableQuery.filter(_.id === id).result.headOption } | ||
|
||
/** | ||
* @return | ||
* Get all banks | ||
*/ | ||
def getAll(): Future[List[Bank]] = db.run { brandTableQuery.to[List].result } | ||
|
||
/** | ||
* @param id | ||
* delete bank by id | ||
*/ | ||
def delete(id: Int): Future[Int] = db.run { brandTableQuery.filter(_.id === id).delete } | ||
|
||
} | ||
|
||
private[repo] trait BankTable { this: DBComponent => | ||
|
||
import driver.api._ | ||
|
||
private[BankTable] class BankTable(tag: Tag) extends Table[Bank](tag, "bank") { | ||
val id = column[Int]("id", O.PrimaryKey, O.AutoInc) | ||
val name = column[String]("name") | ||
def * = (name, id.?) <> (Bank.tupled, Bank.unapply) | ||
|
||
} | ||
|
||
protected val brandTableQuery = TableQuery[BankTable] | ||
|
||
protected def brandTableAutoInc = brandTableQuery returning brandTableQuery.map(_.id) | ||
|
||
} | ||
|
||
object BankRepository extends BankRepository with MySqlDBComponent | ||
|
||
case class Bank(name: String, id: Option[Int] = None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
CREATE TABLE bank( | ||
id int PRIMARY KEY auto_increment, | ||
name varchar(200) | ||
); | ||
|
||
CREATE TABLE bankinfo( | ||
id int PRIMARY KEY auto_increment, | ||
owner varchar(200), | ||
bank_id int references bank(id), | ||
branches int | ||
); | ||
|
||
CREATE TABLE bankproduct( | ||
id int PRIMARY KEY auto_increment, | ||
name varchar(200), | ||
bank_id int references bank(id) | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
INSERT INTO bank VALUES(1, 'SBI bank'); | ||
|
||
INSERT INTO bank VALUES(2, 'PNB bank'); | ||
|
||
INSERT INTO bankinfo VALUES(1, 'goverment',1, 10000); | ||
|
||
INSERT INTO bankproduct VALUES(1, 'home loan',1); | ||
|
||
INSERT INTO bankproduct VALUES(2, 'eduction loan',1); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.knol.db.connection | ||
|
||
import org.slf4j.LoggerFactory | ||
import java.util.UUID | ||
|
||
|
||
/** | ||
* @author sky | ||
*/ | ||
trait H2DBComponent extends DBComponent { | ||
|
||
val logger = LoggerFactory.getLogger(this.getClass) | ||
|
||
val driver = slick.driver.H2Driver | ||
|
||
import driver.api._ | ||
|
||
val randomDB = "jdbc:h2:mem:test" + UUID.randomUUID().toString() + ";" | ||
|
||
val h2Url = randomDB + "MODE=MySql;DATABASE_TO_UPPER=false;INIT=runscript from 'src/test/resources/schema.sql'\\;runscript from 'src/test/resources/schemadata.sql'" | ||
|
||
val db: Database = { | ||
logger.info("Creating test connection ..................................") | ||
Database.forURL(url = h2Url, driver = "org.h2.Driver") | ||
} | ||
} |
Oops, something went wrong.