Skip to content

Commit

Permalink
Merge pull request #1088 from nmartynenko/h2-uuid-fix
Browse files Browse the repository at this point in the history
Adjust H2Driver for UUID tests
  • Loading branch information
szeiger committed Mar 12, 2015
2 parents 63b5e75 + 13bf805 commit 4b3ca56
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion slick-testkit/src/codegen/resources/dbs/uuid.sql
@@ -1,3 +1,3 @@
create table "person" ("id" INTEGER NOT NULL PRIMARY KEY,
"uuid" UUID NOT NULL,
"uuid_def" UUID DEFAULT('2f3f866c-d8e6-11e2-bb56-50e549c9b654'));
"uuid_def" UUID DEFAULT('2f3f866c-d8e6-11e2-bb56-50e549c9b654'));
Expand Up @@ -81,7 +81,7 @@ val SimpleA = CustomTyping.SimpleA
}
})
},
new UUIDConfig("CG10", StandardTestDBs.Postgres, "Postgres", Seq("/dbs/uuid.sql")),
new UUIDConfig("CG10", StandardTestDBs.H2Mem, "H2Mem", Seq("/dbs/uuid.sql")),
new Config("Postgres1", StandardTestDBs.Postgres, "Postgres", Nil) {
import tdb.driver.api._
class A(tag: Tag) extends Table[(Int, Array[Byte], Blob)](tag, "a") {
Expand Down Expand Up @@ -121,17 +121,17 @@ val SimpleA = CustomTyping.SimpleA
}
override def code = {
Seq("""
/*default UUID, which is the same as for 'create-uuid.sql'*/
val defaultUUID = java.util.UUID.fromString("2f3f866c-d8e6-11e2-bb56-50e549c9b654")
/*convert UUID for H2*/
implicit object GetUUID extends slick.jdbc.GetResult[java.util.UUID] {
def apply(rs: slick.jdbc.PositionedResult) = rs.nextObject().asInstanceOf[java.util.UUID]
}
/*convert Option[UUID] for H2*/
implicit object GetOptionUUID extends slick.jdbc.GetResult[Option[java.util.UUID]] {
def apply(rs: slick.jdbc.PositionedResult) = Option(rs.nextObject().asInstanceOf[java.util.UUID])
}
""".trim) ++ super.code
| /* default UUID, which is the same as for 'uuid.sql' */
| val defaultUUID = java.util.UUID.fromString("2f3f866c-d8e6-11e2-bb56-50e549c9b654")
| /* convert UUID */
| implicit object GetUUID extends slick.jdbc.GetResult[java.util.UUID] {
| def apply(rs: slick.jdbc.PositionedResult) = rs.nextObject().asInstanceOf[java.util.UUID]
| }
| /* convert Option[UUID] for H2 */
| implicit object GetOptionUUID extends slick.jdbc.GetResult[Option[java.util.UUID]] {
| def apply(rs: slick.jdbc.PositionedResult) = Option(rs.nextObject().asInstanceOf[java.util.UUID])
| }
""".stripMargin) ++ super.code
}
}
})
Expand All @@ -142,18 +142,20 @@ val SimpleA = CustomTyping.SimpleA
| val u2 = UUID.randomUUID()
| val p1 = PersonRow(1, u1)
| val p2 = PersonRow(2, u2)
|
| def assertAll(all: Seq[PersonRow]) = {
| assertEquals( 2, all.size )
| assertEquals( Set(1,2), all.map(_.id).toSet )
| assertEquals( Set(u1, u2), all.map(_.uuid).toSet )
| //it should contain sample UUID
| assert(all.forall(_.uuidDef == Some(defaultUUID)))
| }
|
| DBIO.seq(
| schema.create,
| Person += p1,
| Person += p2,
| Person.result.map { case all => {
| assertEquals( 2, all.size )
| assertEquals( Set(1,2), all.map(_.id).toSet )
| assertEquals( Set(u1, u2), all.map(_.uuid).toSet )
| //it should contain sample UUID
| assert(all.forall(_.uuidDef == Some(defaultUUID)))
| }
| }
| Person.result.map(assertAll)
| ).transactionally
""".stripMargin
}
Expand Down
4 changes: 4 additions & 0 deletions slick/src/main/scala/slick/driver/H2Driver.scala
@@ -1,5 +1,7 @@
package slick.driver

import java.util.UUID

import scala.concurrent.ExecutionContext
import slick.ast._
import slick.util.MacroSupport.macroSupportInterpolation
Expand Down Expand Up @@ -95,6 +97,8 @@ trait H2Driver extends JdbcDriver { driver =>
class JdbcTypes extends super.JdbcTypes {
override val uuidJdbcType = new UUIDJdbcType {
override def sqlTypeName(size: Option[RelationalProfile.ColumnOption.Length]) = "UUID"
override def valueToSQLLiteral(value: UUID) = "'" + value + "'"
override def hasLiteralForm = true
}
}

Expand Down

0 comments on commit 4b3ca56

Please sign in to comment.