Skip to content

Commit

Permalink
Table aliases, if table repeats zio#143
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejbak85 committed Jan 4, 2021
1 parent bdeeb40 commit ac0cfc2
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 74 deletions.
7 changes: 4 additions & 3 deletions core/jvm/src/main/scala/zio/sql/expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zio.sql
import java.time._

import scala.language.implicitConversions
import java.util.UUID

trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {
self: SelectModule with TableModule =>
Expand Down Expand Up @@ -116,16 +117,16 @@ trait ExprModule extends NewtypesModule with FeaturesModule with OpsModule {

def exprName[F, A, B](expr: Expr[F, A, B]): Option[String] =
expr match {
case Expr.Source(_, c) => Some(c.name)
case _ => None
case Expr.Source(_, _, c) => Some(c.name)
case _ => None
}

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))

sealed case class Source[A, B] private[sql] (tableName: TableName, column: Column[B])
sealed case class Source[A, B] private[sql] (tableName: TableName, tableCode: UUID, column: Column[B])
extends InvariantExpr[Features.Source, A, B] {
def typeTag: TypeTag[B] = column.typeTag
}
Expand Down
3 changes: 2 additions & 1 deletion core/jvm/src/main/scala/zio/sql/select.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ trait SelectModule { self: ExprModule with TableModule =>

sealed case class SelectBuilder[F, A, B <: SelectionSet[A]](selection: Selection[F, A, B]) {

def from[A1 <: A](table: Table.Aux[A1]): Read.Select[F, A1, B] =
def from[A1 <: A](table: Table.Aux[A1]): Read.Select[F, A1, B] = {
Read.Select(selection, table, true, Nil)
}
}

/**
Expand Down
19 changes: 11 additions & 8 deletions core/jvm/src/main/scala/zio/sql/table.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait TableModule { self: ExprModule with SelectModule =>

def columnsUntyped: List[Column.Untyped]

protected def mkColumns[T](name: TableName): ColumnsRepr[T]
protected def mkColumns[T](name: TableName, tableCode: UUID): ColumnsRepr[T]
}

object ColumnSet {
Expand All @@ -33,7 +33,7 @@ trait TableModule { self: ExprModule with SelectModule =>

override def columnsUntyped: List[Column.Untyped] = Nil

override protected def mkColumns[T](name: TableName): ColumnsRepr[T] = ()
override protected def mkColumns[T](name: TableName, tableCode: UUID): ColumnsRepr[T] = ()
}

sealed case class Cons[A, B <: ColumnSet](head: Column[A], tail: B) extends ColumnSet { self =>
Expand All @@ -44,19 +44,21 @@ trait TableModule { self: ExprModule with SelectModule =>

override def columnsUntyped: List[Column.Untyped] = head :: tail.columnsUntyped

def table(name0: TableName): Table.Source.Aux_[ColumnsRepr, A :*: B] =
def table(name0: TableName /*, aliasOpt: Option[String] = None*/ ): Table.Source.Aux_[ColumnsRepr, A :*: B] =
new Table.Source {
type Repr[C] = ColumnsRepr[C]
type Cols = A :*: B
val name: TableName = name0
val name: TableName = name0
//val nameAlias: Option[String] = aliasOpt
val tableCode: UUID = UUID.randomUUID()

val columnSchema: ColumnSchema[A :*: B] = ColumnSchema(self)
val columns: ColumnsRepr[TableType] = mkColumns[TableType](name0)
val columns: ColumnsRepr[TableType] = mkColumns[TableType](name0, tableCode)
val columnsUntyped: List[Column.Untyped] = self.columnsUntyped

}

override protected def mkColumns[T](name: TableName): ColumnsRepr[T] =
(Expr.Source(name, head), tail.mkColumns(name))
override protected def mkColumns[T](name: TableName, tableCode: UUID): ColumnsRepr[T] =
(Expr.Source(name, tableCode, head), tail.mkColumns(name, tableCode))
}

def bigDecimal(name: String): Singleton[BigDecimal] = singleton[BigDecimal](name)
Expand Down Expand Up @@ -139,6 +141,7 @@ trait TableModule { self: ExprModule with SelectModule =>
type Repr[_]
type Cols
val name: TableName
val tableCode: UUID
val columnSchema: ColumnSchema[Cols]
val columns: Repr[TableType]

Expand Down
Loading

0 comments on commit ac0cfc2

Please sign in to comment.