Skip to content

Commit

Permalink
Separate specifics for Postgres, MySql, MariaDb
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed Jan 9, 2024
1 parent a0904d3 commit f87aa1b
Show file tree
Hide file tree
Showing 14 changed files with 849 additions and 48 deletions.
10 changes: 8 additions & 2 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ object squery extends CommonScalaModule with SqueryPublishModule {
ivy"ch.qos.logback:logback-classic:1.4.6",
ivy"org.scalameta::munit:1.0.0-M7",
ivy"com.zaxxer:HikariCP:4.0.3",
ivy"org.postgresql:postgresql:42.5.4",
ivy"org.testcontainers:testcontainers:1.17.6",
ivy"org.testcontainers:postgresql:1.17.6"
ivy"org.testcontainers:postgresql:1.17.6",
ivy"org.postgresql:postgresql:42.5.4",
ivy"org.testcontainers:mysql:1.19.3",
ivy"mysql:mysql-connector-java:8.0.33",
ivy"org.testcontainers:mariadb:1.19.3",
ivy"org.mariadb.jdbc:mariadb-java-client:3.3.2"


)
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/files/howtos/HowToPage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait HowToPage extends DocPage {
InterpolateValues,
InterpolateQueries,
DynamicQueries,
Transactions,
Transactions
)

override def pageCategory = Some("How-Tos")
Expand Down
13 changes: 13 additions & 0 deletions squery/src/ba/sake/squery/mariadb/reads.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ba.sake.squery.mariadb

import java.{sql => jsql}
import java.util.UUID
import ba.sake.squery.read.*

given SqlRead[UUID] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[UUID] =
Option(jRes.getString(colName)).map(UUID.fromString)

def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[UUID] =
Option(jRes.getString(colIdx)).map(UUID.fromString)
}
15 changes: 15 additions & 0 deletions squery/src/ba/sake/squery/mariadb/writes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ba.sake.squery.mariadb

import java.{sql => jsql}
import java.util.UUID
import ba.sake.squery.write.*

given SqlWrite[UUID] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
valueOpt: Option[UUID]
): Unit = valueOpt match
case Some(value) => ps.setString(idx, value.toString)
case None => ps.setString(idx, null)
}
13 changes: 13 additions & 0 deletions squery/src/ba/sake/squery/mysql/reads.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ba.sake.squery.mysql

import java.{sql => jsql}
import java.util.UUID
import ba.sake.squery.read.*

given SqlRead[UUID] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[UUID] =
Option(jRes.getString(colName)).map(UUID.fromString)

def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[UUID] =
Option(jRes.getString(colIdx)).map(UUID.fromString)
}
15 changes: 15 additions & 0 deletions squery/src/ba/sake/squery/mysql/writes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ba.sake.squery.mysql

import java.{sql => jsql}
import java.util.UUID
import ba.sake.squery.write.*

given SqlWrite[UUID] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
valueOpt: Option[UUID]
): Unit = valueOpt match
case Some(value) => ps.setString(idx, value.toString)
case None => ps.setString(idx, null)
}
13 changes: 13 additions & 0 deletions squery/src/ba/sake/squery/postgres/reads.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ba.sake.squery.postgres

import java.{sql => jsql}
import java.util.UUID
import ba.sake.squery.read.*

given SqlRead[UUID] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[UUID] =
Option(jRes.getObject(colName, classOf[UUID]))

def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[UUID] =
Option(jRes.getObject(colIdx, classOf[UUID]))
}
15 changes: 15 additions & 0 deletions squery/src/ba/sake/squery/postgres/writes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ba.sake.squery.postgres

import java.{sql => jsql}
import java.util.UUID
import ba.sake.squery.write.*

given SqlWrite[UUID] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
valueOpt: Option[UUID]
): Unit = valueOpt match
case Some(value) => ps.setObject(idx, value)
case None => ps.setNull(idx, jsql.Types.OTHER)
}
8 changes: 0 additions & 8 deletions squery/src/ba/sake/squery/read/SqlRead.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ object SqlRead {
Option(jRes.getTimestamp(colIdx)).map(_.toLocalDateTime())
}

given SqlRead[UUID] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[UUID] =
Option(jRes.getObject(colName, classOf[UUID]))

def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[UUID] =
Option(jRes.getObject(colIdx, classOf[UUID]))
}

// this "cannot fail"
given [T](using sr: SqlRead[T]): SqlRead[Option[T]] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[Option[T]] =
Expand Down
14 changes: 2 additions & 12 deletions squery/src/ba/sake/squery/write/SqlWrite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.TIMESTAMP)
}

given SqlWrite[UUID] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
valueOpt: Option[UUID]
): Unit = valueOpt match
case Some(value) => ps.setObject(idx, value)
case None => ps.setNull(idx, jsql.Types.OTHER)
}

given [T](using sw: SqlWrite[T]): SqlWrite[Option[T]] with {
def write(
ps: jsql.PreparedStatement,
Expand Down Expand Up @@ -175,8 +165,8 @@ object SqlWrite {
case Some(value) =>
val index = $m.ordinal(value)
val label = $labels(index)
ps.setObject(idx, label, jsql.Types.OTHER)
case None => ps.setNull(idx, jsql.Types.OTHER)
ps.setString(idx, label)
case None => ps.setString(idx, null)
}
}

Expand Down
12 changes: 0 additions & 12 deletions squery/test/src/ba/sake/squery/dataTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,4 @@ case class CustomerWithPhoneOpt(c: Customer, p: Option[Phone]) derives SqlReadRo
case class Address(id: Int, name: Option[String]) derives SqlReadRow
case class CustomerWithAddressOpt(c: Customer, a: Option[Address]) derives SqlReadRow

case class Datatypes(
int: Option[Int],
long: Option[Long],
double: Option[Double],
boolean: Option[Boolean],
string: Option[String],
uuid: Option[UUID],
tstz: Option[Instant],
clr: Option[Color]
) derives SqlReadRow

enum Color derives SqlRead, SqlWrite:
case red, green, blue
Loading

0 comments on commit f87aa1b

Please sign in to comment.