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
88import cats .effect .{IO , IOApp }
99import 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(" \n All 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'
0 commit comments