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::FLIP_HUGEINT, Converter::HALF_HUGEINT, Converter::HALF_HUGEINT_BIT
Instance Method Summary
collapse
Methods included from Converter
_to_date, _to_decimal_from_vector, _to_hugeint_from_vector, _to_interval_from_vector, _to_time, _to_uuid_from_vector
Constructor Details
#initialize(con, schema, table) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'ext/duckdb/appender.c', line 56
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/duckdb/appender.rb', line 170
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
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'ext/duckdb/appender.c', line 259
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
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'ext/duckdb/appender.c', line 99
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/duckdb/appender.rb', line 58
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
221
222
223
224
225
226
227
228
229
230
231
|
# File 'ext/duckdb/appender.c', line 221
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
209
210
211
212
213
214
215
216
217
218
219
|
# File 'ext/duckdb/appender.c', line 209
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
125
126
127
128
129
130
131
132
133
134
135
|
# File 'ext/duckdb/appender.c', line 125
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
137
138
139
140
141
142
143
144
145
146
147
|
# File 'ext/duckdb/appender.c', line 137
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
149
150
151
152
153
154
155
156
157
158
159
|
# File 'ext/duckdb/appender.c', line 149
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
113
114
115
116
117
118
119
120
121
122
123
|
# File 'ext/duckdb/appender.c', line 113
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
152
153
154
155
|
# File 'lib/duckdb/appender.rb', line 152
def append_interval(value)
value = Interval.to_interval(value)
_append_interval(value.interval_months, value.interval_days, value.interval_micros)
end
|
#append_null ⇒ Object
273
274
275
276
277
278
279
280
281
|
# File 'ext/duckdb/appender.c', line 273
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
214
215
216
217
218
219
220
|
# File 'lib/duckdb/appender.rb', line 214
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/duckdb/appender.rb', line 88
def append_time(value)
time = case value
when Time
value
else
begin
Time.parse(value)
rescue
raise(ArgumentError, "Cannot parse argument `#{value}` to Time.")
end
end
_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/duckdb/appender.rb', line 119
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_uint16(val) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
|
# File 'ext/duckdb/appender.c', line 173
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
185
186
187
188
189
190
191
192
193
194
195
|
# File 'ext/duckdb/appender.c', line 185
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
197
198
199
200
201
202
203
204
205
206
207
|
# File 'ext/duckdb/appender.c', line 197
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
161
162
163
164
165
166
167
168
169
170
171
|
# File 'ext/duckdb/appender.c', line 161
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
233
234
235
236
237
238
239
240
241
242
243
|
# File 'ext/duckdb/appender.c', line 233
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
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'ext/duckdb/appender.c', line 245
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
79
80
81
82
83
84
85
86
87
|
# File 'ext/duckdb/appender.c', line 79
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
362
363
364
365
366
367
368
369
370
|
# File 'ext/duckdb/appender.c', line 362
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
89
90
91
92
93
94
95
96
97
|
# File 'ext/duckdb/appender.c', line 89
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
352
353
354
355
356
357
358
359
360
|
# File 'ext/duckdb/appender.c', line 352
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;
}
|