Struct Appender

Source
pub struct Appender<'conn> { /* private fields */ }
Expand description

Appender for fast import data

§Thread Safety

Appender is neither Send nor Sync:

  • Not Send because it holds a reference to Connection, which is !Sync
  • Not Sync because DuckDB appenders don’t support concurrent access

To use an appender in another thread, move the Connection to that thread and create the appender there.

If you need to share an Appender across threads, wrap it in a Mutex.

See DuckDB concurrency documentation for more details.

§Wide Tables (Many Columns)

Array literals [value; N] are supported for tables with up to 32 columns.

appender.append_row([0; 32])?;
appender.append_row([1, 2, 3, 4, 5])?;

For tables with more than 32 columns, use one of these alternatives:

§1. Slice approach - convert values to &dyn ToSql

let values: Vec<i32> = vec![0; 100];
let params: Vec<&dyn ToSql> = values.iter().map(|v| v as &dyn ToSql).collect();
appender.append_row(params.as_slice())?;

§2. params! macro - write values explicitly

appender.append_row(params![v1, v2, v3, ..., v50])?;

§3. appender_params_from_iter - pass an iterator directly

use duckdb::appender_params_from_iter;
let values: Vec<i32> = vec![0; 100];
appender.append_row(appender_params_from_iter(values))?;

All three methods can be used interchangeably and mixed in the same appender.

Implementations§

Source§

impl Appender<'_>

Source

pub fn append_rows<P, I>(&mut self, rows: I) -> Result<()>
where I: IntoIterator<Item = P>, P: AppenderParams,

Append multiple rows from Iterator

§Example
fn insert_rows(conn: &Connection) -> Result<()> {
    let mut app = conn.appender("foo")?;
    app.append_rows([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])?;
    Ok(())
}
§Failure

Will return Err if append column count not the same with the table schema

Source

pub fn append_row<P: AppenderParams>(&mut self, params: P) -> Result<()>

Append one row

§Example
fn insert_row(conn: &Connection) -> Result<()> {
    let mut app = conn.appender("foo")?;
    app.append_row([1, 2])?;
    Ok(())
}
§Failure

Will return Err if append column count not the same with the table schema

Source

pub fn flush(&mut self) -> Result<()>

Flush data into DB

Source

pub fn add_column(&mut self, name: &str) -> Result<()>

Add a column to the appender’s active column list.

When columns are added, only those columns need values during append. Other columns will use their DEFAULT value (or NULL if no default).

This flushes any pending data before modifying the column list.

Source

pub fn clear_columns(&mut self) -> Result<()>

Clear the appender’s active column list.

After clearing, all columns become active again and values must be provided for every column during append.

This flushes any pending data before clearing.

Trait Implementations§

Source§

impl Debug for Appender<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Appender<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'conn> Freeze for Appender<'conn>

§

impl<'conn> !RefUnwindSafe for Appender<'conn>

§

impl<'conn> !Send for Appender<'conn>

§

impl<'conn> !Sync for Appender<'conn>

§

impl<'conn> Unpin for Appender<'conn>

§

impl<'conn> !UnwindSafe for Appender<'conn>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.