Crate duckdb

Crate duckdb 

Source
Expand description

duckdb-rs is an ergonomic wrapper for using DuckDB from Rust. It attempts to expose an interface similar to rusqlite.

use duckdb::{params, Connection, Result};
use duckdb::arrow::record_batch::RecordBatch;
use duckdb::arrow::util::pretty::print_batches;

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}

fn main() -> Result<()> {
    let conn = Connection::open_in_memory()?;

    conn.execute_batch(
        r"CREATE SEQUENCE seq;
          CREATE TABLE person (
                  id              INTEGER PRIMARY KEY DEFAULT NEXTVAL('seq'),
                  name            TEXT NOT NULL,
                  data            BLOB
                  );
         ")?;
    let me = Person {
        id: 0,
        name: "Steven".to_string(),
        data: None,
    };
    conn.execute(
        "INSERT INTO person (name, data) VALUES (?, ?)",
        params![me.name, me.data],
    )?;

    let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
    let person_iter = stmt.query_map([], |row| {
        Ok(Person {
            id: row.get(0)?,
            name: row.get(1)?,
            data: row.get(2)?,
        })
    })?;

    for person in person_iter {
        println!("Found person {:?}", person.unwrap());
    }

    // query table by arrow
    let rbs: Vec<RecordBatch> = stmt.query_arrow([])?.collect();
    print_batches(&rbs);
    Ok(())
}

Re-exports§

pub use crate::types::ToSql;
pub use libduckdb_sys as ffi;
pub use arrow;
pub use polars;
pub use polars_arrow as arrow2;

Modules§

core
The core module contains the main functionality of the DuckDB crate.
types
ToSql and FromSql are also implemented for Option<T> where T implements ToSql or FromSql for the cases where you want to know if a value was NULL (which gets translated to None).
vscalar
The duckdb scalar function interface
vtab
The duckdb table function interface

Macros§

params
A macro making it more convenient to pass heterogeneous or long lists of parameters as a &[&dyn ToSql].

Structs§

AndThenRows
An iterator over the mapped resulting rows of a query, with an Error type unifying with Error.
Appender
Appender for fast import data
AppenderParamsFromIter
Adapter type which allows any iterator over ToSql values to implement [Params].
Arrow
A handle for the resulting RecordBatch of a query.
ArrowStream
A handle for the resulting RecordBatch of a query in streaming
CachedStatement
Cacheable statement.
Column
Information about a column of a DuckDB query.
Config
duckdb configuration Refer to https://github.com/duckdb/duckdb/blob/master/src/main/config.cpp
Connection
A connection to a DuckDB database.
DuckdbConnectionManager
An r2d2::ManageConnection for duckdb::Connections.
InterruptHandle
A handle that allows interrupting long-running queries.
Map
F is used to transform the streaming iterator into a fallible iterator.
MappedRows
An iterator over the mapped resulting rows of a query.
ParamsFromIter
Adapter type which allows any iterator over ToSql values to implement Params.
Polars
An handle for the resulting Polars DataFrame of a query.
Row
A single result row of a query.
Rows
An handle for the resulting rows of a query.
Statement
A prepared statement.
Transaction
Represents a transaction on a database connection.

Enums§

AccessMode
duckdb access mode, default is Automatic
DatabaseName
Name for a database within a DuckDB connection.
DefaultNullOrder
duckdb default null order, default is nulls first
DefaultOrder
duckdb default order, default is Asc
DropBehavior
Options for how a Transaction should behave when it is dropped.
Error
Enum listing possible errors from duckdb.
ErrorCode
Error Codes

Constants§

MAIN_DB
Shorthand for DatabaseName::Main.
TEMP_DB
Shorthand for DatabaseName::Temp.

Traits§

AppenderParams
Trait used for sets of parameter passed into SQL statements/queries.
OptionalExt
See the method documentation.
Params
Trait used for sets of parameter passed into SQL statements/queries.
RowIndex
A trait implemented by types that can index into columns of a row.

Functions§

appender_params_from_iter
Constructor function for a [ParamsFromIter]. See its documentation for more.
params_from_iter
Constructor function for a ParamsFromIter. See its documentation for more.

Type Aliases§

Result
A typedef of the result returned by many methods.