Skip to content

Commit

Permalink
Merge pull request zio#748 from jczuchnowski/zio-schema-0.2.1
Browse files Browse the repository at this point in the history
Update zio-schema to 0.2.1
  • Loading branch information
jczuchnowski committed Oct 2, 2022
2 parents e847ed0 + 9c57450 commit 6ce5638
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ addCommandAlias("fmt", "fmtOnce;fmtOnce")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

val zioVersion = "2.0.2"
val zioSchemaVersion = "0.2.0"
val zioSchemaVersion = "0.2.1"
val testcontainersVersion = "1.17.3"
val testcontainersScalaVersion = "0.40.10"
val logbackVersion = "1.2.11"
Expand Down
5 changes: 3 additions & 2 deletions mysql/src/main/scala/zio/sql/mysql/MysqlRenderModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ trait MysqlRenderModule extends MysqlSqlModule { self =>

private def renderInsertValue[Z](z: Z)(implicit render: Renderer, schema: Schema[Z]): Unit =
schema.toDynamic(z) match {
case DynamicValue.Record(listMap) =>
case DynamicValue.Record(_, listMap) =>
listMap.values.toList match {
case head :: Nil => renderDynamicValue(head)
case head :: next =>
Expand All @@ -147,7 +147,7 @@ trait MysqlRenderModule extends MysqlSqlModule { self =>
renderDynamicValues(next)
case Nil => ()
}
case value => renderDynamicValue(value)
case value => renderDynamicValue(value)
}

private def renderDynamicValues(dynValues: List[DynamicValue])(implicit render: Renderer): Unit =
Expand All @@ -170,6 +170,7 @@ trait MysqlRenderModule extends MysqlSqlModule { self =>
render(value)
case StandardType.InstantType(formatter) =>
render(s"'${formatter.format(value.asInstanceOf[Instant])}'")
case ByteType => render(s"'${value}'")
case CharType => render(s"'${value}'")
case IntType => render(value)
case StandardType.MonthDayType => render(s"'${value}'")
Expand Down
1 change: 1 addition & 0 deletions mysql/src/test/scala/zio/sql/mysql/MysqlModuleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ object MysqlModuleSpec extends MysqlRunnableSpec with ShopSchema {
)
implicit val customerRowSchema =
Schema.CaseClass5[UUID, LocalDate, String, String, Boolean, CustomerRow](
TypeId.parse("zio.sql.mysql.CustomerRow"),
Schema.Field("id", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field(
"dateOfBirth",
Expand Down
13 changes: 9 additions & 4 deletions oracle/src/main/scala/zio/sql/oracle/OracleRenderModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ trait OracleRenderModule extends OracleSqlModule { self =>

def renderInsertValue[Z](z: Z, builder: StringBuilder)(implicit schema: Schema[Z]): Unit =
schema.toDynamic(z) match {
case DynamicValue.Record(listMap) =>
case DynamicValue.Record(_, listMap) =>
listMap.values.toList match {
case head :: Nil => renderDynamicValue(head, builder)
case head :: next =>
Expand All @@ -436,7 +436,7 @@ trait OracleRenderModule extends OracleSqlModule { self =>
renderDynamicValues(next, builder)
case Nil => ()
}
case value => renderDynamicValue(value, builder)
case value => renderDynamicValue(value, builder)
}

def renderDynamicValue(dynValue: DynamicValue, builder: StringBuilder): Unit =
Expand Down Expand Up @@ -467,8 +467,8 @@ trait OracleRenderModule extends OracleSqlModule { self =>
val chunk = value.asInstanceOf[Chunk[Object]]
builder.append("'")
for (b <- chunk)
builder.append(String.format("%02x", b))
builder.append(s"'")
builder.append("%02x".format(b))
builder.append("'")
()
case StandardType.LocalDateTimeType(formatter) =>
builder.append(
Expand Down Expand Up @@ -557,6 +557,11 @@ trait OracleRenderModule extends OracleSqlModule { self =>
case DynamicValue.NoneValue =>
builder.append("null")
()
case DynamicValue.Sequence(chunk) =>
builder.append("'")
for (DynamicValue.Primitive(v, _) <- chunk)
builder.append("%02x".format(v))
val _ = builder.append("'")
case _ => ()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import zio.test._
import scala.language.postfixOps
import java.util.UUID
import java.time.format.DateTimeFormatter
import zio.schema.Schema
import zio.schema.{ Schema, TypeId }
import zio.prelude._
import java.time.{ LocalDate, LocalDateTime, Month, Year, YearMonth, ZoneOffset, ZonedDateTime }

Expand Down Expand Up @@ -75,6 +75,7 @@ object OracleSqlModuleSpec extends OracleRunnableSpec with ShopSchema {
)
implicit val customerRowSchema =
Schema.CaseClass5[UUID, LocalDate, String, String, Boolean, CustomerRow](
TypeId.parse("zio.sql.oracle.CustomerRow"),
Schema.Field("id", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field(
"dateOfBirth",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ trait PostgresRenderModule extends PostgresSqlModule { self =>

private def renderInserValue[Z](z: Z)(implicit render: Renderer, schema: Schema[Z]): Unit =
schema.toDynamic(z) match {
case DynamicValue.Record(listMap) =>
case DynamicValue.Record(_, listMap) =>
listMap.values.toList match {
case head :: Nil => renderDynamicValue(head)
case head :: next =>
Expand All @@ -73,7 +73,7 @@ trait PostgresRenderModule extends PostgresSqlModule { self =>
renderDynamicValues(next)
case Nil => ()
}
case value => renderDynamicValue(value)
case value => renderDynamicValue(value)
}

private def renderDynamicValues(dynValues: List[DynamicValue])(implicit render: Renderer): Unit =
Expand All @@ -98,6 +98,7 @@ trait PostgresRenderModule extends PostgresSqlModule { self =>
render(value)
case StandardType.InstantType(formatter) =>
render(s"'${formatter.format(value.asInstanceOf[Instant])}'")
case ByteType => render(s"'${value}'")
case CharType => render(s"'${value}'")
case IntType => render(value)
case StandardType.MonthDayType => render(s"'${value}'")
Expand Down Expand Up @@ -201,7 +202,7 @@ trait PostgresRenderModule extends PostgresSqlModule { self =>
}
case TByteArray =>
render(
lit.value.asInstanceOf[Chunk[Byte]].map("""\%03o""" format _).mkString("E\'", "", "\'")
lit.value.asInstanceOf[Chunk[Byte]].map("""\%03o""".format(_)).mkString("E\'", "", "\'")
) // todo fix `cast` infers correctly but map doesn't work for some reason
case tt @ TChar =>
render("'", tt.cast(lit.value), "'") // todo is this the same as a string? fix escaping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio.test._
import java.time._
import java.util.UUID
import scala.language.postfixOps
import zio.schema.Schema
import zio.schema.{ Schema, TypeId }
import java.time.format.DateTimeFormatter

object PostgresSqlModuleSpec extends PostgresRunnableSpec with DbSchema {
Expand Down Expand Up @@ -424,6 +424,7 @@ object PostgresSqlModuleSpec extends PostgresRunnableSpec with DbSchema {

implicit val customerRowSchema =
Schema.CaseClass7[UUID, String, String, Boolean, LocalDate, String, ZonedDateTime, CustomerRow](
TypeId.parse("zio.sql.postgres.CustomerRow"),
Schema.Field("id", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field("firstName", Schema.primitive[String](zio.schema.StandardType.StringType)),
Schema.Field("lastName", Schema.primitive[String](zio.schema.StandardType.StringType)),
Expand Down Expand Up @@ -480,6 +481,7 @@ object PostgresSqlModuleSpec extends PostgresRunnableSpec with DbSchema {
final case class InputOrders(uuid: UUID, customerId: UUID, localDate: LocalDate)

implicit val inputOrdersSchema = Schema.CaseClass3[UUID, UUID, LocalDate, InputOrders](
TypeId.parse("zio.sql.postgres.InputOrders"),
Schema.Field("uuid", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field("customerId", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field(
Expand Down Expand Up @@ -532,6 +534,7 @@ object PostgresSqlModuleSpec extends PostgresRunnableSpec with DbSchema {
.transform(bigDec => new BigDecimal(bigDec, java.math.MathContext.DECIMAL128), _.bigDecimal)

implicit val orderDetailsRowSchema = Schema.CaseClass4[UUID, UUID, Int, BigDecimal, OrderDetailsRow](
TypeId.parse("zio.sql.postgres.OrderDetailsRow"),
Schema.Field("orderId", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field("productId", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field("quantity", Schema.primitive[Int](zio.schema.StandardType.IntType)),
Expand Down Expand Up @@ -624,6 +627,7 @@ object PostgresSqlModuleSpec extends PostgresRunnableSpec with DbSchema {
)

implicit val personSchema = Schema.CaseClass4[UUID, String, String, Option[LocalDate], Person](
TypeId.parse("zio.sql.postgres.Person"),
Schema.Field("id", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field("firstName", Schema.primitive[String](zio.schema.StandardType.StringType)),
Schema.Field("lastName", Schema.primitive[String](zio.schema.StandardType.StringType)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ trait SqlServerRenderModule extends SqlServerSqlModule { self =>

private def buildInsertValue[Z](z: Z)(implicit render: Renderer, schema: Schema[Z]): Unit =
schema.toDynamic(z) match {
case DynamicValue.Record(listMap) =>
case DynamicValue.Record(_, listMap) =>
listMap.values.toList match {
case head :: Nil => buildDynamicValue(head)
case head :: next =>
Expand All @@ -442,7 +442,7 @@ trait SqlServerRenderModule extends SqlServerSqlModule { self =>
buildDynamicValues(next)
case Nil => ()
}
case value => buildDynamicValue(value)
case value => buildDynamicValue(value)
}

private def buildDynamicValues(dynValues: List[DynamicValue])(implicit render: Renderer): Unit =
Expand All @@ -467,14 +467,15 @@ trait SqlServerRenderModule extends SqlServerSqlModule { self =>
render(value)
case StandardType.InstantType(formatter) =>
render(s"'${formatter.format(value.asInstanceOf[Instant])}'")
case ByteType => render(s"${value}")
case CharType => render(s"N'${value}'")
case IntType => render(value)
case StandardType.MonthDayType => render(s"'${value}'")
case BinaryType =>
val chunk = value.asInstanceOf[Chunk[Object]]
render("CONVERT(VARBINARY(MAX),'")
for (b <- chunk)
render(String.format("%02x", b))
render("%02x".format(b))
render("', 2)")
case StandardType.MonthType => render(s"'${value}'")
case StandardType.LocalDateTimeType(formatter) =>
Expand Down Expand Up @@ -520,6 +521,11 @@ trait SqlServerRenderModule extends SqlServerSqlModule { self =>
buildDynamicValue(right)
case DynamicValue.SomeValue(value) => buildDynamicValue(value)
case DynamicValue.NoneValue => render("null")
case DynamicValue.Sequence(chunk) =>
render("CONVERT(VARBINARY(MAX),'")
for (DynamicValue.Primitive(v, _) <- chunk)
render("%02x".format(v))
render("', 2)")
case _ => ()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ object SqlServerModuleSpec extends SqlServerRunnableSpec with DbSchema {
)
implicit val customerRowSchema =
Schema.CaseClass5[UUID, String, String, Boolean, LocalDate, CustomerRow](
TypeId.parse("zio.sql.sqlserver.CustomerRow"),
Schema.Field("id", Schema.primitive[UUID](zio.schema.StandardType.UUIDType)),
Schema.Field("firstName", Schema.primitive[String](zio.schema.StandardType.StringType)),
Schema.Field("lastName", Schema.primitive[String](zio.schema.StandardType.StringType)),
Expand Down

0 comments on commit 6ce5638

Please sign in to comment.