forked from tursodatabase/libsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.rs
More file actions
294 lines (245 loc) · 9.56 KB
/
connection.rs
File metadata and controls
294 lines (245 loc) · 9.56 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::collections::VecDeque;
use std::fmt;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use crate::auth::{AuthContext, Authorization};
use crate::params::{IntoParams, Params};
use crate::rows::Rows;
use crate::statement::Statement;
use crate::transaction::Transaction;
use crate::{Result, TransactionBehavior};
pub type AuthHook = Arc<dyn Fn(&AuthContext) -> Authorization>;
#[async_trait::async_trait]
pub(crate) trait Conn {
async fn execute(&self, sql: &str, params: Params) -> Result<u64>;
async fn execute_batch(&self, sql: &str) -> Result<BatchRows>;
async fn execute_transactional_batch(&self, sql: &str) -> Result<BatchRows>;
async fn prepare(&self, sql: &str) -> Result<Statement>;
async fn transaction(&self, tx_behavior: TransactionBehavior) -> Result<Transaction>;
fn interrupt(&self) -> Result<()>;
fn busy_timeout(&self, timeout: Duration) -> Result<()>;
fn is_autocommit(&self) -> bool;
fn changes(&self) -> u64;
fn total_changes(&self) -> u64;
fn last_insert_rowid(&self) -> i64;
async fn reset(&self);
fn set_reserved_bytes(&self, _reserved_bytes: i32) -> Result<()> {
Err(crate::Error::ReservedBytesNotSupported)
}
fn get_reserved_bytes(&self) -> Result<i32> {
Err(crate::Error::ReservedBytesNotSupported)
}
fn enable_load_extension(&self, _onoff: bool) -> Result<()> {
Err(crate::Error::LoadExtensionNotSupported)
}
fn load_extension(&self, _dylib_path: &Path, _entry_point: Option<&str>) -> Result<()> {
Err(crate::Error::LoadExtensionNotSupported)
}
fn authorizer(&self, _hook: Option<AuthHook>) -> Result<()> {
Err(crate::Error::AuthorizerNotSupported)
}
}
/// A set of rows returned from `execute_batch`/`execute_transactional_batch`. It is essentially
/// rows of rows for each statement in the batch call.
///
/// # Note
///
/// All rows will be materialized in memory, if you would like to stream them then use `query`
/// instead as this is optimized better for memory usage.
pub struct BatchRows {
inner: VecDeque<Option<Rows>>,
skip_last_amt: usize,
}
impl BatchRows {
#[allow(unused)]
pub(crate) fn empty() -> Self {
Self {
inner: VecDeque::new(),
skip_last_amt: 0,
}
}
#[cfg(any(feature = "hrana", feature = "core"))]
pub(crate) fn new(rows: Vec<Option<Rows>>) -> Self {
Self {
inner: rows.into(),
skip_last_amt: 0,
}
}
#[cfg(feature = "hrana")]
pub(crate) fn new_skip_last(rows: Vec<Option<Rows>>, skip_last_amt: usize) -> Self {
Self {
inner: rows.into(),
skip_last_amt,
}
}
/// Get the next set of rows, it is wrapped in two options, if the first option returns `None`
/// then the set of batch statement results has ended. If the inner option returns `None` then
/// the statement was never executed (potentially due to a conditional).
pub fn next_stmt_row(&mut self) -> Option<Option<Rows>> {
if self.inner.len() <= self.skip_last_amt {
return None;
}
self.inner.pop_front()
}
}
impl fmt::Debug for BatchRows {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BatchRows").finish()
}
}
/// A connection to some libsql database, this can be a remote one or a local one.
#[derive(Clone)]
pub struct Connection {
pub(crate) conn: Arc<dyn Conn + Send + Sync>,
}
impl Connection {
/// Execute sql query provided some type that implements [`IntoParams`] returning
/// on success the number of rows that were changed.
///
/// # Example
///
/// ```rust,no_run
/// # async fn run(conn: &libsql::Connection) {
/// # use libsql::params;
/// conn.execute("INSERT INTO foo (id) VALUES (?1)", [42]).await.unwrap();
/// conn.execute("INSERT INTO foo (id, name) VALUES (?1, ?2)", params![42, "baz"]).await.unwrap();
/// # }
/// ```
///
/// For more info on how to pass params check [`IntoParams`]'s docs.
pub async fn execute(&self, sql: &str, params: impl IntoParams) -> Result<u64> {
tracing::trace!("executing `{}`", sql);
self.conn.execute(sql, params.into_params()?).await
}
/// Execute a batch set of statements.
///
/// # Return
///
/// This returns a `BatchRows` currently only the `remote` and `local` connection supports this feature and
/// all other connection types will return an empty set always.
pub async fn execute_batch(&self, sql: &str) -> Result<BatchRows> {
tracing::trace!("executing batch `{}`", sql);
self.conn.execute_batch(sql).await
}
/// Execute a batch set of statements atomically in a transaction.
///
/// # Return
///
/// This returns a `BatchRows` currently only the `remote` and `local` connection supports this feature and
/// all other connection types will return an empty set always.
pub async fn execute_transactional_batch(&self, sql: &str) -> Result<BatchRows> {
tracing::trace!("executing batch transactional `{}`", sql);
self.conn.execute_transactional_batch(sql).await
}
/// Execute sql query provided some type that implements [`IntoParams`] returning
/// on success the [`Rows`].
///
/// # Example
///
/// ```rust,no_run
/// # async fn run(conn: &libsql::Connection) {
/// # use libsql::params;
/// conn.query("SELECT foo FROM bar WHERE id = ?1", [42]).await.unwrap();
/// conn.query("SELECT foo FROM bar WHERE id = ?1 AND name = ?2", params![42, "baz"]).await.unwrap();
/// # }
/// ```
/// For more info on how to pass params check [`IntoParams`]'s docs and on how to
/// extract values out of the rows check the [`Rows`] docs.
pub async fn query(&self, sql: &str, params: impl IntoParams) -> Result<Rows> {
let stmt = self.prepare(sql).await?;
stmt.query(params).await
}
/// Prepares a cached statement.
pub async fn prepare(&self, sql: &str) -> Result<Statement> {
tracing::trace!("preparing `{}`", sql);
self.conn.prepare(sql).await
}
/// Begin a new transaction in `DEFERRED` mode, which is the default.
pub async fn transaction(&self) -> Result<Transaction> {
tracing::trace!("starting deferred transaction");
self.transaction_with_behavior(TransactionBehavior::Deferred)
.await
}
/// Begin a new transaction in the given [`TransactionBehavior`].
pub async fn transaction_with_behavior(
&self,
tx_behavior: TransactionBehavior,
) -> Result<Transaction> {
tracing::trace!("starting {:?} transaction", tx_behavior);
self.conn.transaction(tx_behavior).await
}
/// Cancel ongoing operations and return at earliest opportunity.
pub fn interrupt(&self) -> Result<()> {
self.conn.interrupt()
}
pub fn busy_timeout(&self, timeout: Duration) -> Result<()> {
self.conn.busy_timeout(timeout)
}
/// Check whether libsql is in `autocommit` or not.
pub fn is_autocommit(&self) -> bool {
self.conn.is_autocommit()
}
/// Check the amount of changes the last query created.
pub fn changes(&self) -> u64 {
self.conn.changes()
}
/// Check the total amount of changes the connection has done.
pub fn total_changes(&self) -> u64 {
self.conn.total_changes()
}
/// Check the last inserted row id.
pub fn last_insert_rowid(&self) -> i64 {
self.conn.last_insert_rowid()
}
pub async fn reset(&self) {
self.conn.reset().await
}
pub fn set_reserved_bytes(&self, reserved_bytes: i32) -> Result<()> {
self.conn.set_reserved_bytes(reserved_bytes)
}
pub fn get_reserved_bytes(&self) -> Result<i32> {
self.conn.get_reserved_bytes()
}
/// Enable loading SQLite extensions from SQL queries and Rust API.
///
/// See [`load_extension`](Connection::load_extension) documentation for more details.
pub fn load_extension_enable(&self) -> Result<()> {
self.conn.enable_load_extension(true)
}
/// Disable loading SQLite extensions from SQL queries and Rust API.
///
/// See [`load_extension`](Connection::load_extension) documentation for more details.
pub fn load_extension_disable(&self) -> Result<()> {
self.conn.enable_load_extension(false)
}
/// Load a SQLite extension from a dynamic library at `dylib_path`, specifying optional
/// entry point `entry_point`.
///
/// # Security
///
/// Loading extensions from dynamic libraries is a potential security risk, as it allows
/// arbitrary code execution. Only load extensions that you trust.
///
/// Extension loading is disabled by default. Please use the [`load_extension_enable`](Connection::load_extension_enable)
/// method to enable it. It's recommended to disable extension loading after you're done
/// loading extensions to avoid SQL injection attacks from loading extensions.
///
/// See SQLite's documentation on `sqlite3_load_extension` for more information:
/// https://sqlite.org/c3ref/load_extension.html
pub fn load_extension<P: AsRef<Path>>(
&self,
dylib_path: P,
entry_point: Option<&str>,
) -> Result<()> {
self.conn.load_extension(dylib_path.as_ref(), entry_point)
}
pub fn authorizer(&self, hook: Option<AuthHook>) -> Result<()> {
self.conn.authorizer(hook)
}
}
impl fmt::Debug for Connection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Connection").finish()
}
}