MasseGuillaume / ScalaKata2

Interactive Playground
https://scastie.scala-lang.org/
98 stars 13 forks source link

add quill to the docker image #39

Closed fwbrasil closed 8 years ago

fwbrasil commented 8 years ago

https://github.com/getquill/quill/issues/336

MasseGuillaume commented 8 years ago

Hey @fwbrasil

I got it working with the security manager :-). Here's my trick:

import com.scalakata._

@instrument class Playground { 
  import io.getquill.{JdbcContext, H2Dialect, Literal}
  object context extends JdbcContext[H2Dialect, Literal](Setup.db)
  import context._

  case class Department(dpt: String)
  case class Employee(emp: String, dpt: String, salary: Int)
  case class Task(emp: String, tsk: String)

  val departmentInsert =
    quote {
      (dpt: String) => query[Department].insert(_.dpt -> dpt)
    }

  val departmentEntries =
    List("Product", "Quality", "Research", "Sales")

  val employeeInsert =
    quote {
      (dpt: String, emp: String) => query[Employee].insert(_.dpt -> dpt, _.emp -> emp)
    }

  val employeeEntries =
    List(
      ("Product", "Alex"),
      ("Product", "Bert"),
      ("Research", "Cora"),
      ("Research", "Drew"),
      ("Research", "Edna"),
      ("Sales", "Fred")
    )

  val taskInsert =
    quote {
      (emp: String, tsk: String) => query[Task].insert(_.emp -> emp, _.tsk -> tsk)
    }

  val taskEntries =
    List(
      ("Alex", "build"),
      ("Bert", "build"),
      ("Cora", "abstract"),
      ("Cora", "build"),
      ("Cora", "design"),
      ("Drew", "abstract"),
      ("Drew", "design"),
      ("Edna", "abstract"),
      ("Edna", "call"),
      ("Edna", "design"),
      ("Fred", "call")
    )

  val `Example 8 expertise naive` =
    quote {
      (u: String) =>
        for {
          d <- query[Department] if (
            (for {
              e <- query[Employee] if (
                e.dpt == d.dpt && (
                  for {
                    t <- query[Task] if (e.emp == t.emp && t.tsk == u)
                  } yield {}
                ).isEmpty
              )
            } yield {}).isEmpty
          )
        } yield d.dpt
    }

    transaction {
      run(query[Department].delete)
      run(query[Employee].delete)
      run(query[Task].delete)

      run(departmentInsert)(departmentEntries)
      run(employeeInsert)(employeeEntries)
      run(taskInsert)(taskEntries)
    }

    run(`Example 8 expertise naive`)("abstract")
}

object Setup {
  import org.h2.jdbcx.JdbcDataSource
  class Database extends JdbcDataSource with java.io.Closeable {
    def close = this.getConnection.close
    setURL("jdbc:h2:mem:test")
    setUser("sa")
  }

  val db = new Database()
  val connection = db.getConnection
  val statement = connection.createStatement
  statement.executeUpdate(
    """
    CREATE TABLE IF NOT EXISTS Person(
        name VARCHAR(255),
        age int
    );

    CREATE TABLE IF NOT EXISTS Couple(
        her VARCHAR(255),
        him VARCHAR(255)
    );

    CREATE TABLE IF NOT EXISTS Department(
        dpt VARCHAR(255)
    );

    CREATE TABLE IF NOT EXISTS Employee(
        emp VARCHAR(255),
        dpt VARCHAR(255),
        salary int
    );

    CREATE TABLE IF NOT EXISTS Task(
        emp VARCHAR(255),
        tsk VARCHAR(255)
    );

    CREATE TABLE IF NOT EXISTS EncodingTestEntity(
        v1 VARCHAR(255),
        v2 DECIMAL(5,2),
        v3 BOOLEAN,
        v4 SMALLINT,
        v5 SMALLINT,
        v6 INTEGER,
        v7 BIGINT,
        v8 FLOAT,
        v9 DOUBLE PRECISIOn,
        v10 BYTEA,
        v11 TIMESTAMP,
        o1 VARCHAR(255),
        o2 DECIMAL(5,2),
        o3 BOOLEAN,
        o4 SMALLINT,
        o5 SMALLINT,
        o6 INTEGER,
        o7 BIGINT,
        o8 FLOAT,
        o9 DOUBLE PRECISIOn,
        o10 BYTEA,
        o11 TIMESTAMP
    );

    CREATE TABLE IF NOT EXISTS TestEntity(
        s VARCHAR(255),
        i INTEGER,
        l BIGINT,
        o INTEGER
    );

    CREATE TABLE IF NOT EXISTS TestEntity2(
        s VARCHAR(255),
        i INTEGER,
        l BIGINT
    );

    CREATE TABLE IF NOT EXISTS TestEntity3(
        s VARCHAR(255),
        i INTEGER,
        l BIGINT
    );

    CREATE TABLE IF NOT EXISTS Product(
        description VARCHAR(255),
        id identity,
        sku BIGINT
    );
    """
  )
}
fwbrasil commented 8 years ago

Awesome!!! Thank you very much!

Did you have problems with the security manager? In what scenario does it fail?

MasseGuillaume commented 8 years ago

It fails with Hikari because it creates thread, access java properties (read, write) and wants reflection.

I just created this bundle https://hub.docker.com/r/masseguillaume/scalakata-bundle-quill I changed the name because I dont want to bump scalakata version and docker hub does not allow metadata in versions (1.1.5+quill).

I'm deploying now.

MasseGuillaume commented 8 years ago

Here you go: http://codepen.io/MasseGuillaume/pen/GqdWoG

http://scalakata.com/gist/57cca1ed32fbce90c36c7588b60093c2

It takes some time to evaluate. When the jit is no warmed up it times out. But after a couple hits it runs fine.