Skip to content

Commit

Permalink
Merge pull request #317 from robmwalsh/render-literals
Browse files Browse the repository at this point in the history
Render literals & break up giant `renderRead` function to allow reuse of e.g. `renderExpr` across renderings
  • Loading branch information
jczuchnowski authored Nov 28, 2020
2 parents e3e9228 + 0207b84 commit ecb9af3
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 349 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ inThisBuild(
)
)

addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("fmtOnce", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("fmt", "fmtOnce;fmtOnce")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

val zioVersion = "1.0.3"
Expand Down
23 changes: 23 additions & 0 deletions core/jvm/src/main/scala/zio/sql/Renderer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package zio.sql

class Renderer(val builder: StringBuilder) extends AnyVal {
//not using vararg to avoid allocating `Seq`s
def apply(s1: Any): Unit = {
val _ = builder.append(s1)
}
def apply(s1: Any, s2: Any): Unit = {
val _ = builder.append(s1).append(s2)
}
def apply(s1: Any, s2: Any, s3: Any): Unit = {
val _ = builder.append(s1).append(s2).append(s3)
}
def apply(s1: Any, s2: Any, s3: Any, s4: Any): Unit = {
val _ = builder.append(s1).append(s2).append(s3).append(s4)
}

override def toString: String = builder.toString()
}

object Renderer {
def apply(): Renderer = new Renderer(new StringBuilder)
}
11 changes: 6 additions & 5 deletions core/jvm/src/main/scala/zio/sql/expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {
def *[F2, A1 <: A, B1 >: B](that: Expr[F2, A1, B1])(implicit ev: IsNumeric[B1]): Expr[F :||: F2, A1, B1] =
Expr.Binary(self, that, BinaryOp.Mul[B1]())

//todo do something special for divide by 0? also Mod/log/whatever else is really a partial function.. PartialExpr?
def /[F2, A1 <: A, B1 >: B](that: Expr[F2, A1, B1])(implicit ev: IsNumeric[B1]): Expr[F :||: F2, A1, B1] =
Expr.Binary(self, that, BinaryOp.Div[B1]())

Expand Down Expand Up @@ -161,7 +162,8 @@ 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] {
sealed case class FunctionCall0[Z: TypeTag](function: FunctionDef[Any, Z])
extends InvariantExpr[Features.Function0, Any, Z] {
def typeTag: TypeTag[Z] = implicitly[TypeTag[Z]]
}

Expand Down Expand Up @@ -217,8 +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] =
Expr.FunctionCall0(self: FunctionDef[A, B1])
def apply[B1 >: B]()(implicit 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])
Expand Down Expand Up @@ -280,8 +282,7 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {

//string functions
val Ascii = FunctionDef[String, Int](FunctionName("ascii"))
val CharLength = FunctionDef[String, Int](FunctionName("character_length"))
val Concat = FunctionDef[(String, String), String](FunctionName("concat"))
val Concat = FunctionDef[(String, String), String](FunctionName("concat")) //todo varargs
val Lower = FunctionDef[String, String](FunctionName("lower"))
val Ltrim = FunctionDef[String, String](FunctionName("ltrim"))
val OctetLength = FunctionDef[String, Int](FunctionName("octet_length"))
Expand Down
1 change: 1 addition & 0 deletions core/jvm/src/main/scala/zio/sql/features.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ trait FeaturesModule {
type Union[_, _]
type Source
type Literal
type Function0

sealed trait IsAggregated[A]

Expand Down
46 changes: 24 additions & 22 deletions core/jvm/src/main/scala/zio/sql/typetag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@ trait TypeTagModule {

type TypeTagExtension[+A]

sealed trait TypeTag[A]
sealed trait TypeTag[+A] {
private[zio] def cast(a: Any): A = a.asInstanceOf[A]
}

object TypeTag {
sealed trait NotNull[A] extends TypeTag[A]
implicit case object TBigDecimal extends NotNull[BigDecimal]
implicit case object TBoolean extends NotNull[Boolean]
implicit case object TByte extends NotNull[Byte]
implicit case object TByteArray extends NotNull[Chunk[Byte]]
implicit case object TChar extends NotNull[Char]
implicit case object TDouble extends NotNull[Double]
implicit case object TFloat extends NotNull[Float]
implicit case object TInstant extends NotNull[Instant]
implicit case object TInt extends NotNull[Int]
implicit case object TLocalDate extends NotNull[LocalDate]
implicit case object TLocalDateTime extends NotNull[LocalDateTime]
implicit case object TLocalTime extends NotNull[LocalTime]
implicit case object TLong extends NotNull[Long]
implicit case object TOffsetDateTime extends NotNull[OffsetDateTime]
implicit case object TOffsetTime extends NotNull[OffsetTime]
implicit case object TShort extends NotNull[Short]
implicit case object TString extends NotNull[String]
implicit case object TUUID extends NotNull[UUID]
implicit case object TZonedDateTime extends NotNull[ZonedDateTime]
sealed case class TDialectSpecific[A](typeTagExtension: TypeTagExtension[A]) extends NotNull[A]
sealed trait NotNull[+A] extends TypeTag[A]
implicit case object TBigDecimal extends NotNull[BigDecimal]
implicit case object TBoolean extends NotNull[Boolean]
implicit case object TByte extends NotNull[Byte]
implicit case object TByteArray extends NotNull[Chunk[Byte]]
implicit case object TChar extends NotNull[Char]
implicit case object TDouble extends NotNull[Double]
implicit case object TFloat extends NotNull[Float]
implicit case object TInstant extends NotNull[Instant]
implicit case object TInt extends NotNull[Int]
implicit case object TLocalDate extends NotNull[LocalDate]
implicit case object TLocalDateTime extends NotNull[LocalDateTime]
implicit case object TLocalTime extends NotNull[LocalTime]
implicit case object TLong extends NotNull[Long]
implicit case object TOffsetDateTime extends NotNull[OffsetDateTime]
implicit case object TOffsetTime extends NotNull[OffsetTime]
implicit case object TShort extends NotNull[Short]
implicit case object TString extends NotNull[String]
implicit case object TUUID extends NotNull[UUID]
implicit case object TZonedDateTime extends NotNull[ZonedDateTime]
sealed case class TDialectSpecific[+A](typeTagExtension: TypeTagExtension[A]) extends NotNull[A]

sealed case class Nullable[A: NotNull]() extends TypeTag[Option[A]] {
def typeTag: TypeTag[A] = implicitly[TypeTag[A]]
Expand Down
Loading

0 comments on commit ecb9af3

Please sign in to comment.