sqlite3 1.5.0.rc1-x86_64-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/API_CHANGES.md +49 -0
  4. data/CHANGELOG.md +419 -0
  5. data/CONTRIBUTING.md +24 -0
  6. data/ChangeLog.cvs +88 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE +27 -0
  9. data/LICENSE-DEPENDENCIES +20 -0
  10. data/README.md +233 -0
  11. data/ext/sqlite3/aggregator.c +273 -0
  12. data/ext/sqlite3/aggregator.h +12 -0
  13. data/ext/sqlite3/backup.c +168 -0
  14. data/ext/sqlite3/backup.h +15 -0
  15. data/ext/sqlite3/database.c +853 -0
  16. data/ext/sqlite3/database.h +17 -0
  17. data/ext/sqlite3/exception.c +98 -0
  18. data/ext/sqlite3/exception.h +8 -0
  19. data/ext/sqlite3/extconf.rb +159 -0
  20. data/ext/sqlite3/sqlite3.c +163 -0
  21. data/ext/sqlite3/sqlite3_ruby.h +45 -0
  22. data/ext/sqlite3/statement.c +442 -0
  23. data/ext/sqlite3/statement.h +16 -0
  24. data/faq/faq.md +431 -0
  25. data/faq/faq.rb +145 -0
  26. data/faq/faq.yml +426 -0
  27. data/lib/sqlite3/2.6/sqlite3_native.so +0 -0
  28. data/lib/sqlite3/2.7/sqlite3_native.so +0 -0
  29. data/lib/sqlite3/3.0/sqlite3_native.so +0 -0
  30. data/lib/sqlite3/3.1/sqlite3_native.so +0 -0
  31. data/lib/sqlite3/constants.rb +50 -0
  32. data/lib/sqlite3/database.rb +741 -0
  33. data/lib/sqlite3/errors.rb +35 -0
  34. data/lib/sqlite3/pragmas.rb +595 -0
  35. data/lib/sqlite3/resultset.rb +187 -0
  36. data/lib/sqlite3/statement.rb +145 -0
  37. data/lib/sqlite3/translator.rb +118 -0
  38. data/lib/sqlite3/value.rb +57 -0
  39. data/lib/sqlite3/version.rb +25 -0
  40. data/lib/sqlite3.rb +15 -0
  41. data/test/helper.rb +27 -0
  42. data/test/test_backup.rb +33 -0
  43. data/test/test_collation.rb +82 -0
  44. data/test/test_database.rb +538 -0
  45. data/test/test_database_flags.rb +95 -0
  46. data/test/test_database_readonly.rb +36 -0
  47. data/test/test_database_readwrite.rb +41 -0
  48. data/test/test_deprecated.rb +44 -0
  49. data/test/test_encoding.rb +155 -0
  50. data/test/test_integration.rb +507 -0
  51. data/test/test_integration_aggregate.rb +336 -0
  52. data/test/test_integration_open_close.rb +30 -0
  53. data/test/test_integration_pending.rb +115 -0
  54. data/test/test_integration_resultset.rb +142 -0
  55. data/test/test_integration_statement.rb +194 -0
  56. data/test/test_result_set.rb +37 -0
  57. data/test/test_sqlite3.rb +25 -0
  58. data/test/test_statement.rb +263 -0
  59. data/test/test_statement_execute.rb +35 -0
  60. metadata +190 -0
data/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # Ruby Interface for SQLite3
2
+
3
+ * Source code: https://github.com/sparklemotion/sqlite3-ruby
4
+ * Mailing list: http://groups.google.com/group/sqlite3-ruby
5
+ * Download: http://rubygems.org/gems/sqlite3
6
+ * Documentation: http://www.rubydoc.info/gems/sqlite3
7
+
8
+ [![Unit tests](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/sqlite3-ruby.yml/badge.svg)](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/sqlite3-ruby.yml)
9
+ [![Native packages](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/gem-install.yml/badge.svg)](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/gem-install.yml)
10
+
11
+
12
+ ## Description
13
+
14
+ This library allows Ruby programs to use the SQLite3 database engine (http://www.sqlite.org).
15
+
16
+ Note that this module is only compatible with SQLite 3.6.16 or newer.
17
+
18
+
19
+ ## Synopsis
20
+
21
+ ``` ruby
22
+ require "sqlite3"
23
+
24
+ # Open a database
25
+ db = SQLite3::Database.new "test.db"
26
+
27
+ # Create a table
28
+ rows = db.execute <<-SQL
29
+ create table numbers (
30
+ name varchar(30),
31
+ val int
32
+ );
33
+ SQL
34
+
35
+ # Execute a few inserts
36
+ {
37
+ "one" => 1,
38
+ "two" => 2,
39
+ }.each do |pair|
40
+ db.execute "insert into numbers values ( ?, ? )", pair
41
+ end
42
+
43
+ # Find a few rows
44
+ db.execute( "select * from numbers" ) do |row|
45
+ p row
46
+ end
47
+ # => ["one", 1]
48
+ # ["two", 2]
49
+
50
+ # Create another table with multiple columns
51
+ db.execute <<-SQL
52
+ create table students (
53
+ name varchar(50),
54
+ email varchar(50),
55
+ grade varchar(5),
56
+ blog varchar(50)
57
+ );
58
+ SQL
59
+
60
+ # Execute inserts with parameter markers
61
+ db.execute("INSERT INTO students (name, email, grade, blog)
62
+ VALUES (?, ?, ?, ?)", ["Jane", "me@janedoe.com", "A", "http://blog.janedoe.com"])
63
+
64
+ db.execute( "select * from students" ) do |row|
65
+ p row
66
+ end
67
+ # => ["Jane", "me@janedoe.com", "A", "http://blog.janedoe.com"]
68
+ ```
69
+
70
+ ## Installation
71
+
72
+ ### Native Gems (recommended)
73
+
74
+ As of v1.5.0 of this library, native (precompiled) gems are available for Ruby 2.6, 2.7, 3.0, and 3.1 on all these platforms:
75
+
76
+ - `aarch64-linux`
77
+ - `arm-linux`
78
+ - `arm64-darwin`
79
+ - `x64-mingw32` / `x64-mingw-ucrt`
80
+ - `x86-linux`
81
+ - `x86_64-darwin`
82
+ - `x86_64-linux`
83
+
84
+ If you are using one of these Ruby versions on one of these platforms, the native gem is the recommended way to install sqlite3-ruby.
85
+
86
+ For example, on a linux system running Ruby 3.1:
87
+
88
+ ``` text
89
+ $ ruby -v
90
+ ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
91
+
92
+ $ time gem install sqlite3
93
+ Fetching sqlite3-1.5.0-x86_64-linux.gem
94
+ Successfully installed sqlite3-1.5.0-x86_64-linux
95
+ 1 gem installed
96
+
97
+ real 0m4.274s
98
+ user 0m0.734s
99
+ sys 0m0.165s
100
+ ```
101
+
102
+ #### Avoiding the precompiled native gem
103
+
104
+ The maintainers strongly urge you to use a native gem if at all possible. It will be a better experience for you and allow us to focus our efforts on improving functionality rather than diagnosing installation issues.
105
+
106
+ If you're on a platform that supports a native gem but you want to avoid using it in your project, do one of the following:
107
+
108
+ - If you're not using Bundler, then run `gem install sqlite3 --platform=ruby`
109
+ - If you are using Bundler
110
+ - version 2.1 or later, then you'll need to run `bundle config set force_ruby_platform true`,
111
+ - version 2.0 or earlier, then you'll need to run `bundle config force_ruby_platform true`
112
+
113
+
114
+ ### Compiling the source gem
115
+
116
+ If you are on a platform or version of Ruby that is not covered by the Native Gems, then the vanilla "ruby platform" (non-native) gem will be installed by the `gem install` or `bundle` commands.
117
+
118
+
119
+ #### Packaged libsqlite3
120
+
121
+ By default, as of v1.5.0 of this library, libsqlite3 v3.38.5 is packaged with the gem and will be compiled and used automatically. This takes a bit longer than the native gem, but will provide a modern, well-supported version of libsqlite3.
122
+
123
+ For example, on a linux system running Ruby 2.5:
124
+
125
+ ``` text
126
+ $ ruby -v
127
+ ruby 2.5.9p229 (2021-04-05 revision 67939) [x86_64-linux]
128
+
129
+ $ time gem install sqlite3
130
+ Building native extensions. This could take a while...
131
+ Successfully installed sqlite3-1.5.0
132
+ 1 gem installed
133
+
134
+ real 0m20.620s
135
+ user 0m23.361s
136
+ sys 0m5.839s
137
+ ```
138
+
139
+
140
+ #### System libsqlite3
141
+
142
+ If you would prefer to build the sqlite3-ruby gem against your system libsqlite3, which requires that you install libsqlite3 and its development files yourself, you may do so by using the `--enable-system-libraries` flag at gem install time.
143
+
144
+ PLEASE NOTE:
145
+
146
+ - only versions of libsqlite3 `>= 3.5.0` are supported,
147
+ - and some library features may depend on how your libsqlite3 was compiled.
148
+
149
+ For example, on a linux system running Ruby 2.5:
150
+
151
+ ``` text
152
+ $ time gem install sqlite3 -- --enable-system-libraries
153
+ Building native extensions with: '--enable-system-libraries'
154
+ This could take a while...
155
+ Successfully installed sqlite3-1.5.0
156
+ 1 gem installed
157
+
158
+ real 0m4.234s
159
+ user 0m3.809s
160
+ sys 0m0.912s
161
+ ```
162
+
163
+ If you're using bundler, you can opt into system libraries like this:
164
+
165
+ ``` sh
166
+ bundle config build.sqlite3 --enable-system-libraries
167
+ ```
168
+
169
+ If you have sqlite3 installed in a non-standard location, you may need to specify the location of the include and lib files by using `--with-sqlite-include` and `--with-sqlite-lib` options (or a `--with-sqlite-dir` option, see [MakeMakefile#dir_config](https://ruby-doc.org/stdlib-3.1.1/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-dir_config)). If you have pkg-config installed and configured properly, this may not be necessary.
170
+
171
+ ``` sh
172
+ gem install sqlite3 -- \
173
+ --enable-system-libraries \
174
+ --with-sqlite3-include=/opt/local/include \
175
+ --with-sqlite3-lib=/opt/local/lib
176
+ ```
177
+
178
+
179
+ #### System libsqlcipher
180
+
181
+ If you'd like to link against a system-installed libsqlcipher, you may do so by using the `--with-sqlcipher` flag:
182
+
183
+ ``` text
184
+ $ time gem install sqlite3 -- --with-sqlcipher
185
+ Building native extensions with: '--with-sqlcipher'
186
+ This could take a while...
187
+ Successfully installed sqlite3-1.5.0
188
+ 1 gem installed
189
+
190
+ real 0m4.772s
191
+ user 0m3.906s
192
+ sys 0m0.896s
193
+ ```
194
+
195
+ If you have sqlcipher installed in a non-standard location, you may need to specify the location of the include and lib files by using `--with-sqlite-include` and `--with-sqlite-lib` options (or a `--with-sqlite-dir` option, see [MakeMakefile#dir_config](https://ruby-doc.org/stdlib-3.1.1/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-dir_config)). If you have pkg-config installed and configured properly, this may not be necessary.
196
+
197
+
198
+ ## Support
199
+
200
+ ### Something has gone wrong! Where do I get help?
201
+
202
+ You can ask for help or support from the
203
+ [sqlite3-ruby mailing list](http://groups.google.com/group/sqlite3-ruby) which
204
+ can be found here:
205
+
206
+ > http://groups.google.com/group/sqlite3-ruby
207
+
208
+
209
+ ### I've found a bug! How do I report it?
210
+
211
+ After contacting the mailing list, you've found that you've uncovered a bug. You can file the bug at the [github issues page](https://github.com/sparklemotion/sqlite3-ruby/issues) which can be found here:
212
+
213
+ > https://github.com/sparklemotion/sqlite3-ruby/issues
214
+
215
+
216
+ ## Usage
217
+
218
+ For help figuring out the SQLite3/Ruby interface, check out the SYNOPSIS as well as the RDoc. It includes examples of usage. If you have any questions that you feel should be addressed in the FAQ, please send them to [the mailing list](http://groups.google.com/group/sqlite3-ruby).
219
+
220
+
221
+ ## Contributing
222
+
223
+ See [`CONTRIBUTING.md`](./CONTRIBUTING.md).
224
+
225
+
226
+ ## License
227
+
228
+ This library is licensed under `BSD-3-Clause`, see [`LICENSE`](./LICENSE).
229
+
230
+
231
+ ### Dependencies
232
+
233
+ The source code of `sqlite` is distributed in the "ruby platform" gem. This code is public domain, see [`LICENSE-DEPENDENCIES`](./LICENSE-DEPENDENCIES) for details.
@@ -0,0 +1,273 @@
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-merory");
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 one_param;
123
+ int exc_status = NUM2INT(rb_iv_get(inst, "-exc_status"));
124
+ int i;
125
+
126
+ if (exc_status) {
127
+ return;
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]);
138
+ }
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
+ }
148
+
149
+ /* we assume that this function is only called once per execution context */
150
+ static void
151
+ rb_sqlite3_aggregator_final(sqlite3_context * ctx)
152
+ {
153
+ VALUE inst = rb_sqlite3_aggregate_instance(ctx);
154
+ VALUE handler_instance = rb_iv_get(inst, "-handler_instance");
155
+ int exc_status = NUM2INT(rb_iv_get(inst, "-exc_status"));
156
+
157
+ if (!exc_status) {
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);
162
+ }
163
+ }
164
+
165
+ if (exc_status) {
166
+ /* the user should never see this, as Statement.step() will pick up the
167
+ * outstanding exception and raise it instead of generating a new one
168
+ * for SQLITE_ERROR with message "Ruby Exception occurred" */
169
+ sqlite3_result_error(ctx, "Ruby Exception occurred", -1);
170
+ }
171
+
172
+ rb_sqlite3_aggregate_instance_destroy(ctx);
173
+ }
174
+
175
+ /* call-seq: define_aggregator2(aggregator)
176
+ *
177
+ * Define an aggregrate function according to a factory object (the "handler")
178
+ * that knows how to obtain to all the information. The handler must provide
179
+ * the following class methods:
180
+ *
181
+ * +arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
182
+ * message is optional, and if the handler does not respond to it,
183
+ * the function will have an arity of -1.
184
+ * +name+:: this is the name of the function. The handler _must_ implement
185
+ * this message.
186
+ * +new+:: this must be implemented by the handler. It should return a new
187
+ * instance of the object that will handle a specific invocation of
188
+ * the function.
189
+ *
190
+ * The handler instance (the object returned by the +new+ message, described
191
+ * above), must respond to the following messages:
192
+ *
193
+ * +step+:: this is the method that will be called for each step of the
194
+ * aggregate function's evaluation. It should take parameters according
195
+ * to the *arity* definition.
196
+ * +finalize+:: this is the method that will be called to finalize the
197
+ * aggregate function's evaluation. It should not take arguments.
198
+ *
199
+ * Note the difference between this function and #create_aggregate_handler
200
+ * is that no FunctionProxy ("ctx") object is involved. This manifests in two
201
+ * ways: The return value of the aggregate function is the return value of
202
+ * +finalize+ and neither +step+ nor +finalize+ take an additional "ctx"
203
+ * parameter.
204
+ */
205
+ VALUE
206
+ rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name)
207
+ {
208
+ /* define_aggregator is added as a method to SQLite3::Database in database.c */
209
+ sqlite3RubyPtr ctx;
210
+ int arity, status;
211
+ VALUE aw;
212
+ VALUE aggregators;
213
+
214
+ Data_Get_Struct(self, sqlite3Ruby, ctx);
215
+ if (!ctx->db) {
216
+ rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");
217
+ }
218
+
219
+ if (rb_respond_to(aggregator, rb_intern("arity"))) {
220
+ VALUE ruby_arity = rb_funcall(aggregator, rb_intern("arity"), 0);
221
+ arity = NUM2INT(ruby_arity);
222
+ } else {
223
+ arity = -1;
224
+ }
225
+
226
+ if (arity < -1 || arity > 127) {
227
+ #ifdef PRIsVALUE
228
+ rb_raise(rb_eArgError, "%"PRIsVALUE" arity=%d out of range -1..127",
229
+ self, arity);
230
+ #else
231
+ rb_raise(rb_eArgError, "Aggregator arity=%d out of range -1..127", arity);
232
+ #endif
233
+ }
234
+
235
+ if (!rb_ivar_defined(self, rb_intern("-aggregators"))) {
236
+ rb_iv_set(self, "-aggregators", rb_ary_new());
237
+ }
238
+ aggregators = rb_iv_get(self, "-aggregators");
239
+
240
+ aw = rb_class_new_instance(0, NULL, cAggregatorWrapper);
241
+ rb_iv_set(aw, "-handler_klass", aggregator);
242
+ rb_iv_set(aw, "-instances", rb_ary_new());
243
+
244
+ status = sqlite3_create_function(
245
+ ctx->db,
246
+ StringValueCStr(ruby_name),
247
+ arity,
248
+ SQLITE_UTF8,
249
+ (void*)aw,
250
+ NULL,
251
+ rb_sqlite3_aggregator_step,
252
+ rb_sqlite3_aggregator_final
253
+ );
254
+
255
+ if (status != SQLITE_OK) {
256
+ rb_sqlite3_raise(ctx->db, status);
257
+ return self; // just in case rb_sqlite3_raise returns.
258
+ }
259
+
260
+ rb_ary_push(aggregators, aw);
261
+
262
+ return self;
263
+ }
264
+
265
+ void
266
+ rb_sqlite3_aggregator_init(void)
267
+ {
268
+ rb_gc_register_address(&cAggregatorWrapper);
269
+ rb_gc_register_address(&cAggregatorInstance);
270
+ /* rb_class_new generatos class with undefined allocator in ruby 1.9 */
271
+ cAggregatorWrapper = rb_funcall(rb_cClass, rb_intern("new"), 0);
272
+ cAggregatorInstance = rb_funcall(rb_cClass, rb_intern("new"), 0);
273
+ }
@@ -0,0 +1,12 @@
1
+ #ifndef SQLITE3_AGGREGATOR_RUBY
2
+ #define SQLITE3_AGGREGATOR_RUBY
3
+
4
+ #include <sqlite3_ruby.h>
5
+
6
+ VALUE
7
+ rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name);
8
+
9
+ void
10
+ rb_sqlite3_aggregator_init(void);
11
+
12
+ #endif
@@ -0,0 +1,168 @@
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 void deallocate(void * ctx)
12
+ {
13
+ sqlite3BackupRubyPtr c = (sqlite3BackupRubyPtr)ctx;
14
+ xfree(c);
15
+ }
16
+
17
+ static VALUE allocate(VALUE klass)
18
+ {
19
+ sqlite3BackupRubyPtr ctx = xcalloc((size_t)1, sizeof(sqlite3BackupRuby));
20
+ return Data_Wrap_Struct(klass, NULL, deallocate, ctx);
21
+ }
22
+
23
+ /* call-seq: SQLite3::Backup.new(dstdb, dstname, srcdb, srcname)
24
+ *
25
+ * Initialize backup the backup.
26
+ *
27
+ * dstdb:
28
+ * the destination SQLite3::Database object.
29
+ * dstname:
30
+ * the destination's database name.
31
+ * srcdb:
32
+ * the source SQLite3::Database object.
33
+ * srcname:
34
+ * the source's database name.
35
+ *
36
+ * The database name is "main", "temp", or the name specified in an
37
+ * ATTACH statement.
38
+ *
39
+ * This feature requires SQLite 3.6.11 or later.
40
+ *
41
+ * require 'sqlite3'
42
+ * sdb = SQLite3::Database.new('src.sqlite3')
43
+ *
44
+ * ddb = SQLite3::Database.new(':memory:')
45
+ * b = SQLite3::Backup.new(ddb, 'main', sdb, 'main')
46
+ * p [b.remaining, b.pagecount] # invalid value; for example [0, 0]
47
+ * begin
48
+ * p b.step(1) #=> OK or DONE
49
+ * p [b.remaining, b.pagecount]
50
+ * end while b.remaining > 0
51
+ * b.finish
52
+ *
53
+ * ddb = SQLite3::Database.new(':memory:')
54
+ * b = SQLite3::Backup.new(ddb, 'main', sdb, 'main')
55
+ * b.step(-1) #=> DONE
56
+ * b.finish
57
+ *
58
+ */
59
+ static VALUE initialize(VALUE self, VALUE dstdb, VALUE dstname, VALUE srcdb, VALUE srcname)
60
+ {
61
+ sqlite3BackupRubyPtr ctx;
62
+ sqlite3RubyPtr ddb_ctx, sdb_ctx;
63
+ sqlite3_backup *pBackup;
64
+
65
+ Data_Get_Struct(self, sqlite3BackupRuby, ctx);
66
+ Data_Get_Struct(dstdb, sqlite3Ruby, ddb_ctx);
67
+ Data_Get_Struct(srcdb, sqlite3Ruby, sdb_ctx);
68
+
69
+ if(!sdb_ctx->db)
70
+ rb_raise(rb_eArgError, "cannot backup from a closed database");
71
+ if(!ddb_ctx->db)
72
+ rb_raise(rb_eArgError, "cannot backup to a closed database");
73
+
74
+ pBackup = sqlite3_backup_init(ddb_ctx->db, StringValuePtr(dstname),
75
+ sdb_ctx->db, StringValuePtr(srcname));
76
+ if( pBackup ){
77
+ ctx->p = pBackup;
78
+ }
79
+ else {
80
+ CHECK(ddb_ctx->db, sqlite3_errcode(ddb_ctx->db));
81
+ }
82
+
83
+ return self;
84
+ }
85
+
86
+ /* call-seq: SQLite3::Backup#step(nPage)
87
+ *
88
+ * Copy database pages up to +nPage+.
89
+ * If negative, copy all remaining source pages.
90
+ *
91
+ * If all pages are copied, it returns SQLite3::Constants::ErrorCode::DONE.
92
+ * When coping is not done, it returns SQLite3::Constants::ErrorCode::OK.
93
+ * When some errors occur, it returns the error code.
94
+ */
95
+ static VALUE step(VALUE self, VALUE nPage)
96
+ {
97
+ sqlite3BackupRubyPtr ctx;
98
+ int status;
99
+
100
+ Data_Get_Struct(self, sqlite3BackupRuby, ctx);
101
+ REQUIRE_OPEN_BACKUP(ctx);
102
+ status = sqlite3_backup_step(ctx->p, NUM2INT(nPage));
103
+ return INT2NUM(status);
104
+ }
105
+
106
+ /* call-seq: SQLite3::Backup#finish
107
+ *
108
+ * Destroy the backup object.
109
+ */
110
+ static VALUE finish(VALUE self)
111
+ {
112
+ sqlite3BackupRubyPtr ctx;
113
+
114
+ Data_Get_Struct(self, sqlite3BackupRuby, ctx);
115
+ REQUIRE_OPEN_BACKUP(ctx);
116
+ (void)sqlite3_backup_finish(ctx->p);
117
+ ctx->p = NULL;
118
+ return Qnil;
119
+ }
120
+
121
+ /* call-seq: SQLite3::Backup#remaining
122
+ *
123
+ * Returns the number of pages still to be backed up.
124
+ *
125
+ * Note that the value is only updated after step() is called,
126
+ * so before calling step() returned value is invalid.
127
+ */
128
+ static VALUE remaining(VALUE self)
129
+ {
130
+ sqlite3BackupRubyPtr ctx;
131
+
132
+ Data_Get_Struct(self, sqlite3BackupRuby, ctx);
133
+ REQUIRE_OPEN_BACKUP(ctx);
134
+ return INT2NUM(sqlite3_backup_remaining(ctx->p));
135
+ }
136
+
137
+ /* call-seq: SQLite3::Backup#pagecount
138
+ *
139
+ * Returns the total number of pages in the source database file.
140
+ *
141
+ * Note that the value is only updated after step() is called,
142
+ * so before calling step() returned value is invalid.
143
+ */
144
+ static VALUE pagecount(VALUE self)
145
+ {
146
+ sqlite3BackupRubyPtr ctx;
147
+
148
+ Data_Get_Struct(self, sqlite3BackupRuby, ctx);
149
+ REQUIRE_OPEN_BACKUP(ctx);
150
+ return INT2NUM(sqlite3_backup_pagecount(ctx->p));
151
+ }
152
+
153
+ void init_sqlite3_backup()
154
+ {
155
+ #if 0
156
+ VALUE mSqlite3 = rb_define_module("SQLite3");
157
+ #endif
158
+ cSqlite3Backup = rb_define_class_under(mSqlite3, "Backup", rb_cObject);
159
+
160
+ rb_define_alloc_func(cSqlite3Backup, allocate);
161
+ rb_define_method(cSqlite3Backup, "initialize", initialize, 4);
162
+ rb_define_method(cSqlite3Backup, "step", step, 1);
163
+ rb_define_method(cSqlite3Backup, "finish", finish, 0);
164
+ rb_define_method(cSqlite3Backup, "remaining", remaining, 0);
165
+ rb_define_method(cSqlite3Backup, "pagecount", pagecount, 0);
166
+ }
167
+
168
+ #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