-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.rs
138 lines (115 loc) · 3.93 KB
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
extern crate alloc;
use alloc::format;
use alloc::string::String;
use serde::Deserialize;
use serde_json as json;
use sqlite::{Connection, ResultCode};
use sqlite_nostd as sqlite;
use sqlite_nostd::ManagedStmt;
use uuid::Uuid;
#[cfg(not(feature = "getrandom"))]
use uuid::Builder;
use crate::error::SQLiteError;
pub fn quote_string(s: &str) -> String {
format!("'{:}'", s.replace("'", "''"))
}
pub fn quote_json_path(s: &str) -> String {
quote_string(&format!("$.{:}", s))
}
pub fn quote_identifier(name: &str) -> String {
format!("\"{:}\"", name.replace("\"", "\"\""))
}
pub fn quote_internal_name(name: &str, local_only: bool) -> String {
if local_only {
quote_identifier_prefixed("ps_data_local__", name)
} else {
quote_identifier_prefixed("ps_data__", name)
}
}
pub fn internal_table_name(name: &str) -> String {
return format!("ps_data__{}", name);
}
pub fn quote_identifier_prefixed(prefix: &str, name: &str) -> String {
return format!("\"{:}{:}\"", prefix, name.replace("\"", "\"\""));
}
pub fn extract_table_info(
db: *mut sqlite::sqlite3,
data: &str,
) -> Result<ManagedStmt, SQLiteError> {
// language=SQLite
let statement = db.prepare_v2(
"SELECT
json_extract(?1, '$.name') as name,
ifnull(json_extract(?1, '$.view_name'), json_extract(?1, '$.name')) as view_name,
json_extract(?1, '$.local_only') as local_only,
json_extract(?1, '$.insert_only') as insert_only,
json_extract(?1, '$.include_old') as include_old,
json_extract(?1, '$.include_metadata') as include_metadata,
json_extract(?1, '$.ignore_empty_update') as ignore_empty_update",
)?;
statement.bind_text(1, data, sqlite::Destructor::STATIC)?;
let step_result = statement.step()?;
if step_result != ResultCode::ROW {
return Err(SQLiteError::from(ResultCode::SCHEMA));
}
Ok(statement)
}
pub fn deserialize_string_to_i64<'de, D>(deserializer: D) -> Result<i64, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = json::Value::deserialize(deserializer)?;
match value {
json::Value::String(s) => s.parse::<i64>().map_err(serde::de::Error::custom),
_ => Err(serde::de::Error::custom("Expected a string.")),
}
}
pub fn deserialize_optional_string_to_i64<'de, D>(deserializer: D) -> Result<Option<i64>, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = json::Value::deserialize(deserializer)?;
match value {
json::Value::Null => Ok(None),
json::Value::String(s) => s.parse::<i64>().map(Some).map_err(serde::de::Error::custom),
_ => Err(serde::de::Error::custom("Expected a string or null.")),
}
}
// Use getrandom crate to generate UUID.
// This is not available in all WASM builds - use the default in those cases.
#[cfg(feature = "getrandom")]
pub fn gen_uuid() -> Uuid {
let id = Uuid::new_v4();
id
}
// Default - use sqlite3_randomness to generate UUID
// This uses ChaCha20 PRNG, with /dev/urandom as a seed on unix.
// On Windows, it uses custom logic for the seed, which may not be secure.
// Rather avoid this version for most builds.
#[cfg(not(feature = "getrandom"))]
pub fn gen_uuid() -> Uuid {
let mut random_bytes: [u8; 16] = [0; 16];
sqlite::randomness(&mut random_bytes);
let id = Builder::from_random_bytes(random_bytes).into_uuid();
id
}
pub const MAX_OP_ID: &str = "9223372036854775807";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quote_identifier_test() {
assert_eq!(quote_identifier("test"), "\"test\"");
assert_eq!(quote_identifier("\"quote\""), "\"\"\"quote\"\"\"");
assert_eq!(
quote_identifier("other characters."),
"\"other characters.\""
);
}
#[test]
fn quote_string_test() {
assert_eq!(quote_string("test"), "'test'");
assert_eq!(quote_string("\"quote\""), "'\"quote\"'");
assert_eq!(quote_string("'quote'"), "'''quote'''");
}
}