diff --git a/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala b/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala index bb7624c32..5bdb849ab 100644 --- a/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala +++ b/mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala @@ -1,7 +1,7 @@ package zio.sql.mysql +import java.time.{ LocalDate, LocalTime, Year, ZonedDateTime } import java.sql.ResultSet -import java.time.{ LocalDate, Year } import zio.sql.Sql @@ -33,6 +33,8 @@ trait MysqlSqlModule extends Sql { self => val Degrees = FunctionDef[Double, Double](FunctionName("degrees")) val Log2 = FunctionDef[Double, Double](FunctionName("log2")) val Log10 = FunctionDef[Double, Double](FunctionName("log10")) + val MakeTime = FunctionDef[(Int, Int, Double), LocalTime](FunctionName("maketime")) + val Now = FunctionDef[Any, ZonedDateTime](FunctionName("now")) val Pi = Expr.FunctionCall0[Double](FunctionDef[Any, Double](FunctionName("pi"))) } diff --git a/mysql/src/test/scala/zio/sql/mysql/FunctionDefSpec.scala b/mysql/src/test/scala/zio/sql/mysql/FunctionDefSpec.scala index ff54082f1..629c3f947 100644 --- a/mysql/src/test/scala/zio/sql/mysql/FunctionDefSpec.scala +++ b/mysql/src/test/scala/zio/sql/mysql/FunctionDefSpec.scala @@ -5,6 +5,9 @@ import zio.test._ import zio.test.Assertion._ import java.time.LocalDate +import java.time.{ LocalTime, ZoneId } +import java.time.format.DateTimeFormatter + object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema { import Customers._ @@ -119,6 +122,23 @@ object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, + test("now") { + val timestampFormatter = + DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss").withZone(ZoneId.of("UTC")) + + val query = select(Now()) + + val testResult = execute(query) + + val assertion = + for { + r <- testResult.runCollect + } yield assert(timestampFormatter.format(r.head))( + Assertion.matchesRegex("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}") + ) + + assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) + }, test("bit_length") { val query = select(BitLength("hello")) @@ -145,6 +165,19 @@ object FunctionDefSpec extends MysqlRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, + test("maketime") { + val query = select(MakeTime(12, 15, 30.5)) from customers + + val expected = LocalTime.parse("12:15:30.5") + + val testResult = execute(query) + + val assertion = for { + r <- testResult.runCollect + } yield assert(r.head)(equalTo(expected)) + + assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) + }, test("pi") { val query = select(Pi) from customers