View Source Duckdbex (Duckdbex v0.1.0)
DuckDB API module
Link to this section Summary
Functions
Creates the Appender to load bulk data into a DuckDB database.
Append row into a DuckDB database table.
Append multiple rows into a DuckDB database table at once.
Flush the changes made by the appender and close it.
Commit the changes made by the appender.
Creates connection object to work with database.
Execute the prepared statement
Execute the prepared statement with the given list of parameters
Fetches all data from the query result.
Fetches a data chunk from the query result.
Convert a duckdb hugeint record to erlang/elixir integer.
Convert an erlang/elixir integer to a DuckDB hugeint.
Opens database in the memory.
If the path to the file is specified, then opens the database in the file.
Opens database in the specified file.
Prepare the specified query, returning a reference to the prepared statement object
Issues a query to the database and returns a result reference.
Issues a query to the database with parameters and returns a result reference.
Link to this section Types
Link to this section Functions
@spec appender(connection(), binary()) :: {:ok, appender()} | {:error, reason()}
Creates the Appender to load bulk data into a DuckDB database.
This is the recommended way to load bulk data.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "CREATE TABLE table_1 (data INTEGER);")
iex> {:ok, _appender} = Duckdbex.appender(conn, "table_1")
Append row into a DuckDB database table.
Any values added to the appender are cached prior to being inserted into the database system for performance reasons. That means that, while appending, the rows might not be immediately visible in the system. The cache is automatically flushed when the appender goes out of scope or when Duckdbex.appender_close(appender) is called. The cache can also be manually flushed using the Duckdbex.appender_flush(appender) method. After either flush or close is called, all the data has been written to the database system.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "CREATE TABLE table_1 (data INTEGER);")
iex> {:ok, appender} = Duckdbex.appender(conn, "table_1")
iex> :ok = Duckdbex.appender_add_row(appender, [1])
Append multiple rows into a DuckDB database table at once.
Any values added to the appender are cached prior to being inserted into the database system for performance reasons. That means that, while appending, the rows might not be immediately visible in the system. The cache is automatically flushed when the appender goes out of scope or when Duckdbex.appender_close/1
is called. The cache can also be manually flushed using the Duckdbex.appender_flush/1
method. After either flush or close is called, all the data has been written to the database system.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "CREATE TABLE table_1 (the_n1 INTEGER, the_str1 STRING);")
iex> {:ok, appender} = Duckdbex.appender(conn, "table_1")
iex> :ok = Duckdbex.appender_add_rows(appender, [[1, "one"], [2, "two"]])
Flush the changes made by the appender and close it.
The appender cannot be used after this point
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "CREATE TABLE table_1 (the_n1 INTEGER, the_str1 STRING);")
iex> {:ok, appender} = Duckdbex.appender(conn, "table_1")
iex> :ok = Duckdbex.appender_add_rows(appender, [[1, "one"], [2, "two"]])
iex> {:ok, res} = Duckdbex.query(conn, "SELECT * FROM table_1;")
iex> [] = Duckdbex.fetch_all(res)
iex> :ok = Duckdbex.appender_close(appender)
iex> {:ok, res} = Duckdbex.query(conn, "SELECT * FROM table_1;")
iex> [[1, "one"], [2, "two"]] = Duckdbex.fetch_all(res)
@spec appender_flush(appender()) :: :ok | {:error, reason()}
@spec appender_flush(appender()) :: :ok | {:error, reason()}
Commit the changes made by the appender.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "CREATE TABLE table_1 (the_n1 INTEGER, the_str1 STRING);")
iex> {:ok, appender} = Duckdbex.appender(conn, "table_1")
iex> :ok = Duckdbex.appender_add_rows(appender, [[1, "one"], [2, "two"]])
iex> {:ok, res} = Duckdbex.query(conn, "SELECT * FROM table_1;")
iex> [] = Duckdbex.fetch_all(res)
iex> :ok = Duckdbex.appender_flush(appender)
iex> {:ok, res} = Duckdbex.query(conn, "SELECT * FROM table_1;")
iex> [[1, "one"], [2, "two"]] = Duckdbex.fetch_all(res)
@spec connection(db()) :: {:ok, connection()} | {:error, reason()}
Creates connection object to work with database.
To work with database the connection object is requiered. Connection object hold a shared reference to database, so it is possible to forget the database reference and hold the connection reference only.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, _conn} = Duckdbex.connection(db)
@spec execute_statement(statement()) :: {:ok, query_result()} | {:error, reason()}
Execute the prepared statement
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, stmt} = Duckdbex.prepare_statement(conn, "SELECT 1;")
iex> {:ok, res} = Duckdbex.execute_statement(stmt)
iex> [[1]] = Duckdbex.fetch_all(res)
@spec execute_statement(statement(), list()) :: {:ok, query_result()} | {:error, reason()}
Execute the prepared statement with the given list of parameters
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, stmt} = Duckdbex.prepare_statement(conn, "SELECT 1 WHERE $1 = 1;")
iex> {:ok, res} = Duckdbex.execute_statement(stmt, [1])
iex> [[1]] = Duckdbex.fetch_all(res)
@spec fetch_all(query_result()) :: :ok | {:error, reason()}
Fetches all data from the query result.
Returns empty list if there are no result to fetch.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, res} = Duckdbex.query(conn, "SELECT 1;")
iex> [[1]] = Duckdbex.fetch_all(res)
@spec fetch_chunk(query_result()) :: :ok | {:error, reason()}
Fetches a data chunk from the query result.
Returns empty list if there are no more results to fetch.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, res} = Duckdbex.query(conn, "SELECT 1;")
iex> [[1]] = Duckdbex.fetch_chunk(res)
Convert a duckdb hugeint record to erlang/elixir integer.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "CREATE TABLE hugeints(value HUGEINT);")
iex> {:ok, _res} = Duckdbex.query(conn, "INSERT INTO hugeints VALUES (98233720368547758080000::hugeint);")
iex> {:ok, res} = Duckdbex.query(conn, "SELECT * FROM hugeints;")
iex> [[hugeint = {5325, 4808176044395724800}]] = Duckdbex.fetch_all(res)
iex> 98233720368547758080000 = Duckdbex.hugeint_to_integer(hugeint)
Convert an erlang/elixir integer to a DuckDB hugeint.
For more information on DuckDB numeric types, see DuckDB Numeric Data Types For more information on DuckDB numeric types.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "CREATE TABLE hugeints(value HUGEINT);")
iex> {:ok, _res} = Duckdbex.query(conn, "INSERT INTO hugeints VALUES (98233720368547758080000::hugeint);")
iex> hugeint = Duckdbex.integer_to_hugeint(98233720368547758080000)
iex> {:ok, res} = Duckdbex.query(conn, "SELECT * FROM hugeints WHERE value = $1", [{:hugeint, hugeint}])
iex> [[{5325, 4808176044395724800}]] = Duckdbex.fetch_all(res)
Opens database in the memory.
examples
Examples
iex> {:ok, _db} = Duckdbex.open()
@spec open(binary() | Duckdbex.Config.t()) :: {:ok, db()} | {:error, reason()}
If the path to the file is specified, then opens the database in the file.
If specified file does not exist, a new database file with the given name will be created automatically. If database config is specified, then opens the database in the memory with the custom database config.
examples
Examples
iex> {:ok, _db} = Duckdbex.open("my_database.duckdb")
iex> {:ok, _db} = Duckdbex.open(%Duckdbex.Config{access_mode: :automatic})
@spec open(binary(), Duckdbex.Config.t() | nil) :: {:ok, db()} | {:error, reason()}
Opens database in the specified file.
If specified file does not exist, a new database file with the given name will be created automatically.
examples
Examples
iex> {:ok, _db} = Duckdbex.open("my_database.duckdb", %Duckdbex.Config{})
@spec prepare_statement(connection(), binary()) :: {:ok, statement()} | {:error, reason()}
Prepare the specified query, returning a reference to the prepared statement object
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _stmt} = Duckdbex.prepare_statement(conn, "SELECT 1 WHERE $1 = 1;")
@spec query(connection(), binary()) :: {:ok, query_result()} | {:error, reason()}
Issues a query to the database and returns a result reference.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "SELECT 1;")
@spec query(connection(), binary(), list()) :: {:ok, query_result()} | {:error, reason()}
Issues a query to the database with parameters and returns a result reference.
examples
Examples
iex> {:ok, db} = Duckdbex.open()
iex> {:ok, conn} = Duckdbex.connection(db)
iex> {:ok, _res} = Duckdbex.query(conn, "SELECT 1 WHERE $1 = 1;", [1])