sqlite3 1.6.3 → 2.10.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +615 -1
- data/CONTRIBUTING.md +103 -6
- data/{faq/faq.md → FAQ.md} +66 -79
- data/INSTALLATION.md +267 -0
- data/LICENSE +18 -22
- data/README.md +87 -124
- data/dependencies.yml +10 -11
- data/ext/sqlite3/aggregator.c +178 -178
- data/ext/sqlite3/aggregator.h +7 -4
- data/ext/sqlite3/backup.c +92 -65
- data/ext/sqlite3/backup.h +2 -2
- data/ext/sqlite3/database.c +964 -477
- data/ext/sqlite3/database.h +26 -4
- data/ext/sqlite3/exception.c +213 -94
- data/ext/sqlite3/exception.h +26 -1
- data/ext/sqlite3/extconf.rb +59 -29
- data/ext/sqlite3/sqlite3.c +190 -115
- data/ext/sqlite3/sqlite3_ruby.h +5 -2
- data/ext/sqlite3/statement.c +672 -297
- data/ext/sqlite3/statement.h +4 -3
- data/ext/sqlite3/timespec.h +29 -0
- data/lib/sqlite3/constants.rb +195 -47
- data/lib/sqlite3/database.rb +272 -225
- data/lib/sqlite3/errors.rb +54 -1
- data/lib/sqlite3/fork_safety.rb +66 -0
- data/lib/sqlite3/pragmas.rb +194 -141
- data/lib/sqlite3/resultset.rb +30 -111
- data/lib/sqlite3/statement.rb +105 -25
- data/lib/sqlite3/value.rb +17 -20
- data/lib/sqlite3/version.rb +2 -21
- data/lib/sqlite3/version_info.rb +17 -0
- data/lib/sqlite3.rb +8 -4
- data/ports/archives/sqlite-autoconf-3540100.tar.gz +0 -0
- metadata +16 -48
- data/API_CHANGES.md +0 -49
- data/ChangeLog.cvs +0 -88
- data/Gemfile +0 -11
- data/LICENSE-DEPENDENCIES +0 -20
- data/faq/faq.rb +0 -145
- data/faq/faq.yml +0 -426
- data/lib/sqlite3/translator.rb +0 -118
- data/ports/archives/sqlite-autoconf-3420000.tar.gz +0 -0
- data/test/helper.rb +0 -27
- data/test/test_backup.rb +0 -33
- data/test/test_collation.rb +0 -82
- data/test/test_database.rb +0 -569
- data/test/test_database_flags.rb +0 -95
- data/test/test_database_readonly.rb +0 -36
- data/test/test_database_readwrite.rb +0 -41
- data/test/test_deprecated.rb +0 -49
- data/test/test_encoding.rb +0 -165
- data/test/test_integration.rb +0 -507
- data/test/test_integration_aggregate.rb +0 -336
- data/test/test_integration_open_close.rb +0 -30
- data/test/test_integration_pending.rb +0 -115
- data/test/test_integration_resultset.rb +0 -142
- data/test/test_integration_statement.rb +0 -194
- data/test/test_pragmas.rb +0 -22
- data/test/test_result_set.rb +0 -47
- data/test/test_sqlite3.rb +0 -30
- data/test/test_statement.rb +0 -288
- data/test/test_statement_execute.rb +0 -39
data/ext/sqlite3/database.c
CHANGED
|
@@ -10,110 +10,255 @@
|
|
|
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
|
-
|
|
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)
|
|
16
61
|
{
|
|
17
|
-
|
|
18
|
-
|
|
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
|
+
static void
|
|
78
|
+
database_mark(void *ctx)
|
|
79
|
+
{
|
|
80
|
+
sqlite3RubyPtr c = (sqlite3RubyPtr)ctx;
|
|
81
|
+
|
|
82
|
+
/* sqlite holds raw pointers to these, so they must not move. */
|
|
83
|
+
rb_gc_mark(c->busy_handler);
|
|
84
|
+
rb_gc_mark(c->trace_handler);
|
|
85
|
+
rb_gc_mark(c->authorizer);
|
|
86
|
+
|
|
87
|
+
rb_sqlite3_pin_array_and_contents(c->functions);
|
|
88
|
+
rb_sqlite3_pin_array_and_contents(c->collations);
|
|
89
|
+
rb_sqlite3_aggregator_pin(c->aggregators);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static void
|
|
93
|
+
deallocate(void *ctx)
|
|
94
|
+
{
|
|
95
|
+
close_or_discard_db((sqlite3RubyPtr)ctx);
|
|
96
|
+
xfree(ctx);
|
|
97
|
+
}
|
|
19
98
|
|
|
20
|
-
|
|
21
|
-
|
|
99
|
+
static size_t
|
|
100
|
+
database_memsize(const void *ctx)
|
|
101
|
+
{
|
|
102
|
+
const sqlite3RubyPtr c = (const sqlite3RubyPtr)ctx;
|
|
103
|
+
// NB: can't account for ctx->db because the type is incomplete.
|
|
104
|
+
return sizeof(*c);
|
|
22
105
|
}
|
|
23
106
|
|
|
24
|
-
static
|
|
107
|
+
static const rb_data_type_t database_type = {
|
|
108
|
+
.wrap_struct_name = "SQLite3::Database",
|
|
109
|
+
.function = {
|
|
110
|
+
.dmark = database_mark,
|
|
111
|
+
.dfree = deallocate,
|
|
112
|
+
.dsize = database_memsize,
|
|
113
|
+
},
|
|
114
|
+
.flags = RUBY_TYPED_WB_PROTECTED, // Not freed immediately because the dfree function do IOs.
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
static VALUE
|
|
118
|
+
allocate(VALUE klass)
|
|
25
119
|
{
|
|
26
|
-
|
|
27
|
-
|
|
120
|
+
sqlite3RubyPtr ctx;
|
|
121
|
+
VALUE object = TypedData_Make_Struct(klass, sqlite3Ruby, &database_type, ctx);
|
|
122
|
+
ctx->owner = getpid();
|
|
123
|
+
return object;
|
|
28
124
|
}
|
|
29
125
|
|
|
30
126
|
static char *
|
|
31
127
|
utf16_string_value_ptr(VALUE str)
|
|
32
128
|
{
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
129
|
+
VALUE utf16str, codepoints;
|
|
130
|
+
|
|
131
|
+
StringValue(str);
|
|
132
|
+
|
|
133
|
+
utf16str = rb_str_dup(str);
|
|
134
|
+
if (!UTF16_LE_P(utf16str) && !UTF16_BE_P(utf16str)) {
|
|
135
|
+
/* an untagged string (the utf16 option) holds native-byte-order UTF-16 */
|
|
136
|
+
const union { uint16_t u16; uint8_t u8[2]; } native = { 1 };
|
|
137
|
+
rb_enc_associate_index(utf16str, rb_enc_find_index(native.u8[0] ? "UTF-16LE" : "UTF-16BE"));
|
|
138
|
+
}
|
|
139
|
+
codepoints = rb_funcall(utf16str, rb_intern("codepoints"), 0);
|
|
140
|
+
if (RTEST(rb_funcall(codepoints, rb_intern("include?"), 1, INT2FIX(0)))) {
|
|
141
|
+
rb_raise(rb_eArgError, "string contains null char");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
rb_str_buf_cat(str, "\x00\x00", 2L);
|
|
145
|
+
return RSTRING_PTR(str);
|
|
36
146
|
}
|
|
37
147
|
|
|
38
|
-
|
|
148
|
+
sqlite3RubyPtr
|
|
149
|
+
sqlite3_database_unwrap(VALUE database)
|
|
150
|
+
{
|
|
151
|
+
sqlite3RubyPtr ctx;
|
|
152
|
+
TypedData_Get_Struct(database, sqlite3Ruby, &database_type, ctx);
|
|
153
|
+
return ctx;
|
|
154
|
+
}
|
|
39
155
|
|
|
40
|
-
static VALUE
|
|
156
|
+
static VALUE
|
|
157
|
+
rb_sqlite3_open_v2(VALUE self, VALUE file, VALUE mode, VALUE zvfs)
|
|
41
158
|
{
|
|
42
|
-
|
|
43
|
-
|
|
159
|
+
sqlite3RubyPtr ctx;
|
|
160
|
+
int status;
|
|
161
|
+
int flags;
|
|
44
162
|
|
|
45
|
-
|
|
163
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
164
|
+
REQUIRE_CLOSED_DB(ctx);
|
|
46
165
|
|
|
47
166
|
#if defined TAINTING_SUPPORT
|
|
48
167
|
# if defined StringValueCStr
|
|
49
|
-
|
|
50
|
-
|
|
168
|
+
StringValuePtr(file);
|
|
169
|
+
rb_check_safe_obj(file);
|
|
51
170
|
# else
|
|
52
|
-
|
|
171
|
+
Check_SafeStr(file);
|
|
53
172
|
# endif
|
|
54
173
|
#endif
|
|
55
174
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
175
|
+
flags = NUM2INT(mode);
|
|
176
|
+
status = sqlite3_open_v2(
|
|
177
|
+
StringValueCStr(file),
|
|
178
|
+
&ctx->db,
|
|
179
|
+
flags,
|
|
180
|
+
NIL_P(zvfs) ? NULL : StringValueCStr(zvfs)
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
if (status != SQLITE_OK) {
|
|
184
|
+
char *msg = sqlite3_mprintf("%s", sqlite3_errmsg(ctx->db));
|
|
185
|
+
sqlite3_close_v2(ctx->db);
|
|
186
|
+
ctx->db = NULL;
|
|
187
|
+
CHECK_MSG(ctx->db, status, msg);
|
|
188
|
+
}
|
|
62
189
|
|
|
63
|
-
|
|
190
|
+
if (flags & SQLITE_OPEN_READONLY) {
|
|
191
|
+
ctx->flags |= SQLITE3_RB_DATABASE_READONLY;
|
|
192
|
+
}
|
|
64
193
|
|
|
65
|
-
|
|
194
|
+
return self;
|
|
66
195
|
}
|
|
67
196
|
|
|
68
|
-
static VALUE
|
|
197
|
+
static VALUE
|
|
198
|
+
rb_sqlite3_disable_quirk_mode(VALUE self)
|
|
69
199
|
{
|
|
70
200
|
#if defined SQLITE_DBCONFIG_DQS_DDL
|
|
71
|
-
|
|
72
|
-
|
|
201
|
+
sqlite3RubyPtr ctx;
|
|
202
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
73
203
|
|
|
74
|
-
|
|
204
|
+
if (!ctx->db) { return Qfalse; }
|
|
75
205
|
|
|
76
|
-
|
|
77
|
-
|
|
206
|
+
sqlite3_db_config(ctx->db, SQLITE_DBCONFIG_DQS_DDL, 0, (void *)0);
|
|
207
|
+
sqlite3_db_config(ctx->db, SQLITE_DBCONFIG_DQS_DML, 0, (void *)0);
|
|
78
208
|
|
|
79
|
-
|
|
209
|
+
return Qtrue;
|
|
80
210
|
#else
|
|
81
|
-
|
|
211
|
+
return Qfalse;
|
|
82
212
|
#endif
|
|
83
213
|
}
|
|
84
214
|
|
|
85
|
-
/*
|
|
215
|
+
/*
|
|
216
|
+
* Close the database and release all associated resources.
|
|
86
217
|
*
|
|
87
|
-
*
|
|
218
|
+
* ⚠ Writable connections that are carried across a <tt>fork()</tt> are not completely
|
|
219
|
+
* closed. {Sqlite does not support forking}[https://www.sqlite.org/howtocorrupt.html],
|
|
220
|
+
* and fully closing a writable connection that has been carried across a fork may corrupt the
|
|
221
|
+
* database. Since it is an incomplete close, not all memory resources are freed, but this is safer
|
|
222
|
+
* than risking data loss.
|
|
223
|
+
*
|
|
224
|
+
* See rdoc-ref:adr/2024-09-fork-safety.md for more information on fork safety.
|
|
88
225
|
*/
|
|
89
|
-
static VALUE
|
|
226
|
+
static VALUE
|
|
227
|
+
sqlite3_rb_close(VALUE self)
|
|
90
228
|
{
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
229
|
+
sqlite3RubyPtr ctx;
|
|
230
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
231
|
+
|
|
232
|
+
close_or_discard_db(ctx);
|
|
94
233
|
|
|
95
|
-
|
|
96
|
-
|
|
234
|
+
return self;
|
|
235
|
+
}
|
|
97
236
|
|
|
98
|
-
|
|
237
|
+
/* private method, primarily for testing */
|
|
238
|
+
static VALUE
|
|
239
|
+
sqlite3_rb_discard(VALUE self)
|
|
240
|
+
{
|
|
241
|
+
sqlite3RubyPtr ctx;
|
|
242
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
99
243
|
|
|
100
|
-
|
|
244
|
+
discard_db(ctx);
|
|
101
245
|
|
|
102
|
-
|
|
246
|
+
return self;
|
|
103
247
|
}
|
|
104
248
|
|
|
105
249
|
/* call-seq: db.closed?
|
|
106
250
|
*
|
|
107
251
|
* Returns +true+ if this database instance has been closed (see #close).
|
|
108
252
|
*/
|
|
109
|
-
static VALUE
|
|
253
|
+
static VALUE
|
|
254
|
+
closed_p(VALUE self)
|
|
110
255
|
{
|
|
111
|
-
|
|
112
|
-
|
|
256
|
+
sqlite3RubyPtr ctx;
|
|
257
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
113
258
|
|
|
114
|
-
|
|
259
|
+
if (!ctx->db) { return Qtrue; }
|
|
115
260
|
|
|
116
|
-
|
|
261
|
+
return Qfalse;
|
|
117
262
|
}
|
|
118
263
|
|
|
119
264
|
/* call-seq: total_changes
|
|
@@ -121,20 +266,31 @@ static VALUE closed_p(VALUE self)
|
|
|
121
266
|
* Returns the total number of changes made to this database instance
|
|
122
267
|
* since it was opened.
|
|
123
268
|
*/
|
|
124
|
-
static VALUE
|
|
269
|
+
static VALUE
|
|
270
|
+
total_changes(VALUE self)
|
|
125
271
|
{
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
272
|
+
sqlite3RubyPtr ctx;
|
|
273
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
274
|
+
REQUIRE_OPEN_DB(ctx);
|
|
129
275
|
|
|
130
|
-
|
|
276
|
+
return INT2NUM(sqlite3_total_changes(ctx->db));
|
|
131
277
|
}
|
|
132
278
|
|
|
133
|
-
static
|
|
279
|
+
static VALUE
|
|
280
|
+
tracefunc_protected(VALUE args_value)
|
|
134
281
|
{
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
282
|
+
VALUE *args = (VALUE *)args_value;
|
|
283
|
+
return rb_funcall(args[0], rb_intern("call"), 1, rb_str_new2((const char *)args[1]));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
static void
|
|
287
|
+
tracefunc(void *data, const char *sql)
|
|
288
|
+
{
|
|
289
|
+
sqlite3RubyPtr ctx = (sqlite3RubyPtr)data;
|
|
290
|
+
VALUE args[] = { ctx->trace_handler, (VALUE)sql };
|
|
291
|
+
int failed;
|
|
292
|
+
|
|
293
|
+
rb_sqlite3_protect_callback(tracefunc_protected, (VALUE)args, &failed);
|
|
138
294
|
}
|
|
139
295
|
|
|
140
296
|
/* call-seq:
|
|
@@ -145,34 +301,46 @@ static void tracefunc(void * data, const char *sql)
|
|
|
145
301
|
* statement executed. The block receives one parameter: the SQL statement
|
|
146
302
|
* executed. If the block is +nil+, any existing tracer will be uninstalled.
|
|
147
303
|
*/
|
|
148
|
-
static VALUE
|
|
304
|
+
static VALUE
|
|
305
|
+
trace(int argc, VALUE *argv, VALUE self)
|
|
149
306
|
{
|
|
150
|
-
|
|
151
|
-
|
|
307
|
+
sqlite3RubyPtr ctx;
|
|
308
|
+
VALUE block;
|
|
152
309
|
|
|
153
|
-
|
|
154
|
-
|
|
310
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
311
|
+
REQUIRE_OPEN_DB(ctx);
|
|
155
312
|
|
|
156
|
-
|
|
313
|
+
rb_scan_args(argc, argv, "01", &block);
|
|
157
314
|
|
|
158
|
-
|
|
315
|
+
if (NIL_P(block) && rb_block_given_p()) { block = rb_block_proc(); }
|
|
159
316
|
|
|
160
|
-
|
|
317
|
+
RB_OBJ_WRITE(self, &ctx->trace_handler, block);
|
|
161
318
|
|
|
162
|
-
|
|
319
|
+
sqlite3_trace(ctx->db, NIL_P(block) ? NULL : tracefunc, (void *)ctx);
|
|
163
320
|
|
|
164
|
-
|
|
321
|
+
return self;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
static VALUE
|
|
325
|
+
rb_sqlite3_busy_handler_protected(VALUE args_value)
|
|
326
|
+
{
|
|
327
|
+
VALUE *args = (VALUE *)args_value;
|
|
328
|
+
return rb_funcall(args[0], rb_intern("call"), 1, INT2NUM((int)args[1]));
|
|
165
329
|
}
|
|
166
330
|
|
|
167
|
-
static int
|
|
331
|
+
static int
|
|
332
|
+
rb_sqlite3_busy_handler(void *context, int count)
|
|
168
333
|
{
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
334
|
+
sqlite3RubyPtr ctx = (sqlite3RubyPtr)context;
|
|
335
|
+
VALUE args[] = { ctx->busy_handler, (VALUE)count };
|
|
336
|
+
VALUE result;
|
|
337
|
+
int failed;
|
|
172
338
|
|
|
173
|
-
|
|
339
|
+
result = rb_sqlite3_protect_callback(rb_sqlite3_busy_handler_protected, (VALUE)args, &failed);
|
|
174
340
|
|
|
175
|
-
|
|
341
|
+
if (failed || Qfalse == result) { return 0; }
|
|
342
|
+
|
|
343
|
+
return 1;
|
|
176
344
|
}
|
|
177
345
|
|
|
178
346
|
/* call-seq:
|
|
@@ -189,27 +357,70 @@ static int rb_sqlite3_busy_handler(void * ctx, int count)
|
|
|
189
357
|
*
|
|
190
358
|
* See also the mutually exclusive #busy_timeout.
|
|
191
359
|
*/
|
|
192
|
-
static VALUE
|
|
360
|
+
static VALUE
|
|
361
|
+
busy_handler(int argc, VALUE *argv, VALUE self)
|
|
193
362
|
{
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
363
|
+
sqlite3RubyPtr ctx;
|
|
364
|
+
VALUE block;
|
|
365
|
+
int status;
|
|
366
|
+
|
|
367
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
368
|
+
REQUIRE_OPEN_DB(ctx);
|
|
369
|
+
|
|
370
|
+
rb_scan_args(argc, argv, "01", &block);
|
|
197
371
|
|
|
198
|
-
|
|
199
|
-
|
|
372
|
+
if (NIL_P(block) && rb_block_given_p()) { block = rb_block_proc(); }
|
|
373
|
+
RB_OBJ_WRITE(self, &ctx->busy_handler, block);
|
|
200
374
|
|
|
201
|
-
|
|
375
|
+
status = sqlite3_busy_handler(
|
|
376
|
+
ctx->db,
|
|
377
|
+
NIL_P(block) ? NULL : rb_sqlite3_busy_handler,
|
|
378
|
+
(void *)ctx
|
|
379
|
+
);
|
|
202
380
|
|
|
203
|
-
|
|
381
|
+
CHECK(ctx->db, status);
|
|
204
382
|
|
|
205
|
-
|
|
383
|
+
return self;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
static int
|
|
387
|
+
rb_sqlite3_statement_timeout(void *context)
|
|
388
|
+
{
|
|
389
|
+
sqlite3RubyPtr ctx = (sqlite3RubyPtr)context;
|
|
390
|
+
struct timespec currentTime;
|
|
391
|
+
clock_gettime(CLOCK_MONOTONIC, ¤tTime);
|
|
392
|
+
|
|
393
|
+
if (!timespecisset(&ctx->stmt_deadline)) {
|
|
394
|
+
struct timespec timeout;
|
|
395
|
+
timeout.tv_sec = ctx->stmt_timeout / 1000;
|
|
396
|
+
timeout.tv_nsec = (ctx->stmt_timeout % 1000) * 1000000L;
|
|
397
|
+
timespecadd(¤tTime, &timeout, &ctx->stmt_deadline);
|
|
398
|
+
} else if (timespecafter(¤tTime, &ctx->stmt_deadline)) {
|
|
399
|
+
return 1;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return 0;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/* call-seq: db.statement_timeout = ms
|
|
406
|
+
*
|
|
407
|
+
* Indicates that if a query lasts longer than the indicated number of
|
|
408
|
+
* milliseconds, SQLite should interrupt that query and return an error.
|
|
409
|
+
* By default, SQLite does not interrupt queries. To restore the default
|
|
410
|
+
* behavior, send 0 as the +ms+ parameter.
|
|
411
|
+
*/
|
|
412
|
+
static VALUE
|
|
413
|
+
set_statement_timeout(VALUE self, VALUE milliseconds)
|
|
414
|
+
{
|
|
415
|
+
sqlite3RubyPtr ctx;
|
|
416
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
206
417
|
|
|
207
|
-
|
|
208
|
-
|
|
418
|
+
ctx->stmt_timeout = NUM2INT(milliseconds);
|
|
419
|
+
int n = NUM2INT(milliseconds) == 0 ? -1 : 1000;
|
|
209
420
|
|
|
210
|
-
|
|
421
|
+
sqlite3_progress_handler(ctx->db, n, rb_sqlite3_statement_timeout, (void *)ctx);
|
|
211
422
|
|
|
212
|
-
|
|
423
|
+
return self;
|
|
213
424
|
}
|
|
214
425
|
|
|
215
426
|
/* call-seq: last_insert_row_id
|
|
@@ -217,115 +428,146 @@ static VALUE busy_handler(int argc, VALUE *argv, VALUE self)
|
|
|
217
428
|
* Obtains the unique row ID of the last row to be inserted by this Database
|
|
218
429
|
* instance.
|
|
219
430
|
*/
|
|
220
|
-
static VALUE
|
|
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
|
-
|
|
431
|
+
static VALUE
|
|
432
|
+
last_insert_row_id(VALUE self)
|
|
433
|
+
{
|
|
434
|
+
sqlite3RubyPtr ctx;
|
|
435
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
436
|
+
REQUIRE_OPEN_DB(ctx);
|
|
437
|
+
|
|
438
|
+
return LL2NUM(sqlite3_last_insert_rowid(ctx->db));
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
VALUE
|
|
442
|
+
sqlite3val2rb(sqlite3_value *val)
|
|
443
|
+
{
|
|
444
|
+
VALUE rb_val;
|
|
445
|
+
|
|
446
|
+
switch (sqlite3_value_type(val)) {
|
|
447
|
+
case SQLITE_INTEGER:
|
|
448
|
+
rb_val = LL2NUM(sqlite3_value_int64(val));
|
|
449
|
+
break;
|
|
450
|
+
case SQLITE_FLOAT:
|
|
451
|
+
rb_val = rb_float_new(sqlite3_value_double(val));
|
|
452
|
+
break;
|
|
453
|
+
case SQLITE_TEXT: {
|
|
454
|
+
const char *text = (const char *)sqlite3_value_text(val);
|
|
455
|
+
int len = sqlite3_value_bytes(val);
|
|
456
|
+
rb_val = rb_utf8_str_new(text, len);
|
|
457
|
+
rb_obj_freeze(rb_val);
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
case SQLITE_BLOB: {
|
|
461
|
+
int len = sqlite3_value_bytes(val);
|
|
462
|
+
rb_val = rb_str_new((const char *)sqlite3_value_blob(val), len);
|
|
463
|
+
rb_obj_freeze(rb_val);
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
case SQLITE_NULL:
|
|
467
|
+
rb_val = Qnil;
|
|
468
|
+
break;
|
|
469
|
+
default:
|
|
470
|
+
rb_raise(rb_eRuntimeError, "bad type");
|
|
250
471
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
sqlite3_result_int64(ctx, (sqlite3_int64)FIX2LONG(result));
|
|
267
|
-
break;
|
|
268
|
-
case T_BIGNUM: {
|
|
472
|
+
|
|
473
|
+
return rb_val;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
void
|
|
477
|
+
set_sqlite3_func_result(sqlite3_context *ctx, VALUE result)
|
|
478
|
+
{
|
|
479
|
+
switch (TYPE(result)) {
|
|
480
|
+
case T_NIL:
|
|
481
|
+
sqlite3_result_null(ctx);
|
|
482
|
+
break;
|
|
483
|
+
case T_FIXNUM:
|
|
484
|
+
sqlite3_result_int64(ctx, (sqlite3_int64)FIX2LONG(result));
|
|
485
|
+
break;
|
|
486
|
+
case T_BIGNUM: {
|
|
269
487
|
#if SIZEOF_LONG < 8
|
|
270
|
-
|
|
488
|
+
sqlite3_int64 num64;
|
|
271
489
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
490
|
+
if (bignum_to_int64(result, &num64)) {
|
|
491
|
+
sqlite3_result_int64(ctx, num64);
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
276
494
|
#endif
|
|
495
|
+
}
|
|
496
|
+
case T_FLOAT:
|
|
497
|
+
sqlite3_result_double(ctx, NUM2DBL(result));
|
|
498
|
+
break;
|
|
499
|
+
case T_STRING:
|
|
500
|
+
if (CLASS_OF(result) == cSqlite3Blob
|
|
501
|
+
|| rb_enc_get_index(result) == rb_ascii8bit_encindex()
|
|
502
|
+
) {
|
|
503
|
+
sqlite3_result_blob(
|
|
504
|
+
ctx,
|
|
505
|
+
(const void *)StringValuePtr(result),
|
|
506
|
+
RSTRING_LENINT(result),
|
|
507
|
+
SQLITE_TRANSIENT
|
|
508
|
+
);
|
|
509
|
+
} else {
|
|
510
|
+
sqlite3_result_text(
|
|
511
|
+
ctx,
|
|
512
|
+
(const char *)StringValuePtr(result),
|
|
513
|
+
RSTRING_LENINT(result),
|
|
514
|
+
SQLITE_TRANSIENT
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
break;
|
|
518
|
+
default:
|
|
519
|
+
rb_raise(rb_eRuntimeError, "can't return %s",
|
|
520
|
+
rb_class2name(CLASS_OF(result)));
|
|
277
521
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
typedef struct {
|
|
525
|
+
sqlite3_context *ctx;
|
|
526
|
+
int argc;
|
|
527
|
+
sqlite3_value **argv;
|
|
528
|
+
} rb_sqlite3_func_args_t;
|
|
529
|
+
|
|
530
|
+
static VALUE
|
|
531
|
+
rb_sqlite3_func_protected(VALUE func_args_value)
|
|
532
|
+
{
|
|
533
|
+
rb_sqlite3_func_args_t *args = (rb_sqlite3_func_args_t *)func_args_value;
|
|
534
|
+
VALUE callable = (VALUE)sqlite3_user_data(args->ctx);
|
|
535
|
+
VALUE params = rb_ary_new2(args->argc);
|
|
536
|
+
VALUE result;
|
|
537
|
+
int i;
|
|
538
|
+
|
|
539
|
+
for (i = 0; i < args->argc; i++) {
|
|
540
|
+
VALUE param = sqlite3val2rb(args->argv[i]);
|
|
541
|
+
rb_ary_push(params, param);
|
|
317
542
|
}
|
|
318
|
-
}
|
|
319
543
|
|
|
320
|
-
|
|
544
|
+
result = rb_apply(callable, rb_intern("call"), params);
|
|
545
|
+
|
|
546
|
+
set_sqlite3_func_result(args->ctx, result);
|
|
321
547
|
|
|
322
|
-
|
|
548
|
+
return Qnil;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
static void
|
|
552
|
+
rb_sqlite3_func(sqlite3_context *ctx, int argc, sqlite3_value **argv)
|
|
553
|
+
{
|
|
554
|
+
rb_sqlite3_func_args_t args = { .ctx = ctx, .argc = argc, .argv = argv };
|
|
555
|
+
int failed;
|
|
556
|
+
|
|
557
|
+
rb_sqlite3_protect_callback(rb_sqlite3_func_protected, (VALUE)&args, &failed);
|
|
558
|
+
|
|
559
|
+
if (failed) {
|
|
560
|
+
/* the user should never see this message, because Statement#step will
|
|
561
|
+
* resume the Ruby exception instead */
|
|
562
|
+
sqlite3_result_error(ctx, "Ruby Exception occurred", -1);
|
|
563
|
+
}
|
|
323
564
|
}
|
|
324
565
|
|
|
325
566
|
#ifndef HAVE_RB_PROC_ARITY
|
|
326
|
-
int
|
|
567
|
+
int
|
|
568
|
+
rb_proc_arity(VALUE self)
|
|
327
569
|
{
|
|
328
|
-
|
|
570
|
+
return (int)NUM2INT(rb_funcall(self, rb_intern("arity"), 0));
|
|
329
571
|
}
|
|
330
572
|
#endif
|
|
331
573
|
|
|
@@ -334,33 +576,36 @@ int rb_proc_arity(VALUE self)
|
|
|
334
576
|
* Define a function named +name+ with +args+ using TextRep bitflags +flags+. The arity of the block
|
|
335
577
|
* will be used as the arity for the function defined.
|
|
336
578
|
*/
|
|
337
|
-
static VALUE
|
|
579
|
+
static VALUE
|
|
580
|
+
define_function_with_flags(VALUE self, VALUE name, VALUE flags)
|
|
338
581
|
{
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
582
|
+
sqlite3RubyPtr ctx;
|
|
583
|
+
VALUE block, functions;
|
|
584
|
+
int status;
|
|
342
585
|
|
|
343
|
-
|
|
344
|
-
|
|
586
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
587
|
+
REQUIRE_OPEN_DB(ctx);
|
|
345
588
|
|
|
346
|
-
|
|
589
|
+
block = rb_block_proc();
|
|
347
590
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
591
|
+
status = sqlite3_create_function(
|
|
592
|
+
ctx->db,
|
|
593
|
+
StringValuePtr(name),
|
|
594
|
+
rb_proc_arity(block),
|
|
595
|
+
NUM2INT(flags),
|
|
596
|
+
(void *)block,
|
|
597
|
+
rb_sqlite3_func,
|
|
598
|
+
NULL,
|
|
599
|
+
NULL
|
|
600
|
+
);
|
|
358
601
|
|
|
359
|
-
|
|
602
|
+
CHECK(ctx->db, status);
|
|
360
603
|
|
|
361
|
-
|
|
604
|
+
functions = rb_iv_get(self, "@functions");
|
|
605
|
+
rb_ary_push(functions, block);
|
|
606
|
+
RB_OBJ_WRITE(self, &ctx->functions, functions);
|
|
362
607
|
|
|
363
|
-
|
|
608
|
+
return self;
|
|
364
609
|
}
|
|
365
610
|
|
|
366
611
|
/* call-seq: define_function(name) { |args,...| }
|
|
@@ -368,24 +613,26 @@ static VALUE define_function_with_flags(VALUE self, VALUE name, VALUE flags)
|
|
|
368
613
|
* Define a function named +name+ with +args+. The arity of the block
|
|
369
614
|
* will be used as the arity for the function defined.
|
|
370
615
|
*/
|
|
371
|
-
static VALUE
|
|
616
|
+
static VALUE
|
|
617
|
+
define_function(VALUE self, VALUE name)
|
|
372
618
|
{
|
|
373
|
-
|
|
619
|
+
return define_function_with_flags(self, name, INT2FIX(SQLITE_UTF8));
|
|
374
620
|
}
|
|
375
621
|
|
|
376
622
|
/* call-seq: interrupt
|
|
377
623
|
*
|
|
378
624
|
* Interrupts the currently executing operation, causing it to abort.
|
|
379
625
|
*/
|
|
380
|
-
static VALUE
|
|
626
|
+
static VALUE
|
|
627
|
+
interrupt(VALUE self)
|
|
381
628
|
{
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
629
|
+
sqlite3RubyPtr ctx;
|
|
630
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
631
|
+
REQUIRE_OPEN_DB(ctx);
|
|
385
632
|
|
|
386
|
-
|
|
633
|
+
sqlite3_interrupt(ctx->db);
|
|
387
634
|
|
|
388
|
-
|
|
635
|
+
return self;
|
|
389
636
|
}
|
|
390
637
|
|
|
391
638
|
/* call-seq: errmsg
|
|
@@ -393,13 +640,14 @@ static VALUE interrupt(VALUE self)
|
|
|
393
640
|
* Return a string describing the last error to have occurred with this
|
|
394
641
|
* database.
|
|
395
642
|
*/
|
|
396
|
-
static VALUE
|
|
643
|
+
static VALUE
|
|
644
|
+
errmsg(VALUE self)
|
|
397
645
|
{
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
646
|
+
sqlite3RubyPtr ctx;
|
|
647
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
648
|
+
REQUIRE_OPEN_DB(ctx);
|
|
401
649
|
|
|
402
|
-
|
|
650
|
+
return rb_str_new2(sqlite3_errmsg(ctx->db));
|
|
403
651
|
}
|
|
404
652
|
|
|
405
653
|
/* call-seq: errcode
|
|
@@ -407,13 +655,14 @@ static VALUE errmsg(VALUE self)
|
|
|
407
655
|
* Return an integer representing the last error to have occurred with this
|
|
408
656
|
* database.
|
|
409
657
|
*/
|
|
410
|
-
static VALUE
|
|
658
|
+
static VALUE
|
|
659
|
+
errcode_(VALUE self)
|
|
411
660
|
{
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
661
|
+
sqlite3RubyPtr ctx;
|
|
662
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
663
|
+
REQUIRE_OPEN_DB(ctx);
|
|
415
664
|
|
|
416
|
-
|
|
665
|
+
return INT2NUM(sqlite3_errcode(ctx->db));
|
|
417
666
|
}
|
|
418
667
|
|
|
419
668
|
/* call-seq: complete?(sql)
|
|
@@ -421,12 +670,14 @@ static VALUE errcode_(VALUE self)
|
|
|
421
670
|
* Return +true+ if the string is a valid (ie, parsable) SQL statement, and
|
|
422
671
|
* +false+ otherwise.
|
|
423
672
|
*/
|
|
424
|
-
static VALUE
|
|
673
|
+
static VALUE
|
|
674
|
+
complete_p(VALUE UNUSED(self), VALUE sql)
|
|
425
675
|
{
|
|
426
|
-
|
|
427
|
-
|
|
676
|
+
if (sqlite3_complete(StringValuePtr(sql))) {
|
|
677
|
+
return Qtrue;
|
|
678
|
+
}
|
|
428
679
|
|
|
429
|
-
|
|
680
|
+
return Qfalse;
|
|
430
681
|
}
|
|
431
682
|
|
|
432
683
|
/* call-seq: changes
|
|
@@ -435,37 +686,64 @@ static VALUE complete_p(VALUE UNUSED(self), VALUE sql)
|
|
|
435
686
|
* operation performed. Note that a "delete from table" without a where
|
|
436
687
|
* clause will not affect this value.
|
|
437
688
|
*/
|
|
438
|
-
static VALUE
|
|
689
|
+
static VALUE
|
|
690
|
+
changes(VALUE self)
|
|
439
691
|
{
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
692
|
+
sqlite3RubyPtr ctx;
|
|
693
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
694
|
+
REQUIRE_OPEN_DB(ctx);
|
|
443
695
|
|
|
444
|
-
|
|
696
|
+
return INT2NUM(sqlite3_changes(ctx->db));
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
typedef struct {
|
|
700
|
+
VALUE authorizer;
|
|
701
|
+
int action;
|
|
702
|
+
const char *a;
|
|
703
|
+
const char *b;
|
|
704
|
+
const char *c;
|
|
705
|
+
const char *d;
|
|
706
|
+
} rb_sqlite3_auth_args_t;
|
|
707
|
+
|
|
708
|
+
static VALUE
|
|
709
|
+
rb_sqlite3_auth_protected(VALUE auth_args_value)
|
|
710
|
+
{
|
|
711
|
+
rb_sqlite3_auth_args_t *args = (rb_sqlite3_auth_args_t *)auth_args_value;
|
|
712
|
+
VALUE action = INT2NUM(args->action);
|
|
713
|
+
VALUE a = args->a ? rb_str_new2(args->a) : Qnil;
|
|
714
|
+
VALUE b = args->b ? rb_str_new2(args->b) : Qnil;
|
|
715
|
+
VALUE c = args->c ? rb_str_new2(args->c) : Qnil;
|
|
716
|
+
VALUE d = args->d ? rb_str_new2(args->d) : Qnil;
|
|
717
|
+
VALUE result = rb_funcall(args->authorizer, rb_intern("call"), 5, action, a, b, c, d);
|
|
718
|
+
|
|
719
|
+
if (T_FIXNUM == TYPE(result)) { return INT2FIX(NUM2INT(result)); }
|
|
720
|
+
if (Qtrue == result) { return INT2FIX(SQLITE_OK); }
|
|
721
|
+
if (Qfalse == result) { return INT2FIX(SQLITE_DENY); }
|
|
722
|
+
|
|
723
|
+
return INT2FIX(SQLITE_IGNORE);
|
|
445
724
|
}
|
|
446
725
|
|
|
447
|
-
static int
|
|
726
|
+
static int
|
|
727
|
+
rb_sqlite3_auth(
|
|
448
728
|
void *ctx,
|
|
449
729
|
int _action,
|
|
450
|
-
const char *
|
|
451
|
-
const char *
|
|
452
|
-
const char *
|
|
453
|
-
const char *
|
|
730
|
+
const char *_a,
|
|
731
|
+
const char *_b,
|
|
732
|
+
const char *_c,
|
|
733
|
+
const char *_d)
|
|
454
734
|
{
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
VALUE callback = rb_iv_get(self, "@authorizer");
|
|
462
|
-
VALUE result = rb_funcall(callback, rb_intern("call"), 5, action, a, b, c, d);
|
|
735
|
+
sqlite3RubyPtr db_ctx = (sqlite3RubyPtr)ctx;
|
|
736
|
+
rb_sqlite3_auth_args_t args = {
|
|
737
|
+
.authorizer = db_ctx->authorizer, .action = _action, .a = _a, .b = _b, .c = _c, .d = _d
|
|
738
|
+
};
|
|
739
|
+
VALUE result;
|
|
740
|
+
int failed;
|
|
463
741
|
|
|
464
|
-
|
|
465
|
-
if(Qtrue == result) return SQLITE_OK;
|
|
466
|
-
if(Qfalse == result) return SQLITE_DENY;
|
|
742
|
+
result = rb_sqlite3_protect_callback(rb_sqlite3_auth_protected, (VALUE)&args, &failed);
|
|
467
743
|
|
|
468
|
-
|
|
744
|
+
if (failed) { return SQLITE_DENY; }
|
|
745
|
+
|
|
746
|
+
return FIX2INT(result);
|
|
469
747
|
}
|
|
470
748
|
|
|
471
749
|
/* call-seq: set_authorizer = auth
|
|
@@ -478,23 +756,24 @@ static int rb_sqlite3_auth(
|
|
|
478
756
|
* is allowed to proceed. Returning 1 or false causes an authorization error to
|
|
479
757
|
* occur, and returning 2 or nil causes the access to be silently denied.
|
|
480
758
|
*/
|
|
481
|
-
static VALUE
|
|
759
|
+
static VALUE
|
|
760
|
+
set_authorizer(VALUE self, VALUE authorizer)
|
|
482
761
|
{
|
|
483
|
-
|
|
484
|
-
|
|
762
|
+
sqlite3RubyPtr ctx;
|
|
763
|
+
int status;
|
|
485
764
|
|
|
486
|
-
|
|
487
|
-
|
|
765
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
766
|
+
REQUIRE_OPEN_DB(ctx);
|
|
488
767
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
768
|
+
status = sqlite3_set_authorizer(
|
|
769
|
+
ctx->db, NIL_P(authorizer) ? NULL : rb_sqlite3_auth, (void *)ctx
|
|
770
|
+
);
|
|
492
771
|
|
|
493
|
-
|
|
772
|
+
CHECK(ctx->db, status);
|
|
494
773
|
|
|
495
|
-
|
|
774
|
+
RB_OBJ_WRITE(self, &ctx->authorizer, authorizer);
|
|
496
775
|
|
|
497
|
-
|
|
776
|
+
return self;
|
|
498
777
|
}
|
|
499
778
|
|
|
500
779
|
/* call-seq: db.busy_timeout = ms
|
|
@@ -507,15 +786,16 @@ static VALUE set_authorizer(VALUE self, VALUE authorizer)
|
|
|
507
786
|
*
|
|
508
787
|
* See also the mutually exclusive #busy_handler.
|
|
509
788
|
*/
|
|
510
|
-
static VALUE
|
|
789
|
+
static VALUE
|
|
790
|
+
set_busy_timeout(VALUE self, VALUE timeout)
|
|
511
791
|
{
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
792
|
+
sqlite3RubyPtr ctx;
|
|
793
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
794
|
+
REQUIRE_OPEN_DB(ctx);
|
|
515
795
|
|
|
516
|
-
|
|
796
|
+
CHECK(ctx->db, sqlite3_busy_timeout(ctx->db, (int)NUM2INT(timeout)));
|
|
517
797
|
|
|
518
|
-
|
|
798
|
+
return self;
|
|
519
799
|
}
|
|
520
800
|
|
|
521
801
|
/* call-seq: db.extended_result_codes = true
|
|
@@ -523,42 +803,73 @@ static VALUE set_busy_timeout(VALUE self, VALUE timeout)
|
|
|
523
803
|
* Enable extended result codes in SQLite. These result codes allow for more
|
|
524
804
|
* detailed exception reporting, such a which type of constraint is violated.
|
|
525
805
|
*/
|
|
526
|
-
static VALUE
|
|
806
|
+
static VALUE
|
|
807
|
+
set_extended_result_codes(VALUE self, VALUE enable)
|
|
527
808
|
{
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
809
|
+
sqlite3RubyPtr ctx;
|
|
810
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
811
|
+
REQUIRE_OPEN_DB(ctx);
|
|
531
812
|
|
|
532
|
-
|
|
813
|
+
CHECK(ctx->db, sqlite3_extended_result_codes(ctx->db, RTEST(enable) ? 1 : 0));
|
|
533
814
|
|
|
534
|
-
|
|
815
|
+
return self;
|
|
535
816
|
}
|
|
536
817
|
|
|
537
|
-
|
|
818
|
+
typedef struct {
|
|
819
|
+
VALUE comparator;
|
|
820
|
+
int a_len;
|
|
821
|
+
const void *a;
|
|
822
|
+
int b_len;
|
|
823
|
+
const void *b;
|
|
824
|
+
int comparison;
|
|
825
|
+
} rb_comparator_args_t;
|
|
826
|
+
|
|
827
|
+
static VALUE
|
|
828
|
+
rb_comparator_protected(VALUE comparator_args_value)
|
|
538
829
|
{
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
830
|
+
rb_comparator_args_t *args = (rb_comparator_args_t *)comparator_args_value;
|
|
831
|
+
VALUE a_str;
|
|
832
|
+
VALUE b_str;
|
|
833
|
+
VALUE comparison;
|
|
834
|
+
rb_encoding *internal_encoding;
|
|
835
|
+
|
|
836
|
+
internal_encoding = rb_default_internal_encoding();
|
|
837
|
+
|
|
838
|
+
a_str = rb_str_new((const char *)args->a, args->a_len);
|
|
839
|
+
b_str = rb_str_new((const char *)args->b, args->b_len);
|
|
840
|
+
|
|
841
|
+
rb_enc_associate_index(a_str, rb_utf8_encindex());
|
|
842
|
+
rb_enc_associate_index(b_str, rb_utf8_encindex());
|
|
843
|
+
|
|
844
|
+
if (internal_encoding) {
|
|
845
|
+
a_str = rb_str_export_to_enc(a_str, internal_encoding);
|
|
846
|
+
b_str = rb_str_export_to_enc(b_str, internal_encoding);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
comparison = rb_funcall(args->comparator, rb_intern("compare"), 2, a_str, b_str);
|
|
544
850
|
|
|
545
|
-
|
|
851
|
+
args->comparison = NUM2INT(comparison);
|
|
546
852
|
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
b_str = rb_str_new((const char *)b, b_len);
|
|
853
|
+
return Qnil;
|
|
854
|
+
}
|
|
550
855
|
|
|
551
|
-
|
|
552
|
-
|
|
856
|
+
int
|
|
857
|
+
rb_comparator_func(void *ctx, int a_len, const void *a, int b_len, const void *b)
|
|
858
|
+
{
|
|
859
|
+
rb_comparator_args_t args = { .comparator = (VALUE)ctx, .a_len = a_len, .a = a, .b_len = b_len, .b = b };
|
|
860
|
+
int failed;
|
|
553
861
|
|
|
554
|
-
|
|
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
|
-
}
|
|
862
|
+
rb_sqlite3_protect_callback(rb_comparator_protected, (VALUE)&args, &failed);
|
|
558
863
|
|
|
559
|
-
|
|
864
|
+
/* sqlite has no way to report a comparator error */
|
|
865
|
+
return failed ? 0 : args.comparison;
|
|
866
|
+
}
|
|
560
867
|
|
|
561
|
-
|
|
868
|
+
static int
|
|
869
|
+
push_hash_value(VALUE UNUSED(key), VALUE value, VALUE ary)
|
|
870
|
+
{
|
|
871
|
+
rb_ary_push(ary, value);
|
|
872
|
+
return ST_CONTINUE;
|
|
562
873
|
}
|
|
563
874
|
|
|
564
875
|
/* call-seq: db.collation(name, comparator)
|
|
@@ -568,159 +879,173 @@ int rb_comparator_func(void * ctx, int a_len, const void * a, int b_len, const v
|
|
|
568
879
|
* two parameters and returns an integer less than, equal to, or greater than
|
|
569
880
|
* 0.
|
|
570
881
|
*/
|
|
571
|
-
static VALUE
|
|
882
|
+
static VALUE
|
|
883
|
+
collation(VALUE self, VALUE name, VALUE comparator)
|
|
572
884
|
{
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
885
|
+
sqlite3RubyPtr ctx;
|
|
886
|
+
VALUE registry, key, comparators;
|
|
887
|
+
char *key_ptr;
|
|
888
|
+
long i;
|
|
889
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
890
|
+
REQUIRE_OPEN_DB(ctx);
|
|
891
|
+
|
|
892
|
+
/* sqlite holds a raw pointer to the comparator, so keep it alive and
|
|
893
|
+
* unmoved. The registry is private because callers can mutate the public
|
|
894
|
+
* @collations hash, and the snapshot is an array so database_mark can pin
|
|
895
|
+
* it without iterating a hash. sqlite matches collation names without
|
|
896
|
+
* regard to ASCII case, so the registry does too. The key and registry
|
|
897
|
+
* are prepared before sqlite takes the pointer, and the snapshot is
|
|
898
|
+
* published before the public hash, whose write can raise. */
|
|
899
|
+
key = rb_str_new_cstr(StringValuePtr(name));
|
|
900
|
+
key_ptr = RSTRING_PTR(key);
|
|
901
|
+
for (i = 0; i < RSTRING_LEN(key); i++) {
|
|
902
|
+
if (key_ptr[i] >= 'A' && key_ptr[i] <= 'Z') { key_ptr[i] += 'a' - 'A'; }
|
|
903
|
+
}
|
|
904
|
+
if (!rb_ivar_defined(self, rb_intern("-collations"))) {
|
|
905
|
+
rb_iv_set(self, "-collations", rb_hash_new());
|
|
906
|
+
}
|
|
907
|
+
registry = rb_iv_get(self, "-collations");
|
|
908
|
+
|
|
909
|
+
CHECK(ctx->db, sqlite3_create_collation(
|
|
910
|
+
ctx->db,
|
|
911
|
+
RSTRING_PTR(name),
|
|
912
|
+
SQLITE_UTF8,
|
|
913
|
+
(void *)comparator,
|
|
914
|
+
NIL_P(comparator) ? NULL : rb_comparator_func));
|
|
576
915
|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
(void *)comparator,
|
|
582
|
-
NIL_P(comparator) ? NULL : rb_comparator_func));
|
|
916
|
+
rb_hash_aset(registry, key, comparator);
|
|
917
|
+
comparators = rb_ary_new();
|
|
918
|
+
rb_hash_foreach(registry, push_hash_value, comparators);
|
|
919
|
+
RB_OBJ_WRITE(self, &ctx->collations, comparators);
|
|
583
920
|
|
|
584
|
-
|
|
585
|
-
rb_hash_aset(rb_iv_get(self, "@collations"), name, comparator);
|
|
921
|
+
rb_hash_aset(rb_iv_get(self, "@collations"), name, comparator);
|
|
586
922
|
|
|
587
|
-
|
|
923
|
+
return self;
|
|
588
924
|
}
|
|
589
925
|
|
|
590
926
|
#ifdef HAVE_SQLITE3_LOAD_EXTENSION
|
|
591
|
-
|
|
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)
|
|
927
|
+
static VALUE
|
|
928
|
+
load_extension_internal(VALUE self, VALUE file)
|
|
598
929
|
{
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
VALUE errexp;
|
|
603
|
-
Data_Get_Struct(self, sqlite3Ruby, ctx);
|
|
604
|
-
REQUIRE_OPEN_DB(ctx);
|
|
930
|
+
sqlite3RubyPtr ctx;
|
|
931
|
+
int status;
|
|
932
|
+
char *errMsg;
|
|
605
933
|
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
{
|
|
609
|
-
errexp = rb_exc_new2(rb_eRuntimeError, errMsg);
|
|
610
|
-
sqlite3_free(errMsg);
|
|
611
|
-
rb_exc_raise(errexp);
|
|
612
|
-
}
|
|
934
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
935
|
+
REQUIRE_OPEN_DB(ctx);
|
|
613
936
|
|
|
614
|
-
|
|
937
|
+
status = sqlite3_load_extension(ctx->db, StringValuePtr(file), 0, &errMsg);
|
|
938
|
+
|
|
939
|
+
CHECK_MSG(ctx->db, status, errMsg);
|
|
940
|
+
|
|
941
|
+
return self;
|
|
615
942
|
}
|
|
616
943
|
#endif
|
|
617
944
|
|
|
618
|
-
#
|
|
945
|
+
#if defined(HAVE_SQLITE3_ENABLE_LOAD_EXTENSION) && defined(HAVE_SQLITE3_LOAD_EXTENSION)
|
|
619
946
|
/* call-seq: db.enable_load_extension(onoff)
|
|
620
947
|
*
|
|
621
948
|
* Enable or disable extension loading.
|
|
622
949
|
*/
|
|
623
|
-
static VALUE
|
|
950
|
+
static VALUE
|
|
951
|
+
enable_load_extension(VALUE self, VALUE onoff)
|
|
624
952
|
{
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
953
|
+
sqlite3RubyPtr ctx;
|
|
954
|
+
int onoffparam;
|
|
955
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
956
|
+
REQUIRE_OPEN_DB(ctx);
|
|
957
|
+
|
|
958
|
+
if (Qtrue == onoff) {
|
|
959
|
+
onoffparam = 1;
|
|
960
|
+
} else if (Qfalse == onoff) {
|
|
961
|
+
onoffparam = 0;
|
|
962
|
+
} else {
|
|
963
|
+
onoffparam = (int)NUM2INT(onoff);
|
|
964
|
+
}
|
|
637
965
|
|
|
638
|
-
|
|
966
|
+
CHECK(ctx->db, sqlite3_enable_load_extension(ctx->db, onoffparam));
|
|
639
967
|
|
|
640
|
-
|
|
968
|
+
return self;
|
|
641
969
|
}
|
|
642
970
|
#endif
|
|
643
971
|
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
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
|
|
972
|
+
/* call-seq: db.transaction_active?
|
|
973
|
+
*
|
|
974
|
+
* Returns +true+ if there is a transaction active, and +false+ otherwise.
|
|
656
975
|
*
|
|
657
|
-
* Fetch the encoding set on this database
|
|
658
976
|
*/
|
|
659
|
-
static VALUE
|
|
977
|
+
static VALUE
|
|
978
|
+
transaction_active_p(VALUE self)
|
|
660
979
|
{
|
|
661
|
-
|
|
662
|
-
|
|
980
|
+
sqlite3RubyPtr ctx;
|
|
981
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
982
|
+
REQUIRE_OPEN_DB(ctx);
|
|
663
983
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
enc = rb_iv_get(self, "@encoding");
|
|
984
|
+
return sqlite3_get_autocommit(ctx->db) ? Qfalse : Qtrue;
|
|
985
|
+
}
|
|
668
986
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
987
|
+
typedef struct {
|
|
988
|
+
VALUE callback_ary;
|
|
989
|
+
VALUE (*build_row)(VALUE);
|
|
990
|
+
} rb_sqlite3_exec_batch_t;
|
|
672
991
|
|
|
673
|
-
|
|
674
|
-
|
|
992
|
+
typedef struct {
|
|
993
|
+
VALUE callback_ary;
|
|
994
|
+
int count;
|
|
995
|
+
char **data;
|
|
996
|
+
char **columns;
|
|
997
|
+
} rb_sqlite3_exec_row_t;
|
|
675
998
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
* Returns +true+ if there is a transaction active, and +false+ otherwise.
|
|
679
|
-
*
|
|
680
|
-
*/
|
|
681
|
-
static VALUE transaction_active_p(VALUE self)
|
|
999
|
+
static VALUE
|
|
1000
|
+
hash_callback_protected(VALUE row_value)
|
|
682
1001
|
{
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
1002
|
+
rb_sqlite3_exec_row_t *row = (rb_sqlite3_exec_row_t *)row_value;
|
|
1003
|
+
VALUE new_hash = rb_hash_new();
|
|
1004
|
+
int i;
|
|
1005
|
+
|
|
1006
|
+
for (i = 0; i < row->count; i++) {
|
|
1007
|
+
if (row->data[i] == NULL) {
|
|
1008
|
+
rb_hash_aset(new_hash, rb_str_new_cstr(row->columns[i]), Qnil);
|
|
1009
|
+
} else {
|
|
1010
|
+
rb_hash_aset(new_hash, rb_str_new_cstr(row->columns[i]), rb_str_new_cstr(row->data[i]));
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
rb_ary_push(row->callback_ary, new_hash);
|
|
686
1015
|
|
|
687
|
-
|
|
1016
|
+
return Qnil;
|
|
688
1017
|
}
|
|
689
1018
|
|
|
690
|
-
static
|
|
1019
|
+
static VALUE
|
|
1020
|
+
regular_callback_protected(VALUE row_value)
|
|
691
1021
|
{
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
1022
|
+
rb_sqlite3_exec_row_t *row = (rb_sqlite3_exec_row_t *)row_value;
|
|
1023
|
+
VALUE new_ary = rb_ary_new();
|
|
1024
|
+
int i;
|
|
1025
|
+
|
|
1026
|
+
for (i = 0; i < row->count; i++) {
|
|
1027
|
+
if (row->data[i] == NULL) {
|
|
1028
|
+
rb_ary_push(new_ary, Qnil);
|
|
1029
|
+
} else {
|
|
1030
|
+
rb_ary_push(new_ary, rb_str_new_cstr(row->data[i]));
|
|
1031
|
+
}
|
|
700
1032
|
}
|
|
701
|
-
}
|
|
702
1033
|
|
|
703
|
-
|
|
1034
|
+
rb_ary_push(row->callback_ary, new_ary);
|
|
704
1035
|
|
|
705
|
-
|
|
1036
|
+
return Qnil;
|
|
706
1037
|
}
|
|
707
1038
|
|
|
708
|
-
static int
|
|
1039
|
+
static int
|
|
1040
|
+
exec_batch_callback_function(void *batch_ptr, int count, char **data, char **columns)
|
|
709
1041
|
{
|
|
710
|
-
|
|
711
|
-
|
|
1042
|
+
rb_sqlite3_exec_batch_t *batch = (rb_sqlite3_exec_batch_t *)batch_ptr;
|
|
1043
|
+
rb_sqlite3_exec_row_t row = { .callback_ary = batch->callback_ary, .count = count, .data = data, .columns = columns };
|
|
1044
|
+
int failed;
|
|
712
1045
|
|
|
713
|
-
|
|
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]));
|
|
718
|
-
}
|
|
719
|
-
}
|
|
1046
|
+
rb_sqlite3_protect_callback(batch->build_row, (VALUE)&row, &failed);
|
|
720
1047
|
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
return 0;
|
|
1048
|
+
return failed;
|
|
724
1049
|
}
|
|
725
1050
|
|
|
726
1051
|
|
|
@@ -732,31 +1057,38 @@ static int regular_callback_function(VALUE callback_ary, int count, char **data,
|
|
|
732
1057
|
* so the user may parse values with a block.
|
|
733
1058
|
* If no query is made, an empty array will be returned.
|
|
734
1059
|
*/
|
|
735
|
-
static VALUE
|
|
1060
|
+
static VALUE
|
|
1061
|
+
exec_batch(VALUE self, VALUE sql, VALUE results_as_hash)
|
|
736
1062
|
{
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
1063
|
+
sqlite3RubyPtr ctx;
|
|
1064
|
+
int status;
|
|
1065
|
+
VALUE callback_ary = rb_ary_new();
|
|
1066
|
+
rb_sqlite3_exec_batch_t batch = {
|
|
1067
|
+
.callback_ary = callback_ary,
|
|
1068
|
+
.build_row = results_as_hash == Qtrue ? hash_callback_protected : regular_callback_protected
|
|
1069
|
+
};
|
|
1070
|
+
rb_sqlite3_callback_frame_t frame;
|
|
1071
|
+
int callback_state;
|
|
1072
|
+
const char *sql_cstr;
|
|
1073
|
+
char *errMsg;
|
|
1074
|
+
|
|
1075
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
1076
|
+
REQUIRE_OPEN_DB(ctx);
|
|
1077
|
+
|
|
1078
|
+
sql_cstr = StringValuePtr(sql);
|
|
1079
|
+
timespecclear(&ctx->stmt_deadline);
|
|
1080
|
+
|
|
1081
|
+
rb_sqlite3_push_callback_frame(&frame);
|
|
1082
|
+
status = sqlite3_exec(ctx->db, sql_cstr, exec_batch_callback_function, (void *)&batch, &errMsg);
|
|
1083
|
+
|
|
1084
|
+
if ((callback_state = rb_sqlite3_pop_callback_frame(&frame))) {
|
|
1085
|
+
sqlite3_free(errMsg);
|
|
1086
|
+
rb_jump_tag(callback_state);
|
|
1087
|
+
}
|
|
751
1088
|
|
|
752
|
-
|
|
753
|
-
{
|
|
754
|
-
errexp = rb_exc_new2(rb_eRuntimeError, errMsg);
|
|
755
|
-
sqlite3_free(errMsg);
|
|
756
|
-
rb_exc_raise(errexp);
|
|
757
|
-
}
|
|
1089
|
+
CHECK_MSG(ctx->db, status, errMsg);
|
|
758
1090
|
|
|
759
|
-
|
|
1091
|
+
return callback_ary;
|
|
760
1092
|
}
|
|
761
1093
|
|
|
762
1094
|
/* call-seq: db.db_filename(database_name)
|
|
@@ -764,88 +1096,243 @@ static VALUE exec_batch(VALUE self, VALUE sql, VALUE results_as_hash)
|
|
|
764
1096
|
* Returns the file associated with +database_name+. Can return nil or an
|
|
765
1097
|
* empty string if the database is temporary, or in-memory.
|
|
766
1098
|
*/
|
|
767
|
-
static VALUE
|
|
1099
|
+
static VALUE
|
|
1100
|
+
db_filename(VALUE self, VALUE db_name)
|
|
768
1101
|
{
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
1102
|
+
sqlite3RubyPtr ctx;
|
|
1103
|
+
const char *fname;
|
|
1104
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
1105
|
+
REQUIRE_OPEN_DB(ctx);
|
|
773
1106
|
|
|
774
|
-
|
|
1107
|
+
fname = sqlite3_db_filename(ctx->db, StringValueCStr(db_name));
|
|
775
1108
|
|
|
776
|
-
|
|
777
|
-
|
|
1109
|
+
if (fname) { return SQLITE3_UTF8_STR_NEW2(fname); }
|
|
1110
|
+
return Qnil;
|
|
778
1111
|
}
|
|
779
1112
|
|
|
780
|
-
|
|
1113
|
+
#ifdef HAVE_SQLITE3_SERIALIZE
|
|
1114
|
+
/* call-seq: db.serialize(database_name = "main")
|
|
1115
|
+
*
|
|
1116
|
+
* Serialize the database into a binary string.
|
|
1117
|
+
*
|
|
1118
|
+
* [database_name] The name of the database to serialize. The default value
|
|
1119
|
+
* is +main+ which is the main database file. For ATTACHed
|
|
1120
|
+
* databases, use the name given in the ATTACH statement.
|
|
1121
|
+
*
|
|
1122
|
+
* Returns the contents of the database as a binary string. For an ordinary
|
|
1123
|
+
* on-disk database file, this is just a copy of the disk file. For an
|
|
1124
|
+
* in-memory database or a "TEMP" database, this is the same sequence of bytes
|
|
1125
|
+
* which would be written to disk if that database were backed up to disk.
|
|
1126
|
+
*
|
|
1127
|
+
* Note: This method will only be defined if SQLite was compiled without
|
|
1128
|
+
* +SQLITE_OMIT_DESERIALIZE+.
|
|
1129
|
+
*
|
|
1130
|
+
* See also: https://www.sqlite.org/c3ref/serialize.html
|
|
1131
|
+
*/
|
|
1132
|
+
static VALUE
|
|
1133
|
+
serialize(int argc, VALUE *argv, VALUE self)
|
|
781
1134
|
{
|
|
782
|
-
|
|
783
|
-
|
|
1135
|
+
sqlite3RubyPtr ctx;
|
|
1136
|
+
VALUE db_name;
|
|
1137
|
+
const char *zSchema;
|
|
1138
|
+
sqlite3_int64 size;
|
|
1139
|
+
unsigned char *data;
|
|
1140
|
+
VALUE result;
|
|
1141
|
+
|
|
1142
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
1143
|
+
REQUIRE_OPEN_DB(ctx);
|
|
1144
|
+
|
|
1145
|
+
rb_scan_args(argc, argv, "01", &db_name);
|
|
1146
|
+
|
|
1147
|
+
zSchema = NIL_P(db_name) ? NULL : StringValueCStr(db_name);
|
|
1148
|
+
|
|
1149
|
+
data = sqlite3_serialize(ctx->db, zSchema, &size, 0);
|
|
1150
|
+
|
|
1151
|
+
if (data == NULL && size < 0) {
|
|
1152
|
+
rb_raise(rb_path2class("SQLite3::Exception"), "unable to serialize database \"%s\"",
|
|
1153
|
+
zSchema ? zSchema : "main");
|
|
1154
|
+
}
|
|
1155
|
+
if (data == NULL && size > 0) {
|
|
1156
|
+
rb_raise(rb_eNoMemError, "failed to allocate %lld bytes for serialization", (long long)size);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
result = rb_str_new((const char *)data, (long)size);
|
|
1160
|
+
sqlite3_free(data);
|
|
784
1161
|
|
|
785
|
-
|
|
1162
|
+
return result;
|
|
1163
|
+
}
|
|
1164
|
+
#endif
|
|
1165
|
+
|
|
1166
|
+
#ifdef HAVE_SQLITE3_DESERIALIZE
|
|
1167
|
+
/* call-seq: db.deserialize(data, database_name = "main")
|
|
1168
|
+
*
|
|
1169
|
+
* Deserialize a binary string into the database. The database is replaced with
|
|
1170
|
+
* the contents of the serialized data.
|
|
1171
|
+
*
|
|
1172
|
+
* [data] A binary string containing the serialized database content.
|
|
1173
|
+
* This is typically obtained from a prior call to #serialize.
|
|
1174
|
+
*
|
|
1175
|
+
* [database_name] The name of the database to replace. The default value is
|
|
1176
|
+
* +main+ which is the main database file. For ATTACHed
|
|
1177
|
+
* databases, use the name given in the ATTACH statement.
|
|
1178
|
+
*
|
|
1179
|
+
* Raises SQLite3::BusyException if the database has an open read or write
|
|
1180
|
+
* transaction, including one held by an unfinished statement, or is involved in
|
|
1181
|
+
* a backup.
|
|
1182
|
+
*
|
|
1183
|
+
* Deserializing into a database backed by a file detaches the connection from
|
|
1184
|
+
* that file. The file is left unchanged, and #filename(database_name) returns an
|
|
1185
|
+
* empty string afterwards.
|
|
1186
|
+
*
|
|
1187
|
+
* ⚠ An image of a database in WAL mode deserializes without error, but later
|
|
1188
|
+
* statements, even <tt>SELECT 1</tt>, raise SQLite3::CantOpenException. Rails
|
|
1189
|
+
* enables WAL mode by default.
|
|
1190
|
+
*
|
|
1191
|
+
* ⚠ Deserializing untrusted data carries the same risks as opening an untrusted
|
|
1192
|
+
* database file. See https://www.sqlite.org/security.html
|
|
1193
|
+
*
|
|
1194
|
+
* Note: This method will only be defined if the loaded SQLite is version 3.54.0
|
|
1195
|
+
* or later and was compiled without +SQLITE_OMIT_DESERIALIZE+. Older versions do
|
|
1196
|
+
* not raise SQLite3::BusyException; they free memory that an unfinished statement
|
|
1197
|
+
* still uses.
|
|
1198
|
+
*
|
|
1199
|
+
* See also: https://www.sqlite.org/c3ref/deserialize.html
|
|
1200
|
+
*/
|
|
1201
|
+
static VALUE
|
|
1202
|
+
deserialize(int argc, VALUE *argv, VALUE self)
|
|
1203
|
+
{
|
|
1204
|
+
sqlite3RubyPtr ctx;
|
|
1205
|
+
VALUE data, db_name;
|
|
1206
|
+
const char *zSchema;
|
|
1207
|
+
unsigned char *pData;
|
|
1208
|
+
sqlite3_int64 szDb, szBuf;
|
|
1209
|
+
int status;
|
|
1210
|
+
|
|
1211
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
1212
|
+
REQUIRE_OPEN_DB(ctx);
|
|
1213
|
+
|
|
1214
|
+
rb_scan_args(argc, argv, "11", &data, &db_name);
|
|
1215
|
+
|
|
1216
|
+
StringValue(data);
|
|
1217
|
+
zSchema = NIL_P(db_name) ? NULL : StringValueCStr(db_name);
|
|
1218
|
+
szDb = RSTRING_LEN(data);
|
|
1219
|
+
|
|
1220
|
+
/* SQLite's docs advise padding the buffer with about 20 extra bytes, because a maliciously
|
|
1221
|
+
* malformed image may make SQLite read past its end. See https://www.sqlite.org/c3ref/deserialize.html
|
|
1222
|
+
*
|
|
1223
|
+
* With SQLITE_DESERIALIZE_RESIZEABLE set, SQLite copies pages out of the buffer with bounds
|
|
1224
|
+
* checks instead of reading it in place, so the padding is defensive. It also keeps the
|
|
1225
|
+
* allocation nonzero for an empty string, for which sqlite3_malloc64(0) returns NULL.
|
|
1226
|
+
*/
|
|
1227
|
+
szBuf = szDb + 20;
|
|
1228
|
+
pData = sqlite3_malloc64(szBuf);
|
|
1229
|
+
if (pData == NULL) {
|
|
1230
|
+
rb_raise(rb_eNoMemError, "failed to allocate %lld bytes for deserialization", (long long)szBuf);
|
|
1231
|
+
}
|
|
1232
|
+
memcpy(pData, RSTRING_PTR(data), (size_t)szDb);
|
|
1233
|
+
memset(pData + szDb, 0, (size_t)(szBuf - szDb));
|
|
1234
|
+
|
|
1235
|
+
status = sqlite3_deserialize(ctx->db, zSchema, pData, szDb, szDb,
|
|
1236
|
+
SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE);
|
|
1237
|
+
|
|
1238
|
+
CHECK(ctx->db, status);
|
|
1239
|
+
|
|
1240
|
+
return self;
|
|
1241
|
+
}
|
|
1242
|
+
#endif
|
|
1243
|
+
|
|
1244
|
+
static VALUE
|
|
1245
|
+
rb_sqlite3_open16(VALUE self, VALUE file)
|
|
1246
|
+
{
|
|
1247
|
+
int status;
|
|
1248
|
+
sqlite3RubyPtr ctx;
|
|
1249
|
+
|
|
1250
|
+
TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
|
|
1251
|
+
REQUIRE_CLOSED_DB(ctx);
|
|
786
1252
|
|
|
787
1253
|
#if defined TAINTING_SUPPORT
|
|
788
1254
|
#if defined StringValueCStr
|
|
789
|
-
|
|
790
|
-
|
|
1255
|
+
StringValuePtr(file);
|
|
1256
|
+
rb_check_safe_obj(file);
|
|
791
1257
|
#else
|
|
792
|
-
|
|
1258
|
+
Check_SafeStr(file);
|
|
793
1259
|
#endif
|
|
794
1260
|
#endif
|
|
795
1261
|
|
|
796
|
-
|
|
1262
|
+
// sqlite3_open16 implicitly uses flags (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)
|
|
1263
|
+
// see https://www.sqlite.org/capi3ref.html#sqlite3_open
|
|
1264
|
+
// so we do not ever set SQLITE3_RB_DATABASE_READONLY in ctx->flags
|
|
1265
|
+
status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db);
|
|
797
1266
|
|
|
798
|
-
|
|
1267
|
+
if (status != SQLITE_OK) {
|
|
1268
|
+
char *msg = sqlite3_mprintf("%s", sqlite3_errmsg(ctx->db));
|
|
1269
|
+
sqlite3_close_v2(ctx->db);
|
|
1270
|
+
ctx->db = NULL;
|
|
1271
|
+
CHECK_MSG(ctx->db, status, msg);
|
|
1272
|
+
}
|
|
799
1273
|
|
|
800
|
-
|
|
1274
|
+
return INT2NUM(status);
|
|
801
1275
|
}
|
|
802
1276
|
|
|
803
|
-
void
|
|
1277
|
+
void
|
|
1278
|
+
init_sqlite3_database(void)
|
|
804
1279
|
{
|
|
805
1280
|
#if 0
|
|
806
|
-
|
|
1281
|
+
VALUE mSqlite3 = rb_define_module("SQLite3");
|
|
1282
|
+
#endif
|
|
1283
|
+
cSqlite3Database = rb_define_class_under(mSqlite3, "Database", rb_cObject);
|
|
1284
|
+
|
|
1285
|
+
rb_define_alloc_func(cSqlite3Database, allocate);
|
|
1286
|
+
rb_define_private_method(cSqlite3Database, "open_v2", rb_sqlite3_open_v2, 3);
|
|
1287
|
+
rb_define_private_method(cSqlite3Database, "open16", rb_sqlite3_open16, 1);
|
|
1288
|
+
rb_define_method(cSqlite3Database, "collation", collation, 2);
|
|
1289
|
+
rb_define_method(cSqlite3Database, "close", sqlite3_rb_close, 0);
|
|
1290
|
+
rb_define_private_method(cSqlite3Database, "discard", sqlite3_rb_discard, 0);
|
|
1291
|
+
rb_define_method(cSqlite3Database, "closed?", closed_p, 0);
|
|
1292
|
+
rb_define_method(cSqlite3Database, "total_changes", total_changes, 0);
|
|
1293
|
+
rb_define_method(cSqlite3Database, "trace", trace, -1);
|
|
1294
|
+
rb_define_method(cSqlite3Database, "last_insert_row_id", last_insert_row_id, 0);
|
|
1295
|
+
rb_define_method(cSqlite3Database, "define_function", define_function, 1);
|
|
1296
|
+
rb_define_method(cSqlite3Database, "define_function_with_flags", define_function_with_flags, 2);
|
|
1297
|
+
/* public "define_aggregator" is now a shim around define_aggregator2
|
|
1298
|
+
* implemented in Ruby */
|
|
1299
|
+
rb_define_private_method(cSqlite3Database, "define_aggregator2", rb_sqlite3_define_aggregator2, 2);
|
|
1300
|
+
rb_define_private_method(cSqlite3Database, "disable_quirk_mode", rb_sqlite3_disable_quirk_mode, 0);
|
|
1301
|
+
rb_define_method(cSqlite3Database, "interrupt", interrupt, 0);
|
|
1302
|
+
rb_define_method(cSqlite3Database, "errmsg", errmsg, 0);
|
|
1303
|
+
rb_define_method(cSqlite3Database, "errcode", errcode_, 0);
|
|
1304
|
+
rb_define_method(cSqlite3Database, "complete?", complete_p, 1);
|
|
1305
|
+
rb_define_method(cSqlite3Database, "changes", changes, 0);
|
|
1306
|
+
rb_define_method(cSqlite3Database, "authorizer=", set_authorizer, 1);
|
|
1307
|
+
rb_define_method(cSqlite3Database, "busy_handler", busy_handler, -1);
|
|
1308
|
+
rb_define_method(cSqlite3Database, "busy_timeout=", set_busy_timeout, 1);
|
|
1309
|
+
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
|
|
1310
|
+
rb_define_method(cSqlite3Database, "statement_timeout=", set_statement_timeout, 1);
|
|
807
1311
|
#endif
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
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);
|
|
1312
|
+
rb_define_method(cSqlite3Database, "extended_result_codes=", set_extended_result_codes, 1);
|
|
1313
|
+
rb_define_method(cSqlite3Database, "transaction_active?", transaction_active_p, 0);
|
|
1314
|
+
rb_define_private_method(cSqlite3Database, "exec_batch", exec_batch, 2);
|
|
1315
|
+
rb_define_private_method(cSqlite3Database, "db_filename", db_filename, 1);
|
|
837
1316
|
|
|
838
1317
|
#ifdef HAVE_SQLITE3_LOAD_EXTENSION
|
|
839
|
-
|
|
1318
|
+
rb_define_private_method(cSqlite3Database, "load_extension_internal", load_extension_internal, 1);
|
|
1319
|
+
#ifdef HAVE_SQLITE3_ENABLE_LOAD_EXTENSION
|
|
1320
|
+
rb_define_method(cSqlite3Database, "enable_load_extension", enable_load_extension, 1);
|
|
1321
|
+
#endif
|
|
840
1322
|
#endif
|
|
841
1323
|
|
|
842
|
-
#ifdef
|
|
843
|
-
|
|
1324
|
+
#ifdef HAVE_SQLITE3_SERIALIZE
|
|
1325
|
+
rb_define_method(cSqlite3Database, "serialize", serialize, -1);
|
|
844
1326
|
#endif
|
|
845
1327
|
|
|
846
|
-
|
|
1328
|
+
#ifdef HAVE_SQLITE3_DESERIALIZE
|
|
1329
|
+
/* Before 3.54.0, sqlite3_deserialize frees memory that an unfinished statement still uses. */
|
|
1330
|
+
if (sqlite3_libversion_number() >= 3054000) {
|
|
1331
|
+
rb_define_method(cSqlite3Database, "deserialize", deserialize, -1);
|
|
1332
|
+
}
|
|
1333
|
+
#endif
|
|
847
1334
|
|
|
848
|
-
|
|
1335
|
+
rb_sqlite3_aggregator_init();
|
|
849
1336
|
}
|
|
850
1337
|
|
|
851
1338
|
#ifdef _MSC_VER
|