Skip to content

Commit d314994

Browse files
committed
update example scripts
1 parent 104ede9 commit d314994

File tree

2 files changed

+148
-5
lines changed

2 files changed

+148
-5
lines changed

scripts/example-cats-effect-usage.scala

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// This script demonstrates basic usage of the duck4s-cats-effect library after publishing locally
33
//> using scala "3.8.2"
44
//> using repository "ivy2Local"
5-
//> using dep "com.softinio:duck4s_3:0.1.2-4-1bed287" // Update this version to match your locally published version
6-
//> using dep "com.softinio:duck4s-cats-effect_3:0.1.2-4-1bed287" // Update this version to match your locally published version
5+
//> using dep "com.softinio:duck4s_3:0.1.3-5-104ede9" // Update this version to match your locally published version
6+
//> using dep "com.softinio:duck4s-cats-effect_3:0.1.3-5-104ede9" // Update this version to match your locally published version
77

88
import cats.effect.{IO, IOApp}
99
import com.softinio.duck4s.effect.*
@@ -17,6 +17,8 @@ object TestDuck4sCatsEffect extends IOApp.Simple:
1717
testStream >>
1818
testTransaction >>
1919
testRollback >>
20+
testNewTypes >>
21+
testBatchNewTypes >>
2022
IO.println("\nAll tests passed!")
2123

2224
def testConnection: IO[Unit] =
@@ -85,6 +87,74 @@ object TestDuck4sCatsEffect extends IOApp.Simple:
8587
yield ()
8688
}
8789

90+
// New in 0.1.4: setFloat, setDate, setTimestamp, setObject (UUID),
91+
// setBigDecimal, setBytes; and ResultSet.getBytes
92+
def testNewTypes: IO[Unit] =
93+
DuckDBIO.connect().use { conn =>
94+
val ts = java.sql.Timestamp.valueOf("2024-06-15 10:30:00")
95+
val date = java.sql.Date.valueOf("2024-06-15")
96+
val uuid = java.util.UUID.fromString("550e8400-e29b-41d4-a716-446655440000")
97+
val bytes = "hello".getBytes("UTF-8")
98+
99+
for
100+
_ <- conn.executeUpdateIO("""
101+
CREATE TABLE events (
102+
id INTEGER,
103+
score FLOAT,
104+
price DECIMAL(10,2),
105+
recorded DATE,
106+
created TIMESTAMP,
107+
uid UUID,
108+
payload BLOB
109+
)
110+
""")
111+
_ <- conn.withPreparedStatementIO("INSERT INTO events VALUES (?, ?, ?, ?, ?, ?, ?)") { stmt =>
112+
for
113+
_ <- IO.blocking(stmt.setInt(1, 1))
114+
_ <- IO.blocking(stmt.setFloat(2, 9.5f))
115+
_ <- IO.blocking(stmt.setBigDecimal(3, BigDecimal("19.99")))
116+
_ <- IO.blocking(stmt.setDate(4, date))
117+
_ <- IO.blocking(stmt.setTimestamp(5, ts))
118+
_ <- IO.blocking(stmt.setObject(6, uuid))
119+
_ <- IO.blocking(stmt.setBytes(7, bytes))
120+
n <- IO.blocking(stmt.executeUpdate())
121+
yield n
122+
}
123+
rs <- conn.executeQueryIO("SELECT * FROM events WHERE id = 1")
124+
_ <- IO:
125+
assert(rs.next(), "expected a row")
126+
assert(rs.getFloat("score") == 9.5f)
127+
val blob = rs.getBytes("payload")
128+
assert(new String(blob) == "hello", s"expected 'hello' blob, got '${new String(blob)}'")
129+
rs.close()
130+
_ <- IO.println("0.1.4 new PreparedStatement types (setFloat/setDate/setTimestamp/setObject/setBigDecimal/setBytes) and ResultSet.getBytes OK")
131+
yield ()
132+
}
133+
134+
// New in 0.1.4: batch binders for Float, BigDecimal, UUID, Date, Timestamp, Blob
135+
def testBatchNewTypes: IO[Unit] =
136+
DuckDBIO.connect().use { conn =>
137+
val ts = java.sql.Timestamp.valueOf("2024-01-01 00:00:00")
138+
val date = java.sql.Date.valueOf("2024-01-01")
139+
val uuid = java.util.UUID.randomUUID()
140+
val bytes = "data".getBytes("UTF-8")
141+
142+
for
143+
_ <- conn.executeUpdateIO("CREATE TABLE readings (id INTEGER, ts TIMESTAMP, uid UUID, val FLOAT, dec DECIMAL(10,2), data BLOB)")
144+
result <- conn.withBatchIO("INSERT INTO readings VALUES (?, ?, ?, ?, ?, ?)") { batch =>
145+
for
146+
_ <- IO.blocking(batch.addBatch((1, ts, uuid, 1.5f, BigDecimal("9.99"), bytes)))
147+
r <- IO.blocking(batch.executeBatch()).flatMap {
148+
case Right(r) => IO.pure(r)
149+
case Left(e) => IO.raiseError(new RuntimeException(e.toString))
150+
}
151+
yield r
152+
}
153+
_ <- IO(assert(result.successCount == 1, s"expected 1 success, got ${result.successCount}"))
154+
_ <- IO.println(s"0.1.4 batch with new types OK: ${result.successCount} inserted")
155+
yield ()
156+
}
157+
88158
// To run this example:
89159
// 1. First publish both modules locally: mill __.publishLocal
90160
// 2. Check the published version: mill show 'duck4s[3.8.2].publishVersion'

scripts/example-usage.scala

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// This script demonstrates basic usage of the duck4s library after publishing locally
33
//> using scala "3.8.2"
44
//> using repository "ivy2Local"
5-
//> using dep "com.softinio:duck4s_3:0.1.2-4-1bed287" // Update this version to match your locally published version
5+
//> using dep "com.softinio:duck4s_3:0.1.3-5-104ede9" // Update this version to match your locally published version
66

77
import com.softinio.duck4s.*
88
import com.softinio.duck4s.algebra.*
@@ -44,13 +44,86 @@ import com.softinio.duck4s.algebra.*
4444
}
4545
}
4646
} match {
47-
case Right(_) => println("Query completed")
47+
case Right(_) => println("Basic query completed")
4848
case Left(error) => println(s"❌ Query failed: $error")
4949
}
5050

51+
// Test new 0.1.4 PreparedStatement features: setFloat, setDate, setTimestamp,
52+
// setObject (UUID), setBigDecimal, setBytes; and ResultSet.getBytes
53+
println("\nTesting 0.1.4 new type support...")
54+
55+
connection.executeUpdate("""
56+
CREATE TABLE events (
57+
id INTEGER,
58+
score FLOAT,
59+
price DECIMAL(10,2),
60+
recorded DATE,
61+
created TIMESTAMP,
62+
uid UUID,
63+
payload BLOB
64+
)
65+
""") match {
66+
case Right(_) => println("✅ Created events table")
67+
case Left(error) => println(s"❌ Failed to create events table: $error")
68+
}
69+
70+
val ts = java.sql.Timestamp.valueOf("2024-06-15 10:30:00")
71+
val date = java.sql.Date.valueOf("2024-06-15")
72+
val uuid = java.util.UUID.fromString("550e8400-e29b-41d4-a716-446655440000")
73+
val bytes = "hello".getBytes("UTF-8")
74+
75+
connection.withPreparedStatement("INSERT INTO events VALUES (?, ?, ?, ?, ?, ?, ?)") { stmt =>
76+
for
77+
_ <- stmt.setInt(1, 1)
78+
_ <- stmt.setFloat(2, 9.5f)
79+
_ <- stmt.setBigDecimal(3, BigDecimal("19.99"))
80+
_ <- stmt.setDate(4, date)
81+
_ <- stmt.setTimestamp(5, ts)
82+
_ <- stmt.setObject(6, uuid)
83+
_ <- stmt.setBytes(7, bytes)
84+
count <- stmt.executeUpdate()
85+
yield count
86+
} match {
87+
case Right(n) => println(s"✅ Inserted $n row with new types")
88+
case Left(error) => println(s"❌ Insert with new types failed: $error")
89+
}
90+
91+
connection.executeQuery("SELECT * FROM events WHERE id = 1") match {
92+
case Right(rs) =>
93+
if rs.next() then
94+
val score = rs.getFloat("score")
95+
val price = rs.getBigDecimal("price")
96+
val rec = rs.getDate("recorded")
97+
val created = rs.getTimestamp("created")
98+
val uid = rs.getObject("uid", classOf[java.util.UUID])
99+
val blob = rs.getBytes("payload")
100+
println(s" score=$score price=$price recorded=$rec created=$created uid=$uid payload=${new String(blob)}")
101+
println("✅ Read back all new types successfully")
102+
rs.close()
103+
case Left(error) => println(s"❌ Query for new types failed: $error")
104+
}
105+
106+
// Test batch with new types (Float, BigDecimal, UUID, Date, Timestamp) - max 6-element tuple
107+
println("\nTesting 0.1.4 batch with new types...")
108+
109+
connection.executeUpdate("CREATE TABLE readings (id INTEGER, val FLOAT, dec DECIMAL(10,2), uid UUID, dt DATE, ts TIMESTAMP)") match {
110+
case Right(_) => ()
111+
case Left(error) => println(s"❌ Failed to create readings table: $error")
112+
}
113+
114+
connection.withBatch("INSERT INTO readings VALUES (?, ?, ?, ?, ?, ?)") { batch =>
115+
for
116+
_ <- batch.addBatch((1, 7.5f, BigDecimal("9.99"), uuid, date, ts))
117+
result <- batch.executeBatch()
118+
yield result
119+
} match {
120+
case Right(r) => println(s"✅ Batch with new types: ${r.successCount} inserted, ${r.failureCount} failed")
121+
case Left(error) => println(s"❌ Batch with new types failed: $error")
122+
}
123+
51124
// Close connection
52125
connection.close()
53-
println("\nTest completed successfully!")
126+
println("\nAll tests completed successfully!")
54127
}
55128

56129
// To run this example:

0 commit comments

Comments
 (0)