Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package net.degoes.zio
trait Sql {
type ColumnName
type TableName
sealed trait Table[+A]
/**
* (SELECT *, "foo", table.a + table.b AS sum... FROM table WHERE cond) UNION (SELECT ... FROM table)
* UPDATE table SET ...
* INSERT ... INTO table
* DELETE ... FROM table
*/
sealed trait Sql[-A, +B]
/**
* A `Read[A]` models a selection of a set of values of type `A`.
*/
sealed trait Read[+A]
object Read {
final case class Select[A, B](
selection: Selection[A, B], table: Table[A], where: Where[A]) extends Read[B]
final case class Union[B](left: Read[B], right: Read[B], distinct: Boolean) extends Read[B]
final case class Literal[B](values: Iterable[B]) extends Read[B]
}
/**
* A columnar selection of `B` from a source `A`, modeled as `A => B`.
*/
sealed trait Selection[-A, +B]
object Selection {
final case class Identity[A]() extends Selection[A, A]
final case class Constant[A](value: A) extends Selection[Any, A]
final case class Concat[A, L, R](left: Selection[A, L], right: Selection[A, R])
extends Selection[A, (L, R)]
final case class Computed[A, B](expr: Expr[A, B], name: Option[ColumnName]) extends
Selection[A, B]
}
/**
* Models a function `A => B`.
*/
sealed trait Expr[-A, +B]
/**
* Models a function `A => Boolean` that decides whether to retain elements
* in a source structure `A`.
*
* {{{
* WHERE (age * 2) >= 32 && name IS NOT NULL
* }}}
*/
sealed trait Where[-A] { self =>
def && [A1 <: A](that: Where[A1]): Where[A1] = Where.And(self, that)
def || [A1 <: A](that: Where[A1]): Where[A1] = Where.Or(self, that)
}
object Where {
final case class And[A](left: Where[A], right: Where[A]) extends Where[A]
final case class Or[A](left: Where[A], right: Where[A]) extends Where[A]
final case class Relation[A, B](expr: Expr[A, B], predicate: Predicate[B]) extends Where[A]
}
sealed trait Predicate[-A]
object Predicate {
final case class Equals[A](right: A) extends Predicate[A]
case object IsNull extends Predicate[Any]
case object IsNotNull extends Predicate[Any]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.