-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathprepared_statement.c
More file actions
605 lines (485 loc) · 24.5 KB
/
Copy pathprepared_statement.c
File metadata and controls
605 lines (485 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include "ruby-duckdb.h"
VALUE cDuckDBPreparedStatement;
static void destroy_prepared_statement(rubyDuckDBPreparedStatement *p);
static void deallocate(void *ctx);
static VALUE allocate(VALUE klass);
static size_t memsize(const void *p);
static VALUE prepared_statement_initialize(VALUE self, VALUE con, VALUE query);
static VALUE prepared_statement_nparams(VALUE self);
static VALUE prepared_statement_execute(VALUE self);
static VALUE prepared_statement_destroy(VALUE self);
static idx_t check_index(VALUE vidx);
static VALUE prepared_statement_bind_parameter_index(VALUE self, VALUE name);
static VALUE prepared_statement_parameter_name(VALUE self, VALUE vidx);
static VALUE prepared_statement_clear_bindings(VALUE self);
static VALUE prepared_statement_bind_bool(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement_bind_int8(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement_bind_int16(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement_bind_int32(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement_bind_int64(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement_bind_float(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement_bind_double(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement_bind_varchar(VALUE self, VALUE vidx, VALUE str);
static VALUE prepared_statement_bind_blob(VALUE self, VALUE vidx, VALUE blob);
static VALUE prepared_statement_bind_null(VALUE self, VALUE vidx);
static VALUE prepared_statement__statement_type(VALUE self);
static VALUE prepared_statement__param_type(VALUE self, VALUE vidx);
static VALUE prepared_statement__bind_uint8(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement__bind_uint16(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement__bind_uint32(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement__bind_uint64(VALUE self, VALUE vidx, VALUE val);
static VALUE prepared_statement__bind_date(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day);
static VALUE prepared_statement__bind_time(VALUE self, VALUE vidx, VALUE hour, VALUE min, VALUE sec, VALUE micros);
static VALUE prepared_statement__bind_timestamp(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day, VALUE hour, VALUE min, VALUE sec, VALUE micros);
static VALUE prepared_statement__bind_interval(VALUE self, VALUE vidx, VALUE months, VALUE days, VALUE micros);
static VALUE prepared_statement__bind_hugeint(VALUE self, VALUE vidx, VALUE lower, VALUE upper);
static VALUE prepared_statement__bind_uhugeint(VALUE self, VALUE vidx, VALUE lower, VALUE upper);
static VALUE prepared_statement__bind_decimal(VALUE self, VALUE vidx, VALUE lower, VALUE upper, VALUE width, VALUE scale);
static VALUE prepared_statement__bind_value(VALUE self, VALUE vidx, VALUE val);
static const rb_data_type_t prepared_statement_data_type = {
"DuckDB/PreparedStatement",
{NULL, deallocate, memsize,},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};
static void destroy_prepared_statement(rubyDuckDBPreparedStatement *p) {
if (p->prepared_statement) {
duckdb_destroy_prepare(&(p->prepared_statement));
}
}
static void deallocate(void *ctx) {
rubyDuckDBPreparedStatement *p = (rubyDuckDBPreparedStatement *)ctx;
destroy_prepared_statement(p);
// duckdb_destroy_prepare(&(p->prepared_statement));
xfree(p);
}
static VALUE allocate(VALUE klass) {
rubyDuckDBPreparedStatement *ctx = xcalloc((size_t)1, sizeof(rubyDuckDBPreparedStatement));
return TypedData_Wrap_Struct(klass, &prepared_statement_data_type, ctx);
}
static size_t memsize(const void *p) {
return sizeof(rubyDuckDBPreparedStatement);
}
VALUE rbduckdb_prepared_statement_new(duckdb_connection con, duckdb_extracted_statements extracted_statements, idx_t index) {
VALUE obj;
rubyDuckDBPreparedStatement *ctx;
obj = allocate(cDuckDBPreparedStatement);
TypedData_Get_Struct(obj, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_prepare_extracted_statement(con, extracted_statements, index, &(ctx->prepared_statement)) == DuckDBError) {
const char *error = duckdb_prepare_error(ctx->prepared_statement);
rb_raise(eDuckDBError, "%s", error ? error : "Failed to create DuckDB::PreparedStatement object.");
}
return obj;
}
static VALUE prepared_statement_initialize(VALUE self, VALUE con, VALUE query) {
rubyDuckDBConnection *ctxcon;
rubyDuckDBPreparedStatement *ctx;
if (!rb_obj_is_kind_of(con, cDuckDBConnection)) {
rb_raise(rb_eTypeError, "1st argument should be instance of DackDB::Connection");
}
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
ctxcon = rbduckdb_get_struct_connection(con);
if (duckdb_prepare(ctxcon->con, StringValuePtr(query), &(ctx->prepared_statement)) == DuckDBError) {
const char *error = duckdb_prepare_error(ctx->prepared_statement);
rb_raise(eDuckDBError, "%s", error ? error : "Failed to prepare statement(Database connection closed?).");
}
return self;
}
static VALUE prepared_statement_nparams(VALUE self) {
rubyDuckDBPreparedStatement *ctx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
return ULL2NUM(duckdb_nparams(ctx->prepared_statement));
}
/* :nodoc: */
typedef struct {
duckdb_prepared_statement prepared_statement;
duckdb_result *out_result;
duckdb_state retval;
} prepared_statement_execute_nogvl_args;
/* :nodoc: */
static void prepared_statement_execute_nogvl(void *ptr) {
prepared_statement_execute_nogvl_args *args = (prepared_statement_execute_nogvl_args *)ptr;
args->retval = duckdb_execute_prepared(args->prepared_statement, args->out_result);
}
static VALUE prepared_statement_execute(VALUE self) {
rubyDuckDBPreparedStatement *ctx;
rubyDuckDBResult *ctxr;
const char *error;
VALUE result = rbduckdb_create_result();
VALUE msg;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
ctxr = rbduckdb_get_struct_result(result);
prepared_statement_execute_nogvl_args args = {
.prepared_statement = ctx->prepared_statement,
.out_result = &(ctxr->result),
.retval = DuckDBError,
};
rb_thread_call_without_gvl((void *)prepared_statement_execute_nogvl, &args, RUBY_UBF_IO, 0);
duckdb_state state = args.retval;
if (state == DuckDBError) {
error = duckdb_prepare_error(args.prepared_statement);
if (error == NULL) {
error = duckdb_result_error(args.out_result);
}
msg = rb_str_new2(error ? error : "Failed to execute prepared statement.");
rb_raise(eDuckDBError, "%s", StringValuePtr(msg));
}
return result;
}
/*
* :nodoc:
*/
static VALUE prepared_statement_destroy(VALUE self) {
rubyDuckDBPreparedStatement *ctx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
destroy_prepared_statement(ctx);
/*
ctx->prepared_statement = NULL;
*/
return Qnil;
}
static idx_t check_index(VALUE vidx) {
idx_t idx = NUM2ULL(vidx);
if (idx <= 0) {
rb_raise(rb_eArgError, "index of parameter must be greater than 0");
}
return idx;
}
static VALUE prepared_statement_bind_parameter_index(VALUE self, VALUE name) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_parameter_index(ctx->prepared_statement, &idx, StringValuePtr(name)) == DuckDBError) {;
rb_raise(rb_eArgError, "parameter '%s' not found", StringValuePtr(name));
}
return ULL2NUM(idx);
}
static VALUE prepared_statement_parameter_name(VALUE self, VALUE vidx) {
rubyDuckDBPreparedStatement *ctx;
VALUE vname;
const char *name;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
name = duckdb_parameter_name(ctx->prepared_statement, idx);
if (name == NULL) {
rb_raise(eDuckDBError, "fail to get name of %llu parameter", (unsigned long long)idx);
}
vname = rb_str_new2(name);
duckdb_free((void *)name);
return vname;
}
/*
* call-seq:
* prepared_statement.clear_bindings -> DuckDB::PreparedStatement
*
* clear all bindings of prepared statement.
*/
static VALUE prepared_statement_clear_bindings(VALUE self) {
rubyDuckDBPreparedStatement *ctx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_clear_bindings(ctx->prepared_statement) == DuckDBError) {
const char *error = duckdb_prepare_error(ctx->prepared_statement);
rb_raise(eDuckDBError, "fail to clear bindings. %s", error);
}
return self;
}
static VALUE prepared_statement_bind_bool(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (val != Qtrue && val != Qfalse) {
rb_raise(rb_eArgError, "binding value must be boolean");
}
if (duckdb_bind_boolean(ctx->prepared_statement, idx, (val == Qtrue)) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_int8(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
int8_t i8val = (int8_t)NUM2INT(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_int8(ctx->prepared_statement, idx, i8val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_int16(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
int16_t i16val = NUM2INT(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_int16(ctx->prepared_statement, idx, i16val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_int32(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
int32_t i32val = NUM2INT(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_int32(ctx->prepared_statement, idx, i32val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_int64(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
int64_t i64val = NUM2LL(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_int64(ctx->prepared_statement, idx, i64val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_float(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
double dbl = NUM2DBL(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_float(ctx->prepared_statement, idx, (float)dbl) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_double(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
double dbl = NUM2DBL(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_double(ctx->prepared_statement, idx, dbl) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_varchar(VALUE self, VALUE vidx, VALUE str) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_varchar(ctx->prepared_statement, idx, StringValuePtr(str)) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_blob(VALUE self, VALUE vidx, VALUE blob) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_blob(ctx->prepared_statement, idx, (const void *)StringValuePtr(blob), (idx_t)RSTRING_LEN(blob)) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
static VALUE prepared_statement_bind_null(VALUE self, VALUE vidx) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_null(ctx->prepared_statement, idx) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__statement_type(VALUE self) {
rubyDuckDBPreparedStatement *ctx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
return INT2FIX(duckdb_prepared_statement_type(ctx->prepared_statement));
}
/* :nodoc: */
static VALUE prepared_statement__param_type(VALUE self, VALUE vidx) {
rubyDuckDBPreparedStatement *ctx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
return INT2FIX(duckdb_param_type(ctx->prepared_statement, NUM2ULL(vidx)));
}
/* :nodoc: */
static VALUE prepared_statement__bind_uint8(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
uint8_t ui8val = (uint8_t)NUM2UINT(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_uint8(ctx->prepared_statement, idx, ui8val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_uint16(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
uint16_t ui16val = (uint16_t)NUM2UINT(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_uint16(ctx->prepared_statement, idx, ui16val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_uint32(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
uint32_t ui32val = (uint32_t)NUM2UINT(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_uint32(ctx->prepared_statement, idx, ui32val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_uint64(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
uint64_t ui64val = (uint64_t)NUM2ULL(val);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_uint64(ctx->prepared_statement, idx, ui64val) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_date(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day) {
rubyDuckDBPreparedStatement *ctx;
duckdb_date dt;
idx_t idx = check_index(vidx);
dt = rbduckdb_to_duckdb_date_from_value(year, month, day);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_date(ctx->prepared_statement, idx, dt) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_time(VALUE self, VALUE vidx, VALUE hour, VALUE min, VALUE sec, VALUE micros){
rubyDuckDBPreparedStatement *ctx;
duckdb_time time;
idx_t idx = check_index(vidx);
time = rbduckdb_to_duckdb_time_from_value(hour, min, sec, micros);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_time(ctx->prepared_statement, idx, time) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_timestamp(VALUE self, VALUE vidx, VALUE year, VALUE month, VALUE day, VALUE hour, VALUE min, VALUE sec, VALUE micros) {
duckdb_timestamp timestamp;
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
timestamp = rbduckdb_to_duckdb_timestamp_from_value(year, month, day, hour, min, sec, micros);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
if (duckdb_bind_timestamp(ctx->prepared_statement, idx, timestamp) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_interval(VALUE self, VALUE vidx, VALUE months, VALUE days, VALUE micros) {
duckdb_interval interval;
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
rbduckdb_to_duckdb_interval_from_value(&interval, months, days, micros);
if (duckdb_bind_interval(ctx->prepared_statement, idx, interval) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_hugeint(VALUE self, VALUE vidx, VALUE lower, VALUE upper) {
duckdb_hugeint hugeint;
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
hugeint.lower = NUM2ULL(lower);
hugeint.upper = NUM2LL(upper);
if (duckdb_bind_hugeint(ctx->prepared_statement, idx, hugeint) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_uhugeint(VALUE self, VALUE vidx, VALUE lower, VALUE upper) {
duckdb_uhugeint uhugeint;
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
uhugeint.lower = NUM2ULL(lower);
uhugeint.upper = NUM2ULL(upper);
if (duckdb_bind_uhugeint(ctx->prepared_statement, idx, uhugeint) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_decimal(VALUE self, VALUE vidx, VALUE lower, VALUE upper, VALUE width, VALUE scale) {
duckdb_hugeint hugeint;
duckdb_decimal decimal;
rubyDuckDBPreparedStatement *ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
hugeint.lower = NUM2ULL(lower);
hugeint.upper = NUM2LL(upper);
decimal.value = hugeint;
decimal.width = (uint8_t)NUM2UINT(width);
decimal.scale = (uint8_t)NUM2UINT(scale);
if (duckdb_bind_decimal(ctx->prepared_statement, idx, decimal) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
/* :nodoc: */
static VALUE prepared_statement__bind_value(VALUE self, VALUE vidx, VALUE val) {
rubyDuckDBPreparedStatement *ctx;
rubyDuckDBValue *val_ctx;
idx_t idx = check_index(vidx);
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
val_ctx = get_struct_value(val);
if (duckdb_bind_value(ctx->prepared_statement, idx, val_ctx->value) == DuckDBError) {
rb_raise(eDuckDBError, "fail to bind %llu parameter", (unsigned long long)idx);
}
return self;
}
rubyDuckDBPreparedStatement *rbduckdb_get_struct_prepared_statement(VALUE self) {
rubyDuckDBPreparedStatement *ctx;
TypedData_Get_Struct(self, rubyDuckDBPreparedStatement, &prepared_statement_data_type, ctx);
return ctx;
}
void rbduckdb_init_prepared_statement(void) {
#if 0
VALUE mDuckDB = rb_define_module("DuckDB");
#endif
cDuckDBPreparedStatement = rb_define_class_under(mDuckDB, "PreparedStatement", rb_cObject);
rb_define_alloc_func(cDuckDBPreparedStatement, allocate);
rb_define_method(cDuckDBPreparedStatement, "initialize", prepared_statement_initialize, 2);
rb_define_method(cDuckDBPreparedStatement, "execute", prepared_statement_execute, 0);
rb_define_method(cDuckDBPreparedStatement, "destroy", prepared_statement_destroy, 0);
rb_define_method(cDuckDBPreparedStatement, "nparams", prepared_statement_nparams, 0);
rb_define_method(cDuckDBPreparedStatement, "bind_parameter_index", prepared_statement_bind_parameter_index, 1);
rb_define_method(cDuckDBPreparedStatement, "parameter_name", prepared_statement_parameter_name, 1);
rb_define_method(cDuckDBPreparedStatement, "clear_bindings", prepared_statement_clear_bindings, 0);
rb_define_method(cDuckDBPreparedStatement, "bind_bool", prepared_statement_bind_bool, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_int8", prepared_statement_bind_int8, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_int16", prepared_statement_bind_int16, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_int32", prepared_statement_bind_int32, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_int64", prepared_statement_bind_int64, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_float", prepared_statement_bind_float, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_double", prepared_statement_bind_double, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_varchar", prepared_statement_bind_varchar, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_blob", prepared_statement_bind_blob, 2);
rb_define_method(cDuckDBPreparedStatement, "bind_null", prepared_statement_bind_null, 1);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uint8", prepared_statement__bind_uint8, 2);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uint16", prepared_statement__bind_uint16, 2);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uint32", prepared_statement__bind_uint32, 2);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uint64", prepared_statement__bind_uint64, 2);
rb_define_private_method(cDuckDBPreparedStatement, "_statement_type", prepared_statement__statement_type, 0);
rb_define_private_method(cDuckDBPreparedStatement, "_param_type", prepared_statement__param_type, 1);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_date", prepared_statement__bind_date, 4);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_time", prepared_statement__bind_time, 5);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_timestamp", prepared_statement__bind_timestamp, 8);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_interval", prepared_statement__bind_interval, 4);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_hugeint", prepared_statement__bind_hugeint, 3);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_uhugeint", prepared_statement__bind_uhugeint, 3);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_decimal", prepared_statement__bind_decimal, 5);
rb_define_private_method(cDuckDBPreparedStatement, "_bind_value", prepared_statement__bind_value, 2);
}