class DuckDB::Appender
The DuckDB::Appender
encapsulates DuckDB
Appender
.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR)') appender = con.appender('users') appender.append_row(1, 'Alice')
Public Class Methods
Source
static VALUE appender_initialize(VALUE self, VALUE con, VALUE schema, VALUE table) { rubyDuckDBConnection *ctxcon; rubyDuckDBAppender *ctx; char *pschema = 0; if (!rb_obj_is_kind_of(con, cDuckDBConnection)) { rb_raise(rb_eTypeError, "1st argument should be instance of DackDB::Connection"); } TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); ctxcon = get_struct_connection(con); if (schema != Qnil) { pschema = StringValuePtr(schema); } if (duckdb_appender_create(ctxcon->con, pschema, StringValuePtr(table), &(ctx->appender)) == DuckDBError) { rb_raise(eDuckDBError, "failed to create appender"); } return self; }
Public Instance Methods
Source
# File lib/duckdb/appender.rb, line 340 def append(value) case value when NilClass append_null when Float append_double(value) when Integer case value when RANGE_INT16 append_int16(value) when RANGE_INT32 append_int32(value) when RANGE_INT64 append_int64(value) else append_hugeint(value) end when String blob?(value) ? append_blob(value) : append_varchar(value) when TrueClass, FalseClass append_bool(value) when Time append_timestamp(value) when Date append_date(value) when DuckDB::Interval append_interval(value) else raise(DuckDB::Error, "not supported type #{value} (#{value.class})") end end
appends value.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR)') appender = con.appender('users') appender.append(1) appender.append('Alice') appender.end_row
Source
static VALUE appender_append_blob(VALUE self, VALUE val) { rubyDuckDBAppender *ctx; char *pval = StringValuePtr(val); idx_t length = (idx_t)RSTRING_LEN(val); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_blob(ctx->appender, (void *)pval, length) == DuckDBError) { rb_raise(eDuckDBError, "failed to append blob"); } return self; }
Source
# File lib/duckdb/appender.rb, line 114 def append_bool(value) return self if _append_bool(value) raise_appender_error('failed to append_bool') end
Appends a boolean value to the current row in the appender.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, active BOOLEAN)') appender = con.appender('users') appender .append_int32(1) .append_bool(true) .end_row .flush
Source
# File lib/duckdb/appender.rb, line 269 def append_date(value) date = _parse_date(value) _append_date(date.year, date.month, date.day) end
appends date value.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE dates (date_value DATE)') appender = con.appender('dates') appender.append_date(Date.today) # or # appender.append_date(Time.now) # appender.append_date('2021-10-10') appender.end_row appender.flush
Source
HAVE_DUCKDB_H_GE_V1_1_0 static VALUE appender_append_default(VALUE self) { rubyDuckDBAppender *ctx; TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_default(ctx->appender) == DuckDBError) { rb_raise(eDuckDBError, "failed to append default"); } return self; }
Source
static VALUE appender_append_double(VALUE self, VALUE val) { rubyDuckDBAppender *ctx; double dval = NUM2DBL(val); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_double(ctx->appender, dval) == DuckDBError) { rb_raise(eDuckDBError, "failed to append double"); } return self; }
Source
static VALUE appender_append_float(VALUE self, VALUE val) { rubyDuckDBAppender *ctx; float fval = (float)NUM2DBL(val); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_float(ctx->appender, fval) == DuckDBError) { rb_raise(eDuckDBError, "failed to append float"); } return self; }
Source
# File lib/duckdb/appender.rb, line 236 def append_hugeint(value) lower, upper = integer_to_hugeint(value) _append_hugeint(lower, upper) end
appends huge int value.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE numbers (num HUGEINT)') appender = con.appender('numbers') appender .append_hugeint(-170_141_183_460_469_231_731_687_303_715_884_105_727) .end_row
Source
# File lib/duckdb/appender.rb, line 157 def append_int16(value) return self if _append_int16(value) raise_appender_error('failed to append_int16') end
Appends an int16(SMALLINT) value to the current row in the appender.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, age SMALLINT)') appender = con.appender('users') appender .append_int32(1) .append_int16(20) .end_row .flush
Source
# File lib/duckdb/appender.rb, line 178 def append_int32(value) return self if _append_int32(value) raise_appender_error('failed to append_int32') end
Appends an int32(INTEGER) value to the current row in the appender.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, age INTEGER)') appender = con.appender('users') appender .append_int32(1) .append_int32(20) .end_row .flush
Source
# File lib/duckdb/appender.rb, line 199 def append_int64(value) return self if _append_int64(value) raise_appender_error('failed to append_int64') end
Appends an int64(BIGINT) value to the current row in the appender.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, age BIGINT)') appender = con.appender('users') appender .append_int32(1) .append_int64(20) .end_row .flush
Source
# File lib/duckdb/appender.rb, line 136 def append_int8(value) return self if _append_int8(value) raise_appender_error('failed to append_int8') end
Appends an int8(TINYINT) value to the current row in the appender.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, age TINYINT)') appender = con.appender('users') appender .append_int32(1) .append_int8(20) .end_row .flush
Source
# File lib/duckdb/appender.rb, line 325 def append_interval(value) value = Interval.to_interval(value) _append_interval(value.interval_months, value.interval_days, value.interval_micros) end
appends interval. The argument must be ISO8601 duration format. WARNING: This method is expremental.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE intervals (interval_value INTERVAL)') appender = con.appender('intervals') appender .append_interval('P1Y2D') # => append 1 year 2 days interval. .end_row .flush
Source
static VALUE appender_append_null(VALUE self) { rubyDuckDBAppender *ctx; TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_null(ctx->appender) == DuckDBError) { rb_raise(eDuckDBError, "failed to append null"); } return self; }
Source
# File lib/duckdb/appender.rb, line 381 def append_row(*args) args.each do |arg| append(arg) end end_row end
append a row.
appender.append_row(1, 'Alice')
is same as:
appender.append(2) appender.append('Alice') appender.end_row
Source
# File lib/duckdb/appender.rb, line 287 def append_time(value) time = _parse_time(value) _append_time(time.hour, time.min, time.sec, time.usec) end
appends time value.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE times (time_value TIME)') appender = con.appender('times') appender.append_time(Time.now) # or # appender.append_time('01:01:01') appender.end_row appender.flush
Source
# File lib/duckdb/appender.rb, line 306 def append_timestamp(value) time = to_time(value) _append_timestamp(time.year, time.month, time.day, time.hour, time.min, time.sec, time.nsec / 1000) end
appends timestamp value.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE timestamps (timestamp_value TIMESTAMP)') appender = con.appender('timestamps') appender.append_time(Time.now) # or # appender.append_time(Date.today) # appender.append_time('2021-08-01 01:01:01') appender.end_row appender.flush
Source
# File lib/duckdb/appender.rb, line 251 def append_uhugeint(value) lower, upper = integer_to_hugeint(value) _append_uhugeint(lower, upper) end
appends unsigned huge int value.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE numbers (num UHUGEINT)') appender = con.appender('numbers') appender .append_hugeint(340_282_366_920_938_463_463_374_607_431_768_211_455) .end_row
Source
static VALUE appender_append_uint16(VALUE self, VALUE val) { rubyDuckDBAppender *ctx; uint16_t ui16val = (uint16_t)NUM2UINT(val); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_uint16(ctx->appender, ui16val) == DuckDBError) { rb_raise(eDuckDBError, "failed to append uint16"); } return self; }
Source
static VALUE appender_append_uint32(VALUE self, VALUE val) { rubyDuckDBAppender *ctx; uint32_t ui32val = (uint32_t)NUM2UINT(val); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_uint32(ctx->appender, ui32val) == DuckDBError) { rb_raise(eDuckDBError, "failed to append uint32"); } return self; }
Source
static VALUE appender_append_uint64(VALUE self, VALUE val) { rubyDuckDBAppender *ctx; uint64_t ui64val = (uint64_t)NUM2ULL(val); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_uint64(ctx->appender, ui64val) == DuckDBError) { rb_raise(eDuckDBError, "failed to append uint64"); } return self; }
Source
# File lib/duckdb/appender.rb, line 220 def append_uint8(value) return self if _append_uint8(value) raise_appender_error('failed to append_uint8') end
Appends an uint8 value to the current row in the appender.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, age UTINYINT)') appender = con.appender('users') appender .append_int32(1) .append_uint8(20) .end_row .flush
Source
static VALUE appender_append_varchar(VALUE self, VALUE val) { rubyDuckDBAppender *ctx; char *pval = StringValuePtr(val); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_varchar(ctx->appender, pval) == DuckDBError) { rb_raise(eDuckDBError, "failed to append varchar"); } return self; }
Source
static VALUE appender_append_varchar_length(VALUE self, VALUE val, VALUE len) { rubyDuckDBAppender *ctx; char *pval = StringValuePtr(val); idx_t length = (idx_t)NUM2ULL(len); TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); if (duckdb_append_varchar_length(ctx->appender, pval, length) == DuckDBError) { rb_raise(eDuckDBError, "failed to append varchar with length"); } return self; }
Source
# File lib/duckdb/appender.rb, line 30 def begin_row self end
Source
# File lib/duckdb/appender.rb, line 93 def close return self if _close raise_appender_error('failed to close') end
Closes the appender by flushing all intermediate states and closing it for further appends. If flushing the data triggers a constraint violation or any other error, then all data is invalidated, and this method raises DuckDB::Error
.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR)') appender = con.appender('users') appender .append_int32(1) .append_varchar('Alice') .end_row .close
Source
# File lib/duckdb/appender.rb, line 47 def end_row return self if _end_row raise_appender_error('failed to end_row') end
Finish the current row of appends. After end_row
is called, the next row can be appended.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR)') appender = con.appender('users') appender .append_int32(1) .append_varchar('Alice') .end_row
Source
static VALUE appender_error_message(VALUE self) { rubyDuckDBAppender *ctx; const char *msg; TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx); msg = duckdb_appender_error(ctx->appender); if (msg == NULL) { return Qnil; } return rb_str_new2(msg); }
Returns the error message of the appender. If there is no error, then it returns nil.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR)') appender = con.appender('users') appender.error_message # => nil
Source
# File lib/duckdb/appender.rb, line 70 def flush return self if _flush raise_appender_error('failed to flush') end
Flushes the appender to the table, forcing the cache of the appender to be cleared. If flushing the data triggers a constraint violation or any other error, then all data is invalidated, and this method raises DuckDB::Error
.
require 'duckdb' db = DuckDB::Database.open con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR)') appender = con.appender('users') appender .append_int32(1) .append_varchar('Alice') .end_row .flush