Skip to content

Commit

Permalink
Cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed Jan 6, 2024
1 parent d44a3dc commit 56b5fda
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 32 deletions.
2 changes: 2 additions & 0 deletions DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ git push --atomic origin main $VERSION

# TODOs

- update scastie!
- more tutorials
- privatize stuff
- test more databases
- sql parser reorders OFFSET LIMIT ???
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ case class Phone(id: Int, number: String) derives SqlReadRow
// joined row
case class CustomerWithPhone(c: Customer, p: Phone) derives SqlReadRow

val ds = new org.h2.jdbcx.JdbcDataSource()
val ds = JdbcDataSource()
ds.setURL("jdbc:h2:mem:")

val ctx = new SqueryContext(ds)
val ctx = SqueryContext(ds)

ctx.run {
val res: Seq[CustomerWithPhone] = sql"""
Expand Down
2 changes: 1 addition & 1 deletion docs/src/files/tutorials/GettingStarted.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object GettingStarted extends TutorialPage {
ds.setUsername(..)
ds.setPassword(..)

val ctx = new SqueryContext(ds)
val ctx = SqueryContext(ds)
```
""".md
)
Expand Down
3 changes: 2 additions & 1 deletion examples/cli/hello.sc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
//> using dep "com.lihaoyi::pprint:0.8.1"

import ba.sake.squery.*
import org.h2.jdbcx.JdbcDataSource

val ds = new org.h2.jdbcx.JdbcDataSource()
val ds = JdbcDataSource()
ds.setURL("jdbc:h2:mem:")

val ctx = SqueryContext(ds)
Expand Down
2 changes: 1 addition & 1 deletion squery/src/ba/sake/squery/SqueryAddAliasesVisitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private class SqueryAddAliasesVisitor extends SelectVisitor, SelectItemVisitor {
val alias = s""" "$selectExpressionItem" """.trim
if usedAliases.contains(alias) then throw SqueryException(s"Alias '$alias' is already used in this query.")
usedAliases += alias
selectExpressionItem.setAlias(new Alias(alias))
selectExpressionItem.setAlias(Alias(alias))

override def visit(withItem: WithItem): Unit = {}

Expand Down
2 changes: 1 addition & 1 deletion squery/src/ba/sake/squery/query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ object Query {
val stmt = CCJSqlParserUtil.parse(query)
stmt match
case selectStmt: Select =>
selectStmt.getSelectBody().accept(new SqueryAddAliasesVisitor())
selectStmt.getSelectBody().accept(SqueryAddAliasesVisitor())
selectStmt.getSelectBody().toString()
case updateStmt: Update =>
if updateStmt.getWhere() == null then
Expand Down
22 changes: 11 additions & 11 deletions squery/src/ba/sake/squery/read/SqlRead.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,74 @@ trait SqlRead[T]:
object SqlRead {
def apply[T](using sqlRead: SqlRead[T]): SqlRead[T] = sqlRead

given SqlRead[String] = new {
given SqlRead[String] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[String] =
Option(jRes.getString(colName))
def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[String] =
Option(jRes.getString(colIdx))
}

given SqlRead[Boolean] = new {
given SqlRead[Boolean] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[Boolean] =
Option(jRes.getBoolean(colName)).filterNot(_ => jRes.wasNull())
def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[Boolean] =
Option(jRes.getBoolean(colIdx)).filterNot(_ => jRes.wasNull())
}

given SqlRead[Int] = new {
given SqlRead[Int] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[Int] =
Option(jRes.getInt(colName)).filterNot(_ => jRes.wasNull())
def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[Int] =
Option(jRes.getInt(colIdx)).filterNot(_ => jRes.wasNull())
}

given SqlRead[Long] = new {
given SqlRead[Long] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[Long] =
Option(jRes.getLong(colName)).filterNot(_ => jRes.wasNull())
def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[Long] =
Option(jRes.getLong(colIdx)).filterNot(_ => jRes.wasNull())
}

given SqlRead[Double] = new {
given SqlRead[Double] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[Double] =
Option(jRes.getDouble(colName)).filterNot(_ => jRes.wasNull())
def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[Double] =
Option(jRes.getDouble(colIdx)).filterNot(_ => jRes.wasNull())
}

given SqlRead[Instant] = new {
given SqlRead[Instant] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[Instant] =
Option(jRes.getTimestamp(colName)).map(_.toInstant)

def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[Instant] =
Option(jRes.getTimestamp(colIdx)).map(_.toInstant)
}

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

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

given SqlRead[LocalDate] = new {
given SqlRead[LocalDate] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[LocalDate] =
Option(jRes.getDate(colName)).map(_.toLocalDate())

def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[LocalDate] =
Option(jRes.getDate(colIdx)).map(_.toLocalDate())
}

given SqlRead[LocalDateTime] = new {
given SqlRead[LocalDateTime] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[LocalDateTime] =
Option(jRes.getTimestamp(colName)).map(_.toLocalDateTime())

def readByIdx(jRes: jsql.ResultSet, colIdx: Int): Option[LocalDateTime] =
Option(jRes.getTimestamp(colIdx)).map(_.toLocalDateTime())
}

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

Expand All @@ -91,7 +91,7 @@ object SqlRead {
}

// this "cannot fail"
given [T](using sr: SqlRead[T]): SqlRead[Option[T]] = new {
given [T](using sr: SqlRead[T]): SqlRead[Option[T]] with {
def readByName(jRes: jsql.ResultSet, colName: String): Option[Option[T]] =
Some(sr.readByName(jRes, colName))

Expand Down
2 changes: 1 addition & 1 deletion squery/src/ba/sake/squery/read/SqlReadRow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object SqlReadRow:
def apply[T](using sqlReadRow: SqlReadRow[T]): SqlReadRow[T] = sqlReadRow

// cannot fail, it will *at least* give you a None
given [T](using srr: SqlReadRow[T]): SqlReadRow[Option[T]] = new {
given [T](using srr: SqlReadRow[T]): SqlReadRow[Option[T]] with {
def readRow(jRes: ResultSet, prefix: Option[String]): Option[Option[T]] =
Some(srr.readRow(jRes, prefix))
}
Expand Down
3 changes: 2 additions & 1 deletion squery/src/ba/sake/squery/sql.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ type SqlInterpolatorArg = SqlArgument[?] | Query
*/
extension (sc: StringContext) {

// TODO implement as a macro, so we get a statically known string literal... !?? ZOMG :OO
def sql(args: SqlInterpolatorArg*): Query =
val stringPartsIter = sc.parts.iterator
val argsIter = args.iterator
var sb = new StringBuilder(stringPartsIter.next())
var sb = StringBuilder(stringPartsIter.next())
val allArgs = ListBuffer.empty[SqlArgument[?]]

while stringPartsIter.hasNext do {
Expand Down
22 changes: 11 additions & 11 deletions squery/src/ba/sake/squery/write/SqlWrite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object SqlWrite {

def apply[T](using sqlWrite: SqlWrite[T]): SqlWrite[T] = sqlWrite

given SqlWrite[String] = new {
given SqlWrite[String] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -29,7 +29,7 @@ object SqlWrite {
case None => ps.setString(idx, null)
}

given SqlWrite[Boolean] = new {
given SqlWrite[Boolean] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -39,7 +39,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.BOOLEAN)
}

given SqlWrite[Int] = new {
given SqlWrite[Int] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -49,7 +49,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.INTEGER)
}

given SqlWrite[Long] = new {
given SqlWrite[Long] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -59,7 +59,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.BIGINT)
}

given SqlWrite[Double] = new {
given SqlWrite[Double] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -69,7 +69,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.DOUBLE)
}

given SqlWrite[Instant] = new {
given SqlWrite[Instant] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -79,7 +79,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.TIMESTAMP_WITH_TIMEZONE)
}

given SqlWrite[OffsetDateTime] = new {
given SqlWrite[OffsetDateTime] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -89,7 +89,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.TIMESTAMP_WITH_TIMEZONE)
}

given SqlWrite[LocalDate] = new {
given SqlWrite[LocalDate] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -99,7 +99,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.DATE)
}

given SqlWrite[LocalDateTime] = new {
given SqlWrite[LocalDateTime] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -118,7 +118,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.TIMESTAMP)
}

given SqlWrite[UUID] = new {
given SqlWrite[UUID] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand All @@ -128,7 +128,7 @@ object SqlWrite {
case None => ps.setNull(idx, jsql.Types.OTHER)
}

given [T](using sw: SqlWrite[T]): SqlWrite[Option[T]] = new {
given [T](using sw: SqlWrite[T]): SqlWrite[Option[T]] with {
def write(
ps: jsql.PreparedStatement,
idx: Int,
Expand Down
4 changes: 2 additions & 2 deletions squery/test/src/ba/sake/squery/PostgresSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class PostgresSuite extends munit.FunSuite {
def apply() = ctx

override def beforeAll(): Unit = {
container = new PostgreSQLContainer("postgres:9.6.12")
container = PostgreSQLContainer("postgres:9.6.12")
container.start()

val ds = com.zaxxer.hikari.HikariDataSource()
ds.setJdbcUrl(container.getJdbcUrl())
ds.setUsername(container.getUsername())
ds.setPassword(container.getPassword())

ctx = new SqueryContext(ds)
ctx = SqueryContext(ds)

ctx.run {
sql"""
Expand Down

0 comments on commit 56b5fda

Please sign in to comment.