sqlite3 1.3.13-x64-mingw32 → 1.5.0-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sqlite3 might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/.gemtest +0 -0
- data/{API_CHANGES.rdoc → API_CHANGES.md} +3 -4
- data/CHANGELOG.md +425 -0
- data/CONTRIBUTING.md +24 -0
- data/Gemfile +2 -14
- data/LICENSE-DEPENDENCIES +20 -0
- data/README.md +233 -0
- data/ext/sqlite3/aggregator.c +274 -0
- data/ext/sqlite3/aggregator.h +12 -0
- data/ext/sqlite3/database.c +171 -206
- data/ext/sqlite3/database.h +2 -0
- data/ext/sqlite3/exception.c +6 -2
- data/ext/sqlite3/extconf.rb +236 -55
- data/ext/sqlite3/sqlite3.c +12 -1
- data/ext/sqlite3/sqlite3_ruby.h +3 -7
- data/ext/sqlite3/statement.c +15 -20
- data/faq/faq.md +431 -0
- data/faq/faq.yml +1 -1
- data/lib/sqlite3/2.6/sqlite3_native.so +0 -0
- data/lib/sqlite3/2.7/sqlite3_native.so +0 -0
- data/lib/sqlite3/3.0/sqlite3_native.so +0 -0
- data/lib/sqlite3/constants.rb +2 -1
- data/lib/sqlite3/database.rb +202 -52
- data/lib/sqlite3/errors.rb +1 -10
- data/lib/sqlite3/pragmas.rb +17 -10
- data/lib/sqlite3/resultset.rb +2 -10
- data/lib/sqlite3/statement.rb +2 -1
- data/lib/sqlite3/translator.rb +1 -1
- data/lib/sqlite3/version.rb +3 -5
- data/test/helper.rb +9 -0
- data/test/test_database.rb +126 -11
- data/test/test_database_flags.rb +95 -0
- data/test/test_database_readwrite.rb +41 -0
- data/test/test_integration.rb +12 -81
- data/test/test_integration_aggregate.rb +336 -0
- data/test/test_integration_resultset.rb +0 -17
- data/test/test_sqlite3.rb +9 -0
- data/test/test_statement.rb +11 -8
- metadata +54 -85
- data/CHANGELOG.rdoc +0 -292
- data/Manifest.txt +0 -52
- data/README.rdoc +0 -118
- data/Rakefile +0 -10
- data/lib/sqlite3/2.0/sqlite3_native.so +0 -0
- data/lib/sqlite3/2.1/sqlite3_native.so +0 -0
- data/lib/sqlite3/2.2/sqlite3_native.so +0 -0
- data/lib/sqlite3/2.3/sqlite3_native.so +0 -0
- data/lib/sqlite3/2.4/sqlite3_native.so +0 -0
- data/setup.rb +0 -1333
- data/tasks/faq.rake +0 -9
- data/tasks/gem.rake +0 -38
- data/tasks/native.rake +0 -52
- data/tasks/vendor_sqlite3.rake +0 -97
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, the latest available version of libsqlite3 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,274 @@
|
|
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_class_new generatos class with undefined allocator in ruby 1.9 */
|
269
|
+
cAggregatorWrapper = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
270
|
+
rb_gc_register_mark_object(cAggregatorWrapper);
|
271
|
+
|
272
|
+
cAggregatorInstance = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
273
|
+
rb_gc_register_mark_object(cAggregatorInstance);
|
274
|
+
}
|