diff options
author | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-12 18:42:53 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-12 18:42:53 -0800 |
commit | 861ceb03e8faeb564dd027b13250b5604af8645f (patch) | |
tree | 4fb65e94ede0a888551617af1cf394590851a700 /src/main | |
parent | 828cc12194663f107a08251bbabc256ae0d7c936 (diff) | |
parent | 5b1a91290035cb71dde82d10f1f6ee173d24273b (diff) | |
download | driver-core-0.9.28.tar.gz driver-core-0.9.28.tar.bz2 driver-core-0.9.28.zip |
Merge pull request #6 from drivergroup/uuid-type-mapperv0.9.28
Type mappers for uuid/serial/natural primary keys in separate traits
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/scala/xyz/driver/core/database.scala | 30 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/core/generators.scala | 4 |
2 files changed, 29 insertions, 5 deletions
diff --git a/src/main/scala/xyz/driver/core/database.scala b/src/main/scala/xyz/driver/core/database.scala index d948be7..a82e345 100644 --- a/src/main/scala/xyz/driver/core/database.scala +++ b/src/main/scala/xyz/driver/core/database.scala @@ -34,19 +34,39 @@ object database { trait ColumnTypes { val profile: JdbcProfile - import profile.api._ - implicit def `xyz.driver.core.Id.columnType`[T] = - MappedColumnType.base[Id[T], String](_.value, Id[T](_)) + implicit def `xyz.driver.core.Id.columnType`[T]: BaseColumnType[Id[T]] - implicit def `xyz.driver.core.Name.columnType`[T] = + implicit def `xyz.driver.core.Name.columnType`[T]: BaseColumnType[Name[T]] = MappedColumnType.base[Name[T], String](_.value, Name[T](_)) - implicit def `xyz.driver.core.time.Time.columnType` = + implicit def `xyz.driver.core.time.Time.columnType`: BaseColumnType[Time] = MappedColumnType.base[Time, Long](_.millis, Time(_)) } + object ColumnTypes { + trait UUID extends ColumnTypes { + import profile.api._ + + override implicit def `xyz.driver.core.Id.columnType`[T] = + MappedColumnType + .base[Id[T], java.util.UUID](id => java.util.UUID.fromString(id.value), uuid => Id[T](uuid.toString)) + } + trait SerialId extends ColumnTypes { + import profile.api._ + + override implicit def `xyz.driver.core.Id.columnType`[T] = + MappedColumnType.base[Id[T], Long](_.value.toLong, serialId => Id[T](serialId.toString)) + } + trait NaturalId extends ColumnTypes { + import profile.api._ + + override implicit def `xyz.driver.core.Id.columnType`[T] = + MappedColumnType.base[Id[T], String](_.value, Id[T](_)) + } + } + trait DatabaseObject extends ColumnTypes { def createTables(): Future[Unit] diff --git a/src/main/scala/xyz/driver/core/generators.scala b/src/main/scala/xyz/driver/core/generators.scala index 7d33446..c61cb94 100644 --- a/src/main/scala/xyz/driver/core/generators.scala +++ b/src/main/scala/xyz/driver/core/generators.scala @@ -26,6 +26,10 @@ object generators { def nextId[T](maxLength: Int): Id[T] = Id[T](nextString(maxLength)) + def nextNumericId[T](): Id[T] = Id[T](nextLong.abs.toString) + + def nextNumericId[T](maxValue: Int): Id[T] = Id[T](nextInt(maxValue).toString) + def nextName[T](maxLength: Int = DefaultMaxLength): Name[T] = Name[T](nextString(maxLength)) def nextUuid() = java.util.UUID.randomUUID |