stoolap 0.4.2 → 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 +73 -737
- 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
|
@@ -12,15 +12,16 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
|
-
use std::sync::Mutex;
|
|
15
|
+
use std::sync::{Arc, Mutex};
|
|
16
16
|
|
|
17
|
-
use magnus::{
|
|
17
|
+
use magnus::{Error, RArray, RHash, TryConvert, Value};
|
|
18
18
|
|
|
19
|
-
use stoolap::api::
|
|
20
|
-
|
|
19
|
+
use stoolap::api::{
|
|
20
|
+
Database as ApiDatabase, Statement as ApiStatement, Transaction as ApiTransaction,
|
|
21
|
+
};
|
|
21
22
|
|
|
22
23
|
use crate::database::{first_row_to_hash, rows_to_hashes, rows_to_raw};
|
|
23
|
-
use crate::error::{raise, to_magnus};
|
|
24
|
+
use crate::error::{arity_error, raise, to_magnus};
|
|
24
25
|
use crate::statement::PreparedStatement;
|
|
25
26
|
use crate::value::{parse_params, BindParams};
|
|
26
27
|
|
|
@@ -32,12 +33,14 @@ use crate::value::{parse_params, BindParams};
|
|
|
32
33
|
#[magnus::wrap(class = "Stoolap::Transaction", free_immediately, size)]
|
|
33
34
|
pub struct Transaction {
|
|
34
35
|
tx: Mutex<Option<ApiTransaction>>,
|
|
36
|
+
db: Arc<ApiDatabase>,
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
impl Transaction {
|
|
38
|
-
pub fn from_tx(tx: ApiTransaction) -> Self {
|
|
40
|
+
pub fn from_tx(tx: ApiTransaction, db: Arc<ApiDatabase>) -> Self {
|
|
39
41
|
Self {
|
|
40
42
|
tx: Mutex::new(Some(tx)),
|
|
43
|
+
db,
|
|
41
44
|
}
|
|
42
45
|
}
|
|
43
46
|
|
|
@@ -103,7 +106,6 @@ impl Transaction {
|
|
|
103
106
|
/// Execute the same SQL with multiple parameter sets. Returns total rows affected.
|
|
104
107
|
pub fn execute_batch(&self, sql: String, params_list: RArray) -> Result<i64, Error> {
|
|
105
108
|
use stoolap::api::ParamVec;
|
|
106
|
-
use stoolap::parser::Parser;
|
|
107
109
|
|
|
108
110
|
let mut all_params: Vec<ParamVec> = Vec::with_capacity(params_list.len());
|
|
109
111
|
for item in params_list.into_iter() {
|
|
@@ -117,22 +119,12 @@ impl Transaction {
|
|
|
117
119
|
}
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
let
|
|
121
|
-
let program = parser.parse_program().map_err(|e| raise(e.to_string()))?;
|
|
122
|
-
if program.statements.len() > 1 {
|
|
123
|
-
return Err(raise(
|
|
124
|
-
"execute_batch accepts exactly one SQL statement; use exec() for multi-statement SQL",
|
|
125
|
-
));
|
|
126
|
-
}
|
|
127
|
-
let stmt = program
|
|
128
|
-
.statements
|
|
129
|
-
.first()
|
|
130
|
-
.ok_or_else(|| raise("No SQL statement found"))?;
|
|
122
|
+
let stmt = self.db.prepare(&sql).map_err(to_magnus)?;
|
|
131
123
|
|
|
132
124
|
self.with_tx(|tx| {
|
|
133
125
|
let mut total = 0i64;
|
|
134
126
|
for params in all_params {
|
|
135
|
-
total +=
|
|
127
|
+
total += stmt.execute_in(tx, params).map_err(to_magnus)?;
|
|
136
128
|
}
|
|
137
129
|
Ok(total)
|
|
138
130
|
})
|
|
@@ -140,59 +132,43 @@ impl Transaction {
|
|
|
140
132
|
|
|
141
133
|
/// Execute a prepared statement within the transaction. Returns rows affected.
|
|
142
134
|
pub fn execute_prepared(&self, args: &[Value]) -> Result<i64, Error> {
|
|
143
|
-
let (
|
|
135
|
+
let (stmt, params) = parse_stmt_args(args)?;
|
|
144
136
|
let bind = parse_params(params)?;
|
|
145
137
|
self.with_tx(|tx| match bind {
|
|
146
|
-
BindParams::Positional(p) => tx
|
|
147
|
-
|
|
148
|
-
.map_err(to_magnus),
|
|
149
|
-
BindParams::Named(n) => tx
|
|
150
|
-
.execute_prepared_named(plan.statement.as_ref(), n)
|
|
151
|
-
.map_err(to_magnus),
|
|
138
|
+
BindParams::Positional(p) => stmt.execute_in(tx, p).map_err(to_magnus),
|
|
139
|
+
BindParams::Named(n) => stmt.execute_named_in(tx, n).map_err(to_magnus),
|
|
152
140
|
})
|
|
153
141
|
}
|
|
154
142
|
|
|
155
143
|
/// Query rows using a prepared statement. Returns Array of Hashes.
|
|
156
144
|
pub fn query_prepared(&self, args: &[Value]) -> Result<RArray, Error> {
|
|
157
|
-
let (
|
|
145
|
+
let (stmt, params) = parse_stmt_args(args)?;
|
|
158
146
|
let bind = parse_params(params)?;
|
|
159
147
|
let rows = self.with_tx(|tx| match bind {
|
|
160
|
-
BindParams::Positional(p) => tx
|
|
161
|
-
|
|
162
|
-
.map_err(to_magnus),
|
|
163
|
-
BindParams::Named(n) => tx
|
|
164
|
-
.query_prepared_named(plan.statement.as_ref(), n)
|
|
165
|
-
.map_err(to_magnus),
|
|
148
|
+
BindParams::Positional(p) => stmt.query_in(tx, p).map_err(to_magnus),
|
|
149
|
+
BindParams::Named(n) => stmt.query_named_in(tx, n).map_err(to_magnus),
|
|
166
150
|
})?;
|
|
167
151
|
rows_to_hashes(rows)
|
|
168
152
|
}
|
|
169
153
|
|
|
170
154
|
/// Query a single row using a prepared statement. Returns Hash or nil.
|
|
171
155
|
pub fn query_one_prepared(&self, args: &[Value]) -> Result<Value, Error> {
|
|
172
|
-
let (
|
|
156
|
+
let (stmt, params) = parse_stmt_args(args)?;
|
|
173
157
|
let bind = parse_params(params)?;
|
|
174
158
|
let rows = self.with_tx(|tx| match bind {
|
|
175
|
-
BindParams::Positional(p) => tx
|
|
176
|
-
|
|
177
|
-
.map_err(to_magnus),
|
|
178
|
-
BindParams::Named(n) => tx
|
|
179
|
-
.query_prepared_named(plan.statement.as_ref(), n)
|
|
180
|
-
.map_err(to_magnus),
|
|
159
|
+
BindParams::Positional(p) => stmt.query_in(tx, p).map_err(to_magnus),
|
|
160
|
+
BindParams::Named(n) => stmt.query_named_in(tx, n).map_err(to_magnus),
|
|
181
161
|
})?;
|
|
182
162
|
first_row_to_hash(rows)
|
|
183
163
|
}
|
|
184
164
|
|
|
185
165
|
/// Query rows using a prepared statement in raw format.
|
|
186
166
|
pub fn query_raw_prepared(&self, args: &[Value]) -> Result<RHash, Error> {
|
|
187
|
-
let (
|
|
167
|
+
let (stmt, params) = parse_stmt_args(args)?;
|
|
188
168
|
let bind = parse_params(params)?;
|
|
189
169
|
let rows = self.with_tx(|tx| match bind {
|
|
190
|
-
BindParams::Positional(p) => tx
|
|
191
|
-
|
|
192
|
-
.map_err(to_magnus),
|
|
193
|
-
BindParams::Named(n) => tx
|
|
194
|
-
.query_prepared_named(plan.statement.as_ref(), n)
|
|
195
|
-
.map_err(to_magnus),
|
|
170
|
+
BindParams::Positional(p) => stmt.query_in(tx, p).map_err(to_magnus),
|
|
171
|
+
BindParams::Named(n) => stmt.query_named_in(tx, n).map_err(to_magnus),
|
|
196
172
|
})?;
|
|
197
173
|
rows_to_raw(rows)
|
|
198
174
|
}
|
|
@@ -232,13 +208,19 @@ impl Transaction {
|
|
|
232
208
|
}
|
|
233
209
|
|
|
234
210
|
fn parse_sql_args(args: &[Value]) -> Result<(String, Option<Value>), Error> {
|
|
235
|
-
|
|
236
|
-
|
|
211
|
+
match *args {
|
|
212
|
+
[sql] => Ok((String::try_convert(sql)?, None)),
|
|
213
|
+
[sql, params] => Ok((String::try_convert(sql)?, Some(params))),
|
|
214
|
+
_ => Err(arity_error(args.len(), 1, 2)),
|
|
215
|
+
}
|
|
237
216
|
}
|
|
238
217
|
|
|
239
|
-
fn parse_stmt_args(args: &[Value]) -> Result<(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
218
|
+
fn parse_stmt_args(args: &[Value]) -> Result<(&ApiStatement, Option<Value>), Error> {
|
|
219
|
+
let (stmt, params) = match *args {
|
|
220
|
+
[stmt] => (stmt, None),
|
|
221
|
+
[stmt, params] => (stmt, Some(params)),
|
|
222
|
+
_ => return Err(arity_error(args.len(), 1, 2)),
|
|
223
|
+
};
|
|
224
|
+
let stmt: &PreparedStatement = TryConvert::try_convert(stmt)?;
|
|
225
|
+
Ok((stmt.inner(), params))
|
|
244
226
|
}
|
data/ext/stoolap/src/value.rs
CHANGED
|
@@ -13,16 +13,19 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
15
|
use std::cell::RefCell;
|
|
16
|
+
use std::os::raw::c_int;
|
|
16
17
|
|
|
17
18
|
use chrono::{TimeZone, Utc};
|
|
18
19
|
use magnus::{
|
|
19
20
|
prelude::*,
|
|
20
21
|
r_hash::ForEach,
|
|
22
|
+
rb_sys::{AsRawValue, FromRawValue},
|
|
21
23
|
value::{Lazy, ReprValue},
|
|
22
24
|
Error, Float, Integer, RArray, RClass, RHash, RModule, RString, Ruby, Symbol, TryConvert,
|
|
23
25
|
Value,
|
|
24
26
|
};
|
|
25
27
|
|
|
28
|
+
use rb_sys::timespec;
|
|
26
29
|
use stoolap::api::{NamedParams, ParamVec};
|
|
27
30
|
use stoolap::core::Value as SValue;
|
|
28
31
|
|
|
@@ -30,16 +33,13 @@ use crate::error::{raise, type_error};
|
|
|
30
33
|
|
|
31
34
|
/// Cached `Time` class. Lazy-initialised on first use; subsequent calls
|
|
32
35
|
/// are a direct pointer load, avoiding `const_get("Time")` on every
|
|
33
|
-
/// timestamp parameter
|
|
36
|
+
/// timestamp parameter.
|
|
34
37
|
static TIME_CLASS: Lazy<RClass> = Lazy::new(|ruby| {
|
|
35
38
|
ruby.class_object()
|
|
36
39
|
.const_get("Time")
|
|
37
40
|
.expect("Ruby core class Time must exist")
|
|
38
41
|
});
|
|
39
42
|
|
|
40
|
-
/// Cached `:nsec` symbol used for `Time.at(secs, nsecs, :nsec)`.
|
|
41
|
-
static NSEC_SYM: Lazy<Symbol> = Lazy::new(|ruby| ruby.to_symbol("nsec"));
|
|
42
|
-
|
|
43
43
|
/// Cached `JSON` module used to serialise Hash/Array params.
|
|
44
44
|
/// `lib/stoolap.rb` does `require "json"` so the constant is
|
|
45
45
|
/// guaranteed to exist by the time any binding method runs. The
|
|
@@ -114,11 +114,12 @@ pub fn ruby_to_value(val: Value) -> Result<SValue, Error> {
|
|
|
114
114
|
return Ok(SValue::null_unknown());
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
// Boolean
|
|
118
|
-
|
|
117
|
+
// Boolean: true and false are immediates, so compare them directly
|
|
118
|
+
// instead of looking up the class of every parameter.
|
|
119
|
+
if val.as_raw() == ruby.qtrue().as_raw() {
|
|
119
120
|
return Ok(SValue::Boolean(true));
|
|
120
121
|
}
|
|
121
|
-
if val.
|
|
122
|
+
if val.as_raw() == ruby.qfalse().as_raw() {
|
|
122
123
|
return Ok(SValue::Boolean(false));
|
|
123
124
|
}
|
|
124
125
|
|
|
@@ -152,10 +153,18 @@ pub fn ruby_to_value(val: Value) -> Result<SValue, Error> {
|
|
|
152
153
|
// Time -> Timestamp (UTC)
|
|
153
154
|
let time_class = ruby.get_inner(&TIME_CLASS);
|
|
154
155
|
if val.is_kind_of(time_class) {
|
|
155
|
-
|
|
156
|
-
|
|
156
|
+
// `rb_time_timespec` raises for a Time outside the system range, so
|
|
157
|
+
// run it under `protect` to get an `Error` instead of a longjmp.
|
|
158
|
+
let mut spec = timespec {
|
|
159
|
+
tv_sec: 0,
|
|
160
|
+
tv_nsec: 0,
|
|
161
|
+
};
|
|
162
|
+
magnus::rb_sys::protect(|| {
|
|
163
|
+
spec = unsafe { rb_sys::rb_time_timespec(val.as_raw()) };
|
|
164
|
+
ruby.qnil().as_raw()
|
|
165
|
+
})?;
|
|
157
166
|
let dt = Utc
|
|
158
|
-
.timestamp_opt(
|
|
167
|
+
.timestamp_opt(spec.tv_sec, spec.tv_nsec as u32)
|
|
159
168
|
.single()
|
|
160
169
|
.ok_or_else(|| raise("invalid Time value"))?;
|
|
161
170
|
return Ok(SValue::Timestamp(dt));
|
|
@@ -187,7 +196,7 @@ pub fn parse_params(params: Option<Value>) -> Result<BindParams, Error> {
|
|
|
187
196
|
if let Some(hash) = RHash::from_value(params) {
|
|
188
197
|
let mut named = NamedParams::new();
|
|
189
198
|
let mut err: Option<Error> = None;
|
|
190
|
-
|
|
199
|
+
hash.foreach(|key: Value, val: Value| {
|
|
191
200
|
// Extract the key as `&str` without an intermediate owned String.
|
|
192
201
|
// Symbols return a `Cow<str>` from `name()`; RStrings expose their
|
|
193
202
|
// bytes via `unsafe as_str` (valid while the Hash holds the key).
|
|
@@ -234,7 +243,7 @@ pub fn parse_params(params: Option<Value>) -> Result<BindParams, Error> {
|
|
|
234
243
|
Ok(ForEach::Stop)
|
|
235
244
|
}
|
|
236
245
|
}
|
|
237
|
-
})
|
|
246
|
+
})?;
|
|
238
247
|
if let Some(e) = err {
|
|
239
248
|
return Err(e);
|
|
240
249
|
}
|
|
@@ -253,9 +262,11 @@ pub fn parse_params(params: Option<Value>) -> Result<BindParams, Error> {
|
|
|
253
262
|
Err(type_error("Parameters must be an Array, Hash, or nil"))
|
|
254
263
|
}
|
|
255
264
|
|
|
265
|
+
/// `rb_time_timespec_new` offset that makes a UTC Time (`INT_MAX - 1`).
|
|
266
|
+
const UTC_OFFSET: c_int = c_int::MAX - 1;
|
|
267
|
+
|
|
256
268
|
/// Convert a Stoolap Value to a Ruby Value.
|
|
257
|
-
pub fn value_to_ruby(val: &SValue) -> Result<Value, Error> {
|
|
258
|
-
let ruby = Ruby::get().expect("must hold the Ruby VM lock");
|
|
269
|
+
pub fn value_to_ruby(ruby: &Ruby, val: &SValue) -> Result<Value, Error> {
|
|
259
270
|
match val {
|
|
260
271
|
SValue::Null(_) => Ok(ruby.qnil().as_value()),
|
|
261
272
|
SValue::Boolean(b) => Ok(if *b {
|
|
@@ -267,17 +278,13 @@ pub fn value_to_ruby(val: &SValue) -> Result<Value, Error> {
|
|
|
267
278
|
SValue::Float(f) => Ok(ruby.float_from_f64(*f).as_value()),
|
|
268
279
|
SValue::Text(s) => Ok(ruby.str_new(s.as_str()).as_value()),
|
|
269
280
|
SValue::Timestamp(ts) => {
|
|
270
|
-
//
|
|
271
|
-
//
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
let nsecs = ts.timestamp_subsec_nanos() as i64;
|
|
278
|
-
let t: Value = time_class.funcall("at", (secs, nsecs, nsec_sym))?;
|
|
279
|
-
let utc: Value = t.funcall("utc", ())?;
|
|
280
|
-
Ok(utc)
|
|
281
|
+
// One C call builds the UTC Time directly, instead of
|
|
282
|
+
// `Time.at(secs, nsecs, :nsec).utc` through two method calls.
|
|
283
|
+
let spec = timespec {
|
|
284
|
+
tv_sec: ts.timestamp() as _,
|
|
285
|
+
tv_nsec: ts.timestamp_subsec_nanos() as _,
|
|
286
|
+
};
|
|
287
|
+
Ok(unsafe { Value::from_raw(rb_sys::rb_time_timespec_new(&spec, UTC_OFFSET)) })
|
|
281
288
|
}
|
|
282
289
|
SValue::Extension(_) => {
|
|
283
290
|
if let Some(floats) = val.as_vector_f32() {
|
data/lib/stoolap/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: stoolap
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0.beta.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Stoolap Contributors
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-10-25 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rb_sys
|