Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hkarim committed Jan 17, 2018
1 parent 79b5202 commit 6b4295d
Show file tree
Hide file tree
Showing 30 changed files with 302 additions and 628 deletions.
2 changes: 0 additions & 2 deletions .scalafmt → .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ rewrite.rules=[
SortImports
]
unindentTopLevelOperators = true
indentOperator = spray

37 changes: 13 additions & 24 deletions jam-core/src/main/scala/jam/data/Iso.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,38 @@ trait Iso[L, R] {
def isoTo(l: L): R
def isoFrom(r: R): L
def compose[A](that: Iso[A, L]): Iso[A, R] = Iso.compose(this, that)
def swap: Iso[R, L] = Iso.instance[R, L](isoFrom)(isoTo)
def swap: Iso[R, L] = Iso.instance[R, L](isoFrom)(isoTo)
def liftF[F[_]: Functor]: Iso[F[L], F[R]] =
Iso.liftF[F, L, R](Functor[F], this)
}

trait IsoLowPriorityInstances {

def valueType[L <: AnyVal, R](
implicit g: Generic.Aux[L, R :: HNil]): Iso[L, R] =
def valueType[L <: AnyVal, R](implicit g: Generic.Aux[L, R :: HNil]): Iso[L, R] =
Iso.instance[L, R](a => g.to(a).head)(v => g.from(v :: HNil))

implicit def isoString[A <: AnyVal](
implicit g: Generic.Aux[A, String :: HNil]): Iso[A, String] =
implicit def isoString[A <: AnyVal](implicit g: Generic.Aux[A, String :: HNil]): Iso[A, String] =
valueType[A, String]

implicit def isoInt[A <: AnyVal](
implicit g: Generic.Aux[A, Int :: HNil]): Iso[A, Int] =
implicit def isoInt[A <: AnyVal](implicit g: Generic.Aux[A, Int :: HNil]): Iso[A, Int] =
valueType[A, Int]

implicit def isoShort[A <: AnyVal](
implicit g: Generic.Aux[A, Short :: HNil]): Iso[A, Short] =
implicit def isoShort[A <: AnyVal](implicit g: Generic.Aux[A, Short :: HNil]): Iso[A, Short] =
valueType[A, Short]

implicit def isoLong[A <: AnyVal](
implicit g: Generic.Aux[A, Long :: HNil]): Iso[A, Long] =
implicit def isoLong[A <: AnyVal](implicit g: Generic.Aux[A, Long :: HNil]): Iso[A, Long] =
valueType[A, Long]

implicit def isoFloat[A <: AnyVal](
implicit g: Generic.Aux[A, Float :: HNil]): Iso[A, Float] =
implicit def isoFloat[A <: AnyVal](implicit g: Generic.Aux[A, Float :: HNil]): Iso[A, Float] =
valueType[A, Float]

implicit def isoDouble[A <: AnyVal](
implicit g: Generic.Aux[A, Double :: HNil]): Iso[A, Double] =
implicit def isoDouble[A <: AnyVal](implicit g: Generic.Aux[A, Double :: HNil]): Iso[A, Double] =
valueType[A, Double]

implicit def isoBoolean[A <: AnyVal](
implicit g: Generic.Aux[A, Boolean :: HNil]): Iso[A, Boolean] =
implicit def isoBoolean[A <: AnyVal](implicit g: Generic.Aux[A, Boolean :: HNil]): Iso[A, Boolean] =
valueType[A, Boolean]

implicit def isoUUID[A <: AnyVal](
implicit g: Generic.Aux[A, UUID :: HNil]): Iso[A, UUID] =
implicit def isoUUID[A <: AnyVal](implicit g: Generic.Aux[A, UUID :: HNil]): Iso[A, UUID] =
valueType[A, UUID]
}

Expand All @@ -64,19 +55,17 @@ object Iso extends IsoInstances {
def apply[L, R](implicit iso: Iso[L, R]): Iso[L, R] = iso

def instance[L, R](f: L => R)(g: R => L): Iso[L, R] = new Iso[L, R] {
def isoTo(l: L): R = f(l)
def isoTo(l: L): R = f(l)
def isoFrom(r: R): L = g(r)
}

def compose[A, B, C](f: Iso[B, C], g: Iso[A, B]): Iso[A, C] =
Iso.instance[A, C](a => f.isoTo(g.isoTo(a)))(c => g.isoFrom(f.isoFrom(c)))

def liftF[F[_]: Functor, L, R](implicit iso: Iso[L, R]): Iso[F[L], F[R]] =
Iso.instance[F[L], F[R]](fl => Functor[F].map(fl)(iso.isoTo))(fr =>
Functor[F].map(fr)(iso.isoFrom))
Iso.instance[F[L], F[R]](fl => Functor[F].map(fl)(iso.isoTo))(fr => Functor[F].map(fr)(iso.isoFrom))

implicit def isoOption[L, R](
implicit iso: Iso[L, R]): Iso[Option[L], Option[R]] =
implicit def isoOption[L, R](implicit iso: Iso[L, R]): Iso[Option[L], Option[R]] =
liftF[Option, L, R]

}
108 changes: 45 additions & 63 deletions jam-core/src/main/scala/jam/sql/Backend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ trait Backend[DBF[_], R[_], W[_]]

type Fr

def query[A: Read](n: Expression[A])(
implicit ns: NamingStrategy): DBF[Vector[A]]
def query[A: Read](n: Expression[A])(implicit ns: NamingStrategy): DBF[Vector[A]]
def update[A](n: DMLNode[A])(implicit ns: NamingStrategy): DBF[Int]

def const(value: String): Fr
Expand All @@ -24,29 +23,26 @@ trait Backend[DBF[_], R[_], W[_]]
(n: DQLNode[A]) => query(n)

implicit def backendEffect: Jam[DBF] = new Jam[DBF] {
def query[A](n: DQLNode[A])(implicit r: Decode[DBF, A],
ns: NamingStrategy): DBF[Vector[A]] = r(n)
def query[A](n: DQLNode[A])(implicit r: Decode[DBF, A], ns: NamingStrategy): DBF[Vector[A]] = r(n)
def update[A](n: DMLNode[A])(implicit ns: NamingStrategy): DBF[Int] =
update(n)
}

def run(n: Node)(implicit ns: NamingStrategy, M: Monoid[Fr]): Fr = n match {
case EntityName(e) => const(ns.name(e))
case PropertyName(p) => const(ns.name(p))
case PropertyAliasNode(a, v) =>
const(s"${a.name}.") |+| run(PropertyName(v))
case SubstitutedExpression(a, _) => const(a.name)
case EncloseExpression(v) => const("(") |+| run(v) |+| const(")")
case AsNode(a, v: DQLNode[_], t) => const("(") |+| run(t(v)) |+| const(")") |+| const(" as ") |+| const(a.name)
case AsNode(a, v, t) => run(t(v)) |+| const(" as ") |+| const(a.name)
case p: Property[_] => run(PropertyName(p))
case c: Composite[_] =>
c.properties.vector.map(PropertyName).map(run).sep(comma)
case BindNode(v) => v.sep(comma)
case LiteralNode(v) => v.sep(comma)
case PropertyList(ps) => ps.map(p => run(PropertyName(p))).sep(comma)
case EntityPropertyListNode(e, pl) =>
run(EntityName(e)) |+| run(pl).enclose
case EntityName(e) => const(ns.name(e))
case PropertyName(p) => const(ns.name(p))
case PropertyAliasNode(a, v) => const(s"${a.name}.") |+| run(PropertyName(v))
case SubstitutedExpression(a, _) => const(a.name)
case EncloseExpression(v) => const("(") |+| run(v) |+| const(")")
case AsNode(a, v: DQLNode[_], t) => const("(") |+| run(t(v)) |+| const(")") |+| const(" as ") |+| const(a.name)
case AsNode(a, v, t) => run(t(v)) |+| const(" as ") |+| const(a.name)
case p: Property[_] => run(PropertyName(p))
case c: Composite[_] => c.properties.vector.map(PropertyName).map(run).sep(comma)
case BindNode(v) => v.sep(comma)
case LiteralNode(v) => v.sep(comma)
case PropertyList(ps) => ps.map(p => run(PropertyName(p))).sep(comma)
case EntityPropertyListNode(e, pl) => run(EntityName(e)) |+| run(pl).enclose

case SetPropertyNode(p, v) =>
v match {
case SelectNode(_, _, _) =>
Expand All @@ -66,54 +62,40 @@ trait Backend[DBF[_], R[_], W[_]]
.sep(comma)

case FunctionNode(name, e) => const(name) |+| run(e).enclose
case ExpressionList(es) => es.map(run).sep(comma)

case FromNode(DQL, vs, t) =>
const("from ") |+| vs.map(v => run(t(v))).sep(comma)
case JoinNode(p, JoinType.InnerJoin, v, t) =>
run(p) |+| const(" inner join ") |+| run(t(v))
case JoinNode(p, JoinType.LeftOuterJoin, v, t) =>
run(p) |+| const(" left outer join ") |+| run(t(v))
case JoinNode(p, JoinType.RightOuterJoin, v, t) =>
run(p) |+| const(" right outer join ") |+| run(t(v))
case JoinNode(p, JoinType.CrossJoin, v, t) =>
run(p) |+| const(" cross join ") |+| run(t(v))
case OnNode(p, v, t) => run(p) |+| const(" on ") |+| run(t(v)).enclose
case DQLWhereNode(p, v, t) => run(p) |+| const(" where ") |+| run(t(v))
case GroupByNode(p, vs, t) =>
run(p) |+| const(" group by ") |+| vs.map(v => run(t(v))).sep(comma)
case HavingNode(p, v, t) => run(p) |+| const(" having ") |+| run(t(v))
case OrderByNode(p, vs, t) =>
run(p) |+| const(" order by ") |+| vs.map(v => run(t(v))).sep(comma)
case LimitNode(p, v, t) => run(p) |+| const(" limit ") |+| run(t(v))
case SelectNode(DQL, v, t) => const("select ") |+| run(t(v))
case SelectNode(p, v, t) =>
const("select ") |+| run(t(v)) |+| const(" ") |+| run(p)

case UnionNode(p, UnionType.Union, v, t) =>
run(p) |+| const(" union ") |+| run(t(v))
case UnionNode(p, UnionType.UnionAll, v, t) =>
run(p) |+| const(" union all ") |+| run(t(v))
case ExpressionList(es) => es.map(run).sep(comma)

case FromNode(DQL, vs, t) => const("from ") |+| vs.map(v => run(t(v))).sep(comma)
case JoinNode(p, JoinType.InnerJoin, v, t) => run(p) |+| const(" inner join ") |+| run(t(v))
case JoinNode(p, JoinType.LeftOuterJoin, v, t) => run(p) |+| const(" left outer join ") |+| run(t(v))
case JoinNode(p, JoinType.RightOuterJoin, v, t) => run(p) |+| const(" right outer join ") |+| run(t(v))
case JoinNode(p, JoinType.CrossJoin, v, t) => run(p) |+| const(" cross join ") |+| run(t(v))
case OnNode(p, v, t) => run(p) |+| const(" on ") |+| run(t(v)).enclose
case DQLWhereNode(p, v, t) => run(p) |+| const(" where ") |+| run(t(v))
case GroupByNode(p, vs, t) => run(p) |+| const(" group by ") |+| vs.map(v => run(t(v))).sep(comma)
case HavingNode(p, v, t) => run(p) |+| const(" having ") |+| run(t(v))
case OrderByNode(p, vs, t) => run(p) |+| const(" order by ") |+| vs.map(v => run(t(v))).sep(comma)
case LimitNode(p, v, t) => run(p) |+| const(" limit ") |+| run(t(v))
case SelectNode(DQL, v, t) => const("select ") |+| run(t(v))
case SelectNode(p, v, t) => const("select ") |+| run(t(v)) |+| const(" ") |+| run(p)

case UnionNode(p, UnionType.Union, v, t) => run(p) |+| const(" union ") |+| run(t(v))
case UnionNode(p, UnionType.UnionAll, v, t) => run(p) |+| const(" union all ") |+| run(t(v))

case DeleteFromNode(DML, v, t) => const("delete from ") |+| run(t(v))

case InsertIntoNode(DML, v, t) => const("insert into ") |+| run(t(v))
case ValuesNode(p, vs, t) =>
run(p) |+| const(" values ") |+| vs
.map(v => run(t(v)).enclose)
.sep(comma)
case InsertIntoSelect(p, sn) => run(p) |+| const(" ") |+| run(sn)
case ValuesNode(p, vs, t) => run(p) |+| const(" values ") |+| vs.map(v => run(t(v)).enclose).sep(comma)
case InsertIntoSelect(p, sn) => run(p) |+| const(" ") |+| run(sn)

case UpdateNode(DML, v, t) => const("update ") |+| run(t(v))
case SetNode(p, vs, t) =>
run(p) |+| const(" set ") |+| vs.map(v => run(t(v))).sep(comma)
case SetNode(p, vs, t) => run(p) |+| const(" set ") |+| vs.map(v => run(t(v))).sep(comma)

case DMLWhereNode(p, v, t) => run(p) |+| const(" where ") |+| run(t(v))

case ArithmeticOperator.Plus => const(" + ")
case ArithmeticOperator.Minus => const(" - ")
case ArithmeticOperator.Plus => const(" + ")
case ArithmeticOperator.Minus => const(" - ")
case ArithmeticOperator.Multiply => const(" * ")
case ArithmeticOperator.Divide => const(" / ")
case ArithmeticOperator.Divide => const(" / ")

case EqOperator.Eq => const(" = ")
case EqOperator.Ne => const(" <> ")
Expand All @@ -124,7 +106,7 @@ trait Backend[DBF[_], R[_], W[_]]
case PartialOrderOperator.Le => const(" <= ")

case LogicOperator.And => const(" and ")
case LogicOperator.Or => const(" or ")
case LogicOperator.Or => const(" or ")

case InfixNode(op @ LogicOperator.And, l, r) =>
run(l).enclose |+| run(op) |+| run(r).enclose
Expand All @@ -136,11 +118,11 @@ trait Backend[DBF[_], R[_], W[_]]

case InNode(l, r, not) =>
(l, r.toList) match {
case (ExpressionList(_) | PropertyList(_),
(n: DQLNode[_]) :: Nil) =>
case (ExpressionList(_) | PropertyList(_), (n: DQLNode[_]) :: Nil) =>
run(l).enclose |+| (if (not) const(" not in ") else const(" in ")) |+| run(n).enclose
case (ExpressionList(_) | PropertyList(_), _) =>
run(l).enclose |+| (if (not) const(" not in ") else const(" in ")) |+| r.map(v => run(v).enclose)
run(l).enclose |+| (if (not) const(" not in ") else const(" in ")) |+| r
.map(v => run(v).enclose)
.sep(comma)
.enclose
case _ =>
Expand All @@ -156,7 +138,7 @@ trait Backend[DBF[_], R[_], W[_]]
case IsNullNode(v, not) =>
run(v) |+| (if (not) const(" is not null") else const(" is null"))

case AscNode(e) => run(e) |+| const(" asc")
case AscNode(e) => run(e) |+| const(" asc")
case DescNode(e) => run(e) |+| const(" desc")

}
Expand Down
2 changes: 1 addition & 1 deletion jam-core/src/main/scala/jam/sql/BackendAst.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jam.sql

trait BackendAst[DBF[_], R[_], W[_]] { self: Backend[DBF, R, W] =>

case class BindNode[A](fragment: Vector[Fr]) extends Expression[A]
case class BindNode[A](fragment: Vector[Fr]) extends Expression[A]
case class LiteralNode[A](fragment: Vector[Fr]) extends Expression[A]

}
47 changes: 14 additions & 33 deletions jam-core/src/main/scala/jam/sql/BackendSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import cats.Monoid
import cats.syntax.monoid._
import shapeless._

trait BackendSyntax[DBF[_], R[_], W[_]] extends ModelSyntax {
self: Backend[DBF, R, W] =>
trait BackendSyntax[DBF[_], R[_], W[_]] extends ModelSyntax { self: Backend[DBF, R, W] =>

protected[this] def interleave(xs: TraversableOnce[Fr], sep: Fr)(
implicit M: Monoid[Fr]): Fr = {
var b = const("")
protected[this] def interleave(xs: TraversableOnce[Fr], sep: Fr)(implicit M: Monoid[Fr]): Fr = {
var b = const("")
var first = true
xs.foreach { x =>
if (first) first = false
Expand All @@ -23,40 +21,29 @@ trait BackendSyntax[DBF[_], R[_], W[_]] extends ModelSyntax {
//def ++(r: Fr): Fr = M.combine(l, r)
def enclose: Fr = const("(") |+| l |+| const(")")
}
protected[this] implicit class FrTraversableOps(frs: TraversableOnce[Fr])(
implicit M: Monoid[Fr]) {
protected[this] implicit class FrTraversableOps(frs: TraversableOnce[Fr])(implicit M: Monoid[Fr]) {
def sep(by: Fr): Fr = interleave(frs, by)
}

trait EntitySyntax[E] {
def findOne(e: Entity[E])(expression: Expression[Boolean])(
implicit r: Read[E],
ns: NamingStrategy): DBF[Option[E]]
def find(e: Entity[E])(expression: Expression[Boolean])(
implicit r: Read[E],
ns: NamingStrategy): DBF[Vector[E]]
def findOne(e: Entity[E])(expression: Expression[Boolean])(implicit r: Read[E], ns: NamingStrategy): DBF[Option[E]]
def find(e: Entity[E])(expression: Expression[Boolean])(implicit r: Read[E], ns: NamingStrategy): DBF[Vector[E]]
}
object EntitySyntax {
def apply[E](implicit ev: EntitySyntax[E]): EntitySyntax[E] = ev
}

implicit class EntitySyntaxOps[E: EntitySyntax](e: Entity[E]) {
def findOne(expression: Expression[Boolean])(
implicit r: Read[E],
ns: NamingStrategy): DBF[Option[E]] =
def findOne(expression: Expression[Boolean])(implicit r: Read[E], ns: NamingStrategy): DBF[Option[E]] =
EntitySyntax[E].findOne(e)(expression)

def find(expression: Expression[Boolean])(
implicit r: Read[E],
ns: NamingStrategy): DBF[Vector[E]] =
def find(expression: Expression[Boolean])(implicit r: Read[E], ns: NamingStrategy): DBF[Vector[E]] =
EntitySyntax[E].find(e)(expression)
}

trait DQLSyntax[A] {
def query(n: DQLNode[A])(implicit r: Read[A],
ns: NamingStrategy): DBF[Vector[A]]
def one(n: DQLNode[A])(implicit r: Read[A],
ns: NamingStrategy): DBF[Option[A]]
def query(n: DQLNode[A])(implicit r: Read[A], ns: NamingStrategy): DBF[Vector[A]]
def one(n: DQLNode[A])(implicit r: Read[A], ns: NamingStrategy): DBF[Option[A]]
}
object DQLSyntax {
def apply[A](implicit ev: DQLSyntax[A]): DQLSyntax[A] = ev
Expand All @@ -71,20 +58,14 @@ trait BackendSyntax[DBF[_], R[_], W[_]] extends ModelSyntax {
}

trait DQLConversionSyntax[L <: HList] {
def to[A](n: DQLNode[L])(implicit g: Generic.Aux[A, L],
r: Read[L],
ns: NamingStrategy): DBF[Vector[A]]
def to[A](n: DQLNode[L])(implicit g: Generic.Aux[A, L], r: Read[L], ns: NamingStrategy): DBF[Vector[A]]
}
object DQLConversionSyntax {
def apply[L <: HList](
implicit ev: DQLConversionSyntax[L]): DQLConversionSyntax[L] = ev
def apply[L <: HList](implicit ev: DQLConversionSyntax[L]): DQLConversionSyntax[L] = ev
}

implicit class DQLConversionSyntaxOps[L <: HList: DQLConversionSyntax](
n: DQLNode[L]) {
def to[A](implicit g: Generic.Aux[A, L],
r: Read[L],
ns: NamingStrategy): DBF[Vector[A]] =
implicit class DQLConversionSyntaxOps[L <: HList: DQLConversionSyntax](n: DQLNode[L]) {
def to[A](implicit g: Generic.Aux[A, L], r: Read[L], ns: NamingStrategy): DBF[Vector[A]] =
DQLConversionSyntax[L].to[A](n)
}

Expand Down
11 changes: 3 additions & 8 deletions jam-core/src/main/scala/jam/sql/IsoInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ import shapeless.Lazy

trait IsoInstances[DBF[_], R[_], W[_]] { self: Backend[DBF, R, W] =>

implicit def isoRead[LHS, RHS](implicit ev: Iso[LHS, RHS],
r: Lazy[Read[RHS]],
F: Functor[R]): Read[LHS] =
implicit def isoRead[LHS, RHS](implicit ev: Iso[LHS, RHS], r: Lazy[Read[RHS]], F: Functor[R]): Read[LHS] =
Read[RHS](r.value).map(ev.isoFrom)

implicit def isoWrite[LHS, RHS](implicit ev: Iso[LHS, RHS],
w: Lazy[Write[RHS]],
F: Contravariant[W]): Write[LHS] =
implicit def isoWrite[LHS, RHS](implicit ev: Iso[LHS, RHS], w: Lazy[Write[RHS]], F: Contravariant[W]): Write[LHS] =
Write[RHS](w.value).contramap[LHS](ev.isoTo)

implicit def isoLiteral[LHS, RHS](implicit ev: Iso[LHS, RHS],
l: Lazy[Literal[RHS]]): Literal[LHS] =
implicit def isoLiteral[LHS, RHS](implicit ev: Iso[LHS, RHS], l: Lazy[Literal[RHS]]): Literal[LHS] =
Literal[RHS](l.value).contramap[LHS](ev.isoTo)

}
3 changes: 1 addition & 2 deletions jam-core/src/main/scala/jam/sql/Jam.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package jam.sql

trait Jam[F[_]] {
def query[A](n: DQLNode[A])(implicit r: Decode[F, A],
ns: NamingStrategy): F[Vector[A]]
def query[A](n: DQLNode[A])(implicit r: Decode[F, A], ns: NamingStrategy): F[Vector[A]]
def update[A](n: DMLNode[A])(implicit ns: NamingStrategy): F[Int]
}
object Jam {
Expand Down
Loading

0 comments on commit 6b4295d

Please sign in to comment.