Skip to content

Commit

Permalink
Merge pull request zio#577 from sviezypan/bug_fixes
Browse files Browse the repository at this point in the history
DSL, meta-model improvements, group by & NPE bug fixes, nullable aspect
  • Loading branch information
jdegoes committed Feb 11, 2022
2 parents 222483c + b2c035c commit 226a806
Show file tree
Hide file tree
Showing 34 changed files with 1,255 additions and 827 deletions.
74 changes: 37 additions & 37 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
.settings(buildInfoSettings("zio.sql"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-streams" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand Down Expand Up @@ -118,10 +118,10 @@ lazy val examples = project
publish / skip := true,
moduleName := "examples",
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-streams" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion
)
)
.dependsOn(sqlserver)
Expand All @@ -132,11 +132,11 @@ lazy val driver = project
.settings(buildInfoSettings("zio.sql.driver"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand All @@ -147,12 +147,12 @@ lazy val jdbc = project
.settings(buildInfoSettings("zio.sql.jdbc"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-streams" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion,
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
)
)
.settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"))
Expand All @@ -165,10 +165,10 @@ lazy val mysql = project
.settings(buildInfoSettings("zio.sql.mysql"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-streams" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided,
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion,
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "jdbc" % testcontainersVersion % Test,
Expand All @@ -186,10 +186,10 @@ lazy val oracle = project
.settings(buildInfoSettings("zio.sql.oracle"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-streams" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided,
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion,
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "oracle-xe" % testcontainersVersion % Test,
Expand All @@ -207,10 +207,10 @@ lazy val postgres = project
.settings(buildInfoSettings("zio.sql.postgres"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-streams" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided,
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion,
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "postgresql" % testcontainersVersion % Test,
Expand All @@ -228,10 +228,10 @@ lazy val sqlserver = project
.settings(buildInfoSettings("zio.sql.sqlserver"))
.settings(
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % zioVersion % Provided,
"dev.zio" %% "zio-streams" % zioVersion % Provided,
"dev.zio" %% "zio-schema" % zioSchemaVersion % Provided,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion % Provided,
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-schema" % zioSchemaVersion,
"dev.zio" %% "zio-schema-derivation" % zioSchemaVersion,
"org.testcontainers" % "testcontainers" % testcontainersVersion % Test,
"org.testcontainers" % "database-commons" % testcontainersVersion % Test,
"org.testcontainers" % "mssqlserver" % testcontainersVersion % Test,
Expand Down
21 changes: 15 additions & 6 deletions core/jvm/src/main/scala/zio/sql/Sql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ package zio.sql

import zio.schema.Schema

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

/*
Expand All @@ -17,8 +24,10 @@ trait Sql extends SelectModule with DeleteModule with UpdateModule with ExprModu
*
* SELECT ARBITRARY(age), COUNT(*) FROM person GROUP BY age
*/
def select[F, A, B <: SelectionSet[A]](selection: Selection[F, A, B]): SelectBuilder[F, A, B] =
SelectBuilder(selection)
def select[F, A, B <: SelectionSet[A]](selection: Selection[F, A, B])(implicit
i: Features.IsPartiallyAggregated[F]
): Selector[F, A, B, i.Unaggregated] =
Selector[F, A, B, i.Unaggregated](selection)

def subselect[ParentTable]: SubselectPartiallyApplied[ParentTable] = new SubselectPartiallyApplied[ParentTable]

Expand All @@ -41,12 +50,12 @@ trait Sql extends SelectModule with DeleteModule with UpdateModule with ExprModu

def renderUpdate(update: self.Update[_]): String

def insertInto[F, Source, AllColumnIdentities, B <: SelectionSet.Aux[Source, ColsRepr], ColsRepr](
def insertInto[F, Source, AllColumnIdentities, B <: SelectionSet[Source]](
table: Table.Source.Aux_[Source, AllColumnIdentities]
)(
sources: Selection.Aux[F, Source, B, ColsRepr]
sources: Selection[F, Source, B]
) =
InsertBuilder(table, sources)
InsertBuilder[F, Source, AllColumnIdentities, B, sources.ColsRepr](table, sources)

def renderInsert[A: Schema](insert: self.Insert[_, A]): String
}
24 changes: 14 additions & 10 deletions core/jvm/src/main/scala/zio/sql/expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {
def isNotTrue[A1 <: A](implicit ev: B <:< Boolean): Expr[F, A1, Boolean] =
Expr.Property(self, PropertyOp.IsNotTrue)

def as[B1 >: B](name: String): Selection[F, A, SelectionSet.Cons[A, B1, SelectionSet.Empty]] =
Selection.computedAs(self, name)
//TODO https://github.com/zio/zio-sql/issues/564
def as[B1 >: B](name: String): Expr[F, A, B1] = {
val _ = name
self
}

def ascending: Ordering[Expr[F, A, B]] = Ordering.Asc(self)

Expand All @@ -106,6 +109,7 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {
}

object Expr {

implicit val subqueryToExpr = self.Read.Subselect.subselectToExpr _

sealed trait InvariantExpr[F, -A, B] extends Expr[F, A, B] {
Expand All @@ -125,11 +129,12 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {
implicit def expToSelection[F, A, B](
expr: Expr[F, A, B]
): Selection[F, A, SelectionSet.Cons[A, B, SelectionSet.Empty]] =
Selection.computedOption(expr, exprName(expr))
Selection.computedOption[F, A, B](expr, Expr.exprName(expr))

// aggregated F should not be propagated
sealed case class Subselect[F <: Features.Aggregated[_], Repr, Source, Subsource, Head](
subselect: Read.Subselect[F, Repr, _ <: Source, Subsource, Head, SelectionSet.Empty]
) extends InvariantExpr[F, Any, Head] {
) extends InvariantExpr[Features.Derived, Any, Head] {
override def typeTag: TypeTag[Head] = subselect.selection.value.head.toColumn.typeTag
}

Expand Down Expand Up @@ -271,12 +276,11 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {
}

object AggregationDef {
val Count = AggregationDef[Any, Long](FunctionName("count"))
val Sum = AggregationDef[Double, Double](FunctionName("sum"))
def Arbitrary[F, A, B: TypeTag](expr: Expr[F, A, B]) = AggregationDef[B, B](FunctionName("arbitrary"))(expr)
val Avg = AggregationDef[Double, Double](FunctionName("avg"))
def Min[F, A, B: TypeTag](expr: Expr[F, A, B]) = AggregationDef[B, B](FunctionName("min"))(expr)
def Max[F, A, B: TypeTag](expr: Expr[F, A, B]) = AggregationDef[B, B](FunctionName("max"))(expr)
val Count = AggregationDef[Any, Long](FunctionName("count"))
val Sum = AggregationDef[Double, Double](FunctionName("sum"))
val Avg = AggregationDef[Double, Double](FunctionName("avg"))
def Min[F, A, B: TypeTag](expr: Expr[F, A, B]) = AggregationDef[B, B](FunctionName("min"))(expr)
def Max[F, A, B: TypeTag](expr: Expr[F, A, B]) = AggregationDef[B, B](FunctionName("max"))(expr)
}

sealed case class FunctionDef[-A, +B](name: FunctionName) { self =>
Expand Down
85 changes: 73 additions & 12 deletions core/jvm/src/main/scala/zio/sql/features.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package zio.sql

import com.github.ghik.silencer.silent

import scala.annotation.implicitNotFound

trait FeaturesModule {
Expand All @@ -12,21 +10,39 @@ trait FeaturesModule {
type Aggregated[_]
type Union[_, _]
type Source[_]
//TODO make Derived and Join tables return Expr of type "Derived" when .columns is called
type Derived
type Literal
type Function0
type Derived

sealed trait IsNotAggregated[A]
object IsNotAggregated {
implicit def UnionIsNotAgregated[A: IsNotAggregated, B: IsNotAggregated]: IsNotAggregated[Union[A, B]] =
new IsNotAggregated[Union[A, B]] {}

implicit def SourceIsNotAggregated[A]: IsNotAggregated[Source[A]] =
new IsNotAggregated[Source[A]] {}

implicit val LiteralIsNotAggregated: IsNotAggregated[Literal] =
new IsNotAggregated[Literal] {}

sealed trait IsAggregated[A]
implicit val DerivedIsNotAggregated: IsNotAggregated[Derived] =
new IsNotAggregated[Derived] {}

object IsAggregated {
def apply[A](implicit is: IsAggregated[A]): IsAggregated[A] = is
implicit val Function0IsNotAggregated: IsNotAggregated[Function0] =
new IsNotAggregated[Function0] {}
}

sealed trait IsFullyAggregated[A]

object IsFullyAggregated {
def apply[A](implicit is: IsFullyAggregated[A]): IsFullyAggregated[A] = is

implicit def AggregatedIsAggregated[A]: IsAggregated[Aggregated[A]] = new IsAggregated[Aggregated[A]] {}
implicit def AggregatedIsAggregated[A]: IsFullyAggregated[Aggregated[A]] = new IsFullyAggregated[Aggregated[A]] {}

@silent
implicit def UnionIsAggregated[A: IsAggregated, B: IsAggregated]: IsAggregated[Union[A, B]] =
new IsAggregated[Union[A, B]] {}
implicit val LiteralIsAggregated: IsFullyAggregated[Literal] = new IsFullyAggregated[Literal] {}

implicit def UnionIsAggregated[A: IsFullyAggregated, B: IsFullyAggregated]: IsFullyAggregated[Union[A, B]] =
new IsFullyAggregated[Union[A, B]] {}
}

@implicitNotFound("You can only use this function on a column in the source table")
Expand All @@ -35,6 +51,51 @@ trait FeaturesModule {
object IsSource {
implicit def isSource[ColumnIdentity]: IsSource[Source[ColumnIdentity]] = new IsSource[Source[ColumnIdentity]] {}
}
}

sealed trait IsPartiallyAggregated[A] {
type Unaggregated
}

object IsPartiallyAggregated extends IsPartiallyAggregatedLowPriorityImplicits {

type WithRemainder[F, R] = IsPartiallyAggregated[F] {
type Unaggregated = R
}

def apply[A](implicit is: IsPartiallyAggregated[A]): IsPartiallyAggregated.WithRemainder[A, is.Unaggregated] = is

implicit def AggregatedIsAggregated[A]: IsPartiallyAggregated.WithRemainder[Aggregated[A], Any] =
new IsPartiallyAggregated[Aggregated[A]] {
override type Unaggregated = Any
}

implicit def UnionIsAggregated[A, B](implicit
inA: IsPartiallyAggregated[A],
inB: IsPartiallyAggregated[B]
): IsPartiallyAggregated.WithRemainder[Union[A, B], inA.Unaggregated with inB.Unaggregated] =
new IsPartiallyAggregated[Union[A, B]] {
override type Unaggregated = inA.Unaggregated with inB.Unaggregated
}

implicit val LiteralIsAggregated: IsPartiallyAggregated.WithRemainder[Literal, Any] =
new IsPartiallyAggregated[Literal] {
override type Unaggregated = Any
}

implicit val DerivedIsAggregated: IsPartiallyAggregated.WithRemainder[Derived, Any] =
new IsPartiallyAggregated[Derived] {
override type Unaggregated = Any
}

implicit val FunctionIsAggregated: IsPartiallyAggregated.WithRemainder[Function0, Any] =
new IsPartiallyAggregated[Function0] {
override type Unaggregated = Any
}
}

trait IsPartiallyAggregatedLowPriorityImplicits {
implicit def SourceIsAggregated[A]: IsPartiallyAggregated.WithRemainder[Features.Source[A], Features.Source[A]] =
new IsPartiallyAggregated[Features.Source[A]] { override type Unaggregated = Features.Source[A] }
}
}
}
Loading

0 comments on commit 226a806

Please sign in to comment.