Skip to content

Commit

Permalink
Merge branch 'master' into end-to-end-test
Browse files Browse the repository at this point in the history
  • Loading branch information
jczuchnowski committed Oct 18, 2020
2 parents ba90663 + 58d29bb commit bc4e3bf
Show file tree
Hide file tree
Showing 26 changed files with 600 additions and 454 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,5 @@ website/static/api

# vscode
.vscode/

.bsp/
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "2.7.2"
version = "2.7.4"
maxColumn = 120
align = most
continuationIndent.defnSite = 2
Expand Down
23 changes: 11 additions & 12 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ inThisBuild(
addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

val zioVersion = "1.0.1"
val zioVersion = "1.0.3"
val testcontainersVersion = "1.15.0-rc2"

lazy val startPostgres = taskKey[Unit]("Start up Postgres")
startPostgres := startService(Database.Postgres, streams.value)
Expand Down Expand Up @@ -115,8 +116,7 @@ lazy val examples = project
skip in publish := true,
moduleName := "examples"
)
.dependsOn(core.jvm)
.dependsOn(jdbc)
.dependsOn(sqlserver)

lazy val driver = project
.in(file("driver"))
Expand All @@ -140,11 +140,7 @@ lazy val jdbc = project
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-test" % zioVersion % "test",
"dev.zio" %% "zio-test-sbt" % zioVersion % "test",
"org.testcontainers" % "testcontainers" % "1.15.0-rc2" % Test,
"org.testcontainers" % "database-commons" % "1.15.0-rc2" % Test,
"org.testcontainers" % "jdbc" % "1.15.0-rc2" % Test,
"com.dimafeng" %% "testcontainers-scala-core" % "1.0.0-alpha1" % Test
"dev.zio" %% "zio-test-sbt" % zioVersion % "test"
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand All @@ -162,6 +158,7 @@ lazy val mysql = project
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
.dependsOn(jdbc)

lazy val oracle = project
.in(file("oracle"))
Expand All @@ -175,6 +172,7 @@ lazy val oracle = project
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
.dependsOn(jdbc)

lazy val postgres = project
.in(file("postgres"))
Expand All @@ -185,10 +183,10 @@ lazy val postgres = project
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
"org.testcontainers" % "testcontainers" % "1.15.0-rc2" % Test,
"org.testcontainers" % "database-commons" % "1.15.0-rc2" % Test,
"org.testcontainers" % "postgresql" % "1.15.0-rc2" % Test,
"org.testcontainers" % "jdbc" % "1.15.0-rc2" % Test,
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "postgresql" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
"org.postgresql" % "postgresql" % "42.2.11" % Test,
"com.dimafeng" %% "testcontainers-scala-postgresql" % "1.0.0-alpha1" % Test
)
Expand All @@ -209,6 +207,7 @@ lazy val sqlserver = project
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
.dependsOn(jdbc)

lazy val test = project
.in(file("test"))
Expand Down
32 changes: 0 additions & 32 deletions core/jvm/src/main/scala/zio/sql/Renderable.scala

This file was deleted.

4 changes: 2 additions & 2 deletions core/jvm/src/main/scala/zio/sql/Sql.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zio.sql

trait Sql extends SelectModule with DeleteModule with UpdateModule with ExprModule with TableModule {
trait Sql extends SelectModule with DeleteModule with UpdateModule with ExprModule with TableModule { self =>

/*
* (SELECT *, "foo", table.a + table.b AS sum... FROM table WHERE cond) UNION (SELECT ... FROM table)
Expand All @@ -21,5 +21,5 @@ trait Sql extends SelectModule with DeleteModule with UpdateModule with ExprModu

def update[A](table: Table.Aux[A]): UpdateBuilder[A] = UpdateBuilder(table)

def renderRead(read: Read[_]): String = read.render(RenderMode.Pretty(2))
def renderRead(read: self.Read[_]): String
}
17 changes: 1 addition & 16 deletions core/jvm/src/main/scala/zio/sql/delete.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ trait DeleteModule { self: ExprModule with TableModule =>
def where[F1](expr: Expr[F1, A, Boolean]): Delete[F1, A] = Delete(table, expr)
}

sealed case class Delete[F, A](
table: Table.Aux[A],
whereExpr: Expr[F, A, Boolean]
) extends Renderable {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
builder.append("delete from ")
table.renderBuilder(builder, mode)
/*todo check whereExpr
whereExpr match {
case Expr.Literal(true) => ()
case _ =>*/
builder.append(" where ")
whereExpr.renderBuilder(builder, mode)
//}
}
}
sealed case class Delete[F, A](table: Table.Aux[A], whereExpr: Expr[F, A, Boolean])

}
142 changes: 27 additions & 115 deletions core/jvm/src/main/scala/zio/sql/expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait ExprModule extends NewtypesModule with TypeTagModule with FeaturesModule w
* Models a function `A => B`.
* SELECT product.price + 10
*/
sealed trait Expr[F, -A, +B] extends Renderable { self =>
sealed trait Expr[F, -A, +B] { self =>

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.Add[B1]())
Expand Down Expand Up @@ -130,132 +130,49 @@ trait ExprModule extends NewtypesModule with TypeTagModule with FeaturesModule w
): Selection[F, A, SelectionSet.Cons[A, B, SelectionSet.Empty]] =
Selection.computedOption(expr, exprName(expr))

sealed case class Source[A, B] private (tableName: TableName, column: Column[B])
extends Expr[Features.Source, A, B] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
val _ = builder.append(column.name)
}
}
sealed case class Source[A, B] private (tableName: TableName, column: Column[B]) extends Expr[Features.Source, A, B]

sealed case class Unary[F, -A, B](base: Expr[F, A, B], op: UnaryOp[B]) extends Expr[F, A, B] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
op.renderBuilder(builder, mode)
val _ = builder.append(op)
}
}
sealed case class Unary[F, -A, B](base: Expr[F, A, B], op: UnaryOp[B]) extends Expr[F, A, B]

sealed case class Property[F, -A, +B](base: Expr[F, A, B], op: PropertyOp) extends Expr[F, A, Boolean] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
base.renderBuilder(builder, mode)
op.renderBuilder(builder, mode)
}
}
sealed case class Property[F, -A, +B](base: Expr[F, A, B], op: PropertyOp) extends Expr[F, A, Boolean]

sealed case class Binary[F1, F2, A, B](left: Expr[F1, A, B], right: Expr[F2, A, B], op: BinaryOp[B])
extends Expr[Features.Union[F1, F2], A, B] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
left.renderBuilder(builder, mode)
op.renderBuilder(builder, mode)
right.renderBuilder(builder, mode)
}
}
extends Expr[Features.Union[F1, F2], A, B]

sealed case class Relational[F1, F2, A, B](left: Expr[F1, A, B], right: Expr[F2, A, B], op: RelationalOp)
extends Expr[Features.Union[F1, F2], A, Boolean] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
left.renderBuilder(builder, mode)
op.renderBuilder(builder, mode)
right.renderBuilder(builder, mode)
}
}
sealed case class In[F, A, B <: SelectionSet[_]](value: Expr[F, A, B], set: Read[B]) extends Expr[F, A, Boolean] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
builder.append(" in ")
set.renderBuilder(builder, mode)
}
}
extends Expr[Features.Union[F1, F2], A, Boolean]

sealed case class In[F, A, B <: SelectionSet[_]](value: Expr[F, A, B], set: Read[B]) extends Expr[F, A, Boolean]

sealed case class Literal[B: TypeTag](value: B) extends Expr[Features.Literal, Any, B] {
def typeTag: TypeTag[B] = implicitly[TypeTag[B]]

override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
val _ = builder.append(value.toString) //todo fix
}
}

sealed case class AggregationCall[F, A, B, Z](param: Expr[F, A, B], aggregation: AggregationDef[B, Z])
extends Expr[Features.Aggregated[F], A, Z] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
builder.append(aggregation.name.name)
builder.append("(")
param.renderBuilder(builder, mode)
val _ = builder.append(")")
}
}
extends Expr[Features.Aggregated[F], A, Z]

sealed case class FunctionCall1[F, A, B, Z](param: Expr[F, A, B], function: FunctionDef[B, Z])
extends Expr[F, A, Z] {

override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
builder.append(function.name.name)
builder.append("(")
param.renderBuilder(builder, mode)
val _ = builder.append(")")
}
}
sealed case class FunctionCall1[F, A, B, Z](param: Expr[F, A, B], function: FunctionDef[B, Z]) extends Expr[F, A, Z]

sealed case class FunctionCall2[F1, F2, A, B, C, Z](
param1: Expr[F1, A, B],
param2: Expr[F2, A, C],
function: FunctionDef[(B, C), Z]
) extends Expr[Features.Union[F1, F2], A, Z] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
builder.append(function.name.name)
builder.append("(")
param1.renderBuilder(builder, mode)
builder.append(",")
param2.renderBuilder(builder, mode)
val _ = builder.append(")")
}
}
) extends Expr[Features.Union[F1, F2], A, Z]

sealed case class FunctionCall3[F1, F2, F3, A, B, C, D, Z](
param1: Expr[F1, A, B],
param2: Expr[F2, A, C],
param3: Expr[F3, A, D],
function: FunctionDef[(B, C, D), Z]
) extends Expr[Features.Union[F1, Features.Union[F2, F3]], A, Z] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
builder.append(function.name.name)
builder.append("(")
param1.renderBuilder(builder, mode)
builder.append(",")
param2.renderBuilder(builder, mode)
builder.append(",")
param3.renderBuilder(builder, mode)
val _ = builder.append(")")
}
}
) extends Expr[Features.Union[F1, Features.Union[F2, F3]], A, Z]

sealed case class FunctionCall4[F1, F2, F3, F4, A, B, C, D, E, Z](
param1: Expr[F1, A, B],
param2: Expr[F2, A, C],
param3: Expr[F3, A, D],
param4: Expr[F4, A, E],
function: FunctionDef[(B, C, D, E), Z]
) extends Expr[Features.Union[F1, Features.Union[F2, Features.Union[F3, F4]]], A, Z] {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
builder.append(function.name.name)
builder.append("(")
param1.renderBuilder(builder, mode)
builder.append(",")
param2.renderBuilder(builder, mode)
builder.append(",")
param3.renderBuilder(builder, mode)
builder.append(",")
param4.renderBuilder(builder, mode)
val _ = builder.append(")")
}
}
) extends Expr[Features.Union[F1, Features.Union[F2, Features.Union[F3, F4]]], A, Z]
}

sealed case class AggregationDef[-A, +B](name: FunctionName) { self =>
Expand All @@ -277,8 +194,8 @@ trait ExprModule extends NewtypesModule with TypeTagModule with FeaturesModule w

def apply[F, Source](param1: Expr[F, Source, A]): Expr[F, Source, B] = Expr.FunctionCall1(param1, self)

def apply[F1, F2, Source, P1, P2](param1: Expr[F1, Source, P1], param2: Expr[F2, Source, P2])(
implicit ev: (P1, P2) <:< A
def apply[F1, F2, Source, P1, P2](param1: Expr[F1, Source, P1], param2: Expr[F2, Source, P2])(implicit
ev: (P1, P2) <:< A
): Expr[F1 :||: F2, Source, B] =
Expr.FunctionCall2(param1, param2, self.narrow[(P1, P2)])

Expand All @@ -305,14 +222,14 @@ trait ExprModule extends NewtypesModule with TypeTagModule with FeaturesModule w

object FunctionDef {
//math functions
val Abs = FunctionDef[Double, Double](FunctionName("abs"))
val Acos = FunctionDef[Double, Double](FunctionName("acos"))
val Asin = FunctionDef[Double, Double](FunctionName("asin"))
val Atan = FunctionDef[Double, Double](FunctionName("atan"))
val Ceil = FunctionDef[Double, Double](FunctionName("ceil"))
val Cos = FunctionDef[Double, Double](FunctionName("cos"))
val Exp = FunctionDef[Double, Double](FunctionName("exp"))
val Floor = FunctionDef[Double, Double](FunctionName("floor"))
val Abs = FunctionDef[Double, Double](FunctionName("abs"))
val Acos = FunctionDef[Double, Double](FunctionName("acos"))
val Asin = FunctionDef[Double, Double](FunctionName("asin"))
val Atan = FunctionDef[Double, Double](FunctionName("atan"))
val Ceil = FunctionDef[Double, Double](FunctionName("ceil"))
val Cos = FunctionDef[Double, Double](FunctionName("cos"))
val Exp = FunctionDef[Double, Double](FunctionName("exp"))
val Floor = FunctionDef[Double, Double](FunctionName("floor"))
//val Log = FunctionDef[Double, Double](FunctionName("log")) //not part of SQL 2011 spec
val Ln = FunctionDef[Double, Double](FunctionName("ln"))
val Mod = FunctionDef[(Double, Double), Double](FunctionName("mod"))
Expand All @@ -337,26 +254,21 @@ trait ExprModule extends NewtypesModule with TypeTagModule with FeaturesModule w
val Rtrim = FunctionDef[String, String](FunctionName("rtrim"))
val Substring = FunctionDef[(String, Int, Option[Int]), String](FunctionName("substring"))
//TODO substring regex
val Trim = FunctionDef[String, String](FunctionName("trim"))
val Upper = FunctionDef[String, String](FunctionName("upper"))
val Trim = FunctionDef[String, String](FunctionName("trim"))
val Upper = FunctionDef[String, String](FunctionName("upper"))

// date functions
val CurrentTimestamp = FunctionDef[Nothing, Instant](FunctionName("current_timestamp"))
}

sealed trait Set[F, -A] extends Renderable {
sealed trait Set[F, -A] {
type Value

def lhs: Expr[F, A, Value]
def rhs: Expr[_, A, Value]

def typeTag: TypeTag[Value]

override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
lhs.renderBuilder(builder, mode)
builder.append(" = ")
rhs.renderBuilder(builder, mode)
}
}

object Set {
Expand Down
6 changes: 1 addition & 5 deletions core/jvm/src/main/scala/zio/sql/newtypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ trait NewtypesModule {
type ColumnName = String
type TableName = String

sealed case class FunctionName(name: String) extends Renderable {
override private[zio] def renderBuilder(builder: StringBuilder, mode: RenderMode): Unit = {
val _ = builder.append(name)
}
}
sealed case class FunctionName(name: String)

}
Loading

0 comments on commit bc4e3bf

Please sign in to comment.