Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quill-sql: H2 dialect #189

Merged
merged 5 commits into from
Feb 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ReleaseTransformations._
import com.typesafe.sbt.SbtScalariform
import com.typesafe.sbt.SbtScalariform.ScalariformKeys
import scalariform.formatter.preferences._

Expand Down Expand Up @@ -39,7 +38,8 @@ lazy val `quill-jdbc` =
.settings(
libraryDependencies ++= Seq(
"com.zaxxer" % "HikariCP" % "2.3.9",
"mysql" % "mysql-connector-java" % "5.1.36" % "test",
"mysql" % "mysql-connector-java" % "5.1.36" % "test",
"com.h2database" % "h2" % "1.4.190" % "test",
"org.postgresql" % "postgresql" % "9.4-1206-jdbc41"
),
parallelExecution in Test := false
Expand All @@ -62,8 +62,8 @@ lazy val `quill-async` =
.settings(commonSettings: _*)
.settings(
libraryDependencies ++= Seq(
"com.github.mauricio" %% "db-async-common" % "0.2.18",
"com.github.mauricio" %% "mysql-async" % "0.2.18",
"com.github.mauricio" %% "db-async-common" % "0.2.18",
"com.github.mauricio" %% "mysql-async" % "0.2.18",
"com.github.mauricio" %% "postgresql-async" % "0.2.18"
),
parallelExecution in Test := false
Expand Down Expand Up @@ -92,7 +92,10 @@ lazy val commonSettings = Seq(
"com.google.code.findbugs" % "jsr305" % "3.0.1" % "provided" // just to avoid warnings during compilation
),
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Resource,
unmanagedClasspath in Test += baseDirectory.value / "src" / "test" / "resources",
unmanagedClasspath in Test ++= Seq(
baseDirectory.value / "src" / "test" / "resources",
baseDirectory.value / "src" / "test" / "resources" / "sql"
),
scalacOptions ++= Seq(
"-Xfatal-warnings",
"-deprecation",
Expand Down
6 changes: 5 additions & 1 deletion quill-jdbc/src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ testPostgresDB.dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
testPostgresDB.dataSource.user=postgres
testPostgresDB.dataSource.databaseName=quill_test
testPostgresDB.dataSource.portNumber=${?POSTGRES_PORT_5432_TCP_PORT}
testPostgresDB.dataSource.serverName=${?POSTGRES_PORT_5432_TCP_ADDR}
testPostgresDB.dataSource.serverName=${?POSTGRES_PORT_5432_TCP_ADDR}

testH2DB.dataSourceClassName=org.h2.jdbcx.JdbcDataSource
testH2DB.dataSource.url="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:sql/h2-schema.sql'"
testH2DB.dataSource.user=sa
68 changes: 68 additions & 0 deletions quill-jdbc/src/test/resources/sql/h2-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.getquill.sources.jdbc.h2

import io.getquill._
import io.getquill.sources.sql.DepartmentsSpec

class DepartmentsJdbcSpec extends DepartmentsSpec {

override def beforeAll = {
val t = testH2DB.transaction {
testH2DB.run(query[Department].delete)
testH2DB.run(query[Employee].delete)
testH2DB.run(query[Task].delete)

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

"Example 8 - nested naive" in {
testH2DB.run(`Example 8 expertise naive`)(`Example 8 param`) mustEqual `Example 8 expected result`
}

"Example 9 - nested db" in {
testH2DB.run(`Example 9 expertise`)(`Example 9 param`) mustEqual `Example 9 expected result`
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.getquill.sources.jdbc.h2

import io.getquill._
import io.getquill.sources.sql.EncodingSpec

class JdbcEncodingSpec extends EncodingSpec {

"encodes and decodes types" in {
testH2DB.run(delete)
testH2DB.run(insert)(insertValues)
verify(testH2DB.run(query[EncodingTestEntity]))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.getquill.sources.jdbc.h2

import io.getquill._

class JdbcSourceSpec extends Spec {

val badEntity = quote {
query[TestEntity]("TestEntity", _.s -> "a", _.i -> "i", _.l -> "l", _.o -> "o")
}

"probes valid sqls" - {
"""testH2DBWithQueryProbing.run(qr1.filter(_.i == 0))""" must compile
}

"probes invalid sqls" - {
"""testH2DBWithQueryProbing.run(badEntity.map(_.s))""" mustNot compile
}

"provides transaction support" - {
"success" in {
testH2DB.run(qr1.delete)
testH2DB.transaction {
testH2DB.run(qr1.insert(_.i -> 33))
}
testH2DB.run(qr1).map(_.i) mustEqual List(33)
}
"failure" in {
testH2DB.run(qr1.delete)
intercept[IllegalStateException] {
testH2DB.transaction {
testH2DB.run(qr1.insert(_.i -> 33))
throw new IllegalStateException
}
}
testH2DB.run(qr1).isEmpty mustEqual true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.getquill.sources.jdbc.h2

import io.getquill._
import io.getquill.sources.sql.PeopleSpec

class PeopleJdbcSpec extends PeopleSpec {

override def beforeAll = {
val t = testH2DB.transaction {
testH2DB.run(query[Couple].delete)
testH2DB.run(query[Person].filter(_.age > 0).delete)
testH2DB.run(peopleInsert)(peopleEntries)
testH2DB.run(couplesInsert)(couplesEntries)
}
}

"Example 1 - differences" in {
testH2DB.run(`Ex 1 differences`) mustEqual `Ex 1 expected result`
}

"Example 2 - range simple" in {
testH2DB.run(`Ex 2 rangeSimple`)(`Ex 2 param 1`, `Ex 2 param 2`) mustEqual `Ex 2 expected result`
}

"Example 3 - satisfies" in {
testH2DB.run(`Ex 3 satisfies`) mustEqual `Ex 3 expected result`
}

"Example 4 - satisfies" in {
testH2DB.run(`Ex 4 satisfies`) mustEqual `Ex 4 expected result`
}

"Example 5 - compose" in {
testH2DB.run(`Ex 5 compose`)(`Ex 5 param 1`, `Ex 5 param 2`) mustEqual `Ex 5 expected result`
}

"Example 6 - predicate 0" in {
testH2DB.run(satisfies(eval(`Ex 6 predicate`))) mustEqual `Ex 6 expected result`
}

"Example 7 - predicate 1" in {
testH2DB.run(satisfies(eval(`Ex 7 predicate`))) mustEqual `Ex 7 expected result`
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.getquill.sources.jdbc

import io.getquill._
import io.getquill.naming.Literal
import io.getquill.sources.sql.idiom.H2Dialect

package object h2 {

val testH2DB = source(new JdbcSourceConfig[H2Dialect, Literal]("testH2DB"))
val testH2DBWithQueryProbing = source(new JdbcSourceConfig[H2Dialect, Literal]("testH2DB") with QueryProbing)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.getquill.sources.sql.idiom

import java.util.concurrent.atomic.AtomicInteger

trait H2Dialect
extends SqlIdiom
with PositionalVariables {

private[idiom] val preparedStatementId = new AtomicInteger

override def prepare(sql: String) =
s"PREPARE p${preparedStatementId.incrementAndGet} AS ${positionalVariables(sql)}"
}

object H2Dialect extends H2Dialect
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.getquill.sources.sql.idiom

trait PositionalVariables { self: SqlIdiom =>

protected def positionalVariables(sql: String) =
sql.foldLeft((1, "")) {
case ((idx, s), '?') =>
(idx + 1, s + "$" + idx)
case ((idx, s), c) =>
(idx, s + c)
}._2
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ package io.getquill.sources.sql.idiom
import java.util.concurrent.atomic.AtomicInteger

trait PostgresDialect
extends SqlIdiom {
extends SqlIdiom
with PositionalVariables {

private[idiom] val preparedStatementId = new AtomicInteger

override def prepare(sql: String) =
s"PREPARE p${preparedStatementId.incrementAndGet} AS ${positionalVariables(sql)}"

private def positionalVariables(sql: String) =
sql.foldLeft((1, "")) {
case ((idx, s), '?') =>
(idx + 1, s + "$" + idx)
case ((idx, s), c) =>
(idx, s + c)
}._2
}

object PostgresDialect extends PostgresDialect
36 changes: 18 additions & 18 deletions quill-sql/src/test/sql/mysql-schema.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
CREATE TABLE Person(
name VARCHAR(255),
age int(3)
name VARCHAR(255),
age int(3)
);

CREATE TABLE Couple(
her VARCHAR(255),
him VARCHAR(255)
her VARCHAR(255),
him VARCHAR(255)
);

CREATE TABLE Department(
dpt VARCHAR(255)
dpt VARCHAR(255)
);

CREATE TABLE Employee(
emp VARCHAR(255),
dpt VARCHAR(255),
salary int(20)
emp VARCHAR(255),
dpt VARCHAR(255),
salary int(20)
);

CREATE TABLE Task(
emp VARCHAR(255),
tsk VARCHAR(255)
emp VARCHAR(255),
tsk VARCHAR(255)
);

CREATE TABLE EncodingTestEntity(
v1 VARCHAR(255),
v1 VARCHAR(255),
v2 DECIMAL(5,2),
v3 BOOLEAN,
v4 SMALLINT,
Expand All @@ -49,26 +49,26 @@ CREATE TABLE EncodingTestEntity(
);

Create TABLE DateEncodingTestEntity(
v1 date,
v2 datetime,
v3 timestamp
v1 date,
v2 datetime,
v3 timestamp
);

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

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

CREATE TABLE TestEntity3(
s VARCHAR(255),
s VARCHAR(255),
i INTEGER,
l BIGINT
);
);
Loading