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.
Files changed (81) hide show
  1. package/LICENSE +202 -202
  2. package/README.md +8155 -8097
  3. package/bin/jsql +320 -320
  4. package/bin/jsql-server +90 -90
  5. package/bin/jsql-server-control.js +177 -177
  6. package/index.d.ts +412 -385
  7. package/index.js +128 -124
  8. package/lib/ast.js +214 -214
  9. package/lib/btree.js +478 -413
  10. package/lib/cache.js +328 -328
  11. package/lib/client.js +125 -125
  12. package/lib/database.js +1741 -1744
  13. package/lib/date-types.js +182 -182
  14. package/lib/errors.js +89 -89
  15. package/lib/jsql_format.js +497 -497
  16. package/lib/migrate.js +254 -254
  17. package/lib/mod.js +292 -292
  18. package/lib/mongo_server.js +607 -607
  19. package/lib/multiserver.js +140 -140
  20. package/lib/mysql_compat.js +413 -413
  21. package/lib/mysql_server.js +1237 -1237
  22. package/lib/native_client.js +722 -732
  23. package/lib/nedb_compat.js +506 -506
  24. package/lib/pg_server.js +864 -864
  25. package/lib/plugin.js +259 -35
  26. package/lib/plugins/audit.js +37 -0
  27. package/lib/plugins/index.js +72 -0
  28. package/lib/plugins/logger.js +35 -0
  29. package/lib/plugins/metrics.js +38 -0
  30. package/lib/plugins/timestamps.js +57 -0
  31. package/lib/plugins/validation.js +128 -0
  32. package/lib/prepared.js +37 -0
  33. package/lib/query.js +834 -815
  34. package/lib/redis_server.js +571 -571
  35. package/lib/sql.js +4053 -3460
  36. package/lib/sqlite_compat.js +323 -323
  37. package/lib/sqlite_worker.js +264 -264
  38. package/lib/table.js +1351 -1304
  39. package/lib/tui.js +502 -502
  40. package/lib/wasm.js +1 -1
  41. package/lib/wasm_client.js +715 -718
  42. package/lib/web_ui.js +257 -257
  43. package/native/jsql-neo-native.node +0 -0
  44. package/nativesrc/jsql-neo-core/Cargo.lock +462 -455
  45. package/nativesrc/jsql-neo-core/Cargo.toml +26 -24
  46. package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +492 -455
  47. package/nativesrc/jsql-neo-core/src/engine/memory.rs +191 -168
  48. package/nativesrc/jsql-neo-core/src/engine/mod.rs +74 -62
  49. package/nativesrc/jsql-neo-core/src/engine/table.rs +996 -700
  50. package/nativesrc/jsql-neo-core/src/lib.rs +2 -2
  51. package/nativesrc/jsql-neo-core/src/storage/mod.rs +2 -2
  52. package/nativesrc/jsql-neo-core/src/storage/persistent.rs +1 -1
  53. package/nativesrc/jsql-neo-core/src/storage/wal.rs +298 -85
  54. package/nativesrc/jsql-neo-core/src/types.rs +93 -93
  55. package/nativesrc/jsql-neo-native/Cargo.lock +613 -606
  56. package/nativesrc/jsql-neo-native/Cargo.toml +24 -16
  57. package/nativesrc/jsql-neo-native/build.rs +3 -3
  58. package/nativesrc/jsql-neo-native/package.json +7 -7
  59. package/nativesrc/jsql-neo-native/src/lib.rs +281 -269
  60. package/nativesrc/jsql-neo-wasm/Cargo.lock +472 -465
  61. package/nativesrc/jsql-neo-wasm/Cargo.toml +22 -15
  62. package/nativesrc/jsql-neo-wasm/src/lib.rs +463 -447
  63. package/package.json +87 -87
  64. package/postinstall.js +158 -158
  65. package/test/btree.test.js +144 -128
  66. package/test/coverage.js +187 -187
  67. package/test/join.test.js +110 -110
  68. package/test/native.test.js +158 -158
  69. package/test/query-opt.test.js +109 -0
  70. package/test/regress-5.1.0.js +271 -271
  71. package/test/smoke.js +58 -58
  72. package/test/sql-parser.test.js +450 -273
  73. package/test/wasm.test.js +110 -110
  74. package/wasm/browser.d.ts +89 -89
  75. package/wasm/browser.mjs +591 -569
  76. package/wasm/browser_bg.mjs +618 -597
  77. package/wasm/jsql_neo_wasm.d.ts +44 -38
  78. package/wasm/jsql_neo_wasm.js +641 -520
  79. package/wasm/jsql_neo_wasm_bg.wasm +0 -0
  80. package/wasm/jsql_neo_wasm_bg.wasm.d.ts +26 -26
  81. 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 drop_table(&mut self, name: &str) -> Result<(), String> {
39
- self.tables.remove(name).map(|_| ()).ok_or_else(|| format!("table '{}' not found", name))
40
- }
41
-
42
- fn has_table(&self, name: &str) -> bool {
43
- self.tables.contains_key(name)
44
- }
45
-
46
- fn list_tables(&self) -> Vec<String> {
47
- self.tables.keys().cloned().collect()
48
- }
49
-
50
- fn insert(&mut self, table: &str, fields_batch: Vec<HashMap<String, serde_json::Value>>) -> Result<Vec<u64>, String> {
51
- let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
52
- Ok(t.add_rows(fields_batch))
53
- }
54
-
55
- fn find_by_id(&self, table: &str, id: u64) -> Result<Option<Row>, String> {
56
- let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
57
- Ok(t.get_row(id))
58
- }
59
-
60
- fn find_by_id_json(&self, table: &str, id: u64) -> Result<Option<String>, String> {
61
- let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
62
- Ok(t.get_row_json(id))
63
- }
64
-
65
- fn find_by_ids_json(&self, table: &str, ids: &[u64]) -> Result<String, String> {
66
- let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
67
- Ok(t.get_rows_json(ids))
68
- }
69
-
70
- fn find(&self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
71
- limit: Option<usize>, offset: Option<usize>,
72
- cursor: Option<String>,
73
- order_by: &Option<String>, order: &Option<String>) -> Result<(Vec<Row>, Option<String>, bool), String> {
74
- let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
75
- let off = cursor.as_deref().and_then(|c| c.strip_prefix('c')?.parse().ok()).or(offset);
76
- let off_val = off.unwrap_or(0);
77
- let probe_limit = limit.map(|l| l + 1);
78
- let all = t.find_rows(filter, probe_limit, off, order_by, order);
79
- let has_more = limit.map_or(false, |l| all.len() > l);
80
- let rows: Vec<Row> = all.into_iter().take(limit.unwrap_or(usize::MAX)).collect();
81
- let next_cursor = if has_more { Some(format!("c{}", off_val + limit.unwrap())) } else { None };
82
- Ok((rows, next_cursor, has_more))
83
- }
84
-
85
- fn count(&self, table: &str) -> Result<usize, String> {
86
- let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
87
- Ok(t.count())
88
- }
89
-
90
- fn update_by_id(&mut self, table: &str, id: u64, data: HashMap<String, serde_json::Value>) -> Result<bool, String> {
91
- let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
92
- Ok(t.update_row(id, data))
93
- }
94
-
95
- fn remove_by_id(&mut self, table: &str, id: u64) -> Result<bool, String> {
96
- let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
97
- Ok(t.remove_row(id))
98
- }
99
-
100
- fn remove_by_ids(&mut self, table: &str, ids: &[u64]) -> Result<usize, String> {
101
- let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
102
- Ok(t.remove_rows(ids))
103
- }
104
-
105
- fn update_by_ids(&mut self, table: &str, batch: Vec<(u64, HashMap<String, serde_json::Value>)>) -> Result<usize, String> {
106
- let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
107
- Ok(t.update_rows(batch))
108
- }
109
-
110
- fn update_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
111
- data: HashMap<String, serde_json::Value>) -> Result<usize, String> {
112
- let ids: Vec<u64> = {
113
- let t = self.tables.get(table).ok_or_else(|| format!("table '{}' not found", table))?;
114
- t.rows.iter()
115
- .filter(|r| filter.as_ref().map_or(true, |f| t.row_matches(r, f)))
116
- .map(|r| r.id)
117
- .collect()
118
- };
119
- let count = ids.len();
120
- for id in &ids {
121
- let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
122
- t.update_row(*id, data.clone());
123
- }
124
- Ok(count)
125
- }
126
-
127
- fn remove_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>) -> Result<usize, String> {
128
- let t = self.tables.get_mut(table).ok_or_else(|| format!("table '{}' not found", table))?;
129
- let len_before = t.rows.len();
130
- if filter.is_none() {
131
- t.rows.clear();
132
- t.pk_index.clear();
133
- return Ok(len_before);
134
- }
135
- let f = filter.as_ref().unwrap();
136
- let mut to_remove = Vec::new();
137
- for (i, r) in t.rows.iter().enumerate() {
138
- let matches = f.iter().all(|(k, v)| {
139
- t.field_index.get(k).and_then(|&pos| t.get_row_values(r).get(pos)).map_or(false, |fv| fv.eq_json(v))
140
- });
141
- if matches { to_remove.push(i); }
142
- }
143
- for i in to_remove.into_iter().rev() {
144
- t.remove_row_by_idx(i);
145
- }
146
- Ok(len_before - t.rows.len())
147
- }
148
-
149
- fn begin_tx(&mut self) -> Result<String, String> {
150
- let tx_id = format!("tx{}", self.next_tx_id);
151
- self.next_tx_id += 1;
152
- self.txs.insert(tx_id.clone(), self.snapshot());
153
- Ok(tx_id)
154
- }
155
-
156
- fn commit_tx(&mut self, tx_id: &str) -> Result<(), String> {
157
- self.txs.remove(tx_id)
158
- .map(|_| ())
159
- .ok_or_else(|| format!("transaction '{}' not found", tx_id))
160
- }
161
-
162
- fn rollback_tx(&mut self, tx_id: &str) -> Result<(), String> {
163
- let snap = self.txs.remove(tx_id)
164
- .ok_or_else(|| format!("transaction '{}' not found", tx_id))?;
165
- self.tables = snap;
166
- Ok(())
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
- fn drop_table(&mut self, name: &str) -> Result<(), String>;
38
- fn has_table(&self, name: &str) -> bool;
39
- fn list_tables(&self) -> Vec<String>;
40
-
41
- fn insert(&mut self, table: &str, fields: Vec<HashMap<String, serde_json::Value>>) -> Result<Vec<u64>, String>;
42
- fn find_by_id(&self, table: &str, id: u64) -> Result<Option<Row>, String>;
43
- fn find_by_id_json(&self, table: &str, id: u64) -> Result<Option<String>, String>;
44
- fn find_by_ids_json(&self, table: &str, ids: &[u64]) -> Result<String, String>;
45
- fn find(&self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
46
- limit: Option<usize>, offset: Option<usize>,
47
- cursor: Option<String>,
48
- order_by: &Option<String>, order: &Option<String>) -> Result<(Vec<Row>, Option<String>, bool), String>;
49
- fn count(&self, table: &str) -> Result<usize, String>;
50
- fn update_by_id(&mut self, table: &str, id: u64, data: HashMap<String, serde_json::Value>) -> Result<bool, String>;
51
- fn update_by_ids(&mut self, table: &str, batch: Vec<(u64, HashMap<String, serde_json::Value>)>) -> Result<usize, String>;
52
- fn remove_by_id(&mut self, table: &str, id: u64) -> Result<bool, String>;
53
- fn remove_by_ids(&mut self, table: &str, ids: &[u64]) -> Result<usize, String>;
54
-
55
- fn update_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>,
56
- data: HashMap<String, serde_json::Value>) -> Result<usize, String>;
57
- fn remove_by_filter(&mut self, table: &str, filter: &Option<HashMap<String, serde_json::Value>>) -> Result<usize, String>;
58
-
59
- fn begin_tx(&mut self) -> Result<String, String>;
60
- fn commit_tx(&mut self, tx_id: &str) -> Result<(), String>;
61
- fn rollback_tx(&mut self, tx_id: &str) -> Result<(), String>;
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
+ }