Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split db-specific Module traits into more specialized parts #637

Merged
merged 2 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ lazy val jdbc = project
lazy val mysql = project
.in(file("mysql"))
.dependsOn(jdbc % "compile->compile;test->test")
.dependsOn(driver)
.settings(stdSettings("zio-sql-mysql"))
.settings(buildInfoSettings("zio.sql.mysql"))
.settings(
Expand All @@ -175,6 +176,7 @@ lazy val mysql = project
lazy val oracle = project
.in(file("oracle"))
.dependsOn(jdbc % "compile->compile;test->test")
.dependsOn(driver)
.settings(stdSettings("zio-sql-oracle"))
.settings(buildInfoSettings("zio.sql.oracle"))
.settings(
Expand All @@ -192,6 +194,7 @@ lazy val oracle = project
lazy val postgres = project
.in(file("postgres"))
.dependsOn(jdbc % "compile->compile;test->test")
.dependsOn(driver)
.settings(stdSettings("zio-sql-postgres"))
.settings(buildInfoSettings("zio.sql.postgres"))
.settings(
Expand All @@ -209,6 +212,7 @@ lazy val postgres = project
lazy val sqlserver = project
.in(file("sqlserver"))
.dependsOn(jdbc % "compile->compile;test->test")
.dependsOn(driver)
.settings(stdSettings("zio-sql-sqlserver"))
.settings(buildInfoSettings("zio.sql.sqlserver"))
.settings(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zio.sql
package zio.sql.driver

private[sql] class Renderer(val builder: StringBuilder) extends AnyVal {
// not using vararg to avoid allocating `Seq`s
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/scala/zio/sql/Examples.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package zio.sql

import zio.sql.sqlserver.SqlServerModule
import zio.sql.sqlserver.SqlServerJdbcModule

object Examples extends App with ShopSchema with SqlServerModule {
object Examples extends App with ShopSchema with SqlServerJdbcModule {
import this.AggregationDef._
import this.FunctionDef._
import this.OrderDetails._
Expand Down
4 changes: 2 additions & 2 deletions examples/src/main/scala/zio/sql/GroupByExamples.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package zio.sql

import zio.sql.sqlserver.SqlServerModule
import zio.sql.sqlserver.SqlServerJdbcModule

object GroupByExamples extends App with ShopSchema with SqlServerModule {
object GroupByExamples extends App with ShopSchema with SqlServerJdbcModule {
import AggregationDef._
import ColumnSet._

Expand Down
5 changes: 5 additions & 0 deletions mysql/src/main/scala/zio/sql/mysql/MysqlJdbcModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package zio.sql.mysql

import zio.sql.Jdbc

trait MysqlJdbcModule extends MysqlRenderModule with Jdbc
Original file line number Diff line number Diff line change
@@ -1,63 +1,32 @@
package zio.sql.mysql

import zio.Chunk
import zio.sql.{ Jdbc, Renderer }

import java.sql.ResultSet
import java.time.Year
import zio.schema.Schema
import zio.sql.driver.Renderer

trait MysqlModule extends Jdbc { self =>

override type TypeTagExtension[+A] = MysqlSpecific.MysqlTypeTag[A]

object MysqlSpecific {
trait MysqlTypeTag[+A] extends Tag[A] with Decodable[A]

object MysqlTypeTag {
implicit case object TYear extends MysqlTypeTag[Year] {
override def decode(column: Int, resultSet: ResultSet): Either[DecodingError, Year] =
scala.util
.Try(Year.of(resultSet.getByte(column).toInt))
.fold(
_ => Left(DecodingError.UnexpectedNull(column)),
r => Right(r)
)
}

}
}

object MysqlFunctionDef {
val Crc32 = FunctionDef[String, Long](FunctionName("crc32"))
val Degrees = FunctionDef[Double, Double](FunctionName("degrees"))
val Log2 = FunctionDef[Double, Double](FunctionName("log2"))
val Log10 = FunctionDef[Double, Double](FunctionName("log10"))
val Pi = Expr.FunctionCall0[Double](FunctionDef[Any, Double](FunctionName("pi")))
val BitLength = FunctionDef[String, Int](FunctionName("bit_length"))
}
trait MysqlRenderModule extends MysqlSqlModule { self =>

override def renderRead(read: self.Read[_]): String = {
implicit val render: Renderer = Renderer()
MysqlRenderModule.renderReadImpl(read)
MysqlRenderer.renderReadImpl(read)
render.toString
}

override def renderInsert[A: Schema](insert: self.Insert[_, A]): String = ???

override def renderDelete(delete: self.Delete[_]): String = {
implicit val render: Renderer = Renderer()
MysqlRenderModule.renderDeleteImpl(delete)
MysqlRenderer.renderDeleteImpl(delete)
render.toString
}

override def renderUpdate(update: self.Update[_]): String = {
implicit val render: Renderer = Renderer()
MysqlRenderModule.renderUpdateImpl(update)
MysqlRenderer.renderUpdateImpl(update)
render.toString
}

object MysqlRenderModule {
object MysqlRenderer {
def renderDeleteImpl(delete: Delete[_])(implicit render: Renderer) = {
render("DELETE FROM ")
renderTable(delete.table)
Expand Down
38 changes: 38 additions & 0 deletions mysql/src/main/scala/zio/sql/mysql/MysqlSqlModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package zio.sql.mysql

import java.sql.ResultSet
import java.time.Year

import zio.sql.Sql

trait MysqlSqlModule extends Sql { self =>

override type TypeTagExtension[+A] = MysqlSpecific.MysqlTypeTag[A]

object MysqlSpecific {
trait MysqlTypeTag[+A] extends Tag[A] with Decodable[A]

object MysqlTypeTag {
implicit case object TYear extends MysqlTypeTag[Year] {
override def decode(column: Int, resultSet: ResultSet): Either[DecodingError, Year] =
scala.util
.Try(Year.of(resultSet.getByte(column).toInt))
.fold(
_ => Left(DecodingError.UnexpectedNull(column)),
r => Right(r)
)
}

}
}

object MysqlFunctionDef {
val Crc32 = FunctionDef[String, Long](FunctionName("crc32"))
val Degrees = FunctionDef[Double, Double](FunctionName("degrees"))
val Log2 = FunctionDef[Double, Double](FunctionName("log2"))
val Log10 = FunctionDef[Double, Double](FunctionName("log10"))
val Pi = Expr.FunctionCall0[Double](FunctionDef[Any, Double](FunctionName("pi")))
val BitLength = FunctionDef[String, Int](FunctionName("bit_length"))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zio.sql.{ ConnectionPoolConfig, JdbcRunnableSpec }
import zio.test._
import zio.ZLayer

trait MysqlRunnableSpec extends JdbcRunnableSpec with MysqlModule {
trait MysqlRunnableSpec extends JdbcRunnableSpec with MysqlJdbcModule {

private def connProperties(user: String, password: String): Properties = {
val props = new Properties
Expand Down
5 changes: 5 additions & 0 deletions oracle/src/main/scala/zio/sql/oracle/OracleJdbcModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package zio.sql.oracle

import zio.sql.Jdbc

trait OracleJdbcModule extends OracleRenderModule with Jdbc
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package zio.sql.oracle

import zio.sql.Jdbc
import zio.schema.Schema

trait OracleModule extends Jdbc { self =>
trait OracleRenderModule extends OracleSqlModule { self =>

object OracleFunctionDef {
val Sind = FunctionDef[Double, Double](FunctionName("sind"))
override def renderDelete(delete: self.Delete[_]): String = ???

override def renderInsert[A: Schema](insert: self.Insert[_, A]): String = ???

override def renderRead(read: self.Read[_]): String = {
val builder = new StringBuilder
buildReadString(read, builder)
builder.toString()
}

def buildExpr[A, B](expr: self.Expr[_, A, B], builder: StringBuilder): Unit = expr match {
override def renderUpdate(update: self.Update[_]): String = ???

private def buildExpr[A, B](expr: self.Expr[_, A, B], builder: StringBuilder): Unit = expr match {
case Expr.Subselect(subselect) =>
builder.append(" (")
builder.append(renderRead(subselect))
Expand Down Expand Up @@ -129,7 +136,7 @@ trait OracleModule extends Jdbc { self =>
val _ = builder.append(")")
}

def buildReadString(read: self.Read[_], builder: StringBuilder): Unit =
private def buildReadString(read: self.Read[_], builder: StringBuilder): Unit =
read match {
case Read.Mapped(read, _) => buildReadString(read, builder)

Expand Down Expand Up @@ -199,7 +206,7 @@ trait OracleModule extends Jdbc { self =>
val _ = builder.append(" (").append(values.mkString(",")).append(") ") // todo fix needs escaping
}

def buildExprList(expr: Read.ExprSet[_], builder: StringBuilder): Unit =
private def buildExprList(expr: Read.ExprSet[_], builder: StringBuilder): Unit =
expr match {
case Read.ExprSet.ExprCons(head, tail) =>
buildExpr(head, builder)
Expand All @@ -211,7 +218,7 @@ trait OracleModule extends Jdbc { self =>
}
case Read.ExprSet.NoExpr => ()
}
def buildOrderingList(expr: List[Ordering[Expr[_, _, _]]], builder: StringBuilder): Unit =
private def buildOrderingList(expr: List[Ordering[Expr[_, _, _]]], builder: StringBuilder): Unit =
expr match {
case head :: tail =>
head match {
Expand All @@ -229,7 +236,7 @@ trait OracleModule extends Jdbc { self =>
case Nil => ()
}

def buildSelection[A](selectionSet: SelectionSet[A], builder: StringBuilder): Unit =
private def buildSelection[A](selectionSet: SelectionSet[A], builder: StringBuilder): Unit =
selectionSet match {
case cons0 @ SelectionSet.Cons(_, _) =>
object Dummy {
Expand All @@ -247,7 +254,7 @@ trait OracleModule extends Jdbc { self =>
case SelectionSet.Empty => ()
}

def buildColumnSelection[A, B](columnSelection: ColumnSelection[A, B], builder: StringBuilder): Unit =
private def buildColumnSelection[A, B](columnSelection: ColumnSelection[A, B], builder: StringBuilder): Unit =
columnSelection match {
case ColumnSelection.Constant(value, name) =>
builder.append(value.toString()) // todo fix escaping
Expand All @@ -268,7 +275,7 @@ trait OracleModule extends Jdbc { self =>
case _ => () // todo what do we do if we don't have a name?
}
}
def buildTable(table: Table, builder: StringBuilder): Unit =
private def buildTable(table: Table, builder: StringBuilder): Unit =
table match {
case Table.DialectSpecificTable(_) => ???
// The outer reference in this type test cannot be checked at run time?!
Expand All @@ -292,16 +299,4 @@ trait OracleModule extends Jdbc { self =>
buildExpr(on, builder)
val _ = builder.append(" ")
}

override def renderRead(read: self.Read[_]): String = {
val builder = new StringBuilder
buildReadString(read, builder)
builder.toString()
}

override def renderDelete(delete: self.Delete[_]): String = ???

override def renderInsert[A: Schema](insert: self.Insert[_, A]): String = ???

override def renderUpdate(update: self.Update[_]): String = ???
}
11 changes: 11 additions & 0 deletions oracle/src/main/scala/zio/sql/oracle/OracleSqlModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package zio.sql.oracle

import zio.sql.Sql

trait OracleSqlModule extends Sql { self =>

object OracleFunctionDef {
val Sind = FunctionDef[Double, Double](FunctionName("sind"))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package zio.sql.postgresql

import zio.sql.Jdbc

trait PostgresJdbcModule extends PostgresRenderModule with Jdbc
Loading