stoolap 0.4.1 → 0.5.0.beta.1
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/Cargo.lock +78 -739
- data/ext/stoolap/Cargo.toml +4 -3
- data/ext/stoolap/src/database.rs +108 -77
- data/ext/stoolap/src/error.rs +13 -0
- data/ext/stoolap/src/statement.rs +48 -54
- data/ext/stoolap/src/transaction.rs +36 -54
- data/ext/stoolap/src/value.rs +32 -25
- data/lib/stoolap/version.rb +1 -1
- metadata +2 -2
data/ext/stoolap/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "stoolap"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.5.0-beta.1"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
authors = ["Stoolap Contributors"]
|
|
6
6
|
license = "Apache-2.0"
|
|
@@ -14,7 +14,8 @@ name = "stoolap"
|
|
|
14
14
|
crate-type = ["cdylib"]
|
|
15
15
|
|
|
16
16
|
[dependencies]
|
|
17
|
-
stoolap = "
|
|
18
|
-
magnus = "0.8"
|
|
17
|
+
stoolap = { git = "https://github.com/stoolap/stoolap", branch = "main", default-features = false, features = ["parallel", "jemalloc"] }
|
|
18
|
+
magnus = { version = "0.8", features = ["rb-sys"] }
|
|
19
|
+
rb-sys = "0.9"
|
|
19
20
|
chrono = "0.4"
|
|
20
21
|
|
data/ext/stoolap/src/database.rs
CHANGED
|
@@ -12,13 +12,20 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
|
+
use std::os::raw::{c_char, c_long};
|
|
15
16
|
use std::sync::Arc;
|
|
16
17
|
|
|
17
|
-
use magnus::{
|
|
18
|
+
use magnus::{
|
|
19
|
+
prelude::*,
|
|
20
|
+
rb_sys::{AsRawValue, FromRawValue},
|
|
21
|
+
Error, RArray, RHash, Ruby, TryConvert, Value,
|
|
22
|
+
};
|
|
23
|
+
use rb_sys::VALUE;
|
|
18
24
|
|
|
19
25
|
use stoolap::api::{Database as ApiDatabase, Rows};
|
|
26
|
+
use stoolap::core::Row;
|
|
20
27
|
|
|
21
|
-
use crate::error::{raise, to_magnus};
|
|
28
|
+
use crate::error::{arity_error, raise, to_magnus};
|
|
22
29
|
use crate::statement::PreparedStatement;
|
|
23
30
|
use crate::transaction::Transaction;
|
|
24
31
|
use crate::value::{parse_params, value_to_ruby, BindParams};
|
|
@@ -108,7 +115,6 @@ impl Database {
|
|
|
108
115
|
/// Returns total rows affected.
|
|
109
116
|
pub fn execute_batch(&self, sql: String, params_list: RArray) -> Result<i64, Error> {
|
|
110
117
|
use stoolap::api::ParamVec;
|
|
111
|
-
use stoolap::parser::Parser;
|
|
112
118
|
|
|
113
119
|
let mut all_params: Vec<ParamVec> = Vec::with_capacity(params_list.len());
|
|
114
120
|
for item in params_list.into_iter() {
|
|
@@ -122,22 +128,12 @@ impl Database {
|
|
|
122
128
|
}
|
|
123
129
|
}
|
|
124
130
|
|
|
125
|
-
let
|
|
126
|
-
let program = parser.parse_program().map_err(|e| raise(e.to_string()))?;
|
|
127
|
-
if program.statements.len() > 1 {
|
|
128
|
-
return Err(raise(
|
|
129
|
-
"execute_batch accepts exactly one SQL statement; use exec() for multi-statement SQL",
|
|
130
|
-
));
|
|
131
|
-
}
|
|
132
|
-
let stmt = program
|
|
133
|
-
.statements
|
|
134
|
-
.first()
|
|
135
|
-
.ok_or_else(|| raise("No SQL statement found"))?;
|
|
131
|
+
let stmt = self.db.prepare(&sql).map_err(to_magnus)?;
|
|
136
132
|
|
|
137
133
|
let mut tx = self.db.begin().map_err(to_magnus)?;
|
|
138
134
|
let mut total = 0i64;
|
|
139
135
|
for params in all_params {
|
|
140
|
-
total +=
|
|
136
|
+
total += stmt.execute_in(&mut tx, params).map_err(to_magnus)?;
|
|
141
137
|
}
|
|
142
138
|
tx.commit().map_err(to_magnus)?;
|
|
143
139
|
Ok(total)
|
|
@@ -151,7 +147,7 @@ impl Database {
|
|
|
151
147
|
/// Begin a transaction.
|
|
152
148
|
pub fn begin_transaction(&self) -> Result<Transaction, Error> {
|
|
153
149
|
let tx = self.db.begin().map_err(to_magnus)?;
|
|
154
|
-
Ok(Transaction::from_tx(tx))
|
|
150
|
+
Ok(Transaction::from_tx(tx, Arc::clone(&self.db)))
|
|
155
151
|
}
|
|
156
152
|
|
|
157
153
|
/// Close the database connection.
|
|
@@ -172,8 +168,11 @@ impl Database {
|
|
|
172
168
|
|
|
173
169
|
/// Parse `(sql, params=nil)` from a method args slice.
|
|
174
170
|
fn parse_sql_args(args: &[Value]) -> Result<(String, Option<Value>), Error> {
|
|
175
|
-
|
|
176
|
-
|
|
171
|
+
match *args {
|
|
172
|
+
[sql] => Ok((String::try_convert(sql)?, None)),
|
|
173
|
+
[sql, params] => Ok((String::try_convert(sql)?, Some(params))),
|
|
174
|
+
_ => Err(arity_error(args.len(), 1, 2)),
|
|
175
|
+
}
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
/// Translate user-friendly paths into a Stoolap DSN.
|
|
@@ -188,49 +187,58 @@ fn translate_path(path: &str) -> String {
|
|
|
188
187
|
}
|
|
189
188
|
}
|
|
190
189
|
|
|
190
|
+
/// Rows up to this many columns build their Hash with one
|
|
191
|
+
/// `rb_hash_bulk_insert` from a stack buffer; wider rows insert per key.
|
|
192
|
+
const BULK_COLS: usize = 32;
|
|
193
|
+
|
|
194
|
+
/// Build the column-key array for `rows`: frozen, interned UTF-8 strings
|
|
195
|
+
/// (`rb_enc_interned_str`), so an ad hoc query looks its keys up in Ruby's
|
|
196
|
+
/// fstring table instead of allocating a String per column per call, and
|
|
197
|
+
/// `rb_hash_aset` stores them without the dup + freeze a plain String key
|
|
198
|
+
/// costs. The array is a stack local, so the conservative GC marks it and
|
|
199
|
+
/// through it every key while rows are built.
|
|
200
|
+
pub fn column_keys(ruby: &Ruby, rows: &Rows) -> RArray {
|
|
201
|
+
let cols = rows.columns();
|
|
202
|
+
let keys = ruby.ary_new_capa(cols.len());
|
|
203
|
+
for c in cols {
|
|
204
|
+
unsafe {
|
|
205
|
+
let key = rb_sys::rb_enc_interned_str(
|
|
206
|
+
c.as_ptr() as *const c_char,
|
|
207
|
+
c.len() as c_long,
|
|
208
|
+
rb_sys::rb_utf8_encoding(),
|
|
209
|
+
);
|
|
210
|
+
rb_sys::rb_ary_push(keys.as_raw(), key);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
keys
|
|
214
|
+
}
|
|
215
|
+
|
|
191
216
|
/// Convert a `Rows` iterator into an Array of Hashes (String keys).
|
|
192
|
-
///
|
|
193
|
-
/// Uses `rows.advance() + rows.current_row()` which yields `&Row` directly
|
|
194
|
-
/// with zero `take_row()` move and zero ResultRow wrapping per iteration.
|
|
195
|
-
/// Column-name strings are built once into a Ruby `RArray` held as a stack
|
|
196
|
-
/// local, so Ruby's conservative GC scanner can find the `RArray` pointer
|
|
197
|
-
/// on the C stack and trace through to mark every column `RString`. A
|
|
198
|
-
/// `Vec<RString>` would store the keys in a heap-allocated buffer that
|
|
199
|
-
/// Ruby's GC cannot see, and the strings could be collected mid-iteration,
|
|
200
|
-
/// segfaulting on the next `aset`.
|
|
201
217
|
pub fn rows_to_hashes(rows: Rows) -> Result<RArray, Error> {
|
|
202
218
|
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
203
|
-
let
|
|
204
|
-
|
|
205
|
-
for c in rows.columns() {
|
|
206
|
-
let s = ruby.str_new(c);
|
|
207
|
-
s.freeze();
|
|
208
|
-
key_cache.push(s)?;
|
|
209
|
-
}
|
|
210
|
-
rows_to_hashes_with_keys(&ruby, rows, key_cache)
|
|
219
|
+
let keys = column_keys(&ruby, &rows);
|
|
220
|
+
rows_to_hashes_with_keys(&ruby, rows, keys)
|
|
211
221
|
}
|
|
212
222
|
|
|
213
|
-
/// Same as `rows_to_hashes` but reuses a caller-provided key
|
|
223
|
+
/// Same as `rows_to_hashes` but reuses a caller-provided key array
|
|
214
224
|
/// (typically held on a `PreparedStatement` across calls).
|
|
225
|
+
///
|
|
226
|
+
/// Uses `rows.advance() + rows.current_row()` which yields `&Row` directly
|
|
227
|
+
/// with zero `take_row()` move and zero ResultRow wrapping per iteration.
|
|
228
|
+
/// Every Ruby object is created and stored with direct `rb_*` calls rather
|
|
229
|
+
/// than magnus' `push` / `aset`, which wrap each call in `rb_protect`. The
|
|
230
|
+
/// targets are fresh, unfrozen Arrays and Hashes keyed by frozen Strings,
|
|
231
|
+
/// so the calls cannot raise.
|
|
215
232
|
pub fn rows_to_hashes_with_keys(
|
|
216
233
|
ruby: &Ruby,
|
|
217
234
|
mut rows: Rows,
|
|
218
|
-
|
|
235
|
+
keys: RArray,
|
|
219
236
|
) -> Result<RArray, Error> {
|
|
220
237
|
let col_count = rows.columns().len();
|
|
221
238
|
let result = ruby.ary_new();
|
|
222
239
|
while rows.advance() {
|
|
223
|
-
let
|
|
224
|
-
|
|
225
|
-
for i in 0..col_count {
|
|
226
|
-
let key = key_cache.entry::<Value>(i as isize)?;
|
|
227
|
-
let val = match row.get(i) {
|
|
228
|
-
Some(v) => value_to_ruby(v)?,
|
|
229
|
-
None => ruby.qnil().as_value(),
|
|
230
|
-
};
|
|
231
|
-
hash.aset(key, val)?;
|
|
232
|
-
}
|
|
233
|
-
result.push(hash)?;
|
|
240
|
+
let hash = row_to_hash(ruby, rows.current_row(), keys, col_count)?;
|
|
241
|
+
unsafe { rb_sys::rb_ary_push(result.as_raw(), hash) };
|
|
234
242
|
}
|
|
235
243
|
if let Some(err) = rows.error() {
|
|
236
244
|
return Err(to_magnus(err));
|
|
@@ -241,35 +249,20 @@ pub fn rows_to_hashes_with_keys(
|
|
|
241
249
|
/// Take the first row from `Rows` as a Hash, or `nil`.
|
|
242
250
|
pub fn first_row_to_hash(rows: Rows) -> Result<Value, Error> {
|
|
243
251
|
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
244
|
-
let
|
|
245
|
-
|
|
246
|
-
for c in rows.columns() {
|
|
247
|
-
let s = ruby.str_new(c);
|
|
248
|
-
s.freeze();
|
|
249
|
-
key_cache.push(s)?;
|
|
250
|
-
}
|
|
251
|
-
first_row_to_hash_with_keys(&ruby, rows, key_cache)
|
|
252
|
+
let keys = column_keys(&ruby, &rows);
|
|
253
|
+
first_row_to_hash_with_keys(&ruby, rows, keys)
|
|
252
254
|
}
|
|
253
255
|
|
|
254
|
-
/// Same as `first_row_to_hash` but with a caller-provided key
|
|
256
|
+
/// Same as `first_row_to_hash` but with a caller-provided key array.
|
|
255
257
|
pub fn first_row_to_hash_with_keys(
|
|
256
258
|
ruby: &Ruby,
|
|
257
259
|
mut rows: Rows,
|
|
258
|
-
|
|
260
|
+
keys: RArray,
|
|
259
261
|
) -> Result<Value, Error> {
|
|
260
262
|
if rows.advance() {
|
|
261
263
|
let col_count = rows.columns().len();
|
|
262
|
-
let
|
|
263
|
-
|
|
264
|
-
for i in 0..col_count {
|
|
265
|
-
let key = key_cache.entry::<Value>(i as isize)?;
|
|
266
|
-
let val = match row.get(i) {
|
|
267
|
-
Some(v) => value_to_ruby(v)?,
|
|
268
|
-
None => ruby.qnil().as_value(),
|
|
269
|
-
};
|
|
270
|
-
hash.aset(key, val)?;
|
|
271
|
-
}
|
|
272
|
-
return Ok(hash.as_value());
|
|
264
|
+
let hash = row_to_hash(ruby, rows.current_row(), keys, col_count)?;
|
|
265
|
+
return Ok(unsafe { Value::from_raw(hash) });
|
|
273
266
|
}
|
|
274
267
|
if let Some(err) = rows.error() {
|
|
275
268
|
return Err(to_magnus(err));
|
|
@@ -277,6 +270,42 @@ pub fn first_row_to_hash_with_keys(
|
|
|
277
270
|
Ok(ruby.qnil().as_value())
|
|
278
271
|
}
|
|
279
272
|
|
|
273
|
+
/// Build one row Hash. Up to `BULK_COLS` columns, the key/value pairs are
|
|
274
|
+
/// laid out in a stack buffer (scanned by the conservative GC, so values
|
|
275
|
+
/// created for later columns cannot collect earlier ones) and inserted by
|
|
276
|
+
/// a single `rb_hash_bulk_insert`, which sizes the table for them
|
|
277
|
+
/// (`rb_hash_new_capa` would need Ruby 3.2).
|
|
278
|
+
fn row_to_hash(ruby: &Ruby, row: &Row, keys: RArray, col_count: usize) -> Result<VALUE, Error> {
|
|
279
|
+
let nil = ruby.qnil().as_raw();
|
|
280
|
+
unsafe {
|
|
281
|
+
let hash = rb_sys::rb_hash_new();
|
|
282
|
+
let keys_ptr = rb_sys::RARRAY_CONST_PTR(keys.as_raw());
|
|
283
|
+
if col_count <= BULK_COLS {
|
|
284
|
+
let mut pairs = [nil; BULK_COLS * 2];
|
|
285
|
+
for i in 0..col_count {
|
|
286
|
+
pairs[2 * i] = *keys_ptr.add(i);
|
|
287
|
+
pairs[2 * i + 1] = match row.get(i) {
|
|
288
|
+
Some(v) => value_to_ruby(ruby, v)?.as_raw(),
|
|
289
|
+
None => nil,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
rb_sys::rb_hash_bulk_insert((col_count * 2) as c_long, pairs.as_ptr(), hash);
|
|
293
|
+
} else {
|
|
294
|
+
for i in 0..col_count {
|
|
295
|
+
let val = match row.get(i) {
|
|
296
|
+
Some(v) => value_to_ruby(ruby, v)?.as_raw(),
|
|
297
|
+
None => nil,
|
|
298
|
+
};
|
|
299
|
+
rb_sys::rb_hash_aset(hash, *keys_ptr.add(i), val);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// Keep `keys` live on the stack until here (like RB_GC_GUARD) so GC
|
|
303
|
+
// cannot reclaim the array while `keys_ptr` is still being read.
|
|
304
|
+
std::hint::black_box(keys);
|
|
305
|
+
Ok(hash)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
280
309
|
/// Convert `Rows` into `{ "columns" => [..], "rows" => [[..], ..] }`.
|
|
281
310
|
pub fn rows_to_raw(rows: Rows) -> Result<RHash, Error> {
|
|
282
311
|
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
@@ -299,15 +328,17 @@ pub fn rows_to_raw_with_keys(ruby: &Ruby, mut rows: Rows, col_arr: RArray) -> Re
|
|
|
299
328
|
let row_arr = ruby.ary_new();
|
|
300
329
|
while rows.advance() {
|
|
301
330
|
let row = rows.current_row();
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
331
|
+
unsafe {
|
|
332
|
+
let inner = rb_sys::rb_ary_new_capa(col_count as c_long);
|
|
333
|
+
for i in 0..col_count {
|
|
334
|
+
let val = match row.get(i) {
|
|
335
|
+
Some(v) => value_to_ruby(ruby, v)?.as_raw(),
|
|
336
|
+
None => ruby.qnil().as_raw(),
|
|
337
|
+
};
|
|
338
|
+
rb_sys::rb_ary_push(inner, val);
|
|
339
|
+
}
|
|
340
|
+
rb_sys::rb_ary_push(row_arr.as_raw(), inner);
|
|
309
341
|
}
|
|
310
|
-
row_arr.push(inner)?;
|
|
311
342
|
}
|
|
312
343
|
if let Some(err) = rows.error() {
|
|
313
344
|
return Err(to_magnus(err));
|
data/ext/stoolap/src/error.rs
CHANGED
|
@@ -46,6 +46,19 @@ pub fn raise<S: Into<String>>(msg: S) -> Error {
|
|
|
46
46
|
Error::new(ruby.get_inner(&ERROR_CLASS), msg.into())
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
/// Build the `ArgumentError` Ruby raises for a wrong argument count.
|
|
50
|
+
///
|
|
51
|
+
/// Methods taking optional arguments match on the argument slice instead of
|
|
52
|
+
/// using magnus' `scan_args`, which formats a spec string and runs
|
|
53
|
+
/// `rb_scan_args` under `rb_protect` on every call.
|
|
54
|
+
pub fn arity_error(given: usize, min: usize, max: usize) -> Error {
|
|
55
|
+
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
56
|
+
Error::new(
|
|
57
|
+
ruby.exception_arg_error(),
|
|
58
|
+
format!("wrong number of arguments (given {given}, expected {min}..{max})"),
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
49
62
|
/// Build a `TypeError` for invalid Ruby types.
|
|
50
63
|
pub fn type_error<S: Into<String>>(msg: S) -> Error {
|
|
51
64
|
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
@@ -12,20 +12,19 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
|
-
use std::
|
|
15
|
+
use std::cell::RefCell;
|
|
16
|
+
use std::sync::Arc;
|
|
16
17
|
|
|
17
18
|
use magnus::{
|
|
18
|
-
gc, prelude::*,
|
|
19
|
-
Ruby, TypedData, Value,
|
|
19
|
+
gc, prelude::*, value::Opaque, DataTypeFunctions, Error, RArray, RHash, Ruby, TypedData, Value,
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
use stoolap::api::{Database as ApiDatabase, Rows};
|
|
23
|
-
use stoolap::CachedPlanRef;
|
|
22
|
+
use stoolap::api::{Database as ApiDatabase, Rows, Statement as ApiStatement};
|
|
24
23
|
|
|
25
24
|
use crate::database::{
|
|
26
|
-
first_row_to_hash_with_keys, rows_to_hashes_with_keys, rows_to_raw_with_keys,
|
|
25
|
+
column_keys, first_row_to_hash_with_keys, rows_to_hashes_with_keys, rows_to_raw_with_keys,
|
|
27
26
|
};
|
|
28
|
-
use crate::error::{raise, to_magnus};
|
|
27
|
+
use crate::error::{arity_error, raise, to_magnus};
|
|
29
28
|
use crate::value::{parse_params, BindParams};
|
|
30
29
|
|
|
31
30
|
/// A prepared SQL statement.
|
|
@@ -40,18 +39,17 @@ use crate::value::{parse_params, BindParams};
|
|
|
40
39
|
#[magnus(class = "Stoolap::PreparedStatement", free_immediately, size, mark)]
|
|
41
40
|
pub struct PreparedStatement {
|
|
42
41
|
db: Arc<ApiDatabase>,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
///
|
|
46
|
-
///
|
|
47
|
-
///
|
|
48
|
-
|
|
49
|
-
column_keys: OnceLock<Opaque<RArray>>,
|
|
42
|
+
stmt: ApiStatement,
|
|
43
|
+
/// Populated on the first query and rebuilt when the result columns
|
|
44
|
+
/// change, which the column names kept beside the keys detect without
|
|
45
|
+
/// reading Ruby objects. `Opaque<RArray>` is `Send + Sync` and the
|
|
46
|
+
/// contained `RArray` is kept alive by the `mark` callback below.
|
|
47
|
+
column_keys: RefCell<Option<(Vec<String>, Opaque<RArray>)>>,
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
impl DataTypeFunctions for PreparedStatement {
|
|
53
51
|
fn mark(&self, marker: &gc::Marker) {
|
|
54
|
-
if let Some(keys) = self.column_keys.
|
|
52
|
+
if let Some((_, keys)) = &*self.column_keys.borrow() {
|
|
55
53
|
marker.mark(*keys);
|
|
56
54
|
}
|
|
57
55
|
}
|
|
@@ -59,37 +57,35 @@ impl DataTypeFunctions for PreparedStatement {
|
|
|
59
57
|
|
|
60
58
|
impl PreparedStatement {
|
|
61
59
|
pub fn new(db: Arc<ApiDatabase>, sql: &str) -> Result<Self, Error> {
|
|
62
|
-
let
|
|
60
|
+
let stmt = db.prepare(sql).map_err(to_magnus)?;
|
|
63
61
|
Ok(Self {
|
|
64
62
|
db,
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
column_keys: OnceLock::new(),
|
|
63
|
+
stmt,
|
|
64
|
+
column_keys: RefCell::new(None),
|
|
68
65
|
})
|
|
69
66
|
}
|
|
70
67
|
|
|
71
|
-
pub(crate) fn
|
|
72
|
-
&self.
|
|
68
|
+
pub(crate) fn inner(&self) -> &ApiStatement {
|
|
69
|
+
&self.stmt
|
|
73
70
|
}
|
|
74
71
|
|
|
75
72
|
/// Return the cached column-key array, creating it from `rows.columns()`
|
|
76
|
-
/// on first call
|
|
77
|
-
///
|
|
73
|
+
/// on first call and again whenever the columns no longer match it (a
|
|
74
|
+
/// schema change after prepare). The returned `RArray` is rooted
|
|
75
|
+
/// through this struct's `mark` callback and is safe to hold across
|
|
76
|
+
/// subsequent Ruby calls.
|
|
78
77
|
fn ensure_column_keys(&self, ruby: &Ruby, rows: &Rows) -> RArray {
|
|
79
|
-
let
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
for c in cols {
|
|
83
|
-
let s = ruby.str_new(c);
|
|
84
|
-
s.freeze();
|
|
85
|
-
arr.push(s).expect("push to fresh RArray cannot fail");
|
|
78
|
+
if let Some((names, keys)) = &*self.column_keys.borrow() {
|
|
79
|
+
if names.as_slice() == rows.columns() {
|
|
80
|
+
return ruby.get_inner(*keys);
|
|
86
81
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
82
|
+
}
|
|
83
|
+
let keys = column_keys(ruby, rows);
|
|
84
|
+
// Freeze the array itself so user code that receives it from
|
|
85
|
+
// query_raw cannot mutate the internal cache (P1 safety).
|
|
86
|
+
keys.freeze();
|
|
87
|
+
*self.column_keys.borrow_mut() = Some((rows.columns().to_vec(), Opaque::from(keys)));
|
|
88
|
+
keys
|
|
93
89
|
}
|
|
94
90
|
}
|
|
95
91
|
|
|
@@ -98,10 +94,9 @@ impl PreparedStatement {
|
|
|
98
94
|
pub fn execute(&self, args: &[Value]) -> Result<i64, Error> {
|
|
99
95
|
let params = parse_optional(args)?;
|
|
100
96
|
let bind = parse_params(params)?;
|
|
101
|
-
let plan = self.plan.clone();
|
|
102
97
|
match bind {
|
|
103
|
-
BindParams::Positional(p) => self.
|
|
104
|
-
BindParams::Named(n) => self.
|
|
98
|
+
BindParams::Positional(p) => self.stmt.execute(p).map_err(to_magnus),
|
|
99
|
+
BindParams::Named(n) => self.stmt.execute_named(n).map_err(to_magnus),
|
|
105
100
|
}
|
|
106
101
|
}
|
|
107
102
|
|
|
@@ -109,10 +104,9 @@ impl PreparedStatement {
|
|
|
109
104
|
pub fn query(&self, args: &[Value]) -> Result<RArray, Error> {
|
|
110
105
|
let params = parse_optional(args)?;
|
|
111
106
|
let bind = parse_params(params)?;
|
|
112
|
-
let plan = self.plan.clone();
|
|
113
107
|
let rows = match bind {
|
|
114
|
-
BindParams::Positional(p) => self.
|
|
115
|
-
BindParams::Named(n) => self.
|
|
108
|
+
BindParams::Positional(p) => self.stmt.query(p).map_err(to_magnus)?,
|
|
109
|
+
BindParams::Named(n) => self.stmt.query_named(n).map_err(to_magnus)?,
|
|
116
110
|
};
|
|
117
111
|
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
118
112
|
let keys = self.ensure_column_keys(&ruby, &rows);
|
|
@@ -123,10 +117,9 @@ impl PreparedStatement {
|
|
|
123
117
|
pub fn query_one(&self, args: &[Value]) -> Result<Value, Error> {
|
|
124
118
|
let params = parse_optional(args)?;
|
|
125
119
|
let bind = parse_params(params)?;
|
|
126
|
-
let plan = self.plan.clone();
|
|
127
120
|
let rows = match bind {
|
|
128
|
-
BindParams::Positional(p) => self.
|
|
129
|
-
BindParams::Named(n) => self.
|
|
121
|
+
BindParams::Positional(p) => self.stmt.query(p).map_err(to_magnus)?,
|
|
122
|
+
BindParams::Named(n) => self.stmt.query_named(n).map_err(to_magnus)?,
|
|
130
123
|
};
|
|
131
124
|
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
132
125
|
let keys = self.ensure_column_keys(&ruby, &rows);
|
|
@@ -137,10 +130,9 @@ impl PreparedStatement {
|
|
|
137
130
|
pub fn query_raw(&self, args: &[Value]) -> Result<RHash, Error> {
|
|
138
131
|
let params = parse_optional(args)?;
|
|
139
132
|
let bind = parse_params(params)?;
|
|
140
|
-
let plan = self.plan.clone();
|
|
141
133
|
let rows = match bind {
|
|
142
|
-
BindParams::Positional(p) => self.
|
|
143
|
-
BindParams::Named(n) => self.
|
|
134
|
+
BindParams::Positional(p) => self.stmt.query(p).map_err(to_magnus)?,
|
|
135
|
+
BindParams::Named(n) => self.stmt.query_named(n).map_err(to_magnus)?,
|
|
144
136
|
};
|
|
145
137
|
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
146
138
|
let keys = self.ensure_column_keys(&ruby, &rows);
|
|
@@ -163,11 +155,10 @@ impl PreparedStatement {
|
|
|
163
155
|
}
|
|
164
156
|
}
|
|
165
157
|
|
|
166
|
-
let stmt = self.plan.statement.as_ref();
|
|
167
158
|
let mut tx = self.db.begin().map_err(to_magnus)?;
|
|
168
159
|
let mut total = 0i64;
|
|
169
160
|
for params in all_params {
|
|
170
|
-
total +=
|
|
161
|
+
total += self.stmt.execute_in(&mut tx, params).map_err(to_magnus)?;
|
|
171
162
|
}
|
|
172
163
|
tx.commit().map_err(to_magnus)?;
|
|
173
164
|
Ok(total)
|
|
@@ -175,15 +166,18 @@ impl PreparedStatement {
|
|
|
175
166
|
|
|
176
167
|
/// SQL text of this prepared statement.
|
|
177
168
|
pub fn sql(&self) -> String {
|
|
178
|
-
self.
|
|
169
|
+
self.stmt.sql().to_string()
|
|
179
170
|
}
|
|
180
171
|
|
|
181
172
|
pub fn inspect(&self) -> String {
|
|
182
|
-
format!("#<Stoolap::PreparedStatement {:?}>", self.
|
|
173
|
+
format!("#<Stoolap::PreparedStatement {:?}>", self.stmt.sql())
|
|
183
174
|
}
|
|
184
175
|
}
|
|
185
176
|
|
|
186
177
|
fn parse_optional(args: &[Value]) -> Result<Option<Value>, Error> {
|
|
187
|
-
|
|
188
|
-
|
|
178
|
+
match *args {
|
|
179
|
+
[] => Ok(None),
|
|
180
|
+
[params] => Ok(Some(params)),
|
|
181
|
+
_ => Err(arity_error(args.len(), 0, 1)),
|
|
182
|
+
}
|
|
189
183
|
}
|