Skip to content

Commit

Permalink
Merge pull request #859 from sviezypan/select_comma_macro
Browse files Browse the repository at this point in the history
Macro to handle comma separated selections
  • Loading branch information
jczuchnowski committed May 20, 2023
2 parents 1dc612e + 9efbf5f commit 8e2c266
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 1,469 deletions.
6 changes: 2 additions & 4 deletions core/jvm/src/main/scala/zio/sql/Sql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ trait Sql {
def select[F, A, B <: SelectionSet[A]](selection: Selection[F, A, B]): SelectBuilder[F, A, B] =
SelectBuilder[F, A, B](selection)

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

def deleteFrom[T <: Table](table: T): Delete[table.TableType] = Delete(table, true)

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

def insertInto[Source, AllColumnIdentities](
table: Table.Source.Aux_[Source, AllColumnIdentities]
): InsertByCommaBuilder[Source, AllColumnIdentities] = InsertByCommaBuilder(table)
val insertInto: InsertByCommaBuilder = InsertByCommaBuilder()

def renderDelete(delete: Delete[_]): String

Expand Down
4 changes: 1 addition & 3 deletions core/jvm/src/main/scala/zio/sql/expr/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ sealed trait Expr[-F, -A, +B] { self =>

object Expr {

implicit val subqueryToExpr = Read.Subselect.subselectToExpr _

sealed trait InvariantExpr[F, -A, B] extends Expr[F, A, B] {
def typeTag: TypeTag[B]
}
Expand All @@ -134,7 +132,7 @@ object Expr {
implicit def literal[A: TypeTag](a: A): Expr[Features.Literal, Any, A] = Expr.Literal(a)

implicit def some[A: TypeTag.NotNull](someA: Some[A]): Expr[Features.Literal, Any, Option[A]] = {
implicit val typeTagA = TypeTag.Nullable[A]()
implicit val typeTagA: TypeTag.Nullable[A] = TypeTag.Nullable[A]()
Expr.Literal[Option[A]](someA)
}

Expand Down
8 changes: 4 additions & 4 deletions core/jvm/src/main/scala/zio/sql/insert/InsertBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import zio.sql.macros._
import zio.sql.table._
import zio.sql.select._

final case class InsertBuilder[F, Source, AllColumnIdentities, B <: SelectionSet[Source], ColsRepr](
final case class InsertBuilder[F, Source, AllColumnIdentities, B <: SelectionSet[Source]](
table: Table.Source.Aux_[Source, AllColumnIdentities],
sources: Selection.Aux[F, Source, B, ColsRepr]
sources: Selection[F, Source, B]
) {

def values[Z](values: Seq[Z])(implicit
schemaCC: Schema[Z],
schemaValidity: InsertLike[F, ColsRepr, AllColumnIdentities, Z]
schemaValidity: InsertLike[F, sources.ColsRepr, AllColumnIdentities, Z]
): Insert[Source, Z] = Insert(table, sources.value, values)

def values[Z](value: Z)(implicit
schemaCC: Schema[Z],
schemaValidity: InsertLike[F, ColsRepr, AllColumnIdentities, Z]
schemaValidity: InsertLike[F, sources.ColsRepr, AllColumnIdentities, Z]
): Insert[Source, Z] = Insert(table, sources.value, Seq(value))
}
210 changes: 6 additions & 204 deletions core/jvm/src/main/scala/zio/sql/insert/InsertByCommaBuilder.scala

Large diffs are not rendered by default.

175 changes: 147 additions & 28 deletions core/jvm/src/main/scala/zio/sql/select/Read.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,43 +180,162 @@ object Read {
override type GroupByF = self.GroupByF
}

//format: off
def groupBy[F1](expr1: Expr[F1, Source, Any])(implicit verify: GroupByLike[F, F1]): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](selection, table, whereExpr, self.groupByExprs ++ expr1, havingExpr, orderByExprs, offset, limit) {
override type GroupByF = self.GroupByF with F1
}

def groupBy[F1, F2](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any])(implicit verify: GroupByLike[F, F1 with F2]): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](selection, table, whereExpr, self.groupByExprs ++ expr1 ++ expr2, havingExpr, orderByExprs, offset, limit) {
override type GroupByF = self.GroupByF with F1 with F2
def groupBy[F1](expr1: Expr[F1, Source, Any])(implicit
verify: GroupByLike[F, F1]
): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](
selection,
table,
whereExpr,
self.groupByExprs ++ expr1,
havingExpr,
orderByExprs,
offset,
limit
) {
override type GroupByF = self.GroupByF with F1
}

def groupBy[F1, F2, F3](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any], expr3: Expr[F3, Source, Any])(implicit verify: GroupByLike[F, F1 with F2 with F3]): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2 with F3] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](selection, table, whereExpr,self.groupByExprs ++ expr1 ++ expr2 ++ expr3, havingExpr, orderByExprs, offset, limit) {
override type GroupByF = self.GroupByF with F1 with F2 with F3
def groupBy[F1, F2](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any])(implicit
verify: GroupByLike[F, F1 with F2]
): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](
selection,
table,
whereExpr,
self.groupByExprs ++ expr1 ++ expr2,
havingExpr,
orderByExprs,
offset,
limit
) {
override type GroupByF = self.GroupByF with F1 with F2
}

def groupBy[F1, F2, F3](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any], expr3: Expr[F3, Source, Any])(
implicit verify: GroupByLike[F, F1 with F2 with F3]
): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2 with F3] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](
selection,
table,
whereExpr,
self.groupByExprs ++ expr1 ++ expr2 ++ expr3,
havingExpr,
orderByExprs,
offset,
limit
) {
override type GroupByF = self.GroupByF with F1 with F2 with F3
}

def groupBy[F1, F2, F3, F4](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any], expr3: Expr[F3, Source, Any], expr4: Expr[F4, Source, Any])(implicit verify: GroupByLike[F, F1 with F2 with F3 with F4]): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2 with F3 with F4] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](selection, table, whereExpr, self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4, havingExpr, orderByExprs, offset, limit) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4
def groupBy[F1, F2, F3, F4](
expr1: Expr[F1, Source, Any],
expr2: Expr[F2, Source, Any],
expr3: Expr[F3, Source, Any],
expr4: Expr[F4, Source, Any]
)(implicit
verify: GroupByLike[F, F1 with F2 with F3 with F4]
): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2 with F3 with F4] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](
selection,
table,
whereExpr,
self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4,
havingExpr,
orderByExprs,
offset,
limit
) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4
}

def groupBy[F1, F2, F3, F4, F5](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any], expr3: Expr[F3, Source, Any], expr4: Expr[F4, Source, Any], expr5: Expr[F5, Source, Any])(implicit verify: GroupByLike[F, F1 with F2 with F3 with F4 with F5]): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2 with F3 with F4 with F5] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](selection, table, whereExpr, self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4 ++ expr5, havingExpr, orderByExprs, offset, limit) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4 with F5

def groupBy[F1, F2, F3, F4, F5](
expr1: Expr[F1, Source, Any],
expr2: Expr[F2, Source, Any],
expr3: Expr[F3, Source, Any],
expr4: Expr[F4, Source, Any],
expr5: Expr[F5, Source, Any]
)(implicit verify: GroupByLike[F, F1 with F2 with F3 with F4 with F5]): Subselect.WithGroupByF[
F,
Repr,
Source,
Subsource,
Head,
Tail,
self.GroupByF with F1 with F2 with F3 with F4 with F5
] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](
selection,
table,
whereExpr,
self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4 ++ expr5,
havingExpr,
orderByExprs,
offset,
limit
) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4 with F5
}

def groupBy[F1, F2, F3, F4, F5, F6](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any], expr3: Expr[F3, Source, Any], expr4: Expr[F4, Source, Any], expr5: Expr[F5, Source, Any], expr6: Expr[F6, Source, Any])(implicit verify: GroupByLike[F, F1 with F2 with F3 with F4 with F5 with F6]): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](selection, table, whereExpr, self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4 ++ expr5 ++ expr6, havingExpr, orderByExprs, offset, limit) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6

def groupBy[F1, F2, F3, F4, F5, F6](
expr1: Expr[F1, Source, Any],
expr2: Expr[F2, Source, Any],
expr3: Expr[F3, Source, Any],
expr4: Expr[F4, Source, Any],
expr5: Expr[F5, Source, Any],
expr6: Expr[F6, Source, Any]
)(implicit verify: GroupByLike[F, F1 with F2 with F3 with F4 with F5 with F6]): Subselect.WithGroupByF[
F,
Repr,
Source,
Subsource,
Head,
Tail,
self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6
] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](
selection,
table,
whereExpr,
self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4 ++ expr5 ++ expr6,
havingExpr,
orderByExprs,
offset,
limit
) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6
}

//TODO add arities up to 22 if needed
def groupBy[F1, F2, F3, F4, F5, F6, F7](expr1: Expr[F1, Source, Any], expr2: Expr[F2, Source, Any], expr3: Expr[F3, Source, Any], expr4: Expr[F4, Source, Any], expr5: Expr[F5, Source, Any], expr6: Expr[F6, Source, Any], expr7: Expr[F7, Source, Any])(implicit verify: GroupByLike[F, F1 with F2 with F3 with F4 with F5 with F6 with F7]): Subselect.WithGroupByF[F, Repr, Source, Subsource, Head, Tail, self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6 with F7] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](selection, table, whereExpr, self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4 ++ expr5 ++ expr6 ++ expr7, havingExpr, orderByExprs, offset, limit) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6 with F7
// TODO add arities up to 22 if needed
def groupBy[F1, F2, F3, F4, F5, F6, F7](
expr1: Expr[F1, Source, Any],
expr2: Expr[F2, Source, Any],
expr3: Expr[F3, Source, Any],
expr4: Expr[F4, Source, Any],
expr5: Expr[F5, Source, Any],
expr6: Expr[F6, Source, Any],
expr7: Expr[F7, Source, Any]
)(implicit verify: GroupByLike[F, F1 with F2 with F3 with F4 with F5 with F6 with F7]): Subselect.WithGroupByF[
F,
Repr,
Source,
Subsource,
Head,
Tail,
self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6 with F7
] =
new Subselect[F, Repr, Source, Subsource, Head, Tail](
selection,
table,
whereExpr,
self.groupByExprs ++ expr1 ++ expr2 ++ expr3 ++ expr4 ++ expr5 ++ expr6 ++ expr7,
havingExpr,
orderByExprs,
offset,
limit
) {
override type GroupByF = self.GroupByF with F1 with F2 with F3 with F4 with F5 with F6 with F7
}
//format: on

def normalize(implicit
instance: Normalizer[ResultType]
Expand Down
39 changes: 19 additions & 20 deletions core/jvm/src/main/scala/zio/sql/select/SelectAll.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package zio.sql.select

import zio.sql.table._
import zio.sql.expr._
import scala.language.experimental.macros
import scala.language.implicitConversions

final case class SelectAll() {
def from[AllColumnsIdentities, TableType, ColumnsOut, F, Repr, Head, Tail <: SelectionSet[TableType]](
wrapper: SelectAllWrapper[AllColumnsIdentities, TableType, ColumnsOut]
): Read.Subselect[F, Repr, TableType, TableType, Head, Tail] =
macro SelectionMacro.buildSelectAll[AllColumnsIdentities, ColumnsOut, TableType]
}

// TODO check if helper's types can be moved to table or rewrite to macro
def from[A](table: Table.Source.Aux[A])(implicit helper: SelectAllHelper[table.ColumnsOut, A]): Read.Select[
helper.F,
helper.ResultTypeRepr,
A,
helper.ColumnHead,
helper.SelectionTail
] = {
type B0 = SelectionSet.ConsAux[
helper.ResultTypeRepr,
A,
helper.ColumnHead,
helper.SelectionTail
]
val b: B0 = table.all.selection.value.asInstanceOf[B0]
case class SelectAllWrapper[AllColumnsIdentities, TableType, ColumnsOut](
table: Table.Source.WithTableDetails[AllColumnsIdentities, TableType, ColumnsOut],
exprs: List[zio.sql.expr.Expr[_, TableType, _]]
)

Read.Subselect[helper.F, helper.ResultTypeRepr, A, A, helper.ColumnHead, helper.SelectionTail](
Selection[helper.F, A, B0](b),
Some(table),
true
object SelectAllWrapper {
implicit def tableToExprs[AllColumnsIdentities, TableType, ColumnsOut](
table: Table.Source.WithTableDetails[AllColumnsIdentities, TableType, ColumnsOut]
): SelectAllWrapper[AllColumnsIdentities, TableType, ColumnsOut] =
SelectAllWrapper(
table,
table.columns.asInstanceOf[Product].productIterator.toList.map(_.asInstanceOf[Expr[_, TableType, _]])
)
}
}
Loading

0 comments on commit 8e2c266

Please sign in to comment.