Skip to content

Commit

Permalink
zio#732 Formatting, tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bogatyra committed Aug 1, 2022
1 parent d89a961 commit e36800b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
1 change: 0 additions & 1 deletion core/jvm/src/main/scala/zio/sql/expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {
val Ascii = FunctionDef[String, Int](FunctionName("ascii"))
val CharLength = FunctionDef[String, Int](FunctionName("character_length"))
val Concat = FunctionDef[(String, String), String](FunctionName("concat")) // todo varargs
val ConcatWs2 = FunctionDef[(String, String), String](FunctionName("concat_ws"))
val ConcatWs3 = FunctionDef[(String, String, String), String](FunctionName("concat_ws"))
val ConcatWs4 = FunctionDef[(String, String, String, String), String](FunctionName("concat_ws"))
val Lower = FunctionDef[String, String](FunctionName("lower"))
Expand Down
49 changes: 26 additions & 23 deletions oracle/src/main/scala/zio/sql/oracle/OracleRenderModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import scala.collection.mutable
import java.time.OffsetDateTime
import java.time.YearMonth
import java.time.Duration
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
import java.time.format.{ DateTimeFormatter, DateTimeFormatterBuilder }
import java.time.temporal.ChronoField._

trait OracleRenderModule extends OracleSqlModule { self =>
Expand Down Expand Up @@ -46,8 +46,8 @@ trait OracleRenderModule extends OracleSqlModule { self =>
render.toString
}

private object DateFormats {
val fmtTime = new DateTimeFormatterBuilder()
private object DateTimeFormats {
val fmtTime = new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
Expand All @@ -57,53 +57,57 @@ trait OracleRenderModule extends OracleSqlModule { self =>
.appendOffset("+HH:MM", "Z")
.toFormatter()

val fmtTimeOffset = new DateTimeFormatterBuilder()
val fmtTimeOffset = new DateTimeFormatterBuilder()
.append(fmtTime)
.appendFraction(NANO_OF_SECOND, 9, 9, true)
.toFormatter()

val fmtDateTime = new DateTimeFormatterBuilder().parseCaseInsensitive
val fmtDateTime = new DateTimeFormatterBuilder().parseCaseInsensitive
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.append(fmtTime)
.toFormatter()

val fmtDateTimeOffset = new DateTimeFormatterBuilder().parseCaseInsensitive
val fmtDateTimeOffset = new DateTimeFormatterBuilder().parseCaseInsensitive
.append(fmtDateTime)
.appendOffset("+HH:MM", "Z")
.toFormatter()
}

private def buildLit(lit: self.Expr.Literal[_])(builder: StringBuilder): Unit = {
import TypeTag._
val value = lit.value
val value = lit.value
lit.typeTag match {
case TInstant =>
val _ = builder.append( s"""TO_TIMESTAMP_TZ('${DateFormats.fmtDateTimeOffset.format(
value.asInstanceOf[Instant]
)}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9TZH:TZM')""")
val _ = builder.append(s"""TO_TIMESTAMP_TZ('${DateTimeFormats.fmtDateTimeOffset.format(
value.asInstanceOf[Instant]
)}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9TZH:TZM')""")
case TLocalTime =>
val localTime = value.asInstanceOf[LocalTime]
val _ = builder.append(
val _ = builder.append(
s"INTERVAL '${localTime.getHour}:${localTime.getMinute}:${localTime.getSecond}.${localTime.getNano}' HOUR TO SECOND(9)"
)
case TLocalDate =>
val _ = builder.append(s"TO_DATE('${DateTimeFormatter.ISO_LOCAL_DATE.format(value.asInstanceOf[LocalDate])}', 'SYYYY-MM-DD')")
val _ = builder.append(
s"TO_DATE('${DateTimeFormatter.ISO_LOCAL_DATE.format(value.asInstanceOf[LocalDate])}', 'SYYYY-MM-DD')"
)
case TLocalDateTime =>
val _ = builder.append(s"""TO_TIMESTAMP('${DateFormats.fmtDateTime.format(value.asInstanceOf[LocalDateTime])}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9')""")
val _ = builder.append(s"""TO_TIMESTAMP('${DateTimeFormats.fmtDateTime.format(
value.asInstanceOf[LocalDateTime]
)}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9')""")
case TZonedDateTime =>
val _ = builder.append( s"""TO_TIMESTAMP_TZ('${DateFormats.fmtDateTimeOffset.format(
value.asInstanceOf[ZonedDateTime]
)}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9TZH:TZM')""")
val _ = builder.append(s"""TO_TIMESTAMP_TZ('${DateTimeFormats.fmtDateTimeOffset.format(
value.asInstanceOf[ZonedDateTime]
)}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9TZH:TZM')""")
case TOffsetTime =>
val _ = builder.append(
s"TO_TIMESTAMP_TZ('${DateFormats.fmtTimeOffset.format(value.asInstanceOf[OffsetTime])}', 'HH24:MI:SS.FF9TZH:TZM')"
s"TO_TIMESTAMP_TZ('${DateTimeFormats.fmtTimeOffset.format(value.asInstanceOf[OffsetTime])}', 'HH24:MI:SS.FF9TZH:TZM')"
)
case TOffsetDateTime =>
val _ = builder.append(
s"""TO_TIMESTAMP_TZ('${DateFormats.fmtDateTimeOffset.format(
value.asInstanceOf[OffsetDateTime]
)}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9TZH:TZM')"""
s"""TO_TIMESTAMP_TZ('${DateTimeFormats.fmtDateTimeOffset.format(
value.asInstanceOf[OffsetDateTime]
)}', 'SYYYY-MM-DD"T"HH24:MI:SS.FF9TZH:TZM')"""
)

case TBoolean =>
Expand All @@ -123,7 +127,7 @@ trait OracleRenderModule extends OracleSqlModule { self =>
case TDouble =>
val _ = builder.append(value)
case TFloat =>
val _ = builder.append(value)
val _ = builder.append(value)
case TInt =>
val _ = builder.append(value)
case TLong =>
Expand All @@ -141,7 +145,6 @@ trait OracleRenderModule extends OracleSqlModule { self =>
}
}


// TODO: to consider the refactoring and using the implicit `Renderer`, see `renderExpr` in `PostgresRenderModule`
private def buildExpr[A, B](expr: self.Expr[_, A, B], builder: StringBuilder): Unit = expr match {
case Expr.Subselect(subselect) =>
Expand Down Expand Up @@ -184,7 +187,7 @@ trait OracleRenderModule extends OracleSqlModule { self =>
val _ = builder.append("1 = 1")
case Expr.Literal(false) =>
val _ = builder.append("0 = 1")
case literal: Expr.Literal[_] =>
case literal: Expr.Literal[_] =>
val _ = buildLit(literal)(builder)
case Expr.AggregationCall(param, aggregation) =>
builder.append(aggregation.name.name)
Expand Down
2 changes: 1 addition & 1 deletion oracle/src/main/scala/zio/sql/oracle/OracleSqlModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait OracleSqlModule extends Sql { self =>
}

object Dual {
val dual = (string("dummy")).table("dual")
val dual = (string("dummy")).table("dual")
val (dummy) = dual.columns
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ object CommonFunctionDefSpec extends OracleRunnableSpec with ShopSchema {
assertZIO(execute(select(Sin(1.0)).from(dual)).runHead.some)(equalTo(0.8414709848078965))
},
test("sqrt") {
val query = select(Sqrt(121.0)).from(dual)
val query = select(Sqrt(121.0)).from(dual)
val expected = 11.0

val testResult = execute(query)
Expand Down Expand Up @@ -220,7 +220,7 @@ object CommonFunctionDefSpec extends OracleRunnableSpec with ShopSchema {
} yield assert(r.head)(equalTo(expected))

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
} @@ TestAspect.ignore,
} @@ TestAspect.ignore @@ TestAspect.tag("lengthb"),
test("ascii") {
val query = select(Ascii("""x""")).from(dual)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ trait SqlServerRenderModule extends SqlServerSqlModule { self =>

private def renderLit(lit: self.Expr.Literal[_])(implicit render: Renderer): Unit = {
import TypeTag._
val value = lit.value
val value = lit.value
lit.typeTag match {
case TInstant =>
render(s"'${DateTimeFormatter.ISO_INSTANT.format(value.asInstanceOf[Instant])}'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import zio.test.Assertion._
import zio.test._

object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {
import FunctionDef.{CharLength => _, _}
import FunctionDef.{ CharLength => _, _ }
import DbSchema._

private def collectAndCompare[R, E](
Expand All @@ -15,7 +15,7 @@ object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {
) =
assertZIO(testResult.runCollect)(hasSameElementsDistinct(expected))

override def specLayered = suite("Postgres Common FunctionDef")(
override def specLayered = suite("SqlServer Common FunctionDef")(
suite("Schema dependent tests")(
test("concat_ws #2 - combine columns") {

Expand Down Expand Up @@ -137,16 +137,16 @@ object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {
},
test("log") {
assertZIO(execute(select(Log(32.0, 2.0))).runHead.some)(equalTo(5.0))
} @@ TestAspect.ignore,
} @@ TestAspect.tag("different order of params"),
test("acos") {
assertZIO(execute(select(Acos(-1.0))).runHead.some)(equalTo(3.141592653589793))
},
test("asin") {
assertZIO(execute(select(Asin(0.5))).runHead.some)(equalTo(0.5235987755982989))
},
/* test("ln") {
test("ln") {
assertZIO(execute(select(Ln(3.0))).runHead.some)(equalTo(1.0986122886681097))
},*/
} @@ TestAspect.ignore @@ TestAspect.tag("log with one param"),
test("atan") {
assertZIO(execute(select(Atan(10.0))).runHead.some)(equalTo(1.4711276743037347))
},
Expand All @@ -161,7 +161,7 @@ object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {
},
test("ceil") {
assertZIO(execute(select(Ceil(53.7), Ceil(-53.7))).runHead.some)(equalTo((54.0, -53.0)))
} @@ TestAspect.ignore,
} @@ TestAspect.ignore @@ TestAspect.tag("cailing"),
test("sin") {
assertZIO(execute(select(Sin(1.0))).runHead.some)(equalTo(0.8414709848078965))
},
Expand All @@ -177,7 +177,7 @@ object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {
test("round") {
val query = select(Round(10.8124, 2))

val expected = 10.81
val expected = 10.81
val testResult = execute(query)

val assertion = for {
Expand Down Expand Up @@ -238,7 +238,7 @@ object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},
/* test("mod") {
test("mod") {
val query = select(Mod(-15.0, -4.0))

val expected = -3.0
Expand All @@ -250,7 +250,7 @@ object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {
} yield assert(r.head)(equalTo(expected))

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
},*/
} @@ TestAspect.ignore @@ TestAspect.tag("to use % instead"),
test("octet_length") {
val query = select(OctetLength("josé"))

Expand All @@ -263,7 +263,7 @@ object CommonFunctionDefSpec extends SqlServerRunnableSpec with DbSchema {
} yield assert(r.head)(equalTo(expected))

assertion.mapErrorCause(cause => Cause.stackless(cause.untraced))
} @@ TestAspect.ignore,
} @@ TestAspect.ignore @@ TestAspect.tag("datalength"),
test("ascii") {
val query = select(Ascii("""x"""))

Expand Down

0 comments on commit e36800b

Please sign in to comment.