A Scala 3 wrapper for DuckDB JDBC connections providing type-safe, functional access to DuckDB.
This class wraps the underlying JDBC connection and provides idiomatic Scala methods for executing queries, managing transactions, and working with prepared statements.
Value parameters
- underlying
-
The underlying JDBC connection to DuckDB
Attributes
- Constructor
-
Creates a new DuckDB connection wrapper
- See also
-
com.softinio.duck4s.algebra.DuckDBConfig for connection configuration options
com.softinio.duck4s.algebra.DuckDBError for error handling
- Since
-
0.1.0
- Example
-
import com.softinio.duck4s.* import com.softinio.duck4s.algebra.* // Create an in-memory connection val result = DuckDBConnection.withConnection() { conn => for _ <- conn.executeUpdate("CREATE TABLE users (id INTEGER, name VARCHAR)") _ <- conn.executeUpdate("INSERT INTO users VALUES (1, 'Alice')") rs <- conn.executeQuery("SELECT * FROM users") yield while rs.next() do println(s"${rs.getInt("id")}: ${rs.getString("name")}") rs.close() }
- Companion
- object
- Graph
-
- Supertypes
Members list
Value members
Concrete methods
Creates a duplicate of this DuckDB connection.
Creates a duplicate of this DuckDB connection.
This method uses DuckDB's native connection duplication functionality to create a new connection that shares the same database instance but operates independently.
Attributes
- Returns
-
Either a algebra.DuckDBError.InvalidStateError if the connection is not a DuckDB connection, a algebra.DuckDBError.ConnectionError if duplication fails, or a new DuckDBConnection
- Since
-
0.1.0
Executes a SQL query and returns the result set.
Executes a SQL query and returns the result set.
This method executes a SELECT query and returns a result set that can be used to iterate over the returned rows. The result set should be closed when no longer needed.
Value parameters
- sql
-
The SQL SELECT query to execute
Attributes
- Returns
-
Either a algebra.DuckDBError.QueryError if execution fails, or a algebra.DuckDBResultSet
- See also
-
algebra.DuckDBResultSet for result set navigation and data extraction
- Since
-
0.1.0
- Example
-
\n * for rs <- conn.executeQuery("SELECT * FROM users WHERE age > 18") yield while rs.next() do println(s"User: ${rs.getString("name")}") rs.close()
\n *
Executes a SQL update statement (INSERT, UPDATE, DELETE, DDL).
Executes a SQL update statement (INSERT, UPDATE, DELETE, DDL).
This method executes SQL statements that modify the database or its structure. It returns the number of affected rows for DML statements.
Value parameters
- sql
-
The SQL statement to execute (INSERT, UPDATE, DELETE, CREATE TABLE, etc.)
Attributes
- Returns
-
Either a algebra.DuckDBError.QueryError if execution fails, or the number of affected rows
- Since
-
0.1.0
- Example
-
\n * for count <- conn.executeUpdate("INSERT INTO users (name, age) VALUES ('Alice', 25)") yield println(s"$count rows inserted")
\n *
Creates a batch for executing the given SQL statement multiple times.
Creates a batch for executing the given SQL statement multiple times.
This method creates a prepared statement optimized for batch execution, allowing efficient insertion or update of multiple rows.
Value parameters
- sql
-
The SQL statement to prepare for batch execution
Attributes
- Returns
-
Either a algebra.DuckDBError.QueryError if preparation fails, or a algebra.DuckDBBatch
- See also
-
algebra.DuckDBBatch for batch parameter binding and execution
- Since
-
0.1.0
- Example
-
\n * for batch <- conn.prepareBatch("INSERT INTO users (id, name) VALUES (?, ?)") _ <- batch.addBatch(1, "Alice") _ <- batch.addBatch(2, "Bob") result <- batch.executeBatch() yield result
\n *
Creates a prepared statement for the given SQL query.
Creates a prepared statement for the given SQL query.
This method compiles the SQL statement and returns a prepared statement that can be executed multiple times with different parameters efficiently.
Value parameters
- sql
-
The SQL statement to prepare, may contain parameter placeholders (?)
Attributes
- Returns
-
Either a algebra.DuckDBError.QueryError if preparation fails, or a algebra.DuckDBPreparedStatement
- See also
-
algebra.DuckDBPreparedStatement for parameter binding and execution
- Since
-
0.1.0
- Example
-
\n * for stmt <- conn.prepareStatement("SELECT * FROM users WHERE id = ?") _ <- stmt.setInt(1, 42) rs <- stmt.executeQuery() yield rs
\n *
Executes a block of operations with a batch that is automatically closed.
Executes a block of operations with a batch that is automatically closed.
This method creates a batch for the given SQL statement, executes the provided block, and ensures the batch is properly closed regardless of the outcome.
Type parameters
- T
-
The type of the successful result
Value parameters
- block
-
A function that receives the batch and returns an Either with the result
- sql
-
The SQL statement to prepare for batch execution
Attributes
- Returns
-
Either a algebra.DuckDBError.QueryError if preparation or execution fails, or the result of the block
- See also
-
DuckDBConnection.prepareBatch for manual batch management
algebra.DuckDBBatch for batch parameter binding and execution
- Since
-
0.1.0
- Example
-
\n * conn.withBatch("INSERT INTO users (id, name) VALUES (?, ?)") { batch => for _ <- batch.addBatch(1, "Alice") _ <- batch.addBatch(2, "Bob") result <- batch.executeBatch() yield result }
\n *
Executes a block of operations with a prepared statement that is automatically closed.
Executes a block of operations with a prepared statement that is automatically closed.
This method creates a prepared statement, executes the provided block, and ensures the statement is properly closed regardless of the outcome.
Type parameters
- T
-
The type of the successful result
Value parameters
- block
-
A function that receives the prepared statement and returns an Either with the result
- sql
-
The SQL statement to prepare
Attributes
- Returns
-
Either a algebra.DuckDBError.QueryError if preparation or execution fails, or the result of the block
- See also
-
DuckDBConnection.prepareStatement for manual prepared statement management
algebra.DuckDBPreparedStatement for parameter binding and execution
- Since
-
0.1.0
- Example
-
\n * conn.withPreparedStatement("SELECT * FROM users WHERE id = ?") { stmt => for _ <- stmt.setInt(1, 42) rs <- stmt.executeQuery() yield rs }
\n *
Executes a block of operations within a database transaction.
Executes a block of operations within a database transaction.
This method temporarily disables autocommit, executes the provided block, and then either commits the transaction on success or rolls it back on failure. Autocommit is restored regardless of the outcome.
Type parameters
- T
-
The type of the successful result
Value parameters
- block
-
A function that receives this connection and returns an Either with the result
Attributes
- Returns
-
Either a algebra.DuckDBError.TransactionError if the transaction fails, or the result of the block
- Since
-
0.1.0
- Example
-
conn.withTransaction { txConn => for _ <- txConn.executeUpdate("INSERT INTO users VALUES (1, 'Alice')") _ <- txConn.executeUpdate("INSERT INTO users VALUES (2, 'Bob')") yield "Two users inserted" }