sqlite3 1.7.3-aarch64-linux → 2.9.5
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 +394 -1
- data/CONTRIBUTING.md +37 -6
- data/FAQ.md +52 -84
- data/INSTALLATION.md +21 -13
- data/LICENSE +18 -22
- data/README.md +97 -9
- data/dependencies.yml +10 -11
- data/ext/sqlite3/aggregator.c +143 -146
- data/ext/sqlite3/aggregator.h +2 -4
- data/ext/sqlite3/backup.c +74 -65
- data/ext/sqlite3/backup.h +2 -2
- data/ext/sqlite3/database.c +619 -495
- data/ext/sqlite3/database.h +13 -4
- data/ext/sqlite3/exception.c +116 -92
- data/ext/sqlite3/exception.h +5 -1
- data/ext/sqlite3/extconf.rb +39 -27
- data/ext/sqlite3/sqlite3.c +176 -115
- data/ext/sqlite3/sqlite3_ruby.h +2 -2
- data/ext/sqlite3/statement.c +588 -300
- data/ext/sqlite3/statement.h +4 -3
- data/ext/sqlite3/timespec.h +20 -0
- data/lib/sqlite3/constants.rb +195 -47
- data/lib/sqlite3/database.rb +228 -187
- 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 +14 -97
- data/lib/sqlite3/statement.rb +58 -13
- 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-3530200.tar.gz +0 -0
- metadata +28 -45
- data/API_CHANGES.md +0 -49
- data/ChangeLog.cvs +0 -88
- data/Gemfile +0 -10
- data/LICENSE-DEPENDENCIES +0 -20
- data/lib/sqlite3/3.0/sqlite3_native.so +0 -0
- data/lib/sqlite3/3.1/sqlite3_native.so +0 -0
- data/lib/sqlite3/3.2/sqlite3_native.so +0 -0
- data/lib/sqlite3/3.3/sqlite3_native.so +0 -0
- data/lib/sqlite3/translator.rb +0 -117
- 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 -668
- 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 -290
- data/test/test_statement_execute.rb +0 -39
data/INSTALLATION.md
CHANGED
|
@@ -7,32 +7,38 @@ This document will help you install the `sqlite3` ruby gem. It also contains ins
|
|
|
7
7
|
|
|
8
8
|
### Native Gems (recommended)
|
|
9
9
|
|
|
10
|
-
In
|
|
10
|
+
In v2.5.0 and later, native (precompiled) gems are available for recent Ruby versions on these platforms:
|
|
11
11
|
|
|
12
|
-
- `aarch64-linux` (requires: glibc >= 2.29)
|
|
13
|
-
- `
|
|
12
|
+
- `aarch64-linux-gnu` (requires: glibc >= 2.29)
|
|
13
|
+
- `aarch64-linux-musl`
|
|
14
|
+
- `arm-linux-gnu` (requires: glibc >= 2.29)
|
|
15
|
+
- `arm-linux-musl`
|
|
14
16
|
- `arm64-darwin`
|
|
15
|
-
- `x64-
|
|
16
|
-
- `x86-linux` (requires: glibc >= 2.
|
|
17
|
+
- `x64-mingw-ucrt`
|
|
18
|
+
- `x86-linux-gnu` (requires: glibc >= 2.29)
|
|
19
|
+
- `x86-linux-musl`
|
|
17
20
|
- `x86_64-darwin`
|
|
18
|
-
- `x86_64-linux` (requires: glibc >= 2.
|
|
21
|
+
- `x86_64-linux-gnu` (requires: glibc >= 2.29)
|
|
22
|
+
- `x86_64-linux-musl`
|
|
23
|
+
|
|
24
|
+
⚠ Musl linux users should update to Bundler >= 2.5.6 to avoid https://github.com/rubygems/rubygems/issues/7432
|
|
19
25
|
|
|
20
26
|
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.
|
|
21
27
|
|
|
22
|
-
For example, on a linux system running Ruby 3.
|
|
28
|
+
For example, on a linux system running Ruby 3.4:
|
|
23
29
|
|
|
24
30
|
``` text
|
|
25
31
|
$ ruby -v
|
|
26
|
-
ruby 3.
|
|
32
|
+
ruby 3.4.7 (2025-10-08 revision 7a5688e2a2) +PRISM [x86_64-linux]
|
|
27
33
|
|
|
28
34
|
$ time gem install sqlite3
|
|
29
|
-
Fetching sqlite3-
|
|
30
|
-
Successfully installed sqlite3-
|
|
35
|
+
Fetching sqlite3-2.8.1-x86_64-linux-gnu.gem
|
|
36
|
+
Successfully installed sqlite3-2.8.1-x86_64-linux-gnu
|
|
31
37
|
1 gem installed
|
|
32
38
|
|
|
33
|
-
real
|
|
34
|
-
user 0m0.
|
|
35
|
-
sys 0m0.
|
|
39
|
+
real 0m1.273s
|
|
40
|
+
user 0m0.496s
|
|
41
|
+
sys 0m0.078s
|
|
36
42
|
```
|
|
37
43
|
|
|
38
44
|
#### Avoiding the precompiled native gem
|
|
@@ -57,6 +63,8 @@ If you are on a platform or version of Ruby that is not covered by the Native Ge
|
|
|
57
63
|
|
|
58
64
|
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.
|
|
59
65
|
|
|
66
|
+
⚠ A prerequisite to build the gem with the packaged sqlite3 is that you must have `pkgconf` installed.
|
|
67
|
+
|
|
60
68
|
For example, on a linux system running Ruby 2.5:
|
|
61
69
|
|
|
62
70
|
``` text
|
data/LICENSE
CHANGED
|
@@ -1,27 +1,23 @@
|
|
|
1
|
-
Copyright (c) 2004, Jamis Buck
|
|
2
|
-
All rights reserved.
|
|
1
|
+
Copyright (c) 2004-2024, Jamis Buck, Luis Lavena, Aaron Patterson, Mike Dalessio, et al.
|
|
3
2
|
|
|
4
|
-
Redistribution and use in source and binary forms, with or without
|
|
5
|
-
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted
|
|
4
|
+
provided that the following conditions are met:
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions
|
|
7
|
+
and the following disclaimer.
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of
|
|
10
|
+
conditions and the following disclaimer in the documentation and/or other materials provided with
|
|
11
|
+
the distribution.
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
permission.
|
|
13
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to
|
|
14
|
+
endorse or promote products derived from this software without specific prior written permission.
|
|
17
15
|
|
|
18
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
|
17
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
19
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
20
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
21
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
|
22
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
23
|
+
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
|
@@ -9,21 +9,20 @@ Note that this module is only compatible with SQLite 3.6.16 or newer.
|
|
|
9
9
|
* Source code: https://github.com/sparklemotion/sqlite3-ruby
|
|
10
10
|
* Mailing list: http://groups.google.com/group/sqlite3-ruby
|
|
11
11
|
* Download: http://rubygems.org/gems/sqlite3
|
|
12
|
-
* Documentation:
|
|
12
|
+
* Documentation: https://sparklemotion.github.io/sqlite3-ruby/
|
|
13
13
|
|
|
14
|
-
[](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/gem-install.yml)
|
|
14
|
+
[](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/ci.yml)
|
|
16
15
|
|
|
17
16
|
|
|
18
17
|
## Quick start
|
|
19
18
|
|
|
20
|
-
For help understanding the SQLite3 Ruby API, please read the [FAQ](./FAQ.md) and the [full API documentation](https://
|
|
19
|
+
For help understanding the SQLite3 Ruby API, please read the [FAQ](./FAQ.md) and the [full API documentation](https://sparklemotion.github.io/sqlite3-ruby/).
|
|
21
20
|
|
|
22
21
|
A few key classes whose APIs are often-used are:
|
|
23
22
|
|
|
24
|
-
- SQLite3::Database ([rdoc](https://
|
|
25
|
-
- SQLite3::Statement ([rdoc](https://
|
|
26
|
-
- SQLite3::ResultSet ([rdoc](https://
|
|
23
|
+
- SQLite3::Database ([rdoc](https://sparklemotion.github.io/sqlite3-ruby/SQLite3/Database.html))
|
|
24
|
+
- SQLite3::Statement ([rdoc](https://sparklemotion.github.io/sqlite3-ruby/SQLite3/Statement.html))
|
|
25
|
+
- SQLite3::ResultSet ([rdoc](https://sparklemotion.github.io/sqlite3-ruby/SQLite3/ResultSet.html))
|
|
27
26
|
|
|
28
27
|
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) or open a [discussion thread](https://github.com/sparklemotion/sqlite3-ruby/discussions/categories/q-a).
|
|
29
28
|
|
|
@@ -77,6 +76,95 @@ end
|
|
|
77
76
|
# => ["Jane", "me@janedoe.com", "A", "http://blog.janedoe.com"]
|
|
78
77
|
```
|
|
79
78
|
|
|
79
|
+
## Thread Safety
|
|
80
|
+
|
|
81
|
+
When `SQLite3.threadsafe?` returns `true`, then SQLite3 has been compiled to
|
|
82
|
+
support running in a multithreaded environment. However, this doesn't mean
|
|
83
|
+
that all classes in the SQLite3 gem can be considered "thread safe".
|
|
84
|
+
|
|
85
|
+
When `SQLite3.threadsafe?` returns `true`, it is safe to share only
|
|
86
|
+
`SQLite3::Database` instances among threads without providing your own locking
|
|
87
|
+
mechanism. For example, the following code is fine because only the database
|
|
88
|
+
instance is shared among threads:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
require 'sqlite3'
|
|
92
|
+
|
|
93
|
+
db = SQLite3::Database.new ":memory:"
|
|
94
|
+
|
|
95
|
+
latch = Queue.new
|
|
96
|
+
|
|
97
|
+
ts = 10.times.map {
|
|
98
|
+
Thread.new {
|
|
99
|
+
latch.pop
|
|
100
|
+
db.execute "SELECT '#{Thread.current.inspect}'"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
10.times { latch << nil }
|
|
104
|
+
|
|
105
|
+
p ts.map(&:value)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Other instances can be shared among threads, but they require that you provide
|
|
109
|
+
your own locking for thread safety. For example, `SQLite3::Statement` objects
|
|
110
|
+
(prepared statements) are mutable, so applications must take care to add
|
|
111
|
+
appropriate locks to avoid data race conditions when sharing these objects
|
|
112
|
+
among threads.
|
|
113
|
+
|
|
114
|
+
Lets rewrite the above example but use a prepared statement and safely share
|
|
115
|
+
the prepared statement among threads:
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
db = SQLite3::Database.new ":memory:"
|
|
119
|
+
|
|
120
|
+
# Prepare a statement
|
|
121
|
+
stmt = db.prepare "SELECT :inspect"
|
|
122
|
+
stmt_lock = Mutex.new
|
|
123
|
+
|
|
124
|
+
latch = Queue.new
|
|
125
|
+
|
|
126
|
+
ts = 10.times.map {
|
|
127
|
+
Thread.new {
|
|
128
|
+
latch.pop
|
|
129
|
+
|
|
130
|
+
# Add a lock when using the prepared statement.
|
|
131
|
+
# Binding values, and walking over results will mutate the statement, so
|
|
132
|
+
# in order to prevent other threads from "seeing" this thread's data, we
|
|
133
|
+
# must lock when using the statement object
|
|
134
|
+
stmt_lock.synchronize do
|
|
135
|
+
stmt.execute(Thread.current.inspect).to_a
|
|
136
|
+
end
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
10.times { latch << nil }
|
|
141
|
+
|
|
142
|
+
p ts.map(&:value)
|
|
143
|
+
|
|
144
|
+
stmt.close
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
It is generally recommended that if applications want to share a database among
|
|
148
|
+
threads, they _only_ share the database instance object. Other objects are
|
|
149
|
+
fine to share, but may require manual locking for thread safety.
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
## Fork Safety
|
|
153
|
+
|
|
154
|
+
[Sqlite is not fork
|
|
155
|
+
safe](https://www.sqlite.org/howtocorrupt.html#_carrying_an_open_database_connection_across_a_fork_)
|
|
156
|
+
and instructs users to not carry an open writable database connection across a `fork()`. Using an inherited
|
|
157
|
+
connection in the child may corrupt your database, leak memory, or cause other undefined behavior.
|
|
158
|
+
|
|
159
|
+
To help protect users of this gem from accidental corruption due to this lack of fork safety, the gem will immediately close any open writable databases in the child after a fork. Discarding writable
|
|
160
|
+
connections in the child will incur a small one-time memory leak per connection, but that's
|
|
161
|
+
preferable to potentially corrupting your database.
|
|
162
|
+
|
|
163
|
+
Whenever possible, close writable connections in the parent before forking. If absolutely necessary (and you know what you're doing), you may suppress the fork safety warnings by calling `SQLite3::ForkSafety.suppress_warnings!`.
|
|
164
|
+
|
|
165
|
+
See [./adr/2024-09-fork-safety.md](./adr/2024-09-fork-safety.md) for more information and context.
|
|
166
|
+
|
|
167
|
+
|
|
80
168
|
## Support
|
|
81
169
|
|
|
82
170
|
### Installation or database extensions
|
|
@@ -104,7 +192,7 @@ See [`CONTRIBUTING.md`](./CONTRIBUTING.md).
|
|
|
104
192
|
|
|
105
193
|
This library is licensed under `BSD-3-Clause`, see [`LICENSE`](./LICENSE).
|
|
106
194
|
|
|
107
|
-
|
|
108
195
|
### Dependencies
|
|
109
196
|
|
|
110
|
-
The source code of `sqlite` is distributed in the "ruby platform" gem. This code is public domain,
|
|
197
|
+
The source code of `sqlite` is distributed in the "ruby platform" gem. This code is public domain,
|
|
198
|
+
see https://www.sqlite.org/copyright.html for details.
|
data/dependencies.yml
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
:sqlite3:
|
|
1
|
+
sqlite3:
|
|
3
2
|
# checksum verified by first checking the published sha3(256) checksum against https://sqlite.org/download.html:
|
|
4
|
-
#
|
|
3
|
+
# 025328da165109f48abccc6e7478508060804412bed2bd81d47e98ba1b72983b
|
|
5
4
|
#
|
|
6
|
-
# $ sha3sum -a 256 ports/archives/sqlite-autoconf-
|
|
7
|
-
#
|
|
5
|
+
# $ sha3sum -a 256 ports/archives/sqlite-autoconf-3530200.tar.gz
|
|
6
|
+
# 025328da165109f48abccc6e7478508060804412bed2bd81d47e98ba1b72983b ports/archives/sqlite-autoconf-3530200.tar.gz
|
|
8
7
|
#
|
|
9
|
-
# $ sha256sum ports/archives/sqlite-autoconf-
|
|
10
|
-
#
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
-
|
|
14
|
-
|
|
8
|
+
# $ sha256sum ports/archives/sqlite-autoconf-3530200.tar.gz
|
|
9
|
+
# 588ad51949419a56ebe81fe56193d510c559eb94c9a57748387860b5d3069316 ports/archives/sqlite-autoconf-3530200.tar.gz
|
|
10
|
+
version: "3.53.2"
|
|
11
|
+
files:
|
|
12
|
+
- url: "https://sqlite.org/2026/sqlite-autoconf-3530200.tar.gz"
|
|
13
|
+
sha256: "588ad51949419a56ebe81fe56193d510c559eb94c9a57748387860b5d3069316"
|
data/ext/sqlite3/aggregator.c
CHANGED
|
@@ -31,20 +31,20 @@ typedef struct rb_sqlite3_protected_funcall_args {
|
|
|
31
31
|
static VALUE
|
|
32
32
|
rb_sqlite3_protected_funcall_body(VALUE protected_funcall_args_ptr)
|
|
33
33
|
{
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
protected_funcall_args_t *args =
|
|
35
|
+
(protected_funcall_args_t *)protected_funcall_args_ptr;
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
return rb_funcall2(args->self, args->method, args->argc, args->params);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
static VALUE
|
|
41
41
|
rb_sqlite3_protected_funcall(VALUE self, ID method, int argc, VALUE *params,
|
|
42
|
-
int*
|
|
42
|
+
int *exc_status)
|
|
43
43
|
{
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
48
|
}
|
|
49
49
|
|
|
50
50
|
/* called in rb_sqlite3_aggregator_step and rb_sqlite3_aggregator_final. It
|
|
@@ -54,36 +54,36 @@ rb_sqlite3_protected_funcall(VALUE self, ID method, int argc, VALUE *params,
|
|
|
54
54
|
static VALUE
|
|
55
55
|
rb_sqlite3_aggregate_instance(sqlite3_context *ctx)
|
|
56
56
|
{
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
if (!inst_ptr) {
|
|
63
|
+
rb_fatal("SQLite is out-of-memory");
|
|
64
|
+
}
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
inst = *inst_ptr;
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
if (inst == Qfalse) { /* Qfalse == 0 */
|
|
69
|
+
VALUE instances = rb_iv_get(aw, "-instances");
|
|
70
|
+
int exc_status;
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
76
|
|
|
77
|
-
|
|
77
|
+
rb_ary_push(instances, inst);
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
*inst_ptr = inst;
|
|
80
|
+
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
if (inst == Qnil) {
|
|
83
|
+
rb_fatal("SQLite called us back on an already destroyed aggregate instance");
|
|
84
|
+
}
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
return inst;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/* called by rb_sqlite3_aggregator_final. Unlinks and frees the
|
|
@@ -92,89 +92,89 @@ rb_sqlite3_aggregate_instance(sqlite3_context *ctx)
|
|
|
92
92
|
static void
|
|
93
93
|
rb_sqlite3_aggregate_instance_destroy(sqlite3_context *ctx)
|
|
94
94
|
{
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
99
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
if (!inst_ptr || (inst = *inst_ptr)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
104
|
+
if (inst == Qnil) {
|
|
105
|
+
rb_fatal("attempt to destroy aggregate instance twice");
|
|
106
|
+
}
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
112
|
|
|
113
|
-
|
|
113
|
+
*inst_ptr = Qnil;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
static void
|
|
117
|
-
rb_sqlite3_aggregator_step(sqlite3_context *
|
|
117
|
+
rb_sqlite3_aggregator_step(sqlite3_context *ctx, int argc, sqlite3_value **argv)
|
|
118
118
|
{
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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]);
|
|
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;
|
|
138
133
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
147
|
}
|
|
148
148
|
|
|
149
149
|
/* we assume that this function is only called once per execution context */
|
|
150
150
|
static void
|
|
151
|
-
rb_sqlite3_aggregator_final(sqlite3_context *
|
|
151
|
+
rb_sqlite3_aggregator_final(sqlite3_context *ctx)
|
|
152
152
|
{
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
156
|
|
|
157
|
-
if (!exc_status) {
|
|
158
|
-
VALUE result = rb_sqlite3_protected_funcall(
|
|
159
|
-
handler_instance, rb_intern("finalize"), 0, NULL, &exc_status);
|
|
160
157
|
if (!exc_status) {
|
|
161
|
-
|
|
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
|
+
}
|
|
162
163
|
}
|
|
163
|
-
}
|
|
164
164
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
171
|
|
|
172
|
-
|
|
172
|
+
rb_sqlite3_aggregate_instance_destroy(ctx);
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
/* call-seq: define_aggregator2(aggregator)
|
|
176
176
|
*
|
|
177
|
-
* Define an
|
|
177
|
+
* Define an aggregate function according to a factory object (the "handler")
|
|
178
178
|
* that knows how to obtain to all the information. The handler must provide
|
|
179
179
|
* the following class methods:
|
|
180
180
|
*
|
|
@@ -205,69 +205,66 @@ rb_sqlite3_aggregator_final(sqlite3_context * ctx)
|
|
|
205
205
|
VALUE
|
|
206
206
|
rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator, VALUE ruby_name)
|
|
207
207
|
{
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
208
|
+
/* define_aggregator is added as a method to SQLite3::Database in database.c */
|
|
209
|
+
sqlite3RubyPtr ctx = sqlite3_database_unwrap(self);
|
|
210
|
+
int arity, status;
|
|
211
|
+
VALUE aw;
|
|
212
|
+
VALUE aggregators;
|
|
213
|
+
|
|
214
|
+
if (!ctx->db) {
|
|
215
|
+
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (rb_respond_to(aggregator, rb_intern("arity"))) {
|
|
219
|
+
VALUE ruby_arity = rb_funcall(aggregator, rb_intern("arity"), 0);
|
|
220
|
+
arity = NUM2INT(ruby_arity);
|
|
221
|
+
} else {
|
|
222
|
+
arity = -1;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (arity < -1 || arity > 127) {
|
|
226
226
|
#ifdef PRIsVALUE
|
|
227
|
-
|
|
228
|
-
|
|
227
|
+
rb_raise(rb_eArgError, "%"PRIsVALUE" arity=%d out of range -1..127",
|
|
228
|
+
self, arity);
|
|
229
229
|
#else
|
|
230
|
-
|
|
230
|
+
rb_raise(rb_eArgError, "Aggregator arity=%d out of range -1..127", arity);
|
|
231
231
|
#endif
|
|
232
|
-
|
|
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
|
-
rb_ary_push(aggregators, aw);
|
|
260
|
-
|
|
261
|
-
return self;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (!rb_ivar_defined(self, rb_intern("-aggregators"))) {
|
|
235
|
+
rb_iv_set(self, "-aggregators", rb_ary_new());
|
|
236
|
+
}
|
|
237
|
+
aggregators = rb_iv_get(self, "-aggregators");
|
|
238
|
+
|
|
239
|
+
aw = rb_class_new_instance(0, NULL, cAggregatorWrapper);
|
|
240
|
+
rb_iv_set(aw, "-handler_klass", aggregator);
|
|
241
|
+
rb_iv_set(aw, "-instances", rb_ary_new());
|
|
242
|
+
|
|
243
|
+
status = sqlite3_create_function(
|
|
244
|
+
ctx->db,
|
|
245
|
+
StringValueCStr(ruby_name),
|
|
246
|
+
arity,
|
|
247
|
+
SQLITE_UTF8,
|
|
248
|
+
(void *)aw,
|
|
249
|
+
NULL,
|
|
250
|
+
rb_sqlite3_aggregator_step,
|
|
251
|
+
rb_sqlite3_aggregator_final
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
CHECK(ctx->db, status);
|
|
255
|
+
|
|
256
|
+
rb_ary_push(aggregators, aw);
|
|
257
|
+
|
|
258
|
+
return self;
|
|
262
259
|
}
|
|
263
260
|
|
|
264
261
|
void
|
|
265
262
|
rb_sqlite3_aggregator_init(void)
|
|
266
263
|
{
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
264
|
+
/* rb_class_new generatos class with undefined allocator in ruby 1.9 */
|
|
265
|
+
cAggregatorWrapper = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
|
266
|
+
rb_gc_register_mark_object(cAggregatorWrapper);
|
|
270
267
|
|
|
271
|
-
|
|
272
|
-
|
|
268
|
+
cAggregatorInstance = rb_funcall(rb_cClass, rb_intern("new"), 0);
|
|
269
|
+
rb_gc_register_mark_object(cAggregatorInstance);
|
|
273
270
|
}
|
data/ext/sqlite3/aggregator.h
CHANGED
|
@@ -3,10 +3,8 @@
|
|
|
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
|
-
rb_sqlite3_aggregator_init(void);
|
|
8
|
+
void rb_sqlite3_aggregator_init(void);
|
|
11
9
|
|
|
12
10
|
#endif
|