From 8200ef8d8223c1761f674629f66e5dac37e2536c Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Sat, 21 Nov 2020 13:08:24 +0100 Subject: [PATCH 01/12] add timeofday --- .../scala/zio/sql/postgresql/PostgresModule.scala | 1 + .../scala/zio/sql/postgresql/FunctionDefSpec.scala | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 51afe1d1e..8259fbf44 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -8,6 +8,7 @@ trait PostgresModule extends Jdbc { self => object PostgresFunctionDef { val Sind = FunctionDef[Double, Double](FunctionName("sind")) + val Timeofday = FunctionDef[Nothing, String](FunctionName("timeofday")) } override def renderRead(read: self.Read[_]): String = { diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index c0ea0a01a..db67303cd 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -35,6 +35,19 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { r <- testResult.runCollect } yield assert(r.head)(equalTo(expected)) + assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) + }, + testM("timeofday") { + val query = select(Sind(30.0)) from customers + + val expected = 0.5 + + val testResult = execute(query).to[Double, Double](identity) + + val assertion = for { + r <- testResult.runCollect + } yield assert(r.head)(equalTo(expected)) + assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) } ) From d9517cfb89358471925c34b5aec201c3693ae723 Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Sat, 21 Nov 2020 17:37:21 +0100 Subject: [PATCH 02/12] add timeofday func for postgres --- core/jvm/src/main/scala/zio/sql/expr.scala | 7 ++++++ .../zio/sql/postgresql/PostgresModule.scala | 6 ++++- .../zio/sql/postgresql/FunctionDefSpec.scala | 22 ++++++++++--------- .../zio/sql/sqlserver/SqlServerModule.scala | 2 ++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/core/jvm/src/main/scala/zio/sql/expr.scala b/core/jvm/src/main/scala/zio/sql/expr.scala index ba9d9233b..8ff81dec4 100644 --- a/core/jvm/src/main/scala/zio/sql/expr.scala +++ b/core/jvm/src/main/scala/zio/sql/expr.scala @@ -158,6 +158,10 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } + sealed case class FunctionCall0[F, A, B, Z: TypeTag](function: FunctionDef[B, Z]) extends InvariantExpr[F, A, Z] { + def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] + } + sealed case class FunctionCall1[F, A, B, Z: TypeTag](param: Expr[F, A, B], function: FunctionDef[B, Z]) extends InvariantExpr[F, A, Z] { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] @@ -210,6 +214,9 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { sealed case class FunctionDef[-A, +B](name: FunctionName) { self => + def apply[Source, B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Unit, Source, B1] = + Expr.FunctionCall0(self: FunctionDef[A, B1]) + def apply[F, Source, B1 >: B](param1: Expr[F, Source, A])(implicit typeTag: TypeTag[B1]): Expr[F, Source, B1] = Expr.FunctionCall1(param1, self: FunctionDef[A, B1]) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 8259fbf44..6d94cb835 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -7,7 +7,7 @@ import zio.sql.Jdbc trait PostgresModule extends Jdbc { self => object PostgresFunctionDef { - val Sind = FunctionDef[Double, Double](FunctionName("sind")) + val Sind = FunctionDef[Double, Double](FunctionName("sind")) val Timeofday = FunctionDef[Nothing, String](FunctionName("timeofday")) } @@ -41,6 +41,10 @@ trait PostgresModule extends Jdbc { self => builder.append("(") buildExpr(param) val _ = builder.append(")") + case Expr.FunctionCall0(function) => + builder.append(function.name.name) + builder.append("(") + val _ = builder.append(")") case Expr.FunctionCall1(param, function) => builder.append(function.name.name) builder.append("(") diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index db67303cd..b56dc4cc6 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -38,16 +38,18 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("timeofday") { - val query = select(Sind(30.0)) from customers - - val expected = 0.5 - - val testResult = execute(query).to[Double, Double](identity) - - val assertion = for { - r <- testResult.runCollect - } yield assert(r.head)(equalTo(expected)) - + val query = select(Timeofday()) from customers + + val testResult = execute(query).to[String, String](identity) + + val assertion = + for { + r <- testResult.runCollect + } yield assert(r.head)( + matchesRegex( + "[A-Za-z]{3}\\s[A-Za-z]{3}\\s[0-9]{2}\\s(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9].[0-9]{6}\\s[0-9]{4}\\s[A-Za-z]{3}" + ) + ) assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) } ) diff --git a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala index 30fa79833..c0bbfc250 100644 --- a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala +++ b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala @@ -34,6 +34,8 @@ trait SqlServerModule extends Jdbc { self => builder.append("(") buildExpr(param) val _ = builder.append(")") + case Expr.FunctionCall0(function) => + val _ = builder.append(function.name.name) case Expr.FunctionCall1(param, function) => builder.append(function.name.name) builder.append("(") From 550b3cd32414ef8b703cd42cce09dce36abd0fe6 Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Sat, 21 Nov 2020 18:47:54 +0100 Subject: [PATCH 03/12] add current_time func for postgres --- jdbc/src/main/scala/zio/sql/jdbc.scala | 8 +++-- .../zio/sql/postgresql/PostgresModule.scala | 35 +++++++++++-------- .../zio/sql/postgresql/FunctionDefSpec.scala | 18 ++++++++++ 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/jdbc/src/main/scala/zio/sql/jdbc.scala b/jdbc/src/main/scala/zio/sql/jdbc.scala index 73be39c16..34daae4c2 100644 --- a/jdbc/src/main/scala/zio/sql/jdbc.scala +++ b/jdbc/src/main/scala/zio/sql/jdbc.scala @@ -1,8 +1,8 @@ package zio.sql import java.sql._ - import java.io.IOException +import java.time.format.DateTimeFormatter import zio.{ Chunk, Has, IO, Managed, ZIO, ZLayer, ZManaged } import zio.blocking.Blocking @@ -186,7 +186,11 @@ trait Jdbc extends zio.sql.Sql { ) case TLong => tryDecode[Long](column.fold(resultSet.getLong(_), resultSet.getLong(_))) case TOffsetDateTime => ??? - case TOffsetTime => ??? + case TOffsetTime => + val format = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSx") + tryDecode[java.time.OffsetTime]( + java.time.OffsetTime.parse(column.fold(resultSet.getString(_), resultSet.getString(_)), format) + ) case TShort => tryDecode[Short](column.fold(resultSet.getShort(_), resultSet.getShort(_))) case TString => tryDecode[String](column.fold(resultSet.getString(_), resultSet.getString(_))) case TUUID => diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 6d94cb835..89abc5537 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -1,5 +1,7 @@ package zio.sql.postgresql +import java.time.OffsetTime + import zio.sql.Jdbc /** @@ -7,57 +9,60 @@ import zio.sql.Jdbc trait PostgresModule extends Jdbc { self => object PostgresFunctionDef { - val Sind = FunctionDef[Double, Double](FunctionName("sind")) - val Timeofday = FunctionDef[Nothing, String](FunctionName("timeofday")) + val Sind = FunctionDef[Double, Double](FunctionName("sind")) + val Timeofday = FunctionDef[Nothing, String](FunctionName("timeofday")) + val CurrentTime = FunctionDef[Nothing, OffsetTime](FunctionName("current_time")) } override def renderRead(read: self.Read[_]): String = { val builder = new StringBuilder def buildExpr[A, B](expr: self.Expr[_, A, B]): Unit = expr match { - case Expr.Source(tableName, column) => + case Expr.Source(tableName, column) => val _ = builder.append(tableName).append(".").append(column.name) - case Expr.Unary(base, op) => + case Expr.Unary(base, op) => val _ = builder.append(" ").append(op.symbol) buildExpr(base) - case Expr.Property(base, op) => + case Expr.Property(base, op) => buildExpr(base) val _ = builder.append(" ").append(op.symbol) - case Expr.Binary(left, right, op) => + case Expr.Binary(left, right, op) => buildExpr(left) builder.append(" ").append(op.symbol).append(" ") buildExpr(right) - case Expr.Relational(left, right, op) => + case Expr.Relational(left, right, op) => buildExpr(left) builder.append(" ").append(op.symbol).append(" ") buildExpr(right) - case Expr.In(value, set) => + case Expr.In(value, set) => buildExpr(value) buildReadString(set) - case Expr.Literal(value) => + case Expr.Literal(value) => val _ = builder.append(value.toString) //todo fix escaping - case Expr.AggregationCall(param, aggregation) => + case Expr.AggregationCall(param, aggregation) => builder.append(aggregation.name.name) builder.append("(") buildExpr(param) val _ = builder.append(")") - case Expr.FunctionCall0(function) => + case Expr.FunctionCall0(function) if function.name.name == "current_time" => + val _ = builder.append(function.name.name) + case Expr.FunctionCall0(function) => builder.append(function.name.name) builder.append("(") val _ = builder.append(")") - case Expr.FunctionCall1(param, function) => + case Expr.FunctionCall1(param, function) => builder.append(function.name.name) builder.append("(") buildExpr(param) val _ = builder.append(")") - case Expr.FunctionCall2(param1, param2, function) => + case Expr.FunctionCall2(param1, param2, function) => builder.append(function.name.name) builder.append("(") buildExpr(param1) builder.append(",") buildExpr(param2) val _ = builder.append(")") - case Expr.FunctionCall3(param1, param2, param3, function) => + case Expr.FunctionCall3(param1, param2, param3, function) => builder.append(function.name.name) builder.append("(") buildExpr(param1) @@ -66,7 +71,7 @@ trait PostgresModule extends Jdbc { self => builder.append(",") buildExpr(param3) val _ = builder.append(")") - case Expr.FunctionCall4(param1, param2, param3, param4, function) => + case Expr.FunctionCall4(param1, param2, param3, param4, function) => builder.append(function.name.name) builder.append("(") buildExpr(param1) diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index b56dc4cc6..3ea0daeda 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -1,5 +1,7 @@ package zio.sql.postgresql +import java.time.OffsetTime + import zio.Cause import zio.test._ import zio.test.Assertion._ @@ -51,6 +53,22 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { ) ) assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) + }, + testM("current_time") { + val query = select(CurrentTime()) from customers + + val testResult = execute(query).to[OffsetTime, OffsetTime](identity) + + val assertion = + for { + r <- testResult.runCollect + } yield assert(r.head.toString)( + matchesRegex( + "(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]\\.[0-9]{6}\\+[0-9]{2}:[0-9]{2}" + ) + ) + + assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) } ) } From 52855a5b153de9484fd27772e8c926f8f4cfa6f4 Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Sat, 21 Nov 2020 18:58:39 +0100 Subject: [PATCH 04/12] fix --- .../src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index 3ea0daeda..e8c90690e 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -64,7 +64,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { r <- testResult.runCollect } yield assert(r.head.toString)( matchesRegex( - "(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]\\.[0-9]{6}\\+[0-9]{2}:[0-9]{2}" + "(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]\\.[0-9]{6}Z" ) ) From e1fab9b34c23ad0780c89be61bd8d174d0cf8c46 Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Fri, 27 Nov 2020 18:26:43 +0100 Subject: [PATCH 05/12] fix --- core/jvm/src/main/scala/zio/sql/expr.scala | 8 ++++- .../jvm/src/main/scala/zio/sql/features.scala | 1 + jdbc/src/main/scala/zio/sql/jdbc.scala | 18 +++++++--- .../zio/sql/postgresql/PostgresModule.scala | 36 +++++++++---------- .../zio/sql/postgresql/FunctionDefSpec.scala | 4 +-- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/core/jvm/src/main/scala/zio/sql/expr.scala b/core/jvm/src/main/scala/zio/sql/expr.scala index 8ff81dec4..783dd375f 100644 --- a/core/jvm/src/main/scala/zio/sql/expr.scala +++ b/core/jvm/src/main/scala/zio/sql/expr.scala @@ -158,6 +158,11 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } + sealed case class ParenlessFunctionCall0[Z: TypeTag](function: Z) extends InvariantExpr[Features.Function0, Any, Z] { + def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] + } + +// sealed case class FunctionCall0[Z: TypeTag](function: FunctionDef[Any, Z]) extends InvariantExpr[Features.Function0, Any, Z] { sealed case class FunctionCall0[F, A, B, Z: TypeTag](function: FunctionDef[B, Z]) extends InvariantExpr[F, A, Z] { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } @@ -214,7 +219,8 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { sealed case class FunctionDef[-A, +B](name: FunctionName) { self => - def apply[Source, B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Unit, Source, B1] = +// def apply[Source, B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Unit, Source, B1] = + def apply[B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Features.Function0, Any, B1] = Expr.FunctionCall0(self: FunctionDef[A, B1]) def apply[F, Source, B1 >: B](param1: Expr[F, Source, A])(implicit typeTag: TypeTag[B1]): Expr[F, Source, B1] = diff --git a/core/jvm/src/main/scala/zio/sql/features.scala b/core/jvm/src/main/scala/zio/sql/features.scala index ce1cc13ec..1d5ef2ede 100644 --- a/core/jvm/src/main/scala/zio/sql/features.scala +++ b/core/jvm/src/main/scala/zio/sql/features.scala @@ -11,6 +11,7 @@ trait FeaturesModule { type Union[_, _] type Source type Literal + type Function0 sealed trait IsAggregated[A] diff --git a/jdbc/src/main/scala/zio/sql/jdbc.scala b/jdbc/src/main/scala/zio/sql/jdbc.scala index 34daae4c2..230a96591 100644 --- a/jdbc/src/main/scala/zio/sql/jdbc.scala +++ b/jdbc/src/main/scala/zio/sql/jdbc.scala @@ -2,7 +2,7 @@ package zio.sql import java.sql._ import java.io.IOException -import java.time.format.DateTimeFormatter +import java.time.{ OffsetDateTime, OffsetTime, ZoneOffset } import zio.{ Chunk, Has, IO, Managed, ZIO, ZLayer, ZManaged } import zio.blocking.Blocking @@ -185,11 +185,19 @@ trait Jdbc extends zio.sql.Sql { column.fold(resultSet.getTimestamp(_), resultSet.getTimestamp(_)).toLocalDateTime().toLocalTime() ) case TLong => tryDecode[Long](column.fold(resultSet.getLong(_), resultSet.getLong(_))) - case TOffsetDateTime => ??? + case TOffsetDateTime => + tryDecode[OffsetDateTime]( + column + .fold(resultSet.getTimestamp(_), resultSet.getTimestamp(_)) + .toLocalDateTime() + .atOffset(ZoneOffset.UTC) + ) case TOffsetTime => - val format = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSx") - tryDecode[java.time.OffsetTime]( - java.time.OffsetTime.parse(column.fold(resultSet.getString(_), resultSet.getString(_)), format) + tryDecode[OffsetTime]( + column + .fold(resultSet.getTime(_), resultSet.getTime(_)) + .toLocalTime + .atOffset(ZoneOffset.UTC) ) case TShort => tryDecode[Short](column.fold(resultSet.getShort(_), resultSet.getShort(_))) case TString => tryDecode[String](column.fold(resultSet.getString(_), resultSet.getString(_))) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 89abc5537..1e380166a 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -10,59 +10,59 @@ trait PostgresModule extends Jdbc { self => object PostgresFunctionDef { val Sind = FunctionDef[Double, Double](FunctionName("sind")) - val Timeofday = FunctionDef[Nothing, String](FunctionName("timeofday")) - val CurrentTime = FunctionDef[Nothing, OffsetTime](FunctionName("current_time")) + val Timeofday = Expr.ParenlessFunctionCall0[String]("timeofday") + val CurrentTime = FunctionDef[Any, OffsetTime](FunctionName("current_time")) } override def renderRead(read: self.Read[_]): String = { val builder = new StringBuilder def buildExpr[A, B](expr: self.Expr[_, A, B]): Unit = expr match { - case Expr.Source(tableName, column) => + case Expr.Source(tableName, column) => val _ = builder.append(tableName).append(".").append(column.name) - case Expr.Unary(base, op) => + case Expr.Unary(base, op) => val _ = builder.append(" ").append(op.symbol) buildExpr(base) - case Expr.Property(base, op) => + case Expr.Property(base, op) => buildExpr(base) val _ = builder.append(" ").append(op.symbol) - case Expr.Binary(left, right, op) => + case Expr.Binary(left, right, op) => buildExpr(left) builder.append(" ").append(op.symbol).append(" ") buildExpr(right) - case Expr.Relational(left, right, op) => + case Expr.Relational(left, right, op) => buildExpr(left) builder.append(" ").append(op.symbol).append(" ") buildExpr(right) - case Expr.In(value, set) => + case Expr.In(value, set) => buildExpr(value) buildReadString(set) - case Expr.Literal(value) => + case Expr.Literal(value) => val _ = builder.append(value.toString) //todo fix escaping - case Expr.AggregationCall(param, aggregation) => + case Expr.AggregationCall(param, aggregation) => builder.append(aggregation.name.name) builder.append("(") buildExpr(param) val _ = builder.append(")") - case Expr.FunctionCall0(function) if function.name.name == "current_time" => - val _ = builder.append(function.name.name) - case Expr.FunctionCall0(function) => - builder.append(function.name.name) + case Expr.ParenlessFunctionCall0(function) => + builder.append(function) builder.append("(") val _ = builder.append(")") - case Expr.FunctionCall1(param, function) => + case Expr.FunctionCall0(function) => + val _ = builder.append(function.name.name) + case Expr.FunctionCall1(param, function) => builder.append(function.name.name) builder.append("(") buildExpr(param) val _ = builder.append(")") - case Expr.FunctionCall2(param1, param2, function) => + case Expr.FunctionCall2(param1, param2, function) => builder.append(function.name.name) builder.append("(") buildExpr(param1) builder.append(",") buildExpr(param2) val _ = builder.append(")") - case Expr.FunctionCall3(param1, param2, param3, function) => + case Expr.FunctionCall3(param1, param2, param3, function) => builder.append(function.name.name) builder.append("(") buildExpr(param1) @@ -71,7 +71,7 @@ trait PostgresModule extends Jdbc { self => builder.append(",") buildExpr(param3) val _ = builder.append(")") - case Expr.FunctionCall4(param1, param2, param3, param4, function) => + case Expr.FunctionCall4(param1, param2, param3, param4, function) => builder.append(function.name.name) builder.append("(") buildExpr(param1) diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index e8c90690e..23e43f641 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -40,7 +40,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("timeofday") { - val query = select(Timeofday()) from customers + val query = select(Timeofday) from customers val testResult = execute(query).to[String, String](identity) @@ -64,7 +64,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { r <- testResult.runCollect } yield assert(r.head.toString)( matchesRegex( - "(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]\\.[0-9]{6}Z" + "(2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]Z" ) ) From a7bec7502022ccdf9f89fbb15220d0d0eeda1f7e Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Fri, 27 Nov 2020 18:29:49 +0100 Subject: [PATCH 06/12] lint --- core/jvm/src/main/scala/zio/sql/expr.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/jvm/src/main/scala/zio/sql/expr.scala b/core/jvm/src/main/scala/zio/sql/expr.scala index 783dd375f..e19c6c588 100644 --- a/core/jvm/src/main/scala/zio/sql/expr.scala +++ b/core/jvm/src/main/scala/zio/sql/expr.scala @@ -158,7 +158,8 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } - sealed case class ParenlessFunctionCall0[Z: TypeTag](function: Z) extends InvariantExpr[Features.Function0, Any, Z] { + sealed case class ParenlessFunctionCall0[Z: TypeTag](function: Z) + extends InvariantExpr[Features.Function0, Any, Z] { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } From 429c8c84b560730e308288c4cffb3fd4818cb94b Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Fri, 27 Nov 2020 18:37:21 +0100 Subject: [PATCH 07/12] fix render --- .../src/main/scala/zio/sql/postgresql/PostgresModule.scala | 4 ++-- .../src/main/scala/zio/sql/sqlserver/SqlServerModule.scala | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 1e380166a..aa6d79c04 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -45,11 +45,11 @@ trait PostgresModule extends Jdbc { self => buildExpr(param) val _ = builder.append(")") case Expr.ParenlessFunctionCall0(function) => + val _ = builder.append(function) + case Expr.FunctionCall0(function) => builder.append(function) builder.append("(") val _ = builder.append(")") - case Expr.FunctionCall0(function) => - val _ = builder.append(function.name.name) case Expr.FunctionCall1(param, function) => builder.append(function.name.name) builder.append("(") diff --git a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala index c0bbfc250..285cf4421 100644 --- a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala +++ b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala @@ -34,8 +34,12 @@ trait SqlServerModule extends Jdbc { self => builder.append("(") buildExpr(param) val _ = builder.append(")") + case Expr.ParenlessFunctionCall0(function) => + val _ = builder.append(function) case Expr.FunctionCall0(function) => - val _ = builder.append(function.name.name) + builder.append(function) + builder.append("(") + val _ = builder.append(")") case Expr.FunctionCall1(param, function) => builder.append(function.name.name) builder.append("(") From 82d18c62450c5f74734fbb4c94be3c761ce84e7d Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Fri, 27 Nov 2020 19:55:28 +0100 Subject: [PATCH 08/12] fix parenless function --- core/jvm/src/main/scala/zio/sql/expr.scala | 8 +++++++- .../main/scala/zio/sql/postgresql/PostgresModule.scala | 8 ++++---- .../test/scala/zio/sql/postgresql/FunctionDefSpec.scala | 2 +- .../main/scala/zio/sql/sqlserver/SqlServerModule.scala | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/core/jvm/src/main/scala/zio/sql/expr.scala b/core/jvm/src/main/scala/zio/sql/expr.scala index e19c6c588..3a7a1b6a9 100644 --- a/core/jvm/src/main/scala/zio/sql/expr.scala +++ b/core/jvm/src/main/scala/zio/sql/expr.scala @@ -158,7 +158,7 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } - sealed case class ParenlessFunctionCall0[Z: TypeTag](function: Z) + sealed case class ParenlessFunctionCall0[Z: TypeTag](function: ParenlessDef[Z]) extends InvariantExpr[Features.Function0, Any, Z] { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } @@ -201,6 +201,12 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { } } + sealed case class ParenlessDef[+B](name: FunctionName) { self => + + def apply[B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Features.Function0, Any, B1] = + Expr.ParenlessFunctionCall0(self: ParenlessDef[B1]) + } + sealed case class AggregationDef[-A, +B](name: FunctionName) { self => def apply[F, Source, B1 >: B](expr: Expr[F, Source, A])(implicit diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index aa6d79c04..1f834ec66 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -10,8 +10,8 @@ trait PostgresModule extends Jdbc { self => object PostgresFunctionDef { val Sind = FunctionDef[Double, Double](FunctionName("sind")) - val Timeofday = Expr.ParenlessFunctionCall0[String]("timeofday") - val CurrentTime = FunctionDef[Any, OffsetTime](FunctionName("current_time")) + val Timeofday = FunctionDef[Any, String](FunctionName("timeofday")) + val CurrentTime = ParenlessDef[OffsetTime](FunctionName("current_time")) } override def renderRead(read: self.Read[_]): String = { @@ -45,9 +45,9 @@ trait PostgresModule extends Jdbc { self => buildExpr(param) val _ = builder.append(")") case Expr.ParenlessFunctionCall0(function) => - val _ = builder.append(function) + val _ = builder.append(function.name.name) case Expr.FunctionCall0(function) => - builder.append(function) + builder.append(function.name.name) builder.append("(") val _ = builder.append(")") case Expr.FunctionCall1(param, function) => diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index 23e43f641..4cade08eb 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -40,7 +40,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("timeofday") { - val query = select(Timeofday) from customers + val query = select(Timeofday()) from customers val testResult = execute(query).to[String, String](identity) diff --git a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala index 285cf4421..1a9211c8a 100644 --- a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala +++ b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala @@ -35,9 +35,9 @@ trait SqlServerModule extends Jdbc { self => buildExpr(param) val _ = builder.append(")") case Expr.ParenlessFunctionCall0(function) => - val _ = builder.append(function) + val _ = builder.append(function.name.name) case Expr.FunctionCall0(function) => - builder.append(function) + builder.append(function.name.name) builder.append("(") val _ = builder.append(")") case Expr.FunctionCall1(param, function) => From 7ecd2146038f81d8d9623a3e7ff49d92d1bd8c36 Mon Sep 17 00:00:00 2001 From: Antonio Grandinetti Date: Sat, 28 Nov 2020 09:44:47 +0100 Subject: [PATCH 09/12] fix parenless function and funcall0 --- core/jvm/src/main/scala/zio/sql/expr.scala | 17 +++++------------ .../zio/sql/postgresql/PostgresModule.scala | 6 +++--- .../zio/sql/postgresql/FunctionDefSpec.scala | 4 ++-- .../zio/sql/sqlserver/SqlServerModule.scala | 2 +- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/core/jvm/src/main/scala/zio/sql/expr.scala b/core/jvm/src/main/scala/zio/sql/expr.scala index 3a7a1b6a9..1a3ed93ed 100644 --- a/core/jvm/src/main/scala/zio/sql/expr.scala +++ b/core/jvm/src/main/scala/zio/sql/expr.scala @@ -158,13 +158,13 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } - sealed case class ParenlessFunctionCall0[Z: TypeTag](function: ParenlessDef[Z]) + sealed case class ParenlessFunctionCall0[Z: TypeTag](function: FunctionName) extends InvariantExpr[Features.Function0, Any, Z] { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } -// sealed case class FunctionCall0[Z: TypeTag](function: FunctionDef[Any, Z]) extends InvariantExpr[Features.Function0, Any, Z] { - sealed case class FunctionCall0[F, A, B, Z: TypeTag](function: FunctionDef[B, Z]) extends InvariantExpr[F, A, Z] { + sealed case class FunctionCall0[Z: TypeTag](function: FunctionDef[Any, Z]) + extends InvariantExpr[Features.Function0, Any, Z] { def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]] } @@ -201,12 +201,6 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { } } - sealed case class ParenlessDef[+B](name: FunctionName) { self => - - def apply[B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Features.Function0, Any, B1] = - Expr.ParenlessFunctionCall0(self: ParenlessDef[B1]) - } - sealed case class AggregationDef[-A, +B](name: FunctionName) { self => def apply[F, Source, B1 >: B](expr: Expr[F, Source, A])(implicit @@ -226,9 +220,8 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule { sealed case class FunctionDef[-A, +B](name: FunctionName) { self => -// def apply[Source, B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Unit, Source, B1] = - def apply[B1 >: B]()(implicit typeTag: TypeTag[B1]): Expr[Features.Function0, Any, B1] = - Expr.FunctionCall0(self: FunctionDef[A, B1]) + def apply[B1 >: B]()(implicit ev: Any <:< A, typeTag: TypeTag[B1]): Expr[Features.Function0, Any, B1] = + Expr.FunctionCall0(self.asInstanceOf[FunctionDef[Any, B1]]) def apply[F, Source, B1 >: B](param1: Expr[F, Source, A])(implicit typeTag: TypeTag[B1]): Expr[F, Source, B1] = Expr.FunctionCall1(param1, self: FunctionDef[A, B1]) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 1f834ec66..deb227f88 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -10,8 +10,8 @@ trait PostgresModule extends Jdbc { self => object PostgresFunctionDef { val Sind = FunctionDef[Double, Double](FunctionName("sind")) - val Timeofday = FunctionDef[Any, String](FunctionName("timeofday")) - val CurrentTime = ParenlessDef[OffsetTime](FunctionName("current_time")) + val TimeOfDay = FunctionDef[Any, String](FunctionName("timeofday")) + val CurrentTime = Expr.ParenlessFunctionCall0[OffsetTime](FunctionName("current_time")) } override def renderRead(read: self.Read[_]): String = { @@ -45,7 +45,7 @@ trait PostgresModule extends Jdbc { self => buildExpr(param) val _ = builder.append(")") case Expr.ParenlessFunctionCall0(function) => - val _ = builder.append(function.name.name) + val _ = builder.append(function.name) case Expr.FunctionCall0(function) => builder.append(function.name.name) builder.append("(") diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index 4cade08eb..374cabf01 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -40,7 +40,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("timeofday") { - val query = select(Timeofday()) from customers + val query = select(TimeOfDay()) from customers val testResult = execute(query).to[String, String](identity) @@ -55,7 +55,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("current_time") { - val query = select(CurrentTime()) from customers + val query = select(CurrentTime) from customers val testResult = execute(query).to[OffsetTime, OffsetTime](identity) diff --git a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala index 1a9211c8a..b374af3ff 100644 --- a/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala +++ b/sqlserver/src/main/scala/zio/sql/sqlserver/SqlServerModule.scala @@ -35,7 +35,7 @@ trait SqlServerModule extends Jdbc { self => buildExpr(param) val _ = builder.append(")") case Expr.ParenlessFunctionCall0(function) => - val _ = builder.append(function.name.name) + val _ = builder.append(function.name) case Expr.FunctionCall0(function) => builder.append(function.name.name) builder.append("(") From 99fbed9cc2088281557fa98c14608e2177e76c6d Mon Sep 17 00:00:00 2001 From: Jakub Czuchnowski Date: Sat, 28 Nov 2020 17:50:30 +0100 Subject: [PATCH 10/12] Update PostgresModule.scala --- .../main/scala/zio/sql/postgresql/PostgresModule.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index fbc5729b3..65af8d99f 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -163,11 +163,11 @@ trait PostgresModule extends Jdbc { self => renderExpr(p) render(")") case Expr.ParenlessFunctionCall0(fn) => - val _ = builder.append(fn.name) + val _ = render(fn.name) case Expr.FunctionCall0(fn) => - builder.append(fn.name.name) - builder.append("(") - val _ = builder.append(")") + render(fn.name.name) + render("(") + val _ = render(")") case Expr.FunctionCall1(p, fn) => render(fn.name.name, "(") renderExpr(p) From b4d646f4b182159756fde31db4a26e43ce481cef Mon Sep 17 00:00:00 2001 From: Jakub Czuchnowski Date: Sat, 28 Nov 2020 18:08:16 +0100 Subject: [PATCH 11/12] Update PostgresModule.scala --- .../src/main/scala/zio/sql/postgresql/PostgresModule.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala index 65af8d99f..aca669606 100644 --- a/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala +++ b/postgres/src/main/scala/zio/sql/postgresql/PostgresModule.scala @@ -12,14 +12,14 @@ trait PostgresModule extends Jdbc { self => val TimeOfDay = FunctionDef[Any, String](FunctionName("timeofday")) val CurrentTime = Expr.ParenlessFunctionCall0[OffsetTime](FunctionName("current_time")) val CharLength = FunctionDef[String, Int](FunctionName("character_length")) - val Localtime = FunctionDef[Any, LocalTime](FunctionName("localtime")) + val Localtime = Expr.ParenlessFunctionCall0[LocalTime](FunctionName("localtime")) val LocaltimeWithPrecision = FunctionDef[Int, LocalTime](FunctionName("localtime")) - val Localtimestamp = FunctionDef[Any, Instant](FunctionName("localtimestamp")) + val Localtimestamp = Expr.ParenlessFunctionCall0[Instant](FunctionName("localtimestamp")) val LocaltimestampWithPrecision = FunctionDef[Int, Instant](FunctionName("localtimestamp")) val Md5 = FunctionDef[String, String](FunctionName("md5")) val ParseIdent = FunctionDef[String, String](FunctionName("parse_ident")) val Chr = FunctionDef[Int, String](FunctionName("chr")) - val CurrentDate = FunctionDef[Any, LocalDate](FunctionName("current_date")) + val CurrentDate = Expr.ParenlessFunctionCall0[LocalDate](FunctionName("current_date")) val Initcap = FunctionDef[String, String](FunctionName("initcap")) val Repeat = FunctionDef[(String, Int), String](FunctionName("repeat")) val Reverse = FunctionDef[String, String](FunctionName("reverse")) From 6254ec8d48ab0816d67a0af7c4d1b6e253bfc94b Mon Sep 17 00:00:00 2001 From: Jakub Czuchnowski Date: Sat, 28 Nov 2020 18:09:59 +0100 Subject: [PATCH 12/12] Update FunctionDefSpec.scala --- .../src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala index 6834a25ec..aaa288658 100644 --- a/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala +++ b/postgres/src/test/scala/zio/sql/postgresql/FunctionDefSpec.scala @@ -228,7 +228,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("localtime") { - val query = select(Localtime()) from customers + val query = select(Localtime) from customers val testResult = execute(query).to[LocalTime, LocalTime](identity) @@ -251,7 +251,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("localtimestamp") { - val query = select(Localtimestamp()) from customers + val query = select(Localtimestamp) from customers val testResult = execute(query).to[Instant, Instant](identity) @@ -374,7 +374,7 @@ object FunctionDefSpec extends PostgresRunnableSpec with ShopSchema { assertion.mapErrorCause(cause => Cause.stackless(cause.untraced)) }, testM("current_date") { - val query = select(CurrentDate()) from customers + val query = select(CurrentDate) from customers val expected = LocalDate.now()