Skip to content

Instantly share code, notes, and snippets.

@filipelenfers
Last active October 18, 2016 07:57
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipelenfers/7914993 to your computer and use it in GitHub Desktop.
Save filipelenfers/7914993 to your computer and use it in GitHub Desktop.
Slick 2.0.0 + BoneCP
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp
import java.util.Date
import scala.slick.driver.MySQLDriver.simple._
//import scala.slick.driver.H2Driver.simple._
//MultipleDB examples, DAL included: https://github.com/slick/slick-examples/blob/2.0.0-M3/src/main/scala/com/typesafe/slick/examples/lifted/MultiDBExample.scala e https://github.com/slick/slick-examples/blob/2.0.0-M3/src/main/scala/com/typesafe/slick/examples/lifted/MultiDBCakeExample.scala
import com.jolbox.bonecp._;
case class Coffee(id:Option[Int], name: String, price: Double, created:Timestamp = new Timestamp(new Date().getTime), obs:Option[String] )
class Coffees(tag: Tag) extends Table[Coffee](tag, "COFFEES") {
val now = SimpleFunction.nullary[Timestamp]("now")
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("COF_NAME", O.NotNull, O.DBType("varchar(5000)"))
def price = column[Double]("PRICE", O.NotNull)
def created = column[Timestamp]("created") //TODO Slick 2.1 will enable the usage of functions on O.Default, so in the future put O.Default(now) here.
def obs = column[Option[String]]("obs", O.Nullable)
def * = (id.?,name,price,created,obs) <> (Coffee.tupled,Coffee.unapply)
}
object Main {
val coffees = TableQuery[Coffees]
def coffeesForInsert = coffees.map(u => (u.name,u.price,u.created,u.obs).shaped <>
({case (name,price,created,obs) => Coffee(None,name,price,created,obs)}, { (u: Coffee) => Some((u.name,u.price,u.created,u.obs))}))
def main(args: Array[String]): Unit = {
println("Início.")
Class.forName("com.mysql.jdbc.Driver"); // load the DB driver
val ds = new BoneCPDataSource(); // create a new datasource object
ds.setJdbcUrl("jdbc:mysql://localhost/yourDatabase?characterEncoding=utf-8"); // set the JDBC url
ds.setUsername("root"); // set the username
ds.setPassword("root");
ds.setMinConnectionsPerPartition(2);
ds.setMaxConnectionsPerPartition(30);
ds.setPartitionCount(3);
val db = Database.forDataSource(ds)
db.withSession {
implicit session =>
coffees.ddl.drop
coffees.ddl.create
//coffees += Coffee(None,"Teste",6.00,new Timestamp(new Date().getTime),Some("aaaa")) //way 1
val id = (coffeesForInsert returning coffees.map(_.id)) += Coffee(None,"Teste",6.00,new Timestamp(new Date().getTime),None); println(id); //way 2
//val id = (coffeesForInsert returning coffees.map(_.id)) += Coffee(None,"Teste",6.00,new Timestamp(new Date().getTime),Some("aaaa")); println(id);
println(coffees.filter(_.id === id).first)
//println(Coffee.unapply(Coffee(None,"Teste",6.00,new Timestamp(new Date().getTime),"aaaa"))) // transforms the object on a tuple
}
ds.close(); // close the datasource pool
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment