aim-sqlite3 2.9.6.1.rc1-x86_64-darwin
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 +7 -0
- data/.gemtest +0 -0
- data/CHANGELOG.md +1062 -0
- data/CONTRIBUTING.md +130 -0
- data/FAQ.md +399 -0
- data/INSTALLATION.md +267 -0
- data/LICENSE +23 -0
- data/README.md +218 -0
- data/dependencies.yml +13 -0
- data/ext/sqlite3/aggregator.c +314 -0
- data/ext/sqlite3/aggregator.h +14 -0
- data/ext/sqlite3/backup.c +190 -0
- data/ext/sqlite3/backup.h +15 -0
- data/ext/sqlite3/database.c +1263 -0
- data/ext/sqlite3/database.h +36 -0
- data/ext/sqlite3/exception.c +122 -0
- data/ext/sqlite3/exception.h +12 -0
- data/ext/sqlite3/extconf.rb +299 -0
- data/ext/sqlite3/gvl.c +88 -0
- data/ext/sqlite3/gvl.h +15 -0
- data/ext/sqlite3/sqlite3.c +225 -0
- data/ext/sqlite3/sqlite3_ruby.h +49 -0
- data/ext/sqlite3/statement.c +755 -0
- data/ext/sqlite3/statement.h +17 -0
- data/ext/sqlite3/timespec.h +20 -0
- data/lib/sqlite3/3.2/sqlite3_native.bundle +0 -0
- data/lib/sqlite3/3.3/sqlite3_native.bundle +0 -0
- data/lib/sqlite3/3.4/sqlite3_native.bundle +0 -0
- data/lib/sqlite3/4.0/sqlite3_native.bundle +0 -0
- data/lib/sqlite3/cli.rb +8 -0
- data/lib/sqlite3/constants.rb +198 -0
- data/lib/sqlite3/database.rb +798 -0
- data/lib/sqlite3/errors.rb +88 -0
- data/lib/sqlite3/fork_safety.rb +66 -0
- data/lib/sqlite3/pragmas.rb +648 -0
- data/lib/sqlite3/resultset.rb +96 -0
- data/lib/sqlite3/statement.rb +208 -0
- data/lib/sqlite3/value.rb +54 -0
- data/lib/sqlite3/version.rb +4 -0
- data/lib/sqlite3/version_info.rb +17 -0
- data/lib/sqlite3.rb +20 -0
- data/ports/x86_64-apple-darwin20.2/sqlite3/3.53.2/bin/sqlite3 +0 -0
- metadata +105 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
#include <aggregator.h>
|
|
2
|
+
#include <database.h>
|
|
3
|
+
|
|
4
|
+
/* wraps a factory "handler" class. The "-aggregators" instance variable of
|
|
5
|
+
* the SQLite3::Database holds an array of all AggrogatorWrappers.
|
|
6
|
+
*
|
|
7
|
+
* An AggregatorWrapper holds the following instance variables:
|
|
8
|
+
* -handler_klass: the handler that creates the instances.
|
|
9
|
+
* -instances: array of all the cAggregatorInstance objects currently
|
|
10
|
+
* in-flight for this aggregator. */
|
|
11
|
+
static VALUE cAggregatorWrapper;
|
|
12
|
+
|
|
13
|
+
/* wraps a instance of the "handler" class. Loses its reference at the end of
|
|
14
|
+
* the xFinal callback.
|
|
15
|
+
*
|
|
16
|
+
* An AggregatorInstance holds the following instance variables:
|
|
17
|
+
* -handler_instance: the instance to call `step` and `finalize` on.
|
|
18
|
+
* -exc_status: status returned by rb_protect.
|
|
19
|
+
* != 0 if an exception occurred. If an exception occurred
|
|
20
|
+
* `step` and `finalize` won't be called any more. */
|
|
21
|
+
static VALUE cAggregatorInstance;
|
|
22
|
+
|
|
23
|
+
typedef struct rb_sqlite3_protected_funcall_args {
|
|
24
|
+
VALUE self;
|
|
25
|
+
ID method;
|
|
26
|
+
int argc;
|
|
27
|
+
VALUE *params;
|
|
28
|
+
} protected_funcall_args_t;
|
|
29
|
+
|
|
30
|
+
/* why isn't there something like this in the ruby API? */
|
|
31
|
+
static VALUE
|
|
32
|
+
rb_sqlite3_protected_funcall_body(VALUE protected_funcall_args_ptr)
|
|
33
|
+
{
|
|
34
|
+
protected_funcall_args_t *args =
|
|
35
|
+
(protected_funcall_args_t *)protected_funcall_args_ptr;
|
|
36
|
+
|
|
37
|
+
return rb_funcall2(args->self, args->method, args->argc, args->params);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static VALUE
|
|
41
|
+
rb_sqlite3_protected_funcall(VALUE self, ID method, int argc, VALUE *params,
|
|
42
|
+
int *exc_status)
|
|
43
|
+
{
|
|
44
|
+
protected_funcall_args_t args = {
|
|
45
|
+
.self = self, .method = method, .argc = argc, .params = params
|
|
46
|
+
};
|
|
47
|
+
return rb_protect(rb_sqlite3_protected_funcall_body, (VALUE)(&args), exc_status);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/* called in rb_sqlite3_aggregator_step and rb_sqlite3_aggregator_final. It
|
|
51
|
+
* checks if the execution context already has an associated instance. If it
|
|
52
|
+
* has one, it returns it. If there is no instance yet, it creates one and
|
|
53
|
+
* associates it with the context. */
|
|
54
|
+
static VALUE
|
|
55
|
+
rb_sqlite3_aggregate_instance(sqlite3_context *ctx)
|
|
56
|
+
{
|
|
57
|
+
VALUE aw = (VALUE) sqlite3_user_data(ctx);
|
|
58
|
+
VALUE handler_klass = rb_iv_get(aw, "-handler_klass");
|
|
59
|
+
VALUE inst;
|
|
60
|
+
VALUE *inst_ptr = sqlite3_aggregate_context(ctx, (int)sizeof(VALUE));
|
|
61
|
+
|
|
62
|
+
if (!inst_ptr) {
|
|
63
|
+
rb_fatal("SQLite is out-of-memory");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
inst = *inst_ptr;
|
|
67
|
+
|
|
68
|
+
if (inst == Qfalse) { /* Qfalse == 0 */
|
|
69
|
+
VALUE instances = rb_iv_get(aw, "-instances");
|
|
70
|
+
int exc_status;
|
|
71
|
+
|
|
72
|
+
inst = rb_class_new_instance(0, NULL, cAggregatorInstance);
|
|
73
|
+
rb_iv_set(inst, "-handler_instance", rb_sqlite3_protected_funcall(
|
|
74
|
+
handler_klass, rb_intern("new"), 0, NULL, &exc_status));
|
|
75
|
+
rb_iv_set(inst, "-exc_status", INT2NUM(exc_status));
|
|
76
|
+
|
|
77
|
+
rb_ary_push(instances, inst);
|
|
78
|
+
|
|
79
|
+
*inst_ptr = inst;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (inst == Qnil) {
|
|
83
|
+
rb_fatal("SQLite called us back on an already destroyed aggregate instance");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return inst;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* called by rb_sqlite3_aggregator_final. Unlinks and frees the
|
|
90
|
+
* aggregator_instance_t, so the handler_instance won't be marked any more
|
|
91
|
+
* and Ruby's GC may free it. */
|
|
92
|
+
static void
|
|
93
|
+
rb_sqlite3_aggregate_instance_destroy(sqlite3_context *ctx)
|
|
94
|
+
{
|
|
95
|
+
VALUE aw = (VALUE) sqlite3_user_data(ctx);
|
|
96
|
+
VALUE instances = rb_iv_get(aw, "-instances");
|
|
97
|
+
VALUE *inst_ptr = sqlite3_aggregate_context(ctx, 0);
|
|
98
|
+
VALUE inst;
|
|
99
|
+
|
|
100
|
+
if (!inst_ptr || !(inst = *inst_ptr)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (inst == Qnil) {
|
|
105
|
+
rb_fatal("attempt to destroy aggregate instance twice");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
rb_iv_set(inst, "-handler_instance", Qnil); // may catch use-after-free
|
|
109
|
+
if (rb_ary_delete(instances, inst) == Qnil) {
|
|
110
|
+
rb_fatal("must be in instances at that point");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
*inst_ptr = Qnil;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static void
|
|
117
|
+
rb_sqlite3_aggregator_step(sqlite3_context *ctx, int argc, sqlite3_value **argv)
|
|
118
|
+
{
|
|
119
|
+
VALUE inst = rb_sqlite3_aggregate_instance(ctx);
|
|
120
|
+
VALUE handler_instance = rb_iv_get(inst, "-handler_instance");
|
|
121
|
+
VALUE *params = NULL;
|
|
122
|
+
VALUE params_handle = 0;
|
|
123
|
+
VALUE one_param;
|
|
124
|
+
int exc_status = NUM2INT(rb_iv_get(inst, "-exc_status"));
|
|
125
|
+
int i;
|
|
126
|
+
|
|
127
|
+
if (exc_status) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (argc == 1) {
|
|
132
|
+
one_param = sqlite3val2rb(argv[0]);
|
|
133
|
+
params = &one_param;
|
|
134
|
+
}
|
|
135
|
+
if (argc > 1) {
|
|
136
|
+
/* ALLOCV memory is conservatively marked, so stored VALUEs survive a
|
|
137
|
+
* GC raised by a later sqlite3val2rb call. */
|
|
138
|
+
params = ALLOCV_N(VALUE, params_handle, argc);
|
|
139
|
+
for (i = 0; i < argc; i++) {
|
|
140
|
+
params[i] = sqlite3val2rb(argv[i]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
rb_sqlite3_protected_funcall(
|
|
144
|
+
handler_instance, rb_intern("step"), argc, params, &exc_status);
|
|
145
|
+
if (argc > 1) {
|
|
146
|
+
ALLOCV_END(params_handle);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
rb_iv_set(inst, "-exc_status", INT2NUM(exc_status));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* we assume that this function is only called once per execution context */
|
|
153
|
+
static void
|
|
154
|
+
rb_sqlite3_aggregator_final(sqlite3_context *ctx)
|
|
155
|
+
{
|
|
156
|
+
VALUE inst = rb_sqlite3_aggregate_instance(ctx);
|
|
157
|
+
VALUE handler_instance = rb_iv_get(inst, "-handler_instance");
|
|
158
|
+
int exc_status = NUM2INT(rb_iv_get(inst, "-exc_status"));
|
|
159
|
+
|
|
160
|
+
if (!exc_status) {
|
|
161
|
+
VALUE result = rb_sqlite3_protected_funcall(
|
|
162
|
+
handler_instance, rb_intern("finalize"), 0, NULL, &exc_status);
|
|
163
|
+
if (!exc_status) {
|
|
164
|
+
set_sqlite3_func_result(ctx, result);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (exc_status) {
|
|
169
|
+
/* the user should never see this, as Statement.step() will pick up the
|
|
170
|
+
* outstanding exception and raise it instead of generating a new one
|
|
171
|
+
* for SQLITE_ERROR with message "Ruby Exception occurred" */
|
|
172
|
+
sqlite3_result_error(ctx, "Ruby Exception occurred", -1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
rb_sqlite3_aggregate_instance_destroy(ctx);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
typedef struct {
|
|
179
|
+
sqlite3_context *ctx;
|
|
180
|
+
int argc;
|
|
181
|
+
sqlite3_value **argv;
|
|
182
|
+
} aggregator_step_args_t;
|
|
183
|
+
|
|
184
|
+
static void *
|
|
185
|
+
rb_sqlite3_aggregator_step_body(void *ptr)
|
|
186
|
+
{
|
|
187
|
+
aggregator_step_args_t *args = (aggregator_step_args_t *)ptr;
|
|
188
|
+
rb_sqlite3_aggregator_step(args->ctx, args->argc, args->argv);
|
|
189
|
+
return NULL;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static void
|
|
193
|
+
rb_sqlite3_aggregator_step_gvl(sqlite3_context *ctx, int argc, sqlite3_value **argv)
|
|
194
|
+
{
|
|
195
|
+
aggregator_step_args_t args = { ctx, argc, argv };
|
|
196
|
+
rb_sqlite3_with_gvl(rb_sqlite3_aggregator_step_body, &args);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static void *
|
|
200
|
+
rb_sqlite3_aggregator_final_body(void *ctx)
|
|
201
|
+
{
|
|
202
|
+
rb_sqlite3_aggregator_final((sqlite3_context *)ctx);
|
|
203
|
+
return NULL;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
static void
|
|
207
|
+
rb_sqlite3_aggregator_final_gvl(sqlite3_context *ctx)
|
|
208
|
+
{
|
|
209
|
+
rb_sqlite3_with_gvl(rb_sqlite3_aggregator_final_body, ctx);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/* call-seq: define_aggregator2(aggregator)
|
|
213
|
+
*
|
|
214
|
+
* Define an aggregate function according to a factory object (the "handler")
|
|
215
|
+
* that knows how to obtain to all the information. The handler must provide
|
|
216
|
+
* the following class methods:
|
|
217
|
+
*
|
|
218
|
+
* +arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
|
|
219
|
+
* message is optional, and if the handler does not respond to it,
|
|
220
|
+
* the function will have an arity of -1.
|
|
221
|
+
* +name+:: this is the name of the function. The handler _must_ implement
|
|
222
|
+
* this message.
|
|
223
|
+
* +new+:: this must be implemented by the handler. It should return a new
|
|
224
|
+
* instance of the object that will handle a specific invocation of
|
|
225
|
+
* the function.
|
|
226
|
+
*
|
|
227
|
+
* The handler instance (the object returned by the +new+ message, described
|
|
228
|
+
* above), must respond to the following messages:
|
|
229
|
+
*
|
|
230
|
+
* +step+:: this is the method that will be called for each step of the
|
|
231
|
+
* aggregate function's evaluation. It should take parameters according
|
|
232
|
+
* to the *arity* definition.
|
|
233
|
+
* +finalize+:: this is the method that will be called to finalize the
|
|
234
|
+
* aggregate function's evaluation. It should not take arguments.
|
|
235
|
+
*
|
|
236
|
+
* Note the difference between this function and #create_aggregate_handler
|
|
237
|
+
* is that no FunctionProxy ("ctx") object is involved. This manifests in two
|
|
238
|
+
* ways: The return value of the aggregate function is the return value of
|
|
239
|
+
* +finalize+ and neither +step+ nor +finalize+ take an additional "ctx"
|
|
240
|
+
* parameter.
|
|
241
|
+
*/
|
|
242
|
+
VALUE
|
|
243
|
+
rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name)
|
|
244
|
+
{
|
|
245
|
+
/* define_aggregator is added as a method to SQLite3::Database in database.c */
|
|
246
|
+
sqlite3RubyPtr ctx = sqlite3_database_unwrap(self);
|
|
247
|
+
int arity, status;
|
|
248
|
+
VALUE aw;
|
|
249
|
+
VALUE aggregators;
|
|
250
|
+
|
|
251
|
+
if (!ctx->db) {
|
|
252
|
+
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (rb_respond_to(aggregator, rb_intern("arity"))) {
|
|
256
|
+
VALUE ruby_arity = rb_funcall(aggregator, rb_intern("arity"), 0);
|
|
257
|
+
arity = NUM2INT(ruby_arity);
|
|
258
|
+
} else {
|
|
259
|
+
arity = -1;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (arity < -1 || arity > 127) {
|
|
263
|
+
#ifdef PRIsVALUE
|
|
264
|
+
rb_raise(rb_eArgError, "%"PRIsVALUE" arity=%d out of range -1..127",
|
|
265
|
+
self, arity);
|
|
266
|
+
#else
|
|
267
|
+
rb_raise(rb_eArgError, "Aggregator arity=%d out of range -1..127", arity);
|
|
268
|
+
#endif
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (!rb_ivar_defined(self, rb_intern("-aggregators"))) {
|
|
272
|
+
rb_iv_set(self, "-aggregators", rb_ary_new());
|
|
273
|
+
}
|
|
274
|
+
aggregators = rb_iv_get(self, "-aggregators");
|
|
275
|
+
|
|
276
|
+
aw = rb_class_new_instance(0, NULL, cAggregatorWrapper);
|
|
277
|
+
rb_iv_set(aw, "-handler_klass", aggregator);
|
|
278
|
+
rb_iv_set(aw, "-instances", rb_ary_new());
|
|
279
|
+
|
|
280
|
+
status = sqlite3_create_function(
|
|
281
|
+
ctx->db,
|
|
282
|
+
StringValueCStr(ruby_name),
|
|
283
|
+
arity,
|
|
284
|
+
SQLITE_UTF8,
|
|
285
|
+
(void *)aw,
|
|
286
|
+
NULL,
|
|
287
|
+
rb_sqlite3_aggregator_step_gvl,
|
|
288
|
+
rb_sqlite3_aggregator_final_gvl
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
CHECK(ctx->db, status);
|
|
292
|
+
|
|
293
|
+
rb_ary_push(aggregators, aw);
|
|
294
|
+
RB_OBJ_WRITE(self, &ctx->aggregators, aggregators);
|
|
295
|
+
|
|
296
|
+
return self;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
void
|
|
300
|
+
rb_sqlite3_aggregator_pin_instances(VALUE aw)
|
|
301
|
+
{
|
|
302
|
+
rb_sqlite3_pin_array_and_contents(rb_iv_get(aw, "-instances"));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
void
|
|
306
|
+
rb_sqlite3_aggregator_init(void)
|
|
307
|
+
{
|
|
308
|
+
/* rb_class_new generatos class with undefined allocator in ruby 1.9 */
|
|
309
|
+
cAggregatorWrapper = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
|
310
|
+
rb_gc_register_mark_object(cAggregatorWrapper);
|
|
311
|
+
|
|
312
|
+
cAggregatorInstance = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
|
313
|
+
rb_gc_register_mark_object(cAggregatorInstance);
|
|
314
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#ifndef SQLITE3_AGGREGATOR_RUBY
|
|
2
|
+
#define SQLITE3_AGGREGATOR_RUBY
|
|
3
|
+
|
|
4
|
+
#include <sqlite3_ruby.h>
|
|
5
|
+
|
|
6
|
+
VALUE rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name);
|
|
7
|
+
|
|
8
|
+
void rb_sqlite3_aggregator_init(void);
|
|
9
|
+
|
|
10
|
+
/* sqlite stores each live instance's VALUE in its aggregate context, so those
|
|
11
|
+
* must not move either. */
|
|
12
|
+
void rb_sqlite3_aggregator_pin_instances(VALUE aw);
|
|
13
|
+
|
|
14
|
+
#endif
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
#ifdef HAVE_SQLITE3_BACKUP_INIT
|
|
2
|
+
|
|
3
|
+
#include <sqlite3_ruby.h>
|
|
4
|
+
|
|
5
|
+
#define REQUIRE_OPEN_BACKUP(_ctxt) \
|
|
6
|
+
if(!_ctxt->p) \
|
|
7
|
+
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed backup");
|
|
8
|
+
|
|
9
|
+
VALUE cSqlite3Backup;
|
|
10
|
+
|
|
11
|
+
static size_t
|
|
12
|
+
backup_memsize(const void *data)
|
|
13
|
+
{
|
|
14
|
+
sqlite3BackupRubyPtr ctx = (sqlite3BackupRubyPtr)data;
|
|
15
|
+
// NB: can't account for ctx->p because the type is incomplete.
|
|
16
|
+
return sizeof(*ctx);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static const rb_data_type_t backup_type = {
|
|
20
|
+
"SQLite3::Backup",
|
|
21
|
+
{
|
|
22
|
+
NULL,
|
|
23
|
+
RUBY_TYPED_DEFAULT_FREE,
|
|
24
|
+
backup_memsize,
|
|
25
|
+
},
|
|
26
|
+
0,
|
|
27
|
+
0,
|
|
28
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
static VALUE
|
|
32
|
+
allocate(VALUE klass)
|
|
33
|
+
{
|
|
34
|
+
sqlite3BackupRubyPtr ctx;
|
|
35
|
+
return TypedData_Make_Struct(klass, sqlite3BackupRuby, &backup_type, ctx);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/* call-seq: SQLite3::Backup.new(dstdb, dstname, srcdb, srcname)
|
|
39
|
+
*
|
|
40
|
+
* Initialize backup the backup.
|
|
41
|
+
*
|
|
42
|
+
* dstdb:
|
|
43
|
+
* the destination SQLite3::Database object.
|
|
44
|
+
* dstname:
|
|
45
|
+
* the destination's database name.
|
|
46
|
+
* srcdb:
|
|
47
|
+
* the source SQLite3::Database object.
|
|
48
|
+
* srcname:
|
|
49
|
+
* the source's database name.
|
|
50
|
+
*
|
|
51
|
+
* The database name is "main", "temp", or the name specified in an
|
|
52
|
+
* ATTACH statement.
|
|
53
|
+
*
|
|
54
|
+
* This feature requires SQLite 3.6.11 or later.
|
|
55
|
+
*
|
|
56
|
+
* require 'sqlite3'
|
|
57
|
+
* sdb = SQLite3::Database.new('src.sqlite3')
|
|
58
|
+
*
|
|
59
|
+
* ddb = SQLite3::Database.new(':memory:')
|
|
60
|
+
* b = SQLite3::Backup.new(ddb, 'main', sdb, 'main')
|
|
61
|
+
* p [b.remaining, b.pagecount] # invalid value; for example [0, 0]
|
|
62
|
+
* begin
|
|
63
|
+
* p b.step(1) #=> OK or DONE
|
|
64
|
+
* p [b.remaining, b.pagecount]
|
|
65
|
+
* end while b.remaining > 0
|
|
66
|
+
* b.finish
|
|
67
|
+
*
|
|
68
|
+
* ddb = SQLite3::Database.new(':memory:')
|
|
69
|
+
* b = SQLite3::Backup.new(ddb, 'main', sdb, 'main')
|
|
70
|
+
* b.step(-1) #=> DONE
|
|
71
|
+
* b.finish
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
static VALUE
|
|
75
|
+
initialize(VALUE self, VALUE dstdb, VALUE dstname, VALUE srcdb, VALUE srcname)
|
|
76
|
+
{
|
|
77
|
+
sqlite3BackupRubyPtr ctx;
|
|
78
|
+
sqlite3RubyPtr ddb_ctx, sdb_ctx;
|
|
79
|
+
sqlite3_backup *pBackup;
|
|
80
|
+
|
|
81
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
82
|
+
ddb_ctx = sqlite3_database_unwrap(dstdb);
|
|
83
|
+
sdb_ctx = sqlite3_database_unwrap(srcdb);
|
|
84
|
+
|
|
85
|
+
if (!sdb_ctx->db) {
|
|
86
|
+
rb_raise(rb_eArgError, "cannot backup from a closed database");
|
|
87
|
+
}
|
|
88
|
+
if (!ddb_ctx->db) {
|
|
89
|
+
rb_raise(rb_eArgError, "cannot backup to a closed database");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
pBackup = sqlite3_backup_init(ddb_ctx->db, StringValuePtr(dstname),
|
|
93
|
+
sdb_ctx->db, StringValuePtr(srcname));
|
|
94
|
+
if (pBackup) {
|
|
95
|
+
ctx->p = pBackup;
|
|
96
|
+
} else {
|
|
97
|
+
CHECK(ddb_ctx->db, sqlite3_errcode(ddb_ctx->db));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return self;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* call-seq: SQLite3::Backup#step(nPage)
|
|
104
|
+
*
|
|
105
|
+
* Copy database pages up to +nPage+.
|
|
106
|
+
* If negative, copy all remaining source pages.
|
|
107
|
+
*
|
|
108
|
+
* If all pages are copied, it returns SQLite3::Constants::ErrorCode::DONE.
|
|
109
|
+
* When coping is not done, it returns SQLite3::Constants::ErrorCode::OK.
|
|
110
|
+
* When some errors occur, it returns the error code.
|
|
111
|
+
*/
|
|
112
|
+
static VALUE
|
|
113
|
+
step(VALUE self, VALUE nPage)
|
|
114
|
+
{
|
|
115
|
+
sqlite3BackupRubyPtr ctx;
|
|
116
|
+
int status;
|
|
117
|
+
|
|
118
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
119
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
120
|
+
status = sqlite3_backup_step(ctx->p, NUM2INT(nPage));
|
|
121
|
+
return INT2NUM(status);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* call-seq: SQLite3::Backup#finish
|
|
125
|
+
*
|
|
126
|
+
* Destroy the backup object.
|
|
127
|
+
*/
|
|
128
|
+
static VALUE
|
|
129
|
+
finish(VALUE self)
|
|
130
|
+
{
|
|
131
|
+
sqlite3BackupRubyPtr ctx;
|
|
132
|
+
|
|
133
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
134
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
135
|
+
(void)sqlite3_backup_finish(ctx->p);
|
|
136
|
+
ctx->p = NULL;
|
|
137
|
+
return Qnil;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/* call-seq: SQLite3::Backup#remaining
|
|
141
|
+
*
|
|
142
|
+
* Returns the number of pages still to be backed up.
|
|
143
|
+
*
|
|
144
|
+
* Note that the value is only updated after step() is called,
|
|
145
|
+
* so before calling step() returned value is invalid.
|
|
146
|
+
*/
|
|
147
|
+
static VALUE
|
|
148
|
+
remaining(VALUE self)
|
|
149
|
+
{
|
|
150
|
+
sqlite3BackupRubyPtr ctx;
|
|
151
|
+
|
|
152
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
153
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
154
|
+
return INT2NUM(sqlite3_backup_remaining(ctx->p));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* call-seq: SQLite3::Backup#pagecount
|
|
158
|
+
*
|
|
159
|
+
* Returns the total number of pages in the source database file.
|
|
160
|
+
*
|
|
161
|
+
* Note that the value is only updated after step() is called,
|
|
162
|
+
* so before calling step() returned value is invalid.
|
|
163
|
+
*/
|
|
164
|
+
static VALUE
|
|
165
|
+
pagecount(VALUE self)
|
|
166
|
+
{
|
|
167
|
+
sqlite3BackupRubyPtr ctx;
|
|
168
|
+
|
|
169
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
170
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
171
|
+
return INT2NUM(sqlite3_backup_pagecount(ctx->p));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
void
|
|
175
|
+
init_sqlite3_backup(void)
|
|
176
|
+
{
|
|
177
|
+
#if 0
|
|
178
|
+
VALUE mSqlite3 = rb_define_module("SQLite3");
|
|
179
|
+
#endif
|
|
180
|
+
cSqlite3Backup = rb_define_class_under(mSqlite3, "Backup", rb_cObject);
|
|
181
|
+
|
|
182
|
+
rb_define_alloc_func(cSqlite3Backup, allocate);
|
|
183
|
+
rb_define_method(cSqlite3Backup, "initialize", initialize, 4);
|
|
184
|
+
rb_define_method(cSqlite3Backup, "step", step, 1);
|
|
185
|
+
rb_define_method(cSqlite3Backup, "finish", finish, 0);
|
|
186
|
+
rb_define_method(cSqlite3Backup, "remaining", remaining, 0);
|
|
187
|
+
rb_define_method(cSqlite3Backup, "pagecount", pagecount, 0);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#endif
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#if !defined(SQLITE3_BACKUP_RUBY) && defined(HAVE_SQLITE3_BACKUP_INIT)
|
|
2
|
+
#define SQLITE3_BACKUP_RUBY
|
|
3
|
+
|
|
4
|
+
#include <sqlite3_ruby.h>
|
|
5
|
+
|
|
6
|
+
struct _sqlite3BackupRuby {
|
|
7
|
+
sqlite3_backup *p;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
typedef struct _sqlite3BackupRuby sqlite3BackupRuby;
|
|
11
|
+
typedef sqlite3BackupRuby *sqlite3BackupRubyPtr;
|
|
12
|
+
|
|
13
|
+
void init_sqlite3_backup();
|
|
14
|
+
|
|
15
|
+
#endif
|