sqlite3 1.5.4 → 2.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +594 -1
  3. data/CONTRIBUTING.md +112 -6
  4. data/{faq/faq.md → FAQ.md} +52 -84
  5. data/INSTALLATION.md +267 -0
  6. data/LICENSE +18 -22
  7. data/README.md +87 -124
  8. data/dependencies.yml +10 -11
  9. data/ext/sqlite3/aggregator.c +153 -147
  10. data/ext/sqlite3/aggregator.h +6 -4
  11. data/ext/sqlite3/backup.c +86 -64
  12. data/ext/sqlite3/backup.h +2 -2
  13. data/ext/sqlite3/database.c +744 -484
  14. data/ext/sqlite3/database.h +23 -4
  15. data/ext/sqlite3/exception.c +116 -92
  16. data/ext/sqlite3/exception.h +5 -1
  17. data/ext/sqlite3/extconf.rb +68 -33
  18. data/ext/sqlite3/sqlite3.c +177 -115
  19. data/ext/sqlite3/sqlite3_ruby.h +2 -2
  20. data/ext/sqlite3/statement.c +590 -293
  21. data/ext/sqlite3/statement.h +4 -3
  22. data/ext/sqlite3/timespec.h +20 -0
  23. data/lib/sqlite3/constants.rb +195 -47
  24. data/lib/sqlite3/database.rb +252 -195
  25. data/lib/sqlite3/errors.rb +54 -1
  26. data/lib/sqlite3/fork_safety.rb +66 -0
  27. data/lib/sqlite3/pragmas.rb +196 -143
  28. data/lib/sqlite3/resultset.rb +14 -105
  29. data/lib/sqlite3/statement.rb +58 -13
  30. data/lib/sqlite3/value.rb +17 -20
  31. data/lib/sqlite3/version.rb +2 -21
  32. data/lib/sqlite3/version_info.rb +17 -0
  33. data/lib/sqlite3.rb +8 -4
  34. data/ports/archives/sqlite-autoconf-3530200.tar.gz +0 -0
  35. metadata +16 -108
  36. data/API_CHANGES.md +0 -49
  37. data/ChangeLog.cvs +0 -88
  38. data/Gemfile +0 -3
  39. data/LICENSE-DEPENDENCIES +0 -20
  40. data/faq/faq.rb +0 -145
  41. data/faq/faq.yml +0 -426
  42. data/lib/sqlite3/translator.rb +0 -118
  43. data/ports/archives/sqlite-autoconf-3400000.tar.gz +0 -0
  44. data/test/helper.rb +0 -27
  45. data/test/test_backup.rb +0 -33
  46. data/test/test_collation.rb +0 -82
  47. data/test/test_database.rb +0 -545
  48. data/test/test_database_flags.rb +0 -95
  49. data/test/test_database_readonly.rb +0 -36
  50. data/test/test_database_readwrite.rb +0 -41
  51. data/test/test_deprecated.rb +0 -44
  52. data/test/test_encoding.rb +0 -155
  53. data/test/test_integration.rb +0 -507
  54. data/test/test_integration_aggregate.rb +0 -336
  55. data/test/test_integration_open_close.rb +0 -30
  56. data/test/test_integration_pending.rb +0 -115
  57. data/test/test_integration_resultset.rb +0 -142
  58. data/test/test_integration_statement.rb +0 -194
  59. data/test/test_result_set.rb +0 -37
  60. data/test/test_sqlite3.rb +0 -30
  61. data/test/test_statement.rb +0 -263
  62. data/test/test_statement_execute.rb +0 -35
@@ -10,110 +10,300 @@
10
10
  if(!_ctxt->db) \
11
11
  rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");
12
12
 
13
+ #define REQUIRE_CLOSED_DB(_ctxt) \
14
+ if(_ctxt->db) \
15
+ rb_raise(rb_path2class("SQLite3::Exception"), "cannot open a database that is already open");
16
+
13
17
  VALUE cSqlite3Database;
14
18
 
15
- static void deallocate(void * ctx)
19
+ /* See adr/2024-09-fork-safety.md */
20
+ static void
21
+ discard_db(sqlite3RubyPtr ctx)
22
+ {
23
+ sqlite3_file *sfile;
24
+ int status;
25
+
26
+ // release as much heap memory as possible by deallocating non-essential memory
27
+ // allocations held by the database library. Memory used to cache database pages to
28
+ // improve performance is an example of non-essential memory.
29
+ // on my development machine, this reduces the lost memory from 152k to 69k.
30
+ sqlite3_db_release_memory(ctx->db);
31
+
32
+ // release file descriptors
33
+ #ifdef HAVE_SQLITE3_DB_NAME
34
+ const char *db_name;
35
+ int j_db = 0;
36
+ while ((db_name = sqlite3_db_name(ctx->db, j_db)) != NULL) {
37
+ status = sqlite3_file_control(ctx->db, db_name, SQLITE_FCNTL_FILE_POINTER, &sfile);
38
+ if (status == 0 && sfile->pMethods != NULL) {
39
+ sfile->pMethods->xClose(sfile);
40
+ }
41
+ j_db++;
42
+ }
43
+ #else
44
+ status = sqlite3_file_control(ctx->db, NULL, SQLITE_FCNTL_FILE_POINTER, &sfile);
45
+ if (status == 0 && sfile->pMethods != NULL) {
46
+ sfile->pMethods->xClose(sfile);
47
+ }
48
+ #endif
49
+
50
+ status = sqlite3_file_control(ctx->db, NULL, SQLITE_FCNTL_JOURNAL_POINTER, &sfile);
51
+ if (status == 0 && sfile->pMethods != NULL) {
52
+ sfile->pMethods->xClose(sfile);
53
+ }
54
+
55
+ ctx->db = NULL;
56
+ ctx->flags |= SQLITE3_RB_DATABASE_DISCARDED;
57
+ }
58
+
59
+ static void
60
+ close_or_discard_db(sqlite3RubyPtr ctx)
61
+ {
62
+ if (ctx->db) {
63
+ int is_readonly = (ctx->flags & SQLITE3_RB_DATABASE_READONLY);
64
+
65
+ if (is_readonly || ctx->owner == getpid()) {
66
+ // Ordinary close.
67
+ sqlite3_close_v2(ctx->db);
68
+ ctx->db = NULL;
69
+ } else {
70
+ // This is an open connection carried across a fork(). "Discard" it.
71
+ discard_db(ctx);
72
+ }
73
+ }
74
+ }
75
+
76
+
77
+ void
78
+ rb_sqlite3_pin_array_and_contents(VALUE ary)
79
+ {
80
+ long i;
81
+
82
+ if (NIL_P(ary) || !ary) { return; }
83
+
84
+ rb_gc_mark(ary);
85
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
86
+ rb_gc_mark(RARRAY_AREF(ary, i));
87
+ }
88
+ }
89
+
90
+ static int
91
+ pin_hash_value(VALUE UNUSED(key), VALUE value, VALUE UNUSED(arg))
16
92
  {
17
- sqlite3RubyPtr c = (sqlite3RubyPtr)ctx;
18
- sqlite3 * db = c->db;
93
+ rb_gc_mark(value);
94
+ return ST_CONTINUE;
95
+ }
96
+
97
+ static void
98
+ pin_hash_and_contents(VALUE hash)
99
+ {
100
+ if (NIL_P(hash) || !hash) { return; }
19
101
 
20
- if(db) sqlite3_close(db);
21
- xfree(c);
102
+ rb_gc_mark(hash);
103
+ rb_hash_foreach(hash, pin_hash_value, 0);
22
104
  }
23
105
 
24
- static VALUE allocate(VALUE klass)
106
+ /* Each wrapper also owns live aggregate instances, whose VALUEs sqlite keeps in
107
+ * its own aggregate contexts. */
108
+ static void
109
+ pin_aggregators(VALUE aggregators)
25
110
  {
26
- sqlite3RubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3Ruby));
27
- return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
111
+ long i;
112
+
113
+ rb_sqlite3_pin_array_and_contents(aggregators);
114
+
115
+ if (NIL_P(aggregators) || !aggregators) { return; }
116
+
117
+ for (i = 0; i < RARRAY_LEN(aggregators); i++) {
118
+ rb_sqlite3_aggregator_pin_instances(RARRAY_AREF(aggregators, i));
119
+ }
120
+ }
121
+
122
+ static void
123
+ database_mark(void *ctx)
124
+ {
125
+ sqlite3RubyPtr c = (sqlite3RubyPtr)ctx;
126
+
127
+ /* sqlite holds raw pointers to these, so they must not move. */
128
+ rb_gc_mark(c->busy_handler);
129
+ rb_gc_mark(c->trace_handler);
130
+ rb_gc_mark(c->authorizer);
131
+
132
+ rb_sqlite3_pin_array_and_contents(c->functions);
133
+ pin_hash_and_contents(c->collations);
134
+ pin_aggregators(c->aggregators);
135
+ }
136
+
137
+ static void
138
+ deallocate(void *ctx)
139
+ {
140
+ close_or_discard_db((sqlite3RubyPtr)ctx);
141
+ xfree(ctx);
142
+ }
143
+
144
+ static size_t
145
+ database_memsize(const void *ctx)
146
+ {
147
+ const sqlite3RubyPtr c = (const sqlite3RubyPtr)ctx;
148
+ // NB: can't account for ctx->db because the type is incomplete.
149
+ return sizeof(*c);
150
+ }
151
+
152
+ static const rb_data_type_t database_type = {
153
+ .wrap_struct_name = "SQLite3::Backup",
154
+ .function = {
155
+ .dmark = database_mark,
156
+ .dfree = deallocate,
157
+ .dsize = database_memsize,
158
+ },
159
+ .flags = RUBY_TYPED_WB_PROTECTED, // Not freed immediately because the dfree function do IOs.
160
+ };
161
+
162
+ static VALUE
163
+ allocate(VALUE klass)
164
+ {
165
+ sqlite3RubyPtr ctx;
166
+ VALUE object = TypedData_Make_Struct(klass, sqlite3Ruby, &database_type, ctx);
167
+ ctx->owner = getpid();
168
+ return object;
28
169
  }
29
170
 
30
171
  static char *
31
172
  utf16_string_value_ptr(VALUE str)
32
173
  {
33
- StringValue(str);
34
- rb_str_buf_cat(str, "\x00\x00", 2L);
35
- return RSTRING_PTR(str);
174
+ VALUE utf16str, codepoints;
175
+
176
+ StringValue(str);
177
+
178
+ utf16str = rb_str_dup(str);
179
+ if (!UTF16_LE_P(utf16str) && !UTF16_BE_P(utf16str)) {
180
+ /* an untagged string (the utf16 option) holds native-byte-order UTF-16 */
181
+ const union { uint16_t u16; uint8_t u8[2]; } native = { 1 };
182
+ rb_enc_associate_index(utf16str, rb_enc_find_index(native.u8[0] ? "UTF-16LE" : "UTF-16BE"));
183
+ }
184
+ codepoints = rb_funcall(utf16str, rb_intern("codepoints"), 0);
185
+ if (RTEST(rb_funcall(codepoints, rb_intern("include?"), 1, INT2FIX(0)))) {
186
+ rb_raise(rb_eArgError, "string contains null char");
187
+ }
188
+
189
+ rb_str_buf_cat(str, "\x00\x00", 2L);
190
+ return RSTRING_PTR(str);
36
191
  }
37
192
 
38
- static VALUE sqlite3_rb_close(VALUE self);
193
+ sqlite3RubyPtr
194
+ sqlite3_database_unwrap(VALUE database)
195
+ {
196
+ sqlite3RubyPtr ctx;
197
+ TypedData_Get_Struct(database, sqlite3Ruby, &database_type, ctx);
198
+ return ctx;
199
+ }
39
200
 
40
- static VALUE rb_sqlite3_open_v2(VALUE self, VALUE file, VALUE mode, VALUE zvfs)
201
+ static VALUE
202
+ rb_sqlite3_open_v2(VALUE self, VALUE file, VALUE mode, VALUE zvfs)
41
203
  {
42
- sqlite3RubyPtr ctx;
43
- int status;
204
+ sqlite3RubyPtr ctx;
205
+ int status;
206
+ int flags;
44
207
 
45
- Data_Get_Struct(self, sqlite3Ruby, ctx);
208
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
209
+ REQUIRE_CLOSED_DB(ctx);
46
210
 
47
211
  #if defined TAINTING_SUPPORT
48
212
  # if defined StringValueCStr
49
- StringValuePtr(file);
50
- rb_check_safe_obj(file);
213
+ StringValuePtr(file);
214
+ rb_check_safe_obj(file);
51
215
  # else
52
- Check_SafeStr(file);
216
+ Check_SafeStr(file);
53
217
  # endif
54
218
  #endif
55
219
 
56
- status = sqlite3_open_v2(
57
- StringValuePtr(file),
58
- &ctx->db,
59
- NUM2INT(mode),
60
- NIL_P(zvfs) ? NULL : StringValuePtr(zvfs)
61
- );
220
+ flags = NUM2INT(mode);
221
+ status = sqlite3_open_v2(
222
+ StringValueCStr(file),
223
+ &ctx->db,
224
+ flags,
225
+ NIL_P(zvfs) ? NULL : StringValueCStr(zvfs)
226
+ );
227
+
228
+ if (status != SQLITE_OK) {
229
+ char *msg = sqlite3_mprintf("%s", sqlite3_errmsg(ctx->db));
230
+ sqlite3_close_v2(ctx->db);
231
+ ctx->db = NULL;
232
+ CHECK_MSG(ctx->db, status, msg);
233
+ }
62
234
 
63
- CHECK(ctx->db, status)
235
+ if (flags & SQLITE_OPEN_READONLY) {
236
+ ctx->flags |= SQLITE3_RB_DATABASE_READONLY;
237
+ }
64
238
 
65
- return self;
239
+ return self;
66
240
  }
67
241
 
68
- static VALUE rb_sqlite3_disable_quirk_mode(VALUE self)
242
+ static VALUE
243
+ rb_sqlite3_disable_quirk_mode(VALUE self)
69
244
  {
70
245
  #if defined SQLITE_DBCONFIG_DQS_DDL
71
- sqlite3RubyPtr ctx;
72
- Data_Get_Struct(self, sqlite3Ruby, ctx);
246
+ sqlite3RubyPtr ctx;
247
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
73
248
 
74
- if(!ctx->db) return Qfalse;
249
+ if (!ctx->db) { return Qfalse; }
75
250
 
76
- sqlite3_db_config(ctx->db, SQLITE_DBCONFIG_DQS_DDL, 0, (void*)0);
77
- sqlite3_db_config(ctx->db, SQLITE_DBCONFIG_DQS_DML, 0, (void*)0);
251
+ sqlite3_db_config(ctx->db, SQLITE_DBCONFIG_DQS_DDL, 0, (void *)0);
252
+ sqlite3_db_config(ctx->db, SQLITE_DBCONFIG_DQS_DML, 0, (void *)0);
78
253
 
79
- return Qtrue;
254
+ return Qtrue;
80
255
  #else
81
- return Qfalse;
256
+ return Qfalse;
82
257
  #endif
83
258
  }
84
259
 
85
- /* call-seq: db.close
260
+ /*
261
+ * Close the database and release all associated resources.
86
262
  *
87
- * Closes this database.
263
+ * ⚠ Writable connections that are carried across a <tt>fork()</tt> are not completely
264
+ * closed. {Sqlite does not support forking}[https://www.sqlite.org/howtocorrupt.html],
265
+ * and fully closing a writable connection that has been carried across a fork may corrupt the
266
+ * database. Since it is an incomplete close, not all memory resources are freed, but this is safer
267
+ * than risking data loss.
268
+ *
269
+ * See rdoc-ref:adr/2024-09-fork-safety.md for more information on fork safety.
88
270
  */
89
- static VALUE sqlite3_rb_close(VALUE self)
271
+ static VALUE
272
+ sqlite3_rb_close(VALUE self)
90
273
  {
91
- sqlite3RubyPtr ctx;
92
- sqlite3 * db;
93
- Data_Get_Struct(self, sqlite3Ruby, ctx);
274
+ sqlite3RubyPtr ctx;
275
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
276
+
277
+ close_or_discard_db(ctx);
94
278
 
95
- db = ctx->db;
96
- CHECK(db, sqlite3_close(ctx->db));
279
+ return self;
280
+ }
97
281
 
98
- ctx->db = NULL;
282
+ /* private method, primarily for testing */
283
+ static VALUE
284
+ sqlite3_rb_discard(VALUE self)
285
+ {
286
+ sqlite3RubyPtr ctx;
287
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
99
288
 
100
- rb_iv_set(self, "-aggregators", Qnil);
289
+ discard_db(ctx);
101
290
 
102
- return self;
291
+ return self;
103
292
  }
104
293
 
105
294
  /* call-seq: db.closed?
106
295
  *
107
296
  * Returns +true+ if this database instance has been closed (see #close).
108
297
  */
109
- static VALUE closed_p(VALUE self)
298
+ static VALUE
299
+ closed_p(VALUE self)
110
300
  {
111
- sqlite3RubyPtr ctx;
112
- Data_Get_Struct(self, sqlite3Ruby, ctx);
301
+ sqlite3RubyPtr ctx;
302
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
113
303
 
114
- if(!ctx->db) return Qtrue;
304
+ if (!ctx->db) { return Qtrue; }
115
305
 
116
- return Qfalse;
306
+ return Qfalse;
117
307
  }
118
308
 
119
309
  /* call-seq: total_changes
@@ -121,20 +311,21 @@ static VALUE closed_p(VALUE self)
121
311
  * Returns the total number of changes made to this database instance
122
312
  * since it was opened.
123
313
  */
124
- static VALUE total_changes(VALUE self)
314
+ static VALUE
315
+ total_changes(VALUE self)
125
316
  {
126
- sqlite3RubyPtr ctx;
127
- Data_Get_Struct(self, sqlite3Ruby, ctx);
128
- REQUIRE_OPEN_DB(ctx);
317
+ sqlite3RubyPtr ctx;
318
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
319
+ REQUIRE_OPEN_DB(ctx);
129
320
 
130
- return INT2NUM((long)sqlite3_total_changes(ctx->db));
321
+ return INT2NUM(sqlite3_total_changes(ctx->db));
131
322
  }
132
323
 
133
- static void tracefunc(void * data, const char *sql)
324
+ static void
325
+ tracefunc(void *data, const char *sql)
134
326
  {
135
- VALUE self = (VALUE)data;
136
- VALUE thing = rb_iv_get(self, "@tracefunc");
137
- rb_funcall(thing, rb_intern("call"), 1, rb_str_new2(sql));
327
+ sqlite3RubyPtr ctx = (sqlite3RubyPtr)data;
328
+ rb_funcall(ctx->trace_handler, rb_intern("call"), 1, rb_str_new2(sql));
138
329
  }
139
330
 
140
331
  /* call-seq:
@@ -145,34 +336,37 @@ static void tracefunc(void * data, const char *sql)
145
336
  * statement executed. The block receives one parameter: the SQL statement
146
337
  * executed. If the block is +nil+, any existing tracer will be uninstalled.
147
338
  */
148
- static VALUE trace(int argc, VALUE *argv, VALUE self)
339
+ static VALUE
340
+ trace(int argc, VALUE *argv, VALUE self)
149
341
  {
150
- sqlite3RubyPtr ctx;
151
- VALUE block;
342
+ sqlite3RubyPtr ctx;
343
+ VALUE block;
152
344
 
153
- Data_Get_Struct(self, sqlite3Ruby, ctx);
154
- REQUIRE_OPEN_DB(ctx);
345
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
346
+ REQUIRE_OPEN_DB(ctx);
155
347
 
156
- rb_scan_args(argc, argv, "01", &block);
348
+ rb_scan_args(argc, argv, "01", &block);
157
349
 
158
- if(NIL_P(block) && rb_block_given_p()) block = rb_block_proc();
350
+ if (NIL_P(block) && rb_block_given_p()) { block = rb_block_proc(); }
159
351
 
160
- rb_iv_set(self, "@tracefunc", block);
352
+ RB_OBJ_WRITE(self, &ctx->trace_handler, block);
161
353
 
162
- sqlite3_trace(ctx->db, NIL_P(block) ? NULL : tracefunc, (void *)self);
354
+ sqlite3_trace(ctx->db, NIL_P(block) ? NULL : tracefunc, (void *)ctx);
163
355
 
164
- return self;
356
+ return self;
165
357
  }
166
358
 
167
- static int rb_sqlite3_busy_handler(void * ctx, int count)
359
+ static int
360
+ rb_sqlite3_busy_handler(void *context, int count)
168
361
  {
169
- VALUE self = (VALUE)(ctx);
170
- VALUE handle = rb_iv_get(self, "@busy_handler");
171
- VALUE result = rb_funcall(handle, rb_intern("call"), 1, INT2NUM((long)count));
362
+ sqlite3RubyPtr ctx = (sqlite3RubyPtr)context;
172
363
 
173
- if(Qfalse == result) return 0;
364
+ VALUE handle = ctx->busy_handler;
365
+ VALUE result = rb_funcall(handle, rb_intern("call"), 1, INT2NUM(count));
174
366
 
175
- return 1;
367
+ if (Qfalse == result) { return 0; }
368
+
369
+ return 1;
176
370
  }
177
371
 
178
372
  /* call-seq:
@@ -189,27 +383,68 @@ static int rb_sqlite3_busy_handler(void * ctx, int count)
189
383
  *
190
384
  * See also the mutually exclusive #busy_timeout.
191
385
  */
192
- static VALUE busy_handler(int argc, VALUE *argv, VALUE self)
386
+ static VALUE
387
+ busy_handler(int argc, VALUE *argv, VALUE self)
193
388
  {
194
- sqlite3RubyPtr ctx;
195
- VALUE block;
196
- int status;
389
+ sqlite3RubyPtr ctx;
390
+ VALUE block;
391
+ int status;
392
+
393
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
394
+ REQUIRE_OPEN_DB(ctx);
197
395
 
198
- Data_Get_Struct(self, sqlite3Ruby, ctx);
199
- REQUIRE_OPEN_DB(ctx);
396
+ rb_scan_args(argc, argv, "01", &block);
200
397
 
201
- rb_scan_args(argc, argv, "01", &block);
398
+ if (NIL_P(block) && rb_block_given_p()) { block = rb_block_proc(); }
399
+ RB_OBJ_WRITE(self, &ctx->busy_handler, block);
202
400
 
203
- if(NIL_P(block) && rb_block_given_p()) block = rb_block_proc();
401
+ status = sqlite3_busy_handler(
402
+ ctx->db,
403
+ NIL_P(block) ? NULL : rb_sqlite3_busy_handler,
404
+ (void *)ctx
405
+ );
204
406
 
205
- rb_iv_set(self, "@busy_handler", block);
407
+ CHECK(ctx->db, status);
206
408
 
207
- status = sqlite3_busy_handler(
208
- ctx->db, NIL_P(block) ? NULL : rb_sqlite3_busy_handler, (void *)self);
409
+ return self;
410
+ }
209
411
 
210
- CHECK(ctx->db, status);
412
+ static int
413
+ rb_sqlite3_statement_timeout(void *context)
414
+ {
415
+ sqlite3RubyPtr ctx = (sqlite3RubyPtr)context;
416
+ struct timespec currentTime;
417
+ clock_gettime(CLOCK_MONOTONIC, &currentTime);
418
+
419
+ if (!timespecisset(&ctx->stmt_deadline)) {
420
+ // Set stmt_deadline if not already set
421
+ ctx->stmt_deadline = currentTime;
422
+ } else if (timespecafter(&currentTime, &ctx->stmt_deadline)) {
423
+ return 1;
424
+ }
211
425
 
212
- return self;
426
+ return 0;
427
+ }
428
+
429
+ /* call-seq: db.statement_timeout = ms
430
+ *
431
+ * Indicates that if a query lasts longer than the indicated number of
432
+ * milliseconds, SQLite should interrupt that query and return an error.
433
+ * By default, SQLite does not interrupt queries. To restore the default
434
+ * behavior, send 0 as the +ms+ parameter.
435
+ */
436
+ static VALUE
437
+ set_statement_timeout(VALUE self, VALUE milliseconds)
438
+ {
439
+ sqlite3RubyPtr ctx;
440
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
441
+
442
+ ctx->stmt_timeout = NUM2INT(milliseconds);
443
+ int n = NUM2INT(milliseconds) == 0 ? -1 : 1000;
444
+
445
+ sqlite3_progress_handler(ctx->db, n, rb_sqlite3_statement_timeout, (void *)ctx);
446
+
447
+ return self;
213
448
  }
214
449
 
215
450
  /* call-seq: last_insert_row_id
@@ -217,115 +452,146 @@ static VALUE busy_handler(int argc, VALUE *argv, VALUE self)
217
452
  * Obtains the unique row ID of the last row to be inserted by this Database
218
453
  * instance.
219
454
  */
220
- static VALUE last_insert_row_id(VALUE self)
221
- {
222
- sqlite3RubyPtr ctx;
223
- Data_Get_Struct(self, sqlite3Ruby, ctx);
224
- REQUIRE_OPEN_DB(ctx);
225
-
226
- return LL2NUM(sqlite3_last_insert_rowid(ctx->db));
227
- }
228
-
229
- VALUE sqlite3val2rb(sqlite3_value * val)
230
- {
231
- switch(sqlite3_value_type(val)) {
232
- case SQLITE_INTEGER:
233
- return LL2NUM(sqlite3_value_int64(val));
234
- break;
235
- case SQLITE_FLOAT:
236
- return rb_float_new(sqlite3_value_double(val));
237
- break;
238
- case SQLITE_TEXT:
239
- return rb_str_new2((const char *)sqlite3_value_text(val));
240
- break;
241
- case SQLITE_BLOB: {
242
- /* Sqlite warns calling sqlite3_value_bytes may invalidate pointer from sqlite3_value_blob,
243
- so we explicitly get the length before getting blob pointer.
244
- Note that rb_str_new apparently create string with ASCII-8BIT (BINARY) encoding,
245
- which is what we want, as blobs are binary
246
- */
247
- int len = sqlite3_value_bytes(val);
248
- return rb_str_new((const char *)sqlite3_value_blob(val), len);
249
- break;
455
+ static VALUE
456
+ last_insert_row_id(VALUE self)
457
+ {
458
+ sqlite3RubyPtr ctx;
459
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
460
+ REQUIRE_OPEN_DB(ctx);
461
+
462
+ return LL2NUM(sqlite3_last_insert_rowid(ctx->db));
463
+ }
464
+
465
+ VALUE
466
+ sqlite3val2rb(sqlite3_value *val)
467
+ {
468
+ VALUE rb_val;
469
+
470
+ switch (sqlite3_value_type(val)) {
471
+ case SQLITE_INTEGER:
472
+ rb_val = LL2NUM(sqlite3_value_int64(val));
473
+ break;
474
+ case SQLITE_FLOAT:
475
+ rb_val = rb_float_new(sqlite3_value_double(val));
476
+ break;
477
+ case SQLITE_TEXT: {
478
+ const char *text = (const char *)sqlite3_value_text(val);
479
+ int len = sqlite3_value_bytes(val);
480
+ rb_val = rb_utf8_str_new(text, len);
481
+ rb_obj_freeze(rb_val);
482
+ break;
483
+ }
484
+ case SQLITE_BLOB: {
485
+ int len = sqlite3_value_bytes(val);
486
+ rb_val = rb_str_new((const char *)sqlite3_value_blob(val), len);
487
+ rb_obj_freeze(rb_val);
488
+ break;
489
+ }
490
+ case SQLITE_NULL:
491
+ rb_val = Qnil;
492
+ break;
493
+ default:
494
+ rb_raise(rb_eRuntimeError, "bad type");
250
495
  }
251
- case SQLITE_NULL:
252
- return Qnil;
253
- break;
254
- default:
255
- rb_raise(rb_eRuntimeError, "bad type"); /* FIXME */
256
- }
257
- }
258
-
259
- void set_sqlite3_func_result(sqlite3_context * ctx, VALUE result)
260
- {
261
- switch(TYPE(result)) {
262
- case T_NIL:
263
- sqlite3_result_null(ctx);
264
- break;
265
- case T_FIXNUM:
266
- sqlite3_result_int64(ctx, (sqlite3_int64)FIX2LONG(result));
267
- break;
268
- case T_BIGNUM: {
496
+
497
+ return rb_val;
498
+ }
499
+
500
+ void
501
+ set_sqlite3_func_result(sqlite3_context *ctx, VALUE result)
502
+ {
503
+ switch (TYPE(result)) {
504
+ case T_NIL:
505
+ sqlite3_result_null(ctx);
506
+ break;
507
+ case T_FIXNUM:
508
+ sqlite3_result_int64(ctx, (sqlite3_int64)FIX2LONG(result));
509
+ break;
510
+ case T_BIGNUM: {
269
511
  #if SIZEOF_LONG < 8
270
- sqlite3_int64 num64;
512
+ sqlite3_int64 num64;
271
513
 
272
- if (bignum_to_int64(result, &num64)) {
273
- sqlite3_result_int64(ctx, num64);
274
- break;
275
- }
514
+ if (bignum_to_int64(result, &num64)) {
515
+ sqlite3_result_int64(ctx, num64);
516
+ break;
517
+ }
276
518
  #endif
519
+ }
520
+ case T_FLOAT:
521
+ sqlite3_result_double(ctx, NUM2DBL(result));
522
+ break;
523
+ case T_STRING:
524
+ if (CLASS_OF(result) == cSqlite3Blob
525
+ || rb_enc_get_index(result) == rb_ascii8bit_encindex()
526
+ ) {
527
+ sqlite3_result_blob(
528
+ ctx,
529
+ (const void *)StringValuePtr(result),
530
+ (int)RSTRING_LEN(result),
531
+ SQLITE_TRANSIENT
532
+ );
533
+ } else {
534
+ sqlite3_result_text(
535
+ ctx,
536
+ (const char *)StringValuePtr(result),
537
+ (int)RSTRING_LEN(result),
538
+ SQLITE_TRANSIENT
539
+ );
540
+ }
541
+ break;
542
+ default:
543
+ rb_raise(rb_eRuntimeError, "can't return %s",
544
+ rb_class2name(CLASS_OF(result)));
277
545
  }
278
- case T_FLOAT:
279
- sqlite3_result_double(ctx, NUM2DBL(result));
280
- break;
281
- case T_STRING:
282
- if(CLASS_OF(result) == cSqlite3Blob
283
- || rb_enc_get_index(result) == rb_ascii8bit_encindex()
284
- ) {
285
- sqlite3_result_blob(
286
- ctx,
287
- (const void *)StringValuePtr(result),
288
- (int)RSTRING_LEN(result),
289
- SQLITE_TRANSIENT
290
- );
291
- } else {
292
- sqlite3_result_text(
293
- ctx,
294
- (const char *)StringValuePtr(result),
295
- (int)RSTRING_LEN(result),
296
- SQLITE_TRANSIENT
297
- );
298
- }
299
- break;
300
- default:
301
- rb_raise(rb_eRuntimeError, "can't return %s",
302
- rb_class2name(CLASS_OF(result)));
303
- }
304
- }
305
-
306
- static void rb_sqlite3_func(sqlite3_context * ctx, int argc, sqlite3_value **argv)
307
- {
308
- VALUE callable = (VALUE)sqlite3_user_data(ctx);
309
- VALUE params = rb_ary_new2(argc);
310
- VALUE result;
311
- int i;
312
-
313
- if (argc > 0) {
314
- for(i = 0; i < argc; i++) {
315
- VALUE param = sqlite3val2rb(argv[i]);
316
- rb_ary_push(params, param);
546
+ }
547
+
548
+ typedef struct {
549
+ sqlite3_context *ctx;
550
+ int argc;
551
+ sqlite3_value **argv;
552
+ } rb_sqlite3_func_args_t;
553
+
554
+ static VALUE
555
+ rb_sqlite3_func_protected(VALUE func_args_value)
556
+ {
557
+ rb_sqlite3_func_args_t *args = (rb_sqlite3_func_args_t *)func_args_value;
558
+ VALUE callable = (VALUE)sqlite3_user_data(args->ctx);
559
+ VALUE params = rb_ary_new2(args->argc);
560
+ VALUE result;
561
+ int i;
562
+
563
+ for (i = 0; i < args->argc; i++) {
564
+ VALUE param = sqlite3val2rb(args->argv[i]);
565
+ rb_ary_push(params, param);
317
566
  }
318
- }
319
567
 
320
- result = rb_apply(callable, rb_intern("call"), params);
568
+ result = rb_apply(callable, rb_intern("call"), params);
569
+
570
+ set_sqlite3_func_result(args->ctx, result);
321
571
 
322
- set_sqlite3_func_result(ctx, result);
572
+ return Qnil;
573
+ }
574
+
575
+ static void
576
+ rb_sqlite3_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
577
+ {
578
+ rb_sqlite3_func_args_t args = { .ctx = ctx, .argc = argc, .argv = argv };
579
+ int exc_status;
580
+
581
+ rb_protect(rb_sqlite3_func_protected, (VALUE)&args, &exc_status);
582
+
583
+ if (exc_status) {
584
+ /* the user should never see this message, because Statement#step will
585
+ * re-raise the exception still in rb_errinfo */
586
+ sqlite3_result_error(ctx, "Ruby Exception occurred", -1);
587
+ }
323
588
  }
324
589
 
325
590
  #ifndef HAVE_RB_PROC_ARITY
326
- int rb_proc_arity(VALUE self)
591
+ int
592
+ rb_proc_arity(VALUE self)
327
593
  {
328
- return (int)NUM2INT(rb_funcall(self, rb_intern("arity"), 0));
594
+ return (int)NUM2INT(rb_funcall(self, rb_intern("arity"), 0));
329
595
  }
330
596
  #endif
331
597
 
@@ -334,33 +600,36 @@ int rb_proc_arity(VALUE self)
334
600
  * Define a function named +name+ with +args+ using TextRep bitflags +flags+. The arity of the block
335
601
  * will be used as the arity for the function defined.
336
602
  */
337
- static VALUE define_function_with_flags(VALUE self, VALUE name, VALUE flags)
603
+ static VALUE
604
+ define_function_with_flags(VALUE self, VALUE name, VALUE flags)
338
605
  {
339
- sqlite3RubyPtr ctx;
340
- VALUE block;
341
- int status;
606
+ sqlite3RubyPtr ctx;
607
+ VALUE block, functions;
608
+ int status;
342
609
 
343
- Data_Get_Struct(self, sqlite3Ruby, ctx);
344
- REQUIRE_OPEN_DB(ctx);
610
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
611
+ REQUIRE_OPEN_DB(ctx);
345
612
 
346
- block = rb_block_proc();
613
+ block = rb_block_proc();
347
614
 
348
- status = sqlite3_create_function(
349
- ctx->db,
350
- StringValuePtr(name),
351
- rb_proc_arity(block),
352
- NUM2INT(flags),
353
- (void *)block,
354
- rb_sqlite3_func,
355
- NULL,
356
- NULL
357
- );
615
+ status = sqlite3_create_function(
616
+ ctx->db,
617
+ StringValuePtr(name),
618
+ rb_proc_arity(block),
619
+ NUM2INT(flags),
620
+ (void *)block,
621
+ rb_sqlite3_func,
622
+ NULL,
623
+ NULL
624
+ );
358
625
 
359
- CHECK(ctx->db, status);
626
+ CHECK(ctx->db, status);
360
627
 
361
- rb_hash_aset(rb_iv_get(self, "@functions"), name, block);
628
+ functions = rb_iv_get(self, "@functions");
629
+ rb_ary_push(functions, block);
630
+ RB_OBJ_WRITE(self, &ctx->functions, functions);
362
631
 
363
- return self;
632
+ return self;
364
633
  }
365
634
 
366
635
  /* call-seq: define_function(name) { |args,...| }
@@ -368,24 +637,26 @@ static VALUE define_function_with_flags(VALUE self, VALUE name, VALUE flags)
368
637
  * Define a function named +name+ with +args+. The arity of the block
369
638
  * will be used as the arity for the function defined.
370
639
  */
371
- static VALUE define_function(VALUE self, VALUE name)
640
+ static VALUE
641
+ define_function(VALUE self, VALUE name)
372
642
  {
373
- return define_function_with_flags(self, name, INT2FIX(SQLITE_UTF8));
643
+ return define_function_with_flags(self, name, INT2FIX(SQLITE_UTF8));
374
644
  }
375
645
 
376
646
  /* call-seq: interrupt
377
647
  *
378
648
  * Interrupts the currently executing operation, causing it to abort.
379
649
  */
380
- static VALUE interrupt(VALUE self)
650
+ static VALUE
651
+ interrupt(VALUE self)
381
652
  {
382
- sqlite3RubyPtr ctx;
383
- Data_Get_Struct(self, sqlite3Ruby, ctx);
384
- REQUIRE_OPEN_DB(ctx);
653
+ sqlite3RubyPtr ctx;
654
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
655
+ REQUIRE_OPEN_DB(ctx);
385
656
 
386
- sqlite3_interrupt(ctx->db);
657
+ sqlite3_interrupt(ctx->db);
387
658
 
388
- return self;
659
+ return self;
389
660
  }
390
661
 
391
662
  /* call-seq: errmsg
@@ -393,13 +664,14 @@ static VALUE interrupt(VALUE self)
393
664
  * Return a string describing the last error to have occurred with this
394
665
  * database.
395
666
  */
396
- static VALUE errmsg(VALUE self)
667
+ static VALUE
668
+ errmsg(VALUE self)
397
669
  {
398
- sqlite3RubyPtr ctx;
399
- Data_Get_Struct(self, sqlite3Ruby, ctx);
400
- REQUIRE_OPEN_DB(ctx);
670
+ sqlite3RubyPtr ctx;
671
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
672
+ REQUIRE_OPEN_DB(ctx);
401
673
 
402
- return rb_str_new2(sqlite3_errmsg(ctx->db));
674
+ return rb_str_new2(sqlite3_errmsg(ctx->db));
403
675
  }
404
676
 
405
677
  /* call-seq: errcode
@@ -407,13 +679,14 @@ static VALUE errmsg(VALUE self)
407
679
  * Return an integer representing the last error to have occurred with this
408
680
  * database.
409
681
  */
410
- static VALUE errcode_(VALUE self)
682
+ static VALUE
683
+ errcode_(VALUE self)
411
684
  {
412
- sqlite3RubyPtr ctx;
413
- Data_Get_Struct(self, sqlite3Ruby, ctx);
414
- REQUIRE_OPEN_DB(ctx);
685
+ sqlite3RubyPtr ctx;
686
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
687
+ REQUIRE_OPEN_DB(ctx);
415
688
 
416
- return INT2NUM((long)sqlite3_errcode(ctx->db));
689
+ return INT2NUM(sqlite3_errcode(ctx->db));
417
690
  }
418
691
 
419
692
  /* call-seq: complete?(sql)
@@ -421,12 +694,14 @@ static VALUE errcode_(VALUE self)
421
694
  * Return +true+ if the string is a valid (ie, parsable) SQL statement, and
422
695
  * +false+ otherwise.
423
696
  */
424
- static VALUE complete_p(VALUE UNUSED(self), VALUE sql)
697
+ static VALUE
698
+ complete_p(VALUE UNUSED(self), VALUE sql)
425
699
  {
426
- if(sqlite3_complete(StringValuePtr(sql)))
427
- return Qtrue;
700
+ if (sqlite3_complete(StringValuePtr(sql))) {
701
+ return Qtrue;
702
+ }
428
703
 
429
- return Qfalse;
704
+ return Qfalse;
430
705
  }
431
706
 
432
707
  /* call-seq: changes
@@ -435,37 +710,38 @@ static VALUE complete_p(VALUE UNUSED(self), VALUE sql)
435
710
  * operation performed. Note that a "delete from table" without a where
436
711
  * clause will not affect this value.
437
712
  */
438
- static VALUE changes(VALUE self)
713
+ static VALUE
714
+ changes(VALUE self)
439
715
  {
440
- sqlite3RubyPtr ctx;
441
- Data_Get_Struct(self, sqlite3Ruby, ctx);
442
- REQUIRE_OPEN_DB(ctx);
716
+ sqlite3RubyPtr ctx;
717
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
718
+ REQUIRE_OPEN_DB(ctx);
443
719
 
444
- return INT2NUM(sqlite3_changes(ctx->db));
720
+ return INT2NUM(sqlite3_changes(ctx->db));
445
721
  }
446
722
 
447
- static int rb_sqlite3_auth(
723
+ static int
724
+ rb_sqlite3_auth(
448
725
  void *ctx,
449
726
  int _action,
450
- const char * _a,
451
- const char * _b,
452
- const char * _c,
453
- const char * _d)
727
+ const char *_a,
728
+ const char *_b,
729
+ const char *_c,
730
+ const char *_d)
454
731
  {
455
- VALUE self = (VALUE)ctx;
456
- VALUE action = INT2NUM(_action);
457
- VALUE a = _a ? rb_str_new2(_a) : Qnil;
458
- VALUE b = _b ? rb_str_new2(_b) : Qnil;
459
- VALUE c = _c ? rb_str_new2(_c) : Qnil;
460
- VALUE d = _d ? rb_str_new2(_d) : Qnil;
461
- VALUE callback = rb_iv_get(self, "@authorizer");
462
- VALUE result = rb_funcall(callback, rb_intern("call"), 5, action, a, b, c, d);
463
-
464
- if(T_FIXNUM == TYPE(result)) return (int)NUM2INT(result);
465
- if(Qtrue == result) return SQLITE_OK;
466
- if(Qfalse == result) return SQLITE_DENY;
467
-
468
- return SQLITE_IGNORE;
732
+ sqlite3RubyPtr db_ctx = (sqlite3RubyPtr)ctx;
733
+ VALUE action = INT2NUM(_action);
734
+ VALUE a = _a ? rb_str_new2(_a) : Qnil;
735
+ VALUE b = _b ? rb_str_new2(_b) : Qnil;
736
+ VALUE c = _c ? rb_str_new2(_c) : Qnil;
737
+ VALUE d = _d ? rb_str_new2(_d) : Qnil;
738
+ VALUE result = rb_funcall(db_ctx->authorizer, rb_intern("call"), 5, action, a, b, c, d);
739
+
740
+ if (T_FIXNUM == TYPE(result)) { return (int)NUM2INT(result); }
741
+ if (Qtrue == result) { return SQLITE_OK; }
742
+ if (Qfalse == result) { return SQLITE_DENY; }
743
+
744
+ return SQLITE_IGNORE;
469
745
  }
470
746
 
471
747
  /* call-seq: set_authorizer = auth
@@ -478,23 +754,24 @@ static int rb_sqlite3_auth(
478
754
  * is allowed to proceed. Returning 1 or false causes an authorization error to
479
755
  * occur, and returning 2 or nil causes the access to be silently denied.
480
756
  */
481
- static VALUE set_authorizer(VALUE self, VALUE authorizer)
757
+ static VALUE
758
+ set_authorizer(VALUE self, VALUE authorizer)
482
759
  {
483
- sqlite3RubyPtr ctx;
484
- int status;
760
+ sqlite3RubyPtr ctx;
761
+ int status;
485
762
 
486
- Data_Get_Struct(self, sqlite3Ruby, ctx);
487
- REQUIRE_OPEN_DB(ctx);
763
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
764
+ REQUIRE_OPEN_DB(ctx);
488
765
 
489
- status = sqlite3_set_authorizer(
490
- ctx->db, NIL_P(authorizer) ? NULL : rb_sqlite3_auth, (void *)self
491
- );
766
+ status = sqlite3_set_authorizer(
767
+ ctx->db, NIL_P(authorizer) ? NULL : rb_sqlite3_auth, (void *)ctx
768
+ );
492
769
 
493
- CHECK(ctx->db, status);
770
+ CHECK(ctx->db, status);
494
771
 
495
- rb_iv_set(self, "@authorizer", authorizer);
772
+ RB_OBJ_WRITE(self, &ctx->authorizer, authorizer);
496
773
 
497
- return self;
774
+ return self;
498
775
  }
499
776
 
500
777
  /* call-seq: db.busy_timeout = ms
@@ -507,15 +784,16 @@ static VALUE set_authorizer(VALUE self, VALUE authorizer)
507
784
  *
508
785
  * See also the mutually exclusive #busy_handler.
509
786
  */
510
- static VALUE set_busy_timeout(VALUE self, VALUE timeout)
787
+ static VALUE
788
+ set_busy_timeout(VALUE self, VALUE timeout)
511
789
  {
512
- sqlite3RubyPtr ctx;
513
- Data_Get_Struct(self, sqlite3Ruby, ctx);
514
- REQUIRE_OPEN_DB(ctx);
790
+ sqlite3RubyPtr ctx;
791
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
792
+ REQUIRE_OPEN_DB(ctx);
515
793
 
516
- CHECK(ctx->db, sqlite3_busy_timeout(ctx->db, (int)NUM2INT(timeout)));
794
+ CHECK(ctx->db, sqlite3_busy_timeout(ctx->db, (int)NUM2INT(timeout)));
517
795
 
518
- return self;
796
+ return self;
519
797
  }
520
798
 
521
799
  /* call-seq: db.extended_result_codes = true
@@ -523,42 +801,44 @@ static VALUE set_busy_timeout(VALUE self, VALUE timeout)
523
801
  * Enable extended result codes in SQLite. These result codes allow for more
524
802
  * detailed exception reporting, such a which type of constraint is violated.
525
803
  */
526
- static VALUE set_extended_result_codes(VALUE self, VALUE enable)
804
+ static VALUE
805
+ set_extended_result_codes(VALUE self, VALUE enable)
527
806
  {
528
- sqlite3RubyPtr ctx;
529
- Data_Get_Struct(self, sqlite3Ruby, ctx);
530
- REQUIRE_OPEN_DB(ctx);
807
+ sqlite3RubyPtr ctx;
808
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
809
+ REQUIRE_OPEN_DB(ctx);
531
810
 
532
- CHECK(ctx->db, sqlite3_extended_result_codes(ctx->db, RTEST(enable) ? 1 : 0));
811
+ CHECK(ctx->db, sqlite3_extended_result_codes(ctx->db, RTEST(enable) ? 1 : 0));
533
812
 
534
- return self;
813
+ return self;
535
814
  }
536
815
 
537
- int rb_comparator_func(void * ctx, int a_len, const void * a, int b_len, const void * b)
816
+ int
817
+ rb_comparator_func(void *ctx, int a_len, const void *a, int b_len, const void *b)
538
818
  {
539
- VALUE comparator;
540
- VALUE a_str;
541
- VALUE b_str;
542
- VALUE comparison;
543
- rb_encoding * internal_encoding;
819
+ VALUE comparator;
820
+ VALUE a_str;
821
+ VALUE b_str;
822
+ VALUE comparison;
823
+ rb_encoding *internal_encoding;
544
824
 
545
- internal_encoding = rb_default_internal_encoding();
825
+ internal_encoding = rb_default_internal_encoding();
546
826
 
547
- comparator = (VALUE)ctx;
548
- a_str = rb_str_new((const char *)a, a_len);
549
- b_str = rb_str_new((const char *)b, b_len);
827
+ comparator = (VALUE)ctx;
828
+ a_str = rb_str_new((const char *)a, a_len);
829
+ b_str = rb_str_new((const char *)b, b_len);
550
830
 
551
- rb_enc_associate_index(a_str, rb_utf8_encindex());
552
- rb_enc_associate_index(b_str, rb_utf8_encindex());
831
+ rb_enc_associate_index(a_str, rb_utf8_encindex());
832
+ rb_enc_associate_index(b_str, rb_utf8_encindex());
553
833
 
554
- if(internal_encoding) {
555
- a_str = rb_str_export_to_enc(a_str, internal_encoding);
556
- b_str = rb_str_export_to_enc(b_str, internal_encoding);
557
- }
834
+ if (internal_encoding) {
835
+ a_str = rb_str_export_to_enc(a_str, internal_encoding);
836
+ b_str = rb_str_export_to_enc(b_str, internal_encoding);
837
+ }
558
838
 
559
- comparison = rb_funcall(comparator, rb_intern("compare"), 2, a_str, b_str);
839
+ comparison = rb_funcall(comparator, rb_intern("compare"), 2, a_str, b_str);
560
840
 
561
- return NUM2INT(comparison);
841
+ return NUM2INT(comparison);
562
842
  }
563
843
 
564
844
  /* call-seq: db.collation(name, comparator)
@@ -568,159 +848,126 @@ int rb_comparator_func(void * ctx, int a_len, const void * a, int b_len, const v
568
848
  * two parameters and returns an integer less than, equal to, or greater than
569
849
  * 0.
570
850
  */
571
- static VALUE collation(VALUE self, VALUE name, VALUE comparator)
851
+ static VALUE
852
+ collation(VALUE self, VALUE name, VALUE comparator)
572
853
  {
573
- sqlite3RubyPtr ctx;
574
- Data_Get_Struct(self, sqlite3Ruby, ctx);
575
- REQUIRE_OPEN_DB(ctx);
576
-
577
- CHECK(ctx->db, sqlite3_create_collation(
578
- ctx->db,
579
- StringValuePtr(name),
580
- SQLITE_UTF8,
581
- (void *)comparator,
582
- NIL_P(comparator) ? NULL : rb_comparator_func));
583
-
584
- /* Make sure our comparator doesn't get garbage collected. */
585
- rb_hash_aset(rb_iv_get(self, "@collations"), name, comparator);
586
-
587
- return self;
854
+ sqlite3RubyPtr ctx;
855
+ VALUE collations;
856
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
857
+ REQUIRE_OPEN_DB(ctx);
858
+
859
+ CHECK(ctx->db, sqlite3_create_collation(
860
+ ctx->db,
861
+ StringValuePtr(name),
862
+ SQLITE_UTF8,
863
+ (void *)comparator,
864
+ NIL_P(comparator) ? NULL : rb_comparator_func));
865
+
866
+ /* sqlite holds a raw pointer to the comparator, so keep it alive and unmoved. */
867
+ collations = rb_iv_get(self, "@collations");
868
+ rb_hash_aset(collations, name, comparator);
869
+ RB_OBJ_WRITE(self, &ctx->collations, collations);
870
+
871
+ return self;
588
872
  }
589
873
 
590
874
  #ifdef HAVE_SQLITE3_LOAD_EXTENSION
591
- /* call-seq: db.load_extension(file)
592
- *
593
- * Loads an SQLite extension library from the named file. Extension
594
- * loading must be enabled using db.enable_load_extension(true) prior
595
- * to calling this API.
596
- */
597
- static VALUE load_extension(VALUE self, VALUE file)
875
+ static VALUE
876
+ load_extension_internal(VALUE self, VALUE file)
598
877
  {
599
- sqlite3RubyPtr ctx;
600
- int status;
601
- char *errMsg;
602
- VALUE errexp;
603
- Data_Get_Struct(self, sqlite3Ruby, ctx);
604
- REQUIRE_OPEN_DB(ctx);
878
+ sqlite3RubyPtr ctx;
879
+ int status;
880
+ char *errMsg;
605
881
 
606
- status = sqlite3_load_extension(ctx->db, StringValuePtr(file), 0, &errMsg);
607
- if (status != SQLITE_OK)
608
- {
609
- errexp = rb_exc_new2(rb_eRuntimeError, errMsg);
610
- sqlite3_free(errMsg);
611
- rb_exc_raise(errexp);
612
- }
882
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
883
+ REQUIRE_OPEN_DB(ctx);
613
884
 
614
- return self;
885
+ status = sqlite3_load_extension(ctx->db, StringValuePtr(file), 0, &errMsg);
886
+
887
+ CHECK_MSG(ctx->db, status, errMsg);
888
+
889
+ return self;
615
890
  }
616
891
  #endif
617
892
 
618
- #ifdef HAVE_SQLITE3_ENABLE_LOAD_EXTENSION
893
+ #if defined(HAVE_SQLITE3_ENABLE_LOAD_EXTENSION) && defined(HAVE_SQLITE3_LOAD_EXTENSION)
619
894
  /* call-seq: db.enable_load_extension(onoff)
620
895
  *
621
896
  * Enable or disable extension loading.
622
897
  */
623
- static VALUE enable_load_extension(VALUE self, VALUE onoff)
898
+ static VALUE
899
+ enable_load_extension(VALUE self, VALUE onoff)
624
900
  {
625
- sqlite3RubyPtr ctx;
626
- int onoffparam;
627
- Data_Get_Struct(self, sqlite3Ruby, ctx);
628
- REQUIRE_OPEN_DB(ctx);
629
-
630
- if (Qtrue == onoff) {
631
- onoffparam = 1;
632
- } else if (Qfalse == onoff) {
633
- onoffparam = 0;
634
- } else {
635
- onoffparam = (int)NUM2INT(onoff);
636
- }
901
+ sqlite3RubyPtr ctx;
902
+ int onoffparam;
903
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
904
+ REQUIRE_OPEN_DB(ctx);
905
+
906
+ if (Qtrue == onoff) {
907
+ onoffparam = 1;
908
+ } else if (Qfalse == onoff) {
909
+ onoffparam = 0;
910
+ } else {
911
+ onoffparam = (int)NUM2INT(onoff);
912
+ }
637
913
 
638
- CHECK(ctx->db, sqlite3_enable_load_extension(ctx->db, onoffparam));
914
+ CHECK(ctx->db, sqlite3_enable_load_extension(ctx->db, onoffparam));
639
915
 
640
- return self;
916
+ return self;
641
917
  }
642
918
  #endif
643
919
 
644
- static int enc_cb(void * _self, int UNUSED(columns), char **data, char **UNUSED(names))
645
- {
646
- VALUE self = (VALUE)_self;
647
-
648
- int index = rb_enc_find_index(data[0]);
649
- rb_encoding * e = rb_enc_from_index(index);
650
- rb_iv_set(self, "@encoding", rb_enc_from_encoding(e));
651
-
652
- return 0;
653
- }
654
-
655
- /* call-seq: db.encoding
656
- *
657
- * Fetch the encoding set on this database
658
- */
659
- static VALUE db_encoding(VALUE self)
660
- {
661
- sqlite3RubyPtr ctx;
662
- VALUE enc;
663
-
664
- Data_Get_Struct(self, sqlite3Ruby, ctx);
665
- REQUIRE_OPEN_DB(ctx);
666
-
667
- enc = rb_iv_get(self, "@encoding");
668
-
669
- if(NIL_P(enc)) {
670
- sqlite3_exec(ctx->db, "PRAGMA encoding", enc_cb, (void *)self, NULL);
671
- }
672
-
673
- return rb_iv_get(self, "@encoding");
674
- }
675
-
676
920
  /* call-seq: db.transaction_active?
677
921
  *
678
922
  * Returns +true+ if there is a transaction active, and +false+ otherwise.
679
923
  *
680
924
  */
681
- static VALUE transaction_active_p(VALUE self)
925
+ static VALUE
926
+ transaction_active_p(VALUE self)
682
927
  {
683
- sqlite3RubyPtr ctx;
684
- Data_Get_Struct(self, sqlite3Ruby, ctx);
685
- REQUIRE_OPEN_DB(ctx);
928
+ sqlite3RubyPtr ctx;
929
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
930
+ REQUIRE_OPEN_DB(ctx);
686
931
 
687
- return sqlite3_get_autocommit(ctx->db) ? Qfalse : Qtrue;
932
+ return sqlite3_get_autocommit(ctx->db) ? Qfalse : Qtrue;
688
933
  }
689
934
 
690
- static int hash_callback_function(VALUE callback_ary, int count, char **data, char **columns)
935
+ static int
936
+ hash_callback_function(VALUE callback_ary, int count, char **data, char **columns)
691
937
  {
692
- VALUE new_hash = rb_hash_new();
693
- int i;
694
-
695
- for (i = 0; i < count; i++) {
696
- if (data[i] == NULL) {
697
- rb_hash_aset(new_hash, rb_str_new_cstr(columns[i]), Qnil);
698
- } else {
699
- rb_hash_aset(new_hash, rb_str_new_cstr(columns[i]), rb_str_new_cstr(data[i]));
938
+ VALUE new_hash = rb_hash_new();
939
+ int i;
940
+
941
+ for (i = 0; i < count; i++) {
942
+ if (data[i] == NULL) {
943
+ rb_hash_aset(new_hash, rb_str_new_cstr(columns[i]), Qnil);
944
+ } else {
945
+ rb_hash_aset(new_hash, rb_str_new_cstr(columns[i]), rb_str_new_cstr(data[i]));
946
+ }
700
947
  }
701
- }
702
948
 
703
- rb_ary_push(callback_ary, new_hash);
949
+ rb_ary_push(callback_ary, new_hash);
704
950
 
705
- return 0;
951
+ return 0;
706
952
  }
707
953
 
708
- static int regular_callback_function(VALUE callback_ary, int count, char **data, char **columns)
954
+ static int
955
+ regular_callback_function(VALUE callback_ary, int count, char **data, char **columns)
709
956
  {
710
- VALUE new_ary = rb_ary_new();
711
- int i;
712
-
713
- for (i = 0; i < count; i++) {
714
- if (data[i] == NULL) {
715
- rb_ary_push(new_ary, Qnil);
716
- } else {
717
- rb_ary_push(new_ary, rb_str_new_cstr(data[i]));
957
+ VALUE new_ary = rb_ary_new();
958
+ int i;
959
+
960
+ for (i = 0; i < count; i++) {
961
+ if (data[i] == NULL) {
962
+ rb_ary_push(new_ary, Qnil);
963
+ } else {
964
+ rb_ary_push(new_ary, rb_str_new_cstr(data[i]));
965
+ }
718
966
  }
719
- }
720
967
 
721
- rb_ary_push(callback_ary, new_ary);
968
+ rb_ary_push(callback_ary, new_ary);
722
969
 
723
- return 0;
970
+ return 0;
724
971
  }
725
972
 
726
973
 
@@ -732,31 +979,30 @@ static int regular_callback_function(VALUE callback_ary, int count, char **data,
732
979
  * so the user may parse values with a block.
733
980
  * If no query is made, an empty array will be returned.
734
981
  */
735
- static VALUE exec_batch(VALUE self, VALUE sql, VALUE results_as_hash)
982
+ static VALUE
983
+ exec_batch(VALUE self, VALUE sql, VALUE results_as_hash)
736
984
  {
737
- sqlite3RubyPtr ctx;
738
- int status;
739
- VALUE callback_ary = rb_ary_new();
740
- char *errMsg;
741
- VALUE errexp;
742
-
743
- Data_Get_Struct(self, sqlite3Ruby, ctx);
744
- REQUIRE_OPEN_DB(ctx);
745
-
746
- if(results_as_hash == Qtrue) {
747
- status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)hash_callback_function, (void*)callback_ary, &errMsg);
748
- } else {
749
- status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)regular_callback_function, (void*)callback_ary, &errMsg);
750
- }
985
+ sqlite3RubyPtr ctx;
986
+ int status;
987
+ VALUE callback_ary = rb_ary_new();
988
+ char *errMsg;
989
+
990
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
991
+ REQUIRE_OPEN_DB(ctx);
992
+
993
+ if (results_as_hash == Qtrue) {
994
+ status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)hash_callback_function,
995
+ (void *)callback_ary,
996
+ &errMsg);
997
+ } else {
998
+ status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)regular_callback_function,
999
+ (void *)callback_ary,
1000
+ &errMsg);
1001
+ }
751
1002
 
752
- if (status != SQLITE_OK)
753
- {
754
- errexp = rb_exc_new2(rb_eRuntimeError, errMsg);
755
- sqlite3_free(errMsg);
756
- rb_exc_raise(errexp);
757
- }
1003
+ CHECK_MSG(ctx->db, status, errMsg);
758
1004
 
759
- return callback_ary;
1005
+ return callback_ary;
760
1006
  }
761
1007
 
762
1008
  /* call-seq: db.db_filename(database_name)
@@ -764,88 +1010,102 @@ static VALUE exec_batch(VALUE self, VALUE sql, VALUE results_as_hash)
764
1010
  * Returns the file associated with +database_name+. Can return nil or an
765
1011
  * empty string if the database is temporary, or in-memory.
766
1012
  */
767
- static VALUE db_filename(VALUE self, VALUE db_name)
1013
+ static VALUE
1014
+ db_filename(VALUE self, VALUE db_name)
768
1015
  {
769
- sqlite3RubyPtr ctx;
770
- const char * fname;
771
- Data_Get_Struct(self, sqlite3Ruby, ctx);
772
- REQUIRE_OPEN_DB(ctx);
1016
+ sqlite3RubyPtr ctx;
1017
+ const char *fname;
1018
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
1019
+ REQUIRE_OPEN_DB(ctx);
773
1020
 
774
- fname = sqlite3_db_filename(ctx->db, StringValueCStr(db_name));
1021
+ fname = sqlite3_db_filename(ctx->db, StringValueCStr(db_name));
775
1022
 
776
- if(fname) return SQLITE3_UTF8_STR_NEW2(fname);
777
- return Qnil;
1023
+ if (fname) { return SQLITE3_UTF8_STR_NEW2(fname); }
1024
+ return Qnil;
778
1025
  }
779
1026
 
780
- static VALUE rb_sqlite3_open16(VALUE self, VALUE file)
1027
+ static VALUE
1028
+ rb_sqlite3_open16(VALUE self, VALUE file)
781
1029
  {
782
- int status;
783
- sqlite3RubyPtr ctx;
1030
+ int status;
1031
+ sqlite3RubyPtr ctx;
784
1032
 
785
- Data_Get_Struct(self, sqlite3Ruby, ctx);
1033
+ TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
1034
+ REQUIRE_CLOSED_DB(ctx);
786
1035
 
787
1036
  #if defined TAINTING_SUPPORT
788
1037
  #if defined StringValueCStr
789
- StringValuePtr(file);
790
- rb_check_safe_obj(file);
1038
+ StringValuePtr(file);
1039
+ rb_check_safe_obj(file);
791
1040
  #else
792
- Check_SafeStr(file);
1041
+ Check_SafeStr(file);
793
1042
  #endif
794
1043
  #endif
795
1044
 
796
- status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
1045
+ // sqlite3_open16 implicitly uses flags (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)
1046
+ // see https://www.sqlite.org/capi3ref.html#sqlite3_open
1047
+ // so we do not ever set SQLITE3_RB_DATABASE_READONLY in ctx->flags
1048
+ status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
797
1049
 
798
- CHECK(ctx->db, status)
1050
+ if (status != SQLITE_OK) {
1051
+ char *msg = sqlite3_mprintf("%s", sqlite3_errmsg(ctx->db));
1052
+ sqlite3_close_v2(ctx->db);
1053
+ ctx->db = NULL;
1054
+ CHECK_MSG(ctx->db, status, msg);
1055
+ }
799
1056
 
800
- return INT2NUM(status);
1057
+ return INT2NUM(status);
801
1058
  }
802
1059
 
803
- void init_sqlite3_database()
1060
+ void
1061
+ init_sqlite3_database(void)
804
1062
  {
805
1063
  #if 0
806
- VALUE mSqlite3 = rb_define_module("SQLite3");
1064
+ VALUE mSqlite3 = rb_define_module("SQLite3");
807
1065
  #endif
808
- cSqlite3Database = rb_define_class_under(mSqlite3, "Database", rb_cObject);
809
-
810
- rb_define_alloc_func(cSqlite3Database, allocate);
811
- rb_define_private_method(cSqlite3Database, "open_v2", rb_sqlite3_open_v2, 3);
812
- rb_define_private_method(cSqlite3Database, "open16", rb_sqlite3_open16, 1);
813
- rb_define_method(cSqlite3Database, "collation", collation, 2);
814
- rb_define_method(cSqlite3Database, "close", sqlite3_rb_close, 0);
815
- rb_define_method(cSqlite3Database, "closed?", closed_p, 0);
816
- rb_define_method(cSqlite3Database, "total_changes", total_changes, 0);
817
- rb_define_method(cSqlite3Database, "trace", trace, -1);
818
- rb_define_method(cSqlite3Database, "last_insert_row_id", last_insert_row_id, 0);
819
- rb_define_method(cSqlite3Database, "define_function", define_function, 1);
820
- rb_define_method(cSqlite3Database, "define_function_with_flags", define_function_with_flags, 2);
821
- /* public "define_aggregator" is now a shim around define_aggregator2
822
- * implemented in Ruby */
823
- rb_define_private_method(cSqlite3Database, "define_aggregator2", rb_sqlite3_define_aggregator2, 2);
824
- rb_define_private_method(cSqlite3Database, "disable_quirk_mode", rb_sqlite3_disable_quirk_mode, 0);
825
- rb_define_method(cSqlite3Database, "interrupt", interrupt, 0);
826
- rb_define_method(cSqlite3Database, "errmsg", errmsg, 0);
827
- rb_define_method(cSqlite3Database, "errcode", errcode_, 0);
828
- rb_define_method(cSqlite3Database, "complete?", complete_p, 1);
829
- rb_define_method(cSqlite3Database, "changes", changes, 0);
830
- rb_define_method(cSqlite3Database, "authorizer=", set_authorizer, 1);
831
- rb_define_method(cSqlite3Database, "busy_handler", busy_handler, -1);
832
- rb_define_method(cSqlite3Database, "busy_timeout=", set_busy_timeout, 1);
833
- rb_define_method(cSqlite3Database, "extended_result_codes=", set_extended_result_codes, 1);
834
- rb_define_method(cSqlite3Database, "transaction_active?", transaction_active_p, 0);
835
- rb_define_private_method(cSqlite3Database, "exec_batch", exec_batch, 2);
836
- rb_define_private_method(cSqlite3Database, "db_filename", db_filename, 1);
837
-
838
- #ifdef HAVE_SQLITE3_LOAD_EXTENSION
839
- rb_define_method(cSqlite3Database, "load_extension", load_extension, 1);
1066
+ cSqlite3Database = rb_define_class_under(mSqlite3, "Database", rb_cObject);
1067
+
1068
+ rb_define_alloc_func(cSqlite3Database, allocate);
1069
+ rb_define_private_method(cSqlite3Database, "open_v2", rb_sqlite3_open_v2, 3);
1070
+ rb_define_private_method(cSqlite3Database, "open16", rb_sqlite3_open16, 1);
1071
+ rb_define_method(cSqlite3Database, "collation", collation, 2);
1072
+ rb_define_method(cSqlite3Database, "close", sqlite3_rb_close, 0);
1073
+ rb_define_private_method(cSqlite3Database, "discard", sqlite3_rb_discard, 0);
1074
+ rb_define_method(cSqlite3Database, "closed?", closed_p, 0);
1075
+ rb_define_method(cSqlite3Database, "total_changes", total_changes, 0);
1076
+ rb_define_method(cSqlite3Database, "trace", trace, -1);
1077
+ rb_define_method(cSqlite3Database, "last_insert_row_id", last_insert_row_id, 0);
1078
+ rb_define_method(cSqlite3Database, "define_function", define_function, 1);
1079
+ rb_define_method(cSqlite3Database, "define_function_with_flags", define_function_with_flags, 2);
1080
+ /* public "define_aggregator" is now a shim around define_aggregator2
1081
+ * implemented in Ruby */
1082
+ rb_define_private_method(cSqlite3Database, "define_aggregator2", rb_sqlite3_define_aggregator2, 2);
1083
+ rb_define_private_method(cSqlite3Database, "disable_quirk_mode", rb_sqlite3_disable_quirk_mode, 0);
1084
+ rb_define_method(cSqlite3Database, "interrupt", interrupt, 0);
1085
+ rb_define_method(cSqlite3Database, "errmsg", errmsg, 0);
1086
+ rb_define_method(cSqlite3Database, "errcode", errcode_, 0);
1087
+ rb_define_method(cSqlite3Database, "complete?", complete_p, 1);
1088
+ rb_define_method(cSqlite3Database, "changes", changes, 0);
1089
+ rb_define_method(cSqlite3Database, "authorizer=", set_authorizer, 1);
1090
+ rb_define_method(cSqlite3Database, "busy_handler", busy_handler, -1);
1091
+ rb_define_method(cSqlite3Database, "busy_timeout=", set_busy_timeout, 1);
1092
+ #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1093
+ rb_define_method(cSqlite3Database, "statement_timeout=", set_statement_timeout, 1);
840
1094
  #endif
1095
+ rb_define_method(cSqlite3Database, "extended_result_codes=", set_extended_result_codes, 1);
1096
+ rb_define_method(cSqlite3Database, "transaction_active?", transaction_active_p, 0);
1097
+ rb_define_private_method(cSqlite3Database, "exec_batch", exec_batch, 2);
1098
+ rb_define_private_method(cSqlite3Database, "db_filename", db_filename, 1);
841
1099
 
1100
+ #ifdef HAVE_SQLITE3_LOAD_EXTENSION
1101
+ rb_define_private_method(cSqlite3Database, "load_extension_internal", load_extension_internal, 1);
842
1102
  #ifdef HAVE_SQLITE3_ENABLE_LOAD_EXTENSION
843
- rb_define_method(cSqlite3Database, "enable_load_extension", enable_load_extension, 1);
1103
+ rb_define_method(cSqlite3Database, "enable_load_extension", enable_load_extension, 1);
1104
+ #endif
844
1105
  #endif
845
1106
 
846
- rb_define_method(cSqlite3Database, "encoding", db_encoding, 0);
847
1107
 
848
- rb_sqlite3_aggregator_init();
1108
+ rb_sqlite3_aggregator_init();
849
1109
  }
850
1110
 
851
1111
  #ifdef _MSC_VER