-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathlocal.rs
230 lines (211 loc) · 7.96 KB
/
local.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use crate::{proto, proto::StmtResult, BatchResult, Col, ResultSet, Statement, Value};
use anyhow::Result;
use sqlite3_parser::ast::{Cmd, Stmt};
use sqlite3_parser::lexer::sql::Parser;
use fallible_iterator::FallibleIterator;
/// Database client. This is the main structure used to
/// communicate with the database.
pub struct Client {
db: libsql::Database,
conn: libsql::Connection,
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("local::Client").finish()
}
}
struct ValueWrapper(Value);
impl From<ValueWrapper> for libsql::Value {
fn from(v: ValueWrapper) -> Self {
match v.0 {
Value::Null => libsql::Value::Null,
Value::Integer { value: n } => libsql::Value::Integer(n),
Value::Text { value: s } => libsql::Value::Text(s),
Value::Float { value: d } => libsql::Value::Real(d),
Value::Blob { value: b } => libsql::Value::Blob(b),
}
}
}
impl From<libsql::Value> for ValueWrapper {
fn from(v: libsql::Value) -> Self {
match v {
libsql::Value::Null => ValueWrapper(Value::Null),
libsql::Value::Integer(n) => ValueWrapper(Value::Integer { value: n }),
libsql::Value::Text(s) => ValueWrapper(Value::Text { value: s }),
libsql::Value::Real(d) => ValueWrapper(Value::Float { value: d }),
libsql::Value::Blob(b) => ValueWrapper(Value::Blob { value: b }),
}
}
}
impl Client {
/// Establishes a database client.
///
/// # Arguments
/// * `path` - path of the local database
pub fn new(path: impl Into<String>) -> anyhow::Result<Self> {
let db = libsql::Database::open(path.into())?;
let conn = db.connect()?;
Ok(Self { db, conn })
}
/// Establishes a new in-memory database and connects to it.
pub fn in_memory() -> anyhow::Result<Self> {
let db = libsql::Database::open(":memory:")?;
let conn = db.connect()?;
Ok(Self { db, conn })
}
pub fn from_env() -> anyhow::Result<Self> {
let path = std::env::var("LIBSQL_CLIENT_URL").map_err(|_| {
anyhow::anyhow!("LIBSQL_CLIENT_URL variable should point to your sqld database")
})?;
let path = match path.strip_prefix("file:///") {
Some(path) => path,
None => anyhow::bail!("Local URL needs to start with file:///"),
};
Self::new(path)
}
pub async fn sync(&self) -> anyhow::Result<usize> {
self.db.sync().await.map_err(|e| anyhow::anyhow!("{}", e))
}
/// Executes a batch of SQL statements.
/// Each statement is going to run in its own transaction,
/// unless they're wrapped in BEGIN and END
///
/// # Arguments
/// * `stmts` - SQL statements
///
/// # Examples
///
/// ```
/// # fn f() {
/// let db = libsql_client::local::Client::new("/tmp/example321.db").unwrap();
/// let result = db
/// .raw_batch(["CREATE TABLE t(id)", "INSERT INTO t VALUES (42)"]);
/// # }
/// ```
pub async fn raw_batch(
&self,
stmts: impl IntoIterator<Item = impl Into<Statement>>,
) -> anyhow::Result<BatchResult> {
let mut step_results = vec![];
let mut step_errors = vec![];
for stmt in stmts {
let stmt = stmt.into();
let sql_string = &stmt.sql;
let params: libsql::Params = stmt
.args
.into_iter()
.map(ValueWrapper)
.map(libsql::Value::from)
.collect::<Vec<_>>()
.into();
let stmt = self.conn.prepare(sql_string).await?;
let cols: Vec<Col> = stmt
.columns()
.into_iter()
.map(|c| Col {
name: Some(c.name().to_string()),
})
.collect();
let mut rows = Vec::new();
let mut input_rows = match stmt.query(¶ms).await {
Ok(rows) => rows,
Err(e) => {
step_results.push(None);
step_errors.push(Some(proto::Error {
message: e.to_string(),
}));
break;
}
};
while let Some(row) = input_rows.next()? {
let cells = (0..cols.len())
.map(|i| ValueWrapper::from(row.get_value(i as i32).unwrap()).0)
.collect();
rows.push(cells)
}
let parser = Parser::new(sql_string.as_bytes());
let cmd = parser.last();
let last_insert_rowid = match cmd {
Ok(Some(Cmd::Stmt(Stmt::Insert { .. }))) => Some(self.conn.last_insert_rowid()),
_ => None,
};
let affected_row_count = match cmd {
Ok(Some(
Cmd::Stmt(Stmt::Insert { .. })
| Cmd::Stmt(Stmt::Update { .. })
| Cmd::Stmt(Stmt::Delete { .. }),
)) => self.conn.changes(),
_ => 0,
};
let stmt_result = StmtResult {
cols,
rows,
affected_row_count,
last_insert_rowid,
};
step_results.push(Some(stmt_result));
step_errors.push(None);
}
Ok(BatchResult {
step_results,
step_errors,
})
}
/// Executes a batch of SQL statements, wrapped in "BEGIN", "END", transaction-style.
/// Each statement is going to run in its own transaction,
/// unless they're wrapped in BEGIN and END
///
/// # Arguments
/// * `stmts` - SQL statements
pub async fn batch(
&self,
stmts: impl IntoIterator<Item = impl Into<Statement> + Send> + Send,
) -> Result<Vec<ResultSet>> {
let batch_results = self.raw_batch(
std::iter::once(Statement::new("BEGIN"))
.chain(stmts.into_iter().map(|s| s.into()))
.chain(std::iter::once(Statement::new("END"))),
).await?;
let step_error: Option<proto::Error> = batch_results
.step_errors
.into_iter()
.skip(1)
.find(|e| e.is_some())
.flatten();
if let Some(error) = step_error {
return Err(anyhow::anyhow!(error.message));
}
let mut step_results: Vec<Result<ResultSet>> = batch_results
.step_results
.into_iter()
.skip(1) // BEGIN is not counted in the result, it's implicitly ignored
.map(|maybe_rs| {
maybe_rs
.map(ResultSet::from)
.ok_or_else(|| anyhow::anyhow!("Unexpected missing result set"))
})
.collect();
step_results.pop(); // END is not counted in the result, it's implicitly ignored
// Collect all the results into a single Result
step_results.into_iter().collect::<Result<Vec<ResultSet>>>()
}
/// # Arguments
/// * `stmt` - the SQL statement
pub async fn execute(&self, stmt: impl Into<Statement> + Send) -> Result<ResultSet> {
let results = self.raw_batch(std::iter::once(stmt)).await?;
match (results.step_results.first(), results.step_errors.first()) {
(Some(Some(result)), Some(None)) => Ok(ResultSet::from(result.clone())),
(Some(None), Some(Some(err))) => Err(anyhow::anyhow!(err.message.clone())),
_ => unreachable!(),
}
}
pub async fn execute_in_transaction(&self, _tx_id: u64, stmt: Statement) -> Result<ResultSet> {
self.execute(stmt).await
}
pub async fn commit_transaction(&self, _tx_id: u64) -> Result<()> {
self.execute("COMMIT").await.map(|_| ())
}
pub async fn rollback_transaction(&self, _tx_id: u64) -> Result<()> {
self.execute("ROLLBACK").await.map(|_| ())
}
}