DuckdbEx.Connection (DuckdbEx v0.2.0)
View SourceDuckDB connection management.
This module provides the primary interface to DuckDB databases, mirroring the functionality of the Python DuckDBPyConnection class.
Reference: duckdb-python/src/duckdb_py/include/duckdb_python/pyconnection/pyconnection.hpp
Overview
A connection represents an active session with a DuckDB database using the DuckDB CLI managed through erlexec. Connections can be:
- In-memory (
:memory:) - Persistent (file path)
- Read-only or read-write
Examples
# Connect to an in-memory database
{:ok, conn} = DuckdbEx.Connection.connect(:memory)
# Execute a query
{:ok, result} = DuckdbEx.Connection.execute(conn, "SELECT 1")
# Close connection
DuckdbEx.Connection.close(conn)
Summary
Functions
Begins a transaction.
Creates a checkpoint.
Closes the database connection.
Commits the current transaction.
Opens a connection to a DuckDB database.
Creates a duplicate of the current connection.
Returns the description of the last executed result set.
Executes a SQL query.
Executes a SQL query multiple times with different parameter sets.
Extracts statements from a SQL string.
Fetches all rows from a query result.
Fetches multiple rows from the last result.
Fetches one row from a query result.
Creates a relation from a SQL query with an optional alias.
Creates a relation from a CSV file or list of files.
Creates a relation from a JSON file or list of files.
Creates a relation from a Parquet file or list of files.
Rolls back the current transaction.
Returns the rowcount for the last result set.
Creates a relation from a SQL query.
Creates a relation from a table or view name.
Executes a function within a managed transaction.
Creates a relation from values.
Creates a relation from a view name.
Types
@type t() :: DuckdbEx.Port.t() | DuckdbEx.Cursor.t()
Functions
Begins a transaction.
Starts a new transaction on the connection. All subsequent queries will be executed within the transaction context until commit or rollback is called.
Parameters
conn- The connection
Returns
{:ok, result}- Transaction started successfully{:error, exception}- Failed to start transaction
Examples
{:ok, conn} = DuckdbEx.Connection.connect(:memory)
{:ok, _} = DuckdbEx.Connection.begin(conn)
{:ok, _} = DuckdbEx.Connection.execute(conn, "INSERT INTO users VALUES (1, 'Alice')")
{:ok, _} = DuckdbEx.Connection.commit(conn)Reference: DuckDBPyConnection.begin() in Python
Creates a checkpoint.
Forces a checkpoint of the write-ahead log (WAL) to the database file. This ensures all changes are persisted to disk.
Parameters
conn- The connection
Returns
{:ok, result}- Checkpoint created successfully{:error, exception}- Failed to create checkpoint
Examples
{:ok, _} = DuckdbEx.Connection.checkpoint(conn)Reference: DuckDBPyConnection.checkpoint() in Python
@spec close(t()) :: :ok
Closes the database connection.
After closing, the connection should not be used for any operations.
Parameters
conn- The connection to close
Returns
:ok
Examples
{:ok, conn} = DuckdbEx.Connection.connect(:memory)
:ok = DuckdbEx.Connection.close(conn)Reference: DuckDBPyConnection.close() in Python
Commits the current transaction.
Commits all changes made within the current transaction, making them permanent.
Parameters
conn- The connection
Returns
{:ok, result}- Transaction committed successfully{:error, exception}- Failed to commit transaction
Examples
{:ok, _} = DuckdbEx.Connection.begin(conn)
{:ok, _} = DuckdbEx.Connection.execute(conn, "INSERT INTO users VALUES (1, 'Alice')")
{:ok, _} = DuckdbEx.Connection.commit(conn)Reference: DuckDBPyConnection.commit() in Python
Opens a connection to a DuckDB database.
Parameters
database- Database path or:memory:for in-memory databaseopts- Connection options (keyword list):read_only- Open in read-only mode (default: false):config- Database configuration map (for future use)
Returns
{:ok, conn}- Successfully opened connection{:error, exception}- Connection failed
Examples
{:ok, conn} = DuckdbEx.Connection.connect(:memory)
{:ok, conn} = DuckdbEx.Connection.connect("/path/to/db.duckdb")
{:ok, conn} = DuckdbEx.Connection.connect(:memory, read_only: true)Reference: duckdb.connect() in Python
Creates a duplicate of the current connection.
Returns the description of the last executed result set.
@spec execute(t(), String.t() | DuckdbEx.Statement.t(), list() | map()) :: {:ok, t()} | {:error, term()}
Executes a SQL query.
Parameters
conn- The connectionsql- SQL query stringparams- Query parameters (not yet implemented)
Returns
{:ok, result}- Query executed successfully{:error, exception}- Query failed
Examples
{:ok, result} = DuckdbEx.Connection.execute(conn, "SELECT 1")Reference: DuckDBPyConnection.execute() in Python
@spec executemany(t(), String.t() | DuckdbEx.Statement.t(), list()) :: {:ok, t()} | {:error, term()}
Executes a SQL query multiple times with different parameter sets.
@spec extract_statements(String.t()) :: {:ok, [DuckdbEx.Statement.t()]} | {:error, term()}
Extracts statements from a SQL string.
@spec extract_statements(t(), String.t()) :: {:ok, [DuckdbEx.Statement.t()]} | {:error, term()}
Fetches all rows from a query result.
This is a convenience function that executes a query and returns all rows.
Parameters
conn- The connectionsql- SQL query string
Returns
{:ok, rows}- List of row tuples{:error, exception}- Query failed
Examples
{:ok, rows} = DuckdbEx.Connection.fetch_all(conn, "SELECT * FROM users")Reference: DuckDBPyConnection.execute().fetchall() in Python
@spec fetch_many(t(), non_neg_integer()) :: {:ok, [tuple()]} | {:error, term()}
Fetches multiple rows from the last result.
Parameters
conn- The connectioncount- Number of rows to fetch (default: 1)
@spec fetch_many(t(), String.t(), non_neg_integer()) :: {:ok, [tuple()]} | {:error, term()}
Fetches one row from a query result.
This is a convenience function that executes a query and returns the first row.
Parameters
conn- The connectionsql- SQL query string
Returns
{:ok, row}- Row tuple or nil{:error, exception}- Query failed
Examples
{:ok, row} = DuckdbEx.Connection.fetch_one(conn, "SELECT * FROM users LIMIT 1")Reference: DuckDBPyConnection.execute().fetchone() in Python
Creates a relation from a SQL query with an optional alias.
Creates a relation from a CSV file or list of files.
Creates a relation from a JSON file or list of files.
Creates a relation from a Parquet file or list of files.
Rolls back the current transaction.
Reverts all changes made within the current transaction.
Parameters
conn- The connection
Returns
{:ok, result}- Transaction rolled back successfully{:error, exception}- Failed to rollback transaction
Examples
{:ok, _} = DuckdbEx.Connection.begin(conn)
{:ok, _} = DuckdbEx.Connection.execute(conn, "INSERT INTO users VALUES (1, 'Alice')")
{:ok, _} = DuckdbEx.Connection.rollback(conn)Reference: DuckDBPyConnection.rollback() in Python
Returns the rowcount for the last result set.
@spec sql(t(), String.t(), list() | map()) :: DuckdbEx.Relation.t()
Creates a relation from a SQL query.
Returns a lazy relation that can be composed with other operations before execution. The SQL is not executed until a fetch operation is called.
Parameters
conn- The connectionsql- SQL query string
Returns
A %DuckdbEx.Relation{} struct
Examples
# Create relation (not executed yet)
relation = DuckdbEx.Connection.sql(conn, "SELECT * FROM users")
# Chain operations
result = relation
|> DuckdbEx.Relation.filter("age > 25")
|> DuckdbEx.Relation.fetch_all()Reference: DuckDBPyConnection.sql() in Python
@spec table(t(), String.t()) :: DuckdbEx.Relation.t()
Creates a relation from a table or view name.
Returns a lazy relation representing the entire table or view. The table is not queried until a fetch operation is called.
Parameters
conn- The connectiontable_name- Name of the table or view
Returns
A %DuckdbEx.Relation{} struct
Examples
# Create relation from table
relation = DuckdbEx.Connection.table(conn, "users")
# Chain operations
active_users = relation
|> DuckdbEx.Relation.filter("status = 'active'")
|> DuckdbEx.Relation.fetch_all()Reference: DuckDBPyConnection.table() in Python
Executes a function within a managed transaction.
This is the recommended way to use transactions. The function is executed within a transaction context. If the function completes successfully, the transaction is committed. If an exception is raised or an error occurs, the transaction is automatically rolled back.
Parameters
conn- The connectionfun- A function that takes the connection as an argument
Returns
{:ok, result}- Transaction completed successfully, returns the function's result{:error, exception}- Transaction failed or was rolled back
Examples
# Successful transaction
{:ok, result} = DuckdbEx.Connection.transaction(conn, fn conn ->
{:ok, _} = DuckdbEx.Connection.execute(conn, "INSERT INTO users VALUES (1, 'Alice')")
{:ok, _} = DuckdbEx.Connection.execute(conn, "INSERT INTO users VALUES (2, 'Bob')")
:success
end)
# Transaction with automatic rollback on error
{:error, _} = DuckdbEx.Connection.transaction(conn, fn conn ->
{:ok, _} = DuckdbEx.Connection.execute(conn, "INSERT INTO users VALUES (1, 'Alice')")
raise "Something went wrong!"
end)Reference: Similar to Python context manager pattern with DuckDB transactions
@spec values(t(), term()) :: DuckdbEx.Relation.t()
Creates a relation from values.
A list of values is treated as a single row, while a list of tuples/lists is treated as multiple rows.
@spec view(t(), String.t()) :: DuckdbEx.Relation.t()
Creates a relation from a view name.