Class: DuckDB::Appender
- Inherits:
-
Object
show all
- Includes:
- Converter
- Defined in:
- lib/duckdb/appender.rb,
ext/duckdb/appender.c
Overview
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')
Constant Summary
collapse
- RANGE_INT16 =
-32_768..32_767
- RANGE_INT32 =
-2_147_483_648..2_147_483_647
- RANGE_INT64 =
-9_223_372_036_854_775_808..9_223_372_036_854_775_807
Constants included
from Converter
Converter::EPOCH, Converter::EPOCH_UTC, Converter::FLIP_HUGEINT, Converter::HALF_HUGEINT, Converter::HALF_HUGEINT_BIT
Instance Method Summary
collapse
Methods included from Converter
_parse_date, _parse_time, _to_date, _to_decimal_from_hugeint, _to_decimal_from_value, _to_hugeint_from_vector, _to_infinity, _to_interval_from_vector, _to_query_progress, _to_time, _to_time_from_duckdb_time, _to_time_from_duckdb_time_tz, _to_time_from_duckdb_timestamp_ms, _to_time_from_duckdb_timestamp_ns, _to_time_from_duckdb_timestamp_s, _to_time_from_duckdb_timestamp_tz, _to_uuid_from_vector
Constructor Details
#initialize(con, schema, table) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'ext/duckdb/appender.c', line 57
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;
}
|
Instance Method Details
#append(value) ⇒ Object
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.begin_row
appender.append(1)
appender.append('Alice')
appender.end_row
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/duckdb/appender.rb', line 179
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
|
#append_blob(val) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'ext/duckdb/appender.c', line 260
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");
}
return self;
}
|
#append_bool(val) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'ext/duckdb/appender.c', line 100
static VALUE appender_append_bool(VALUE self, VALUE val) {
rubyDuckDBAppender *ctx;
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (val != Qtrue && val != Qfalse) {
rb_raise(rb_eArgError, "argument must be boolean");
}
if (duckdb_append_bool(ctx->appender, (val == Qtrue)) == DuckDBError) {
rb_raise(eDuckDBError, "failed to append boolean");
}
return self;
}
|
#append_date(value) ⇒ Object
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.begin_row
appender.append_date(Date.today)
appender.end_row
appender.flush
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/duckdb/appender.rb', line 76
def append_date(value)
date = case value
when Date, Time
value
else
begin
Date.parse(value)
rescue
raise(ArgumentError, "Cannot parse argument `#{value}` to Date.")
end
end
_append_date(date.year, date.month, date.day)
end
|
#append_double(val) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
|
# File 'ext/duckdb/appender.c', line 222
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");
}
return self;
}
|
#append_float(val) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
|
# File 'ext/duckdb/appender.c', line 210
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");
}
return self;
}
|
#append_hugeint(value) ⇒ Object
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
.begin_row
.append_hugeint(-170_141_183_460_469_231_731_687_303_715_884_105_727)
.end_row
37
38
39
40
|
# File 'lib/duckdb/appender.rb', line 37
def append_hugeint(value)
lower, upper = integer_to_hugeint(value)
_append_hugeint(lower, upper)
end
|
#append_int16(val) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'ext/duckdb/appender.c', line 126
static VALUE appender_append_int16(VALUE self, VALUE val) {
rubyDuckDBAppender *ctx;
int16_t i16val = (int16_t)NUM2INT(val);
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_append_int16(ctx->appender, i16val) == DuckDBError) {
rb_raise(eDuckDBError, "failed to append");
}
return self;
}
|
#append_int32(val) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
|
# File 'ext/duckdb/appender.c', line 138
static VALUE appender_append_int32(VALUE self, VALUE val) {
rubyDuckDBAppender *ctx;
int32_t i32val = (int32_t)NUM2INT(val);
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_append_int32(ctx->appender, i32val) == DuckDBError) {
rb_raise(eDuckDBError, "failed to append");
}
return self;
}
|
#append_int64(val) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
|
# File 'ext/duckdb/appender.c', line 150
static VALUE appender_append_int64(VALUE self, VALUE val) {
rubyDuckDBAppender *ctx;
int64_t i64val = (int64_t)NUM2LL(val);
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_append_int64(ctx->appender, i64val) == DuckDBError) {
rb_raise(eDuckDBError, "failed to append");
}
return self;
}
|
#append_int8(val) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
|
# File 'ext/duckdb/appender.c', line 114
static VALUE appender_append_int8(VALUE self, VALUE val) {
rubyDuckDBAppender *ctx;
int8_t i8val = (int8_t)NUM2INT(val);
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_append_int8(ctx->appender, i8val) == DuckDBError) {
rb_raise(eDuckDBError, "failed to append");
}
return self;
}
|
#append_interval(value) ⇒ Object
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
.begin_row
.append_interval('P1Y2D') .end_row
.flush
161
162
163
164
|
# File 'lib/duckdb/appender.rb', line 161
def append_interval(value)
value = Interval.to_interval(value)
_append_interval(value.interval_months, value.interval_days, value.interval_micros)
end
|
#append_null ⇒ Object
274
275
276
277
278
279
280
281
282
|
# File 'ext/duckdb/appender.c', line 274
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");
}
return self;
}
|
#append_row(*args) ⇒ Object
append a row.
appender.append_row(1, 'Alice')
is same as:
appender.begin_row
appender.append(1)
appender.append('Alice')
appender.end_row
223
224
225
226
227
228
229
|
# File 'lib/duckdb/appender.rb', line 223
def append_row(*args)
begin_row
args.each do |arg|
append(arg)
end
end_row
end
|
#append_time(value) ⇒ Object
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.begin_row
appender.append_time(Time.now)
appender.end_row
appender.flush
106
107
108
109
110
|
# File 'lib/duckdb/appender.rb', line 106
def append_time(value)
time = _parse_time(value)
_append_time(time.hour, time.min, time.sec, time.usec)
end
|
#append_timestamp(value) ⇒ Object
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.begin_row
appender.append_time(Time.now)
appender.end_row
appender.flush
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/duckdb/appender.rb', line 128
def append_timestamp(value)
time = case value
when Time
value
when Date
value.to_time
else
begin
Time.parse(value)
rescue
raise(ArgumentError, "Cannot parse argument `#{value}` to Time or Date.")
end
end
_append_timestamp(time.year, time.month, time.day, time.hour, time.min, time.sec, time.nsec / 1000)
end
|
#append_uhugeint(value) ⇒ Object
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
.begin_row
.append_hugeint(340_282_366_920_938_463_463_374_607_431_768_211_455)
.end_row
55
56
57
58
|
# File 'lib/duckdb/appender.rb', line 55
def append_uhugeint(value)
lower, upper = integer_to_hugeint(value)
_append_uhugeint(lower, upper)
end
|
#append_uint16(val) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
|
# File 'ext/duckdb/appender.c', line 174
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");
}
return self;
}
|
#append_uint32(val) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
|
# File 'ext/duckdb/appender.c', line 186
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");
}
return self;
}
|
#append_uint64(val) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
|
# File 'ext/duckdb/appender.c', line 198
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");
}
return self;
}
|
#append_uint8(val) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
|
# File 'ext/duckdb/appender.c', line 162
static VALUE appender_append_uint8(VALUE self, VALUE val) {
rubyDuckDBAppender *ctx;
int8_t ui8val = (uint8_t)NUM2UINT(val);
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_append_uint8(ctx->appender, ui8val) == DuckDBError) {
rb_raise(eDuckDBError, "failed to append");
}
return self;
}
|
#append_varchar(val) ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
|
# File 'ext/duckdb/appender.c', line 234
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");
}
return self;
}
|
#append_varchar_length(val, len) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'ext/duckdb/appender.c', line 246
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");
}
return self;
}
|
#begin_row ⇒ Object
80
81
82
83
84
85
86
87
88
|
# File 'ext/duckdb/appender.c', line 80
static VALUE appender_begin_row(VALUE self) {
rubyDuckDBAppender *ctx;
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_appender_begin_row(ctx->appender) == DuckDBError) {
rb_raise(eDuckDBError, "failed to flush");
}
return self;
}
|
#close ⇒ Object
378
379
380
381
382
383
384
385
386
|
# File 'ext/duckdb/appender.c', line 378
static VALUE appender_close(VALUE self) {
rubyDuckDBAppender *ctx;
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_appender_close(ctx->appender) == DuckDBError) {
rb_raise(eDuckDBError, "failed to flush");
}
return self;
}
|
#end_row ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'ext/duckdb/appender.c', line 90
static VALUE appender_end_row(VALUE self) {
rubyDuckDBAppender *ctx;
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_appender_end_row(ctx->appender) == DuckDBError) {
rb_raise(eDuckDBError, "failed to flush");
}
return self;
}
|
#flush ⇒ Object
368
369
370
371
372
373
374
375
376
|
# File 'ext/duckdb/appender.c', line 368
static VALUE appender_flush(VALUE self) {
rubyDuckDBAppender *ctx;
TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
if (duckdb_appender_flush(ctx->appender) == DuckDBError) {
rb_raise(eDuckDBError, "failed to flush");
}
return self;
}
|