Commits

Hector Miuler Malpica Gallegos committed bb19589

pruebas iniciales, pero aún no funciona bien :(

  • Participants

Comments (0)

Files changed (4)

+target
+.idea
+name := "Demo Slick"
+
+version := "0.1"
+
+scalaVersion := "2.10.0-RC1"
+
+libraryDependencies += "junit" % "junit" % "4.10" % "test"
+
+libraryDependencies += "org.slf4j" % "slf4j-api" % "1.6.6"
+
+libraryDependencies += "com.h2database" % "h2" % "1.3.168"
+
+
+libraryDependencies ++= Seq(
+  "org.specs2" % "specs2_2.10.0-RC1" % "1.12.2" % "test",
+  "com.typesafe" % "slick_2.10.0-RC1" % "0.11.2"
+)
+
+resolvers += "Sonatype releases" at "http://oss.sonatype.org/content/repositories/releases/"
+
+resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"

File project/plugins.sbt

+resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
+
+addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0-SNAPSHOT")

File src/main/scala/Main.scala

+import scala.slick.driver.H2Driver
+import H2Driver.simple.Database
+import Database.{threadLocalSession => session}
+import slick.direct.AnnotationMapper.{column, table}
+import slick.direct.{AnnotationMapper, SlickBackend, Queryable}
+import slick.jdbc.GetResult
+
+@table("userx")
+case class User (
+  @column("id") id: Int,
+  @column("name") name: String,
+  @column("age") age: Int
+)
+
+object Main extends App {
+
+
+  val db = Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver")
+
+  db withSession {
+    prepararData
+    pruebaSelect
+    pruebaSelect
+
+    import scala.slick.jdbc.StaticQuery.interpolation
+    val users = Queryable[User]
+    val backend = new SlickBackend(H2Driver, AnnotationMapper)
+    println(backend.result(users, session))
+//    println(backend.result(users.length, session))
+  }
+
+
+  def prepararData = {
+    println("\nprepararData: ======================================================")
+    val users_data = Vector(
+      (1, "Hector", 33),
+      (2, "Miuler", 28)
+    )
+    import scala.slick.jdbc.StaticQuery.interpolation
+
+    val rptCreate = sqlu"create table userx(id int, name varchar(50), age int)".first
+    println("rptCreate: " + rptCreate)
+
+    val rptInsert = for ( (id, name, age) <- users_data ) yield sqlu"insert into userx values ($id, $name, $age)".first
+    println("rptInsert: " + rptInsert)
+  }
+
+  def pruebaSelect = {
+    println("\npruebaSelect: ======================================================")
+    import scala.slick.jdbc.StaticQuery.interpolation
+    implicit val getUsers = GetResult(r => User(r.<<, r.<<, r.<<))
+
+    val a = sql"select * from userx".as[User]
+    a.foreach(println)
+    println("users: " + a)
+  }
+
+}
+
+
+
+
+//#imports
+import scala.slick.driver.H2Driver
+import H2Driver.simple.Database
+import Database.{threadLocalSession => session}
+import scala.slick.direct._
+import scala.slick.direct.AnnotationMapper._
+//#imports
+
+//#schema
+// describe schema for direct embedding
+@table(name="COFFEES")
+case class Coffee(
+                   @column(name="NAME")
+                   name : String,
+                   @column(name="PRICE")
+                   price : Double
+                   )
+//#schema
+
+object DirectEmbedding extends App {
+  //#result
+  val db = Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver")
+  db withSession {
+    //#result
+    //#inserts
+    // fill database with test data (using plain SQL)
+    val coffees_data = Vector(
+      ("Colombian",          1.0),
+      ("French_Roast",       2.0),
+      ("Espresso",           3.0),
+      ("Colombian_Decaf",    4.0),
+      ("French_Roast_Decaf", 5.0)
+    )
+    // create test table
+    import scala.slick.jdbc.StaticQuery.interpolation
+    sqlu"create table COFFEES(NAME varchar(255), PRICE double)".execute
+    (for {
+      (name, sales) <- coffees_data
+    } yield sqlu"insert into COFFEES values ($name, $sales)".first).sum
+    //#inserts
+
+    //#query
+    // query database using direct embedding
+    val q1 = Queryable[Coffee]
+    val q2 = q1.filter( _.price > 3.0 ).map( _ .name )
+    //#query
+
+    //#result
+    // execute query using a chosen db backend
+    val backend = new SlickBackend( H2Driver, AnnotationMapper )
+    println( backend.result( q2, session ) )
+    println( backend.result( q2.length, session ) )
+    //#result
+
+    //#implicitqueryable
+    //
+    val iq1 = ImplicitQueryable( q1, backend, session )
+    val iq2 = iq1.filter( c => c.price > 3.0 )
+    println( iq2.toSeq ) //  <- triggers execution
+    println( iq2.length ) // <- triggers execution
+    //#implicitqueryable
+
+    //#nesting
+    q1.map( c => (c.name, (c, c.price)) )
+    //#nesting
+    //#result
+  }
+  //#result
+}