Skip to content

Commit 21c5433

Browse files
committed
Fix for Option[UUID] field types (fetching null UUIDs from the DB would cause an exception)
1 parent a0caa41 commit 21c5433

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/main/scala/org/squeryl/internals/FieldMapper.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ trait FieldMapper {
9090
v match {
9191
case u: UUID => u
9292
case s: String => UUID.fromString(s)
93+
case _ => sample
9394
}
9495
}
9596
}

src/test/scala/org/squeryl/test/UuidTests.scala

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ package org.squeryl.test
33
import org.squeryl._
44
import org.squeryl.framework.{SchemaTester, RunTestsInsideTransaction}
55
import java.util.UUID
6-
76
import org.squeryl.test.PrimitiveTypeModeForTests._
7+
import framework.SingleTestRun
88

99
object UuidTests {
1010
class UuidAsProperty extends KeyedEntity[Long] {
1111
val id: Long = 0
1212
val uuid = UUID.randomUUID
1313
}
14-
14+
15+
class UuidWithOption(val optionalUuid: Option[UUID]) extends KeyedEntity[Long] {
16+
def this() = this(Some(UUID.randomUUID()))
17+
val id: Long = 0
18+
}
19+
1520
class UuidAsId extends KeyedEntity[UUID] {
1621
var id = UUID.randomUUID
1722
lazy val foreigns = TestSchema.uuidOneToMany.left(this)
@@ -25,6 +30,7 @@ object UuidTests {
2530
val uuidAsProperty = table[UuidAsProperty]
2631
val uuidAsId = table[UuidAsId]
2732
val uuidAsForeignKey = table[UuidAsForeignKey]
33+
val uuidWithOption = table[UuidWithOption]
2834

2935
val uuidOneToMany = oneToManyRelation(uuidAsId, uuidAsForeignKey).via(_.id === _.foreignUuid)
3036

@@ -52,6 +58,27 @@ abstract class UuidTests extends SchemaTester with RunTestsInsideTransaction{
5258
testObject.uuid should equal(uuidAsProperty.where(_.uuid in List(testObject.uuid)).single.uuid)
5359
}
5460

61+
test("UuidOptional", SingleTestRun) {
62+
import TestSchema._
63+
64+
val testObject = new UuidWithOption(None)
65+
testObject.save
66+
67+
val fromDb = uuidWithOption.lookup(testObject.id).get
68+
println(fromDb.optionalUuid)
69+
fromDb.optionalUuid should equal(None)
70+
71+
val uuid = UUID.randomUUID()
72+
73+
update(uuidWithOption)(p =>
74+
where(p.id === testObject.id)
75+
set(p.optionalUuid := Some(uuid))
76+
)
77+
78+
uuidWithOption.lookup(testObject.id).get.optionalUuid should equal(Some(uuid))
79+
80+
}
81+
5582
test("UuidAsId") {
5683
import TestSchema._
5784

0 commit comments

Comments
 (0)