jsql-neo 5.5.0 → 6.0.0
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.
- package/LICENSE +202 -202
- package/README.md +8155 -8097
- package/bin/jsql +320 -320
- package/bin/jsql-server +90 -90
- package/bin/jsql-server-control.js +177 -177
- package/index.d.ts +412 -385
- package/index.js +128 -124
- package/lib/ast.js +214 -214
- package/lib/btree.js +478 -413
- package/lib/cache.js +328 -328
- package/lib/client.js +125 -125
- package/lib/database.js +1741 -1744
- package/lib/date-types.js +182 -182
- package/lib/errors.js +89 -89
- package/lib/jsql_format.js +497 -497
- package/lib/migrate.js +254 -254
- package/lib/mod.js +292 -292
- package/lib/mongo_server.js +607 -607
- package/lib/multiserver.js +140 -140
- package/lib/mysql_compat.js +413 -413
- package/lib/mysql_server.js +1237 -1237
- package/lib/native_client.js +722 -732
- package/lib/nedb_compat.js +506 -506
- package/lib/pg_server.js +864 -864
- package/lib/plugin.js +259 -35
- package/lib/plugins/audit.js +37 -0
- package/lib/plugins/index.js +72 -0
- package/lib/plugins/logger.js +35 -0
- package/lib/plugins/metrics.js +38 -0
- package/lib/plugins/timestamps.js +57 -0
- package/lib/plugins/validation.js +128 -0
- package/lib/prepared.js +37 -0
- package/lib/query.js +834 -815
- package/lib/redis_server.js +571 -571
- package/lib/sql.js +4053 -3460
- package/lib/sqlite_compat.js +323 -323
- package/lib/sqlite_worker.js +264 -264
- package/lib/table.js +1351 -1304
- package/lib/tui.js +502 -502
- package/lib/wasm.js +1 -1
- package/lib/wasm_client.js +715 -718
- package/lib/web_ui.js +257 -257
- package/native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-core/Cargo.lock +462 -455
- package/nativesrc/jsql-neo-core/Cargo.toml +26 -24
- package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +492 -455
- package/nativesrc/jsql-neo-core/src/engine/memory.rs +191 -168
- package/nativesrc/jsql-neo-core/src/engine/mod.rs +74 -62
- package/nativesrc/jsql-neo-core/src/engine/table.rs +996 -700
- package/nativesrc/jsql-neo-core/src/lib.rs +2 -2
- package/nativesrc/jsql-neo-core/src/storage/mod.rs +2 -2
- package/nativesrc/jsql-neo-core/src/storage/persistent.rs +1 -1
- package/nativesrc/jsql-neo-core/src/storage/wal.rs +298 -85
- package/nativesrc/jsql-neo-core/src/types.rs +93 -93
- package/nativesrc/jsql-neo-native/Cargo.lock +613 -606
- package/nativesrc/jsql-neo-native/Cargo.toml +24 -16
- package/nativesrc/jsql-neo-native/build.rs +3 -3
- package/nativesrc/jsql-neo-native/package.json +7 -7
- package/nativesrc/jsql-neo-native/src/lib.rs +281 -269
- package/nativesrc/jsql-neo-wasm/Cargo.lock +472 -465
- package/nativesrc/jsql-neo-wasm/Cargo.toml +22 -15
- package/nativesrc/jsql-neo-wasm/src/lib.rs +463 -447
- package/package.json +87 -87
- package/postinstall.js +158 -158
- package/test/btree.test.js +144 -128
- package/test/coverage.js +187 -187
- package/test/join.test.js +110 -110
- package/test/native.test.js +158 -158
- package/test/query-opt.test.js +109 -0
- package/test/regress-5.1.0.js +271 -271
- package/test/smoke.js +58 -58
- package/test/sql-parser.test.js +450 -273
- package/test/wasm.test.js +110 -110
- package/wasm/browser.d.ts +89 -89
- package/wasm/browser.mjs +591 -569
- package/wasm/browser_bg.mjs +618 -597
- package/wasm/jsql_neo_wasm.d.ts +44 -38
- package/wasm/jsql_neo_wasm.js +641 -520
- package/wasm/jsql_neo_wasm_bg.wasm +0 -0
- package/wasm/jsql_neo_wasm_bg.wasm.d.ts +26 -26
- package/wasm/package.json +11 -11
|
@@ -1,168 +1,191 @@
|
|
|
1
|
-
use std::collections::HashMap;
|
|
2
|
-
use crate::engine::table::Table;
|
|
3
|
-
use crate::engine::{validate_table_name, Engine};
|
|
4
|
-
use crate::types::{Row, TableDefinition};
|
|
5
|
-
|
|
6
|
-
pub struct MemoryEngine {
|
|
7
|
-
pub tables: HashMap<String, Table>,
|
|
8
|
-
txs: HashMap<String, HashMap<String, Table>>,
|
|
9
|
-
next_tx_id: u64,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
impl MemoryEngine {
|
|
13
|
-
pub fn new() -> Self {
|
|
14
|
-
Self { tables: HashMap::new(), txs: HashMap::new(), next_tx_id: 1 }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
pub fn clear(&mut self) {
|
|
18
|
-
self.tables.clear();
|
|
19
|
-
self.txs.clear();
|
|
20
|
-
self.next_tx_id = 1;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
fn snapshot(&self) -> HashMap<String, Table> {
|
|
24
|
-
self.tables.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
impl Engine for MemoryEngine {
|
|
29
|
-
fn create_table(&mut self, def: TableDefinition) -> Result<(), String> {
|
|
30
|
-
validate_table_name(&def.name)?;
|
|
31
|
-
if self.tables.contains_key(&def.name) {
|
|
32
|
-
return Err(format!("table '{}' already exists", def.name));
|
|
33
|
-
}
|
|
34
|
-
self.tables.insert(def.name.clone(), Table::new(def.name, &def.schema));
|
|
35
|
-
Ok(())
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
fn
|
|
39
|
-
self.tables.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
fn
|
|
47
|
-
self.tables.
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
fn
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
let
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
let
|
|
102
|
-
Ok(
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
fn
|
|
106
|
-
let t = self.tables.
|
|
107
|
-
Ok(t.
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
fn
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
self.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
.
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
1
|
+
use std::collections::HashMap;
|
|
2
|
+
use crate::engine::table::{Table, matches_filter};
|
|
3
|
+
use crate::engine::{validate_table_name, Engine};
|
|
4
|
+
use crate::types::{Row, TableDefinition, FieldSchema};
|
|
5
|
+
|
|
6
|
+
pub struct MemoryEngine {
|
|
7
|
+
pub tables: HashMap<String, Table>,
|
|
8
|
+
txs: HashMap<String, HashMap<String, Table>>,
|
|
9
|
+
next_tx_id: u64,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
impl MemoryEngine {
|
|
13
|
+
pub fn new() -> Self {
|
|
14
|
+
Self { tables: HashMap::new(), txs: HashMap::new(), next_tx_id: 1 }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
pub fn clear(&mut self) {
|
|
18
|
+
self.tables.clear();
|
|
19
|
+
self.txs.clear();
|
|
20
|
+
self.next_tx_id = 1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fn snapshot(&self) -> HashMap<String, Table> {
|
|
24
|
+
self.tables.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
impl Engine for MemoryEngine {
|
|
29
|
+
fn create_table(&mut self, def: TableDefinition) -> Result<(), String> {
|
|
30
|
+
validate_table_name(&def.name)?;
|
|
31
|
+
if self.tables.contains_key(&def.name) {
|
|
32
|
+
return Err(format!("table '{}' already exists", def.name));
|
|
33
|
+
}
|
|
34
|
+
self.tables.insert(def.name.clone(), Table::new(def.name, &def.schema));
|
|
35
|
+
Ok(())
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn add_column(&mut self, table: &str, name: &str, fs: FieldSchema) -> Result<(), String> {
|
|
39
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
40
|
+
if !t.add_column(name, &fs) {
|
|
41
|
+
return Err(format!("column '{}' already exists in table '{}'", name, table));
|
|
42
|
+
}
|
|
43
|
+
Ok(())
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fn drop_table(&mut self, name: &str) -> Result<(), String> {
|
|
47
|
+
self.tables.remove(name).map(|_| ()).ok_or_else(|| format!("table '{}' not found", name))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fn has_table(&self, name: &str) -> bool {
|
|
51
|
+
self.tables.contains_key(name)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fn list_tables(&self) -> Vec<String> {
|
|
55
|
+
self.tables.keys().cloned().collect()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fn insert(&mut self, table: &str, fields_batch: Vec<HashMap<String, serde_json::Value>>) -> Result<Vec<u64>, String> {
|
|
59
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
60
|
+
Ok(t.add_rows(fields_batch))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fn find_by_id(&self, table: &str, id: u64) -> Result<Option<Row>, String> {
|
|
64
|
+
let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
65
|
+
Ok(t.get_row(id))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn find_by_id_json(&self, table: &str, id: u64) -> Result<Option<String>, String> {
|
|
69
|
+
let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
70
|
+
Ok(t.get_row_json(id))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fn find_by_ids_json(&self, table: &str, ids: &[u64]) -> Result<String, String> {
|
|
74
|
+
let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
75
|
+
Ok(t.get_rows_json(ids))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn find(&self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
|
|
79
|
+
limit: Option<usize>, offset: Option<usize>,
|
|
80
|
+
cursor: Option<String>,
|
|
81
|
+
order_by: &Option<String>, order: &Option<String>) -> Result<(Vec<Row>, Option<String>, bool), String> {
|
|
82
|
+
let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
83
|
+
let off = cursor.as_deref().and_then(|c| c.strip_prefix('c')?.parse().ok()).or(offset);
|
|
84
|
+
let off_val = off.unwrap_or(0);
|
|
85
|
+
let probe_limit = limit.map(|l| l + 1);
|
|
86
|
+
let all = t.find_rows(filter, probe_limit, off, order_by, order);
|
|
87
|
+
let has_more = limit.map_or(false, |l| all.len() > l);
|
|
88
|
+
let rows: Vec<Row> = all.into_iter().take(limit.unwrap_or(usize::MAX)).collect();
|
|
89
|
+
let next_cursor = if has_more { Some(format!("c{}", off_val + limit.unwrap())) } else { None };
|
|
90
|
+
Ok((rows, next_cursor, has_more))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
fn find_json(&self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
|
|
94
|
+
limit: Option<usize>, offset: Option<usize>,
|
|
95
|
+
cursor: Option<String>,
|
|
96
|
+
order_by: &Option<String>, order: &Option<String>) -> Result<(String, Option<String>, bool), String> {
|
|
97
|
+
let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
98
|
+
let off = cursor.as_deref().and_then(|c| c.strip_prefix('c')?.parse().ok()).or(offset);
|
|
99
|
+
let off_val = off.unwrap_or(0);
|
|
100
|
+
let (json, has_more) = t.find_rows_json(filter, limit, off, order_by, order);
|
|
101
|
+
let next_cursor = if has_more { Some(format!("c{}", off_val + limit.unwrap())) } else { None };
|
|
102
|
+
Ok((json, next_cursor, has_more))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fn count(&self, table: &str) -> Result<usize, String> {
|
|
106
|
+
let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
107
|
+
Ok(t.count())
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
fn update_by_id(&mut self, table: &str, id: u64, data: HashMap<String, serde_json::Value>) -> Result<bool, String> {
|
|
111
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
112
|
+
Ok(t.update_row(id, data))
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
fn remove_by_id(&mut self, table: &str, id: u64) -> Result<bool, String> {
|
|
116
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
117
|
+
Ok(t.remove_row(id))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
fn remove_by_ids(&mut self, table: &str, ids: &[u64]) -> Result<usize, String> {
|
|
121
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
122
|
+
Ok(t.remove_rows(ids))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fn update_by_ids(&mut self, table: &str, batch: Vec<(u64, HashMap<String, serde_json::Value>)>) -> Result<usize, String> {
|
|
126
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
127
|
+
Ok(t.update_rows(batch))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn update_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
|
|
131
|
+
data: HashMap<String, serde_json::Value>) -> Result<usize, String> {
|
|
132
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
133
|
+
let ids: Vec<u64> = match filter {
|
|
134
|
+
Some(f) => {
|
|
135
|
+
let conds = t.parse_filter(f);
|
|
136
|
+
t.rows.iter()
|
|
137
|
+
.filter(|r| matches_filter(t.get_row_values(r), &conds))
|
|
138
|
+
.map(|r| r.id)
|
|
139
|
+
.collect()
|
|
140
|
+
}
|
|
141
|
+
None => t.rows.iter().map(|r| r.id).collect(),
|
|
142
|
+
};
|
|
143
|
+
let count = ids.len();
|
|
144
|
+
for id in &ids {
|
|
145
|
+
t.update_row_ref(*id, &data);
|
|
146
|
+
}
|
|
147
|
+
Ok(count)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fn remove_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>) -> Result<usize, String> {
|
|
151
|
+
let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
|
|
152
|
+
let len_before = t.rows.len();
|
|
153
|
+
let f = match filter {
|
|
154
|
+
None => {
|
|
155
|
+
t.rows.clear();
|
|
156
|
+
t.pk_index.clear();
|
|
157
|
+
return Ok(len_before);
|
|
158
|
+
}
|
|
159
|
+
Some(f) => f,
|
|
160
|
+
};
|
|
161
|
+
let conds = t.parse_filter(f);
|
|
162
|
+
let to_remove: Vec<usize> = t.rows.iter().enumerate()
|
|
163
|
+
.filter(|(_, r)| matches_filter(t.get_row_values(r), &conds))
|
|
164
|
+
.map(|(i, _)| i)
|
|
165
|
+
.collect();
|
|
166
|
+
for i in to_remove.into_iter().rev() {
|
|
167
|
+
t.remove_row_by_idx(i);
|
|
168
|
+
}
|
|
169
|
+
Ok(len_before - t.rows.len())
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
fn begin_tx(&mut self) -> Result<String, String> {
|
|
173
|
+
let tx_id = format!("tx{}", self.next_tx_id);
|
|
174
|
+
self.next_tx_id += 1;
|
|
175
|
+
self.txs.insert(tx_id.clone(), self.snapshot());
|
|
176
|
+
Ok(tx_id)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
fn commit_tx(&mut self, tx_id: &str) -> Result<(), String> {
|
|
180
|
+
self.txs.remove(tx_id)
|
|
181
|
+
.map(|_| ())
|
|
182
|
+
.ok_or_else(|| format!("transaction '{}' not found", tx_id))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
fn rollback_tx(&mut self, tx_id: &str) -> Result<(), String> {
|
|
186
|
+
let snap = self.txs.remove(tx_id)
|
|
187
|
+
.ok_or_else(|| format!("transaction '{}' not found", tx_id))?;
|
|
188
|
+
self.tables = snap;
|
|
189
|
+
Ok(())
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -1,62 +1,74 @@
|
|
|
1
|
-
pub mod table;
|
|
2
|
-
mod memory;
|
|
3
|
-
#[cfg(not(feature = "wasm"))]
|
|
4
|
-
pub mod hybrid;
|
|
5
|
-
|
|
6
|
-
pub use memory::MemoryEngine;
|
|
7
|
-
#[cfg(not(feature = "wasm"))]
|
|
8
|
-
pub use hybrid::HybridEngine;
|
|
9
|
-
pub use table::{Table, RowStore, FieldValue, default_value};
|
|
10
|
-
|
|
11
|
-
pub fn validate_table_name(name: &str) -> Result<(), String> {
|
|
12
|
-
if name.is_empty() {
|
|
13
|
-
return Err("table name must not be empty".to_string());
|
|
14
|
-
}
|
|
15
|
-
if name.len() > 64 {
|
|
16
|
-
return Err("table name too long (max 64 chars)".to_string());
|
|
17
|
-
}
|
|
18
|
-
if name == "." || name == ".." {
|
|
19
|
-
return Err(format!("invalid table name '{}'", name));
|
|
20
|
-
}
|
|
21
|
-
for (i, c) in name.chars().enumerate() {
|
|
22
|
-
if c.is_ascii_control() || c == '/' || c == '\\' {
|
|
23
|
-
return Err(format!("invalid character in table name '{}'", name));
|
|
24
|
-
}
|
|
25
|
-
if c == '.' && (i > 0 && name.as_bytes()[i - 1] == b'.') {
|
|
26
|
-
return Err(format!("invalid table name '{}' ('..' sequences are not allowed)", name));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
Ok(())
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
use std::collections::HashMap;
|
|
33
|
-
use crate::types::{Row, TableDefinition};
|
|
34
|
-
|
|
35
|
-
pub trait Engine {
|
|
36
|
-
fn create_table(&mut self, def: TableDefinition) -> Result<(), String>;
|
|
37
|
-
|
|
38
|
-
fn
|
|
39
|
-
fn
|
|
40
|
-
|
|
41
|
-
fn
|
|
42
|
-
|
|
43
|
-
fn
|
|
44
|
-
fn
|
|
45
|
-
fn
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
fn
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
fn
|
|
62
|
-
|
|
1
|
+
pub mod table;
|
|
2
|
+
mod memory;
|
|
3
|
+
#[cfg(not(feature = "wasm"))]
|
|
4
|
+
pub mod hybrid;
|
|
5
|
+
|
|
6
|
+
pub use memory::MemoryEngine;
|
|
7
|
+
#[cfg(not(feature = "wasm"))]
|
|
8
|
+
pub use hybrid::HybridEngine;
|
|
9
|
+
pub use table::{Table, RowStore, FieldValue, default_value};
|
|
10
|
+
|
|
11
|
+
pub fn validate_table_name(name: &str) -> Result<(), String> {
|
|
12
|
+
if name.is_empty() {
|
|
13
|
+
return Err("table name must not be empty".to_string());
|
|
14
|
+
}
|
|
15
|
+
if name.len() > 64 {
|
|
16
|
+
return Err("table name too long (max 64 chars)".to_string());
|
|
17
|
+
}
|
|
18
|
+
if name == "." || name == ".." {
|
|
19
|
+
return Err(format!("invalid table name '{}'", name));
|
|
20
|
+
}
|
|
21
|
+
for (i, c) in name.chars().enumerate() {
|
|
22
|
+
if c.is_ascii_control() || c == '/' || c == '\\' {
|
|
23
|
+
return Err(format!("invalid character in table name '{}'", name));
|
|
24
|
+
}
|
|
25
|
+
if c == '.' && (i > 0 && name.as_bytes()[i - 1] == b'.') {
|
|
26
|
+
return Err(format!("invalid table name '{}' ('..' sequences are not allowed)", name));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
Ok(())
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
use std::collections::HashMap;
|
|
33
|
+
use crate::types::{Row, TableDefinition, FieldSchema};
|
|
34
|
+
|
|
35
|
+
pub trait Engine {
|
|
36
|
+
fn create_table(&mut self, def: TableDefinition) -> Result<(), String>;
|
|
37
|
+
/// 运行期加列:扩展表结构并为已有行回填默认值(见 `Table::add_column`)。
|
|
38
|
+
fn add_column(&mut self, table: &str, name: &str, fs: FieldSchema) -> Result<(), String>;
|
|
39
|
+
fn drop_table(&mut self, name: &str) -> Result<(), String>;
|
|
40
|
+
fn has_table(&self, name: &str) -> bool;
|
|
41
|
+
fn list_tables(&self) -> Vec<String>;
|
|
42
|
+
|
|
43
|
+
fn insert(&mut self, table: &str, fields: Vec<HashMap<String, serde_json::Value>>) -> Result<Vec<u64>, String>;
|
|
44
|
+
fn find_by_id(&self, table: &str, id: u64) -> Result<Option<Row>, String>;
|
|
45
|
+
fn find_by_id_json(&self, table: &str, id: u64) -> Result<Option<String>, String>;
|
|
46
|
+
fn find_by_ids_json(&self, table: &str, ids: &[u64]) -> Result<String, String>;
|
|
47
|
+
fn find(&self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
|
|
48
|
+
limit: Option<usize>, offset: Option<usize>,
|
|
49
|
+
cursor: Option<String>,
|
|
50
|
+
order_by: &Option<String>, order: &Option<String>) -> Result<(Vec<Row>, Option<String>, bool), String>;
|
|
51
|
+
/// 与 `find` 等价,但直接产出 JSON 字符串,省掉 `Row` / `HashMap<String, Value>`
|
|
52
|
+
/// 中间表示与随之而来的二次序列化。默认实现回退到 `find`。
|
|
53
|
+
fn find_json(&self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
|
|
54
|
+
limit: Option<usize>, offset: Option<usize>,
|
|
55
|
+
cursor: Option<String>,
|
|
56
|
+
order_by: &Option<String>, order: &Option<String>) -> Result<(String, Option<String>, bool), String> {
|
|
57
|
+
let (rows, cursor, has_more) = self.find(table, filter, limit, offset, cursor, order_by, order)?;
|
|
58
|
+
let json = serde_json::to_string(&rows).unwrap_or_else(|_| "[]".to_string());
|
|
59
|
+
Ok((json, cursor, has_more))
|
|
60
|
+
}
|
|
61
|
+
fn count(&self, table: &str) -> Result<usize, String>;
|
|
62
|
+
fn update_by_id(&mut self, table: &str, id: u64, data: HashMap<String, serde_json::Value>) -> Result<bool, String>;
|
|
63
|
+
fn update_by_ids(&mut self, table: &str, batch: Vec<(u64, HashMap<String, serde_json::Value>)>) -> Result<usize, String>;
|
|
64
|
+
fn remove_by_id(&mut self, table: &str, id: u64) -> Result<bool, String>;
|
|
65
|
+
fn remove_by_ids(&mut self, table: &str, ids: &[u64]) -> Result<usize, String>;
|
|
66
|
+
|
|
67
|
+
fn update_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
|
|
68
|
+
data: HashMap<String, serde_json::Value>) -> Result<usize, String>;
|
|
69
|
+
fn remove_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>) -> Result<usize, String>;
|
|
70
|
+
|
|
71
|
+
fn begin_tx(&mut self) -> Result<String, String>;
|
|
72
|
+
fn commit_tx(&mut self, tx_id: &str) -> Result<(), String>;
|
|
73
|
+
fn rollback_tx(&mut self, tx_id: &str) -> Result<(), String>;
|
|
74
|
+
}
|