Skip to content

Commit

Permalink
Add H2 utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed Jan 9, 2024
1 parent f83e946 commit 30aca07
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 5 deletions.
6 changes: 4 additions & 2 deletions DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ git push --atomic origin main $VERSION

# TODOs

- matrix of supported features (or require an implicit.. e.g. Supports[ReturningGeneratedColumns])
- update scastie!
- more tutorials
- write tutorials
- privatize stuff
- test more databases
- neo4j
- cassandra
- sql parser reorders OFFSET LIMIT ???
1 change: 1 addition & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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"com.h2database:h2:2.1.214",
ivy"org.testcontainers:testcontainers:1.17.6",
ivy"org.testcontainers:postgresql:1.17.6",
ivy"org.postgresql:postgresql:42.5.4",
Expand Down
4 changes: 2 additions & 2 deletions docs/src/files/howtos/HowToPage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import utils.*
import Bundle.*

// TODO how to map flat result to List[stuff] groupByOrdered :)
// TODO transactions

trait HowToPage extends DocPage {

Expand All @@ -22,7 +21,8 @@ trait HowToPage extends DocPage {
InterpolateValues,
InterpolateQueries,
DynamicQueries,
Transactions
Transactions,
MapToObject
)

override def pageCategory = Some("How-Tos")
Expand Down
48 changes: 48 additions & 0 deletions docs/src/files/howtos/MapToObject.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package files.howtos

import utils.*
import Bundle.*, Tags.*

object MapToObject extends HowToPage {

override def pageSettings =
super.pageSettings.withTitle("Map Results To Objects")

override def blogSettings =
super.blogSettings.withSections(firstSection)

val firstSection = Section(
"How To Map Flat Rows To Objects",
frag(
s"""
You can do arbitrary SQL commands here.
The most common one is `UPDATE`-ing some rows:
```scala
// returns number of affected rows
def updateCustomers: Int = ctx.run {
sql${Consts.tq}
UPDATE customers
SET name = 'whatever'
WHERE name LIKE 'xyz_%'
${Consts.tq}.update()
}
```

---

But of course you can do other commands as well:
```scala
def createTable: Unit = ctx.run {
sql${Consts.tq}
CREATE TABLE customers(
id SERIAL PRIMARY KEY,
name VARCHAR
)
${Consts.tq}.update()
}
```

""".md
)
)
}
2 changes: 1 addition & 1 deletion examples/cli/hello.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using dep "ba.sake::squery:0.1.0"
//> using dep "ba.sake::squery:0.2.0"
//> using dep "com.h2database:h2:2.1.214"
//> using dep "com.lihaoyi::pprint:0.8.1"

Expand Down
13 changes: 13 additions & 0 deletions squery/src/ba/sake/squery/h2/reads.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ba.sake.squery.h2

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/h2/writes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ba.sake.squery.h2

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)
}
Loading

0 comments on commit 30aca07

Please sign in to comment.