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/aggregator.c
CHANGED
|
@@ -14,167 +14,154 @@ static VALUE cAggregatorWrapper;
|
|
|
14
14
|
* the xFinal callback.
|
|
15
15
|
*
|
|
16
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. */
|
|
17
|
+
* -handler_instance: the instance to call `step` and `finalize` on. */
|
|
21
18
|
static VALUE cAggregatorInstance;
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
/* interned at init, because interning can allocate and rb_sqlite3_aggregator_pin
|
|
21
|
+
* runs during GC */
|
|
22
|
+
static ID id_instances;
|
|
23
|
+
static ID id_handler_instance;
|
|
24
|
+
|
|
25
|
+
/* sqlite's aggregate context for one invocation of an aggregate function. */
|
|
26
|
+
typedef struct {
|
|
27
|
+
/* the AggregatorInstance: Qfalse until the first callback creates it, Qnil
|
|
28
|
+
* once xFinal destroys it */
|
|
29
|
+
VALUE inst;
|
|
30
|
+
/* != 0 once a callback for this invocation raised, threw or was skipped.
|
|
31
|
+
* After that, `step` and `finalize` won't be called any more. */
|
|
32
|
+
int exc_status;
|
|
33
|
+
} aggregate_context_t;
|
|
34
|
+
|
|
35
|
+
typedef struct {
|
|
36
|
+
sqlite3_context *ctx;
|
|
37
|
+
aggregate_context_t *agg;
|
|
26
38
|
int argc;
|
|
27
|
-
|
|
28
|
-
}
|
|
39
|
+
sqlite3_value **argv;
|
|
40
|
+
} aggregator_call_t;
|
|
29
41
|
|
|
30
|
-
/*
|
|
42
|
+
/* called in the step and final callbacks. Returns the invocation's handler
|
|
43
|
+
* instance, creating it on the first call. */
|
|
31
44
|
static VALUE
|
|
32
|
-
|
|
45
|
+
rb_sqlite3_aggregate_handler_instance(aggregator_call_t *call)
|
|
33
46
|
{
|
|
34
|
-
|
|
35
|
-
(protected_funcall_args_t*)protected_funcall_args_ptr;
|
|
36
|
-
|
|
37
|
-
return rb_funcall2(args->self, args->method, args->argc, args->params);
|
|
38
|
-
}
|
|
47
|
+
VALUE aw, handler_instance, inst;
|
|
39
48
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
if (call->agg->inst == Qnil) {
|
|
50
|
+
rb_fatal("SQLite called us back on an already destroyed aggregate instance");
|
|
51
|
+
}
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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));
|
|
53
|
+
if (call->agg->inst == Qfalse) { /* Qfalse == 0 */
|
|
54
|
+
aw = (VALUE) sqlite3_user_data(call->ctx);
|
|
55
|
+
handler_instance = rb_funcall(rb_ivar_get(aw, rb_intern("-handler_klass")), rb_intern("new"), 0);
|
|
61
56
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
inst = rb_class_new_instance(0, NULL, cAggregatorInstance);
|
|
58
|
+
rb_ivar_set(inst, id_handler_instance, handler_instance);
|
|
59
|
+
rb_ary_push(rb_ivar_get(aw, id_instances), inst);
|
|
65
60
|
|
|
66
|
-
|
|
61
|
+
call->agg->inst = inst;
|
|
62
|
+
}
|
|
67
63
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
int exc_status;
|
|
64
|
+
return rb_ivar_get(call->agg->inst, id_handler_instance);
|
|
65
|
+
}
|
|
71
66
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
/* called by rb_sqlite3_aggregator_final, after any skipped or failed callback
|
|
68
|
+
* too, so it runs no Ruby code. Unlinks the instance, so the handler_instance
|
|
69
|
+
* won't be marked any more and Ruby's GC may free it. */
|
|
70
|
+
static void
|
|
71
|
+
rb_sqlite3_aggregate_instance_destroy(sqlite3_context *ctx, aggregate_context_t *agg)
|
|
72
|
+
{
|
|
73
|
+
VALUE instances = rb_ivar_get((VALUE) sqlite3_user_data(ctx), id_instances);
|
|
74
|
+
long i;
|
|
76
75
|
|
|
77
|
-
|
|
76
|
+
if (!agg->inst) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
78
79
|
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
if (agg->inst == Qnil) {
|
|
81
|
+
rb_fatal("attempt to destroy aggregate instance twice");
|
|
82
|
+
}
|
|
81
83
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
rb_ivar_set(agg->inst, id_handler_instance, Qnil); // may catch use-after-free
|
|
85
|
+
for (i = 0; i < RARRAY_LEN(instances) && RARRAY_AREF(instances, i) != agg->inst; i++);
|
|
86
|
+
if (i == RARRAY_LEN(instances)) {
|
|
87
|
+
rb_fatal("must be in instances at that point");
|
|
88
|
+
}
|
|
89
|
+
rb_ary_delete_at(instances, i);
|
|
85
90
|
|
|
86
|
-
|
|
91
|
+
agg->inst = Qnil;
|
|
87
92
|
}
|
|
88
93
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
* and Ruby's GC may free it. */
|
|
92
|
-
static void
|
|
93
|
-
rb_sqlite3_aggregate_instance_destroy(sqlite3_context *ctx)
|
|
94
|
+
static VALUE
|
|
95
|
+
rb_sqlite3_aggregator_step_protected(VALUE call_value)
|
|
94
96
|
{
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
aggregator_call_t *call = (aggregator_call_t *)call_value;
|
|
98
|
+
VALUE handler_instance = rb_sqlite3_aggregate_handler_instance(call);
|
|
99
|
+
VALUE params = rb_ary_new2(call->argc);
|
|
100
|
+
int i;
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
for (i = 0; i < call->argc; i++) {
|
|
103
|
+
rb_ary_push(params, sqlite3val2rb(call->argv[i]));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return rb_apply(handler_instance, rb_intern("step"), params);
|
|
107
|
+
}
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
109
|
+
static VALUE
|
|
110
|
+
rb_sqlite3_aggregator_final_protected(VALUE call_value)
|
|
111
|
+
{
|
|
112
|
+
aggregator_call_t *call = (aggregator_call_t *)call_value;
|
|
113
|
+
VALUE handler_instance = rb_sqlite3_aggregate_handler_instance(call);
|
|
107
114
|
|
|
108
|
-
|
|
109
|
-
if (rb_ary_delete(instances, inst) == Qnil) {
|
|
110
|
-
rb_fatal("must be in instances at that point");
|
|
111
|
-
}
|
|
115
|
+
set_sqlite3_func_result(call->ctx, rb_funcall(handler_instance, rb_intern("finalize"), 0));
|
|
112
116
|
|
|
113
|
-
|
|
117
|
+
return Qnil;
|
|
114
118
|
}
|
|
115
119
|
|
|
116
120
|
static void
|
|
117
|
-
rb_sqlite3_aggregator_step(sqlite3_context *
|
|
121
|
+
rb_sqlite3_aggregator_step(sqlite3_context *ctx, int argc, sqlite3_value **argv)
|
|
118
122
|
{
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (argc == 1) {
|
|
131
|
-
one_param = sqlite3val2rb(argv[0]);
|
|
132
|
-
params = &one_param;
|
|
133
|
-
}
|
|
134
|
-
if (argc > 1) {
|
|
135
|
-
params = xcalloc((size_t)argc, sizeof(VALUE));
|
|
136
|
-
for(i = 0; i < argc; i++) {
|
|
137
|
-
params[i] = sqlite3val2rb(argv[i]);
|
|
123
|
+
aggregator_call_t call = {
|
|
124
|
+
.ctx = ctx, .agg = sqlite3_aggregate_context(ctx, (int)sizeof(aggregate_context_t)), .argc = argc, .argv = argv
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
if (!call.agg) {
|
|
128
|
+
sqlite3_result_error_nomem(ctx);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!call.agg->exc_status) {
|
|
133
|
+
rb_sqlite3_protect_callback(rb_sqlite3_aggregator_step_protected, (VALUE)&call, &call.agg->exc_status);
|
|
138
134
|
}
|
|
139
|
-
}
|
|
140
|
-
rb_sqlite3_protected_funcall(
|
|
141
|
-
handler_instance, rb_intern("step"), argc, params, &exc_status);
|
|
142
|
-
if (argc > 1) {
|
|
143
|
-
xfree(params);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
rb_iv_set(inst, "-exc_status", INT2NUM(exc_status));
|
|
147
135
|
}
|
|
148
136
|
|
|
149
137
|
/* we assume that this function is only called once per execution context */
|
|
150
138
|
static void
|
|
151
|
-
rb_sqlite3_aggregator_final(sqlite3_context *
|
|
139
|
+
rb_sqlite3_aggregator_final(sqlite3_context *ctx)
|
|
152
140
|
{
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
VALUE result = rb_sqlite3_protected_funcall(
|
|
159
|
-
handler_instance, rb_intern("finalize"), 0, NULL, &exc_status);
|
|
160
|
-
if (!exc_status) {
|
|
161
|
-
set_sqlite3_func_result(ctx, result);
|
|
141
|
+
aggregator_call_t call = { .ctx = ctx, .agg = sqlite3_aggregate_context(ctx, (int)sizeof(aggregate_context_t)) };
|
|
142
|
+
|
|
143
|
+
if (!call.agg) {
|
|
144
|
+
sqlite3_result_error_nomem(ctx);
|
|
145
|
+
return;
|
|
162
146
|
}
|
|
163
|
-
}
|
|
164
147
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
* for SQLITE_ERROR with message "Ruby Exception occurred" */
|
|
169
|
-
sqlite3_result_error(ctx, "Ruby Exception occurred", -1);
|
|
170
|
-
}
|
|
148
|
+
if (!call.agg->exc_status) {
|
|
149
|
+
rb_sqlite3_protect_callback(rb_sqlite3_aggregator_final_protected, (VALUE)&call, &call.agg->exc_status);
|
|
150
|
+
}
|
|
171
151
|
|
|
172
|
-
|
|
152
|
+
if (call.agg->exc_status) {
|
|
153
|
+
/* the user should never see this, as Statement.step() will pick up the
|
|
154
|
+
* outstanding exception and raise it instead of generating a new one
|
|
155
|
+
* for SQLITE_ERROR with message "Ruby Exception occurred" */
|
|
156
|
+
sqlite3_result_error(ctx, "Ruby Exception occurred", -1);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
rb_sqlite3_aggregate_instance_destroy(ctx, call.agg);
|
|
173
160
|
}
|
|
174
161
|
|
|
175
162
|
/* call-seq: define_aggregator2(aggregator)
|
|
176
163
|
*
|
|
177
|
-
* Define an
|
|
164
|
+
* Define an aggregate function according to a factory object (the "handler")
|
|
178
165
|
* that knows how to obtain to all the information. The handler must provide
|
|
179
166
|
* the following class methods:
|
|
180
167
|
*
|
|
@@ -205,70 +192,83 @@ rb_sqlite3_aggregator_final(sqlite3_context * ctx)
|
|
|
205
192
|
VALUE
|
|
206
193
|
rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name)
|
|
207
194
|
{
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (arity < -1 || arity > 127) {
|
|
195
|
+
/* define_aggregator is added as a method to SQLite3::Database in database.c */
|
|
196
|
+
sqlite3RubyPtr ctx = sqlite3_database_unwrap(self);
|
|
197
|
+
int arity, status;
|
|
198
|
+
VALUE aw;
|
|
199
|
+
VALUE aggregators;
|
|
200
|
+
|
|
201
|
+
if (!ctx->db) {
|
|
202
|
+
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (rb_respond_to(aggregator, rb_intern("arity"))) {
|
|
206
|
+
VALUE ruby_arity = rb_funcall(aggregator, rb_intern("arity"), 0);
|
|
207
|
+
arity = NUM2INT(ruby_arity);
|
|
208
|
+
} else {
|
|
209
|
+
arity = -1;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (arity < -1 || arity > 127) {
|
|
227
213
|
#ifdef PRIsVALUE
|
|
228
|
-
|
|
229
|
-
|
|
214
|
+
rb_raise(rb_eArgError, "%"PRIsVALUE" arity=%d out of range -1..127",
|
|
215
|
+
self, arity);
|
|
230
216
|
#else
|
|
231
|
-
|
|
217
|
+
rb_raise(rb_eArgError, "Aggregator arity=%d out of range -1..127", arity);
|
|
232
218
|
#endif
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!rb_ivar_defined(self, rb_intern("-aggregators"))) {
|
|
222
|
+
rb_iv_set(self, "-aggregators", rb_ary_new());
|
|
223
|
+
}
|
|
224
|
+
aggregators = rb_iv_get(self, "-aggregators");
|
|
225
|
+
|
|
226
|
+
aw = rb_class_new_instance(0, NULL, cAggregatorWrapper);
|
|
227
|
+
rb_iv_set(aw, "-handler_klass", aggregator);
|
|
228
|
+
rb_iv_set(aw, "-instances", rb_ary_new());
|
|
229
|
+
|
|
230
|
+
status = sqlite3_create_function(
|
|
231
|
+
ctx->db,
|
|
232
|
+
StringValueCStr(ruby_name),
|
|
233
|
+
arity,
|
|
234
|
+
SQLITE_UTF8,
|
|
235
|
+
(void *)aw,
|
|
236
|
+
NULL,
|
|
237
|
+
rb_sqlite3_aggregator_step,
|
|
238
|
+
rb_sqlite3_aggregator_final
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
CHECK(ctx->db, status);
|
|
242
|
+
|
|
243
|
+
rb_ary_push(aggregators, aw);
|
|
244
|
+
RB_OBJ_WRITE(self, &ctx->aggregators, aggregators);
|
|
245
|
+
|
|
246
|
+
return self;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
void
|
|
250
|
+
rb_sqlite3_aggregator_pin(VALUE aggregators)
|
|
251
|
+
{
|
|
252
|
+
long i;
|
|
253
|
+
|
|
254
|
+
if (NIL_P(aggregators) || !aggregators) { return; }
|
|
255
|
+
|
|
256
|
+
rb_sqlite3_pin_array_and_contents(aggregators);
|
|
257
|
+
for (i = 0; i < RARRAY_LEN(aggregators); i++) {
|
|
258
|
+
rb_sqlite3_pin_array_and_contents(rb_ivar_get(RARRAY_AREF(aggregators, i), id_instances));
|
|
259
|
+
}
|
|
263
260
|
}
|
|
264
261
|
|
|
265
262
|
void
|
|
266
263
|
rb_sqlite3_aggregator_init(void)
|
|
267
264
|
{
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
265
|
+
/* rb_class_new generatos class with undefined allocator in ruby 1.9 */
|
|
266
|
+
cAggregatorWrapper = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
|
267
|
+
rb_gc_register_mark_object(cAggregatorWrapper);
|
|
268
|
+
|
|
269
|
+
cAggregatorInstance = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
|
270
|
+
rb_gc_register_mark_object(cAggregatorInstance);
|
|
271
271
|
|
|
272
|
-
|
|
273
|
-
|
|
272
|
+
id_instances = rb_intern("-instances");
|
|
273
|
+
id_handler_instance = rb_intern("-handler_instance");
|
|
274
274
|
}
|
data/ext/sqlite3/aggregator.h
CHANGED
|
@@ -3,10 +3,13 @@
|
|
|
3
3
|
|
|
4
4
|
#include <sqlite3_ruby.h>
|
|
5
5
|
|
|
6
|
-
VALUE
|
|
7
|
-
rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name);
|
|
6
|
+
VALUE rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name);
|
|
8
7
|
|
|
9
|
-
void
|
|
10
|
-
|
|
8
|
+
void rb_sqlite3_aggregator_init(void);
|
|
9
|
+
|
|
10
|
+
/* Pins every aggregator wrapper and its live instances. sqlite holds raw
|
|
11
|
+
* pointers to the wrappers, and stores each instance's VALUE in its aggregate
|
|
12
|
+
* context. */
|
|
13
|
+
void rb_sqlite3_aggregator_pin(VALUE aggregators);
|
|
11
14
|
|
|
12
15
|
#endif
|
data/ext/sqlite3/backup.c
CHANGED
|
@@ -8,16 +8,31 @@
|
|
|
8
8
|
|
|
9
9
|
VALUE cSqlite3Backup;
|
|
10
10
|
|
|
11
|
-
static
|
|
11
|
+
static size_t
|
|
12
|
+
backup_memsize(const void *data)
|
|
12
13
|
{
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
sqlite3BackupRubyPtr ctx = (sqlite3BackupRubyPtr)data;
|
|
15
|
+
// NB: can't account for ctx->p because the type is incomplete.
|
|
16
|
+
return sizeof(*ctx);
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
static
|
|
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)
|
|
18
33
|
{
|
|
19
|
-
|
|
20
|
-
|
|
34
|
+
sqlite3BackupRubyPtr ctx;
|
|
35
|
+
return TypedData_Make_Struct(klass, sqlite3BackupRuby, &backup_type, ctx);
|
|
21
36
|
}
|
|
22
37
|
|
|
23
38
|
/* call-seq: SQLite3::Backup.new(dstdb, dstname, srcdb, srcname)
|
|
@@ -56,31 +71,33 @@ static VALUE allocate(VALUE klass)
|
|
|
56
71
|
* b.finish
|
|
57
72
|
*
|
|
58
73
|
*/
|
|
59
|
-
static VALUE
|
|
74
|
+
static VALUE
|
|
75
|
+
initialize(VALUE self, VALUE dstdb, VALUE dstname, VALUE srcdb, VALUE srcname)
|
|
60
76
|
{
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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;
|
|
84
101
|
}
|
|
85
102
|
|
|
86
103
|
/* call-seq: SQLite3::Backup#step(nPage)
|
|
@@ -92,30 +109,37 @@ static VALUE initialize(VALUE self, VALUE dstdb, VALUE dstname, VALUE srcdb, VAL
|
|
|
92
109
|
* When coping is not done, it returns SQLite3::Constants::ErrorCode::OK.
|
|
93
110
|
* When some errors occur, it returns the error code.
|
|
94
111
|
*/
|
|
95
|
-
static VALUE
|
|
112
|
+
static VALUE
|
|
113
|
+
step(VALUE self, VALUE nPage)
|
|
96
114
|
{
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
115
|
+
sqlite3BackupRubyPtr ctx;
|
|
116
|
+
rb_sqlite3_callback_frame_t frame;
|
|
117
|
+
int pages, status, callback_state;
|
|
118
|
+
|
|
119
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
120
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
121
|
+
pages = NUM2INT(nPage);
|
|
122
|
+
|
|
123
|
+
rb_sqlite3_push_callback_frame(&frame);
|
|
124
|
+
status = sqlite3_backup_step(ctx->p, pages);
|
|
125
|
+
if ((callback_state = rb_sqlite3_pop_callback_frame(&frame))) { rb_jump_tag(callback_state); }
|
|
126
|
+
return INT2NUM(status);
|
|
104
127
|
}
|
|
105
128
|
|
|
106
129
|
/* call-seq: SQLite3::Backup#finish
|
|
107
130
|
*
|
|
108
131
|
* Destroy the backup object.
|
|
109
132
|
*/
|
|
110
|
-
static VALUE
|
|
133
|
+
static VALUE
|
|
134
|
+
finish(VALUE self)
|
|
111
135
|
{
|
|
112
|
-
|
|
136
|
+
sqlite3BackupRubyPtr ctx;
|
|
113
137
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
138
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
139
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
140
|
+
(void)sqlite3_backup_finish(ctx->p);
|
|
141
|
+
ctx->p = NULL;
|
|
142
|
+
return Qnil;
|
|
119
143
|
}
|
|
120
144
|
|
|
121
145
|
/* call-seq: SQLite3::Backup#remaining
|
|
@@ -125,13 +149,14 @@ static VALUE finish(VALUE self)
|
|
|
125
149
|
* Note that the value is only updated after step() is called,
|
|
126
150
|
* so before calling step() returned value is invalid.
|
|
127
151
|
*/
|
|
128
|
-
static VALUE
|
|
152
|
+
static VALUE
|
|
153
|
+
remaining(VALUE self)
|
|
129
154
|
{
|
|
130
|
-
|
|
155
|
+
sqlite3BackupRubyPtr ctx;
|
|
131
156
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
157
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
158
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
159
|
+
return INT2NUM(sqlite3_backup_remaining(ctx->p));
|
|
135
160
|
}
|
|
136
161
|
|
|
137
162
|
/* call-seq: SQLite3::Backup#pagecount
|
|
@@ -141,28 +166,30 @@ static VALUE remaining(VALUE self)
|
|
|
141
166
|
* Note that the value is only updated after step() is called,
|
|
142
167
|
* so before calling step() returned value is invalid.
|
|
143
168
|
*/
|
|
144
|
-
static VALUE
|
|
169
|
+
static VALUE
|
|
170
|
+
pagecount(VALUE self)
|
|
145
171
|
{
|
|
146
|
-
|
|
172
|
+
sqlite3BackupRubyPtr ctx;
|
|
147
173
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
174
|
+
TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
|
|
175
|
+
REQUIRE_OPEN_BACKUP(ctx);
|
|
176
|
+
return INT2NUM(sqlite3_backup_pagecount(ctx->p));
|
|
151
177
|
}
|
|
152
178
|
|
|
153
|
-
void
|
|
179
|
+
void
|
|
180
|
+
init_sqlite3_backup(void)
|
|
154
181
|
{
|
|
155
182
|
#if 0
|
|
156
|
-
|
|
183
|
+
VALUE mSqlite3 = rb_define_module("SQLite3");
|
|
157
184
|
#endif
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
185
|
+
cSqlite3Backup = rb_define_class_under(mSqlite3, "Backup", rb_cObject);
|
|
186
|
+
|
|
187
|
+
rb_define_alloc_func(cSqlite3Backup, allocate);
|
|
188
|
+
rb_define_method(cSqlite3Backup, "initialize", initialize, 4);
|
|
189
|
+
rb_define_method(cSqlite3Backup, "step", step, 1);
|
|
190
|
+
rb_define_method(cSqlite3Backup, "finish", finish, 0);
|
|
191
|
+
rb_define_method(cSqlite3Backup, "remaining", remaining, 0);
|
|
192
|
+
rb_define_method(cSqlite3Backup, "pagecount", pagecount, 0);
|
|
166
193
|
}
|
|
167
194
|
|
|
168
195
|
#endif
|
data/ext/sqlite3/backup.h
CHANGED
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
#include <sqlite3_ruby.h>
|
|
5
5
|
|
|
6
6
|
struct _sqlite3BackupRuby {
|
|
7
|
-
|
|
7
|
+
sqlite3_backup *p;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
typedef struct _sqlite3BackupRuby sqlite3BackupRuby;
|
|
11
|
-
typedef sqlite3BackupRuby *
|
|
11
|
+
typedef sqlite3BackupRuby *sqlite3BackupRubyPtr;
|
|
12
12
|
|
|
13
13
|
void init_sqlite3_backup();
|
|
14
14
|
|