diff options
author | Vlad Uspensky <v.uspenskiy@icloud.com> | 2017-10-20 14:38:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-20 14:38:42 -0700 |
commit | 0cb06d70bd91e1e6a4ab9d97851ef9db7aaedfd6 (patch) | |
tree | dd0d66174b6294251570ce24e8bd7b864beb561a /src/main/scala/xyz/driver/core/database | |
parent | fa6716e0a3223fa0b15966f6bdfe5464e26d8fc2 (diff) | |
parent | ba2c214d2f8f9a1ba6d8265c55476a5281b8fd22 (diff) | |
download | driver-core-1.5.1.tar.gz driver-core-1.5.1.tar.bz2 driver-core-1.5.1.zip |
Merge pull request #76 from drivergroup/refinedv1.5.1
[RFC] Using "Refined" library (https://github.com/fthomas/refined) to…
Diffstat (limited to 'src/main/scala/xyz/driver/core/database')
-rw-r--r-- | src/main/scala/xyz/driver/core/database/database.scala | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/main/scala/xyz/driver/core/database/database.scala b/src/main/scala/xyz/driver/core/database/database.scala index d305eee..26c1027 100644 --- a/src/main/scala/xyz/driver/core/database/database.scala +++ b/src/main/scala/xyz/driver/core/database/database.scala @@ -10,6 +10,11 @@ import com.typesafe.config.Config package database { + import java.sql.SQLDataException + + import eu.timepit.refined.api.{Refined, Validate} + import eu.timepit.refined.refineV + trait Database { val profile: JdbcProfile val database: JdbcProfile#Backend#Database @@ -61,6 +66,33 @@ package database { } } + trait RefinedColumnTypes[T, Predicate] extends ColumnTypes { + import profile.api._ + implicit def `eu.timepit.refined.api.Refined`( + implicit columnType: BaseColumnType[T], + validate: Validate[T, Predicate]): BaseColumnType[T Refined Predicate] + } + + object RefinedColumnTypes { + trait RefinedValue[T, Predicate] extends RefinedColumnTypes[T, Predicate] { + import profile.api._ + override implicit def `eu.timepit.refined.api.Refined`( + implicit columnType: BaseColumnType[T], + validate: Validate[T, Predicate]): BaseColumnType[T Refined Predicate] = + MappedColumnType.base[T Refined Predicate, T]( + _.value, { dbValue => + refineV[Predicate](dbValue) match { + case Left(refinementError) => + throw new SQLDataException( + s"Value in the database doesn't match the refinement constraints: $refinementError") + case Right(refinedValue) => + refinedValue + } + } + ) + } + } + trait IdColumnTypes extends ColumnTypes { import profile.api._ implicit def `xyz.driver.core.Id.columnType`[T]: BaseColumnType[Id[T]] @@ -84,7 +116,7 @@ package database { import profile.api._ override implicit def `xyz.driver.core.Id.columnType`[T] = - MappedColumnType.base[Id[T], String](_.value, Id[T](_)) + MappedColumnType.base[Id[T], String](_.value, Id[T]) } } @@ -117,7 +149,7 @@ package database { MappedColumnType .base[Id[T], java.util.UUID](id => java.util.UUID.fromString(id.value), uuid => Id[T](uuid.toString)) def serialKeyMapper[T] = MappedColumnType.base[Id[T], Long](_.value.toLong, serialId => Id[T](serialId.toString)) - def naturalKeyMapper[T] = MappedColumnType.base[Id[T], String](_.value, Id[T](_)) + def naturalKeyMapper[T] = MappedColumnType.base[Id[T], String](_.value, Id[T]) } trait DatabaseObject extends ColumnTypes { |