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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#![allow(dead_code)]
use crate::local::rows::BatchedRows;
use crate::params::Params;
use crate::{connection::BatchRows, errors};
use super::{Database, Error, Result, Rows, RowsFuture, Statement, Transaction};
use crate::TransactionBehavior;
use libsql_sys::ffi;
use std::{ffi::c_int, fmt, path::Path, sync::Arc};
/// A connection to a libSQL database.
#[derive(Clone)]
pub struct Connection {
pub(crate) raw: *mut ffi::sqlite3,
drop_ref: Arc<()>,
#[cfg(feature = "replication")]
pub(crate) writer: Option<crate::replication::Writer>,
}
impl Drop for Connection {
fn drop(&mut self) {
self.disconnect()
}
}
// SAFETY: This is safe because we compile sqlite3 w/ SQLITE_THREADSAFE=1
unsafe impl Send for Connection {}
// SAFETY: This is safe because we compile sqlite3 w/ SQLITE_THREADSAFE=1
unsafe impl Sync for Connection {}
impl Connection {
/// Connect to the database.
pub(crate) fn connect(db: &Database) -> Result<Connection> {
let mut raw = std::ptr::null_mut();
let db_path = db.db_path.clone();
let err = unsafe {
ffi::sqlite3_open_v2(
std::ffi::CString::new(db_path.as_str())
.unwrap()
.as_c_str()
.as_ptr() as *const _,
&mut raw,
db.flags.bits() as c_int,
std::ptr::null(),
)
};
match err {
ffi::SQLITE_OK => {}
_ => {
return Err(Error::ConnectionFailed(format!(
"Unable to open connection to local database {db_path}: {err}",
)));
}
}
Ok(Connection {
raw,
drop_ref: Arc::new(()),
#[cfg(feature = "replication")]
writer: db.writer()?,
})
}
/// Get a raw handle to the underlying libSQL connection
pub fn handle(&self) -> *mut ffi::sqlite3 {
self.raw
}
/// Create a connection from a raw handle to the underlying libSQL connection
pub fn from_handle(raw: *mut ffi::sqlite3) -> Self {
Self {
raw,
drop_ref: Arc::new(()),
#[cfg(feature = "replication")]
writer: None,
}
}
/// Disconnect from the database.
pub fn disconnect(&mut self) {
if Arc::get_mut(&mut self.drop_ref).is_some() {
unsafe { libsql_sys::ffi::sqlite3_close_v2(self.raw) };
}
}
/// Prepare the SQL statement.
pub fn prepare<S: Into<String>>(&self, sql: S) -> Result<Statement> {
Statement::prepare(self.clone(), self.raw, sql.into().as_str())
}
/// Convenience method to run a prepared statement query.
/// ## Example
///
/// ```rust,no_run,ignore
/// # use libsql::Result;
/// # use libsql::v1::{Connection, Rows};
/// # fn create_tables(conn: &Connection) -> Result<Option<Rows>> {
/// conn.query("SELECT * FROM users WHERE name = ?1;", vec![libsql::Value::from(1)])
/// # }
/// ```
pub fn query<S, P>(&self, sql: S, params: P) -> Result<Option<Rows>>
where
S: Into<String>,
P: TryInto<Params>,
P::Error: Into<crate::BoxError>,
{
let stmt = Statement::prepare(self.clone(), self.raw, sql.into().as_str())?;
let params = params
.try_into()
.map_err(|e| Error::ToSqlConversionFailure(e.into()))?;
let ret = stmt.query(¶ms)?;
Ok(Some(ret))
}
/// Convenience method to run multiple SQL statements (that cannot take any
/// parameters).
///
/// ## Example
///
/// ```rust,no_run,ignore
/// # use libsql::Result;
/// # use libsql::v1::Connection;
/// # fn create_tables(conn: &Connection) -> Result<()> {
/// conn.execute_batch(
/// "BEGIN;
/// CREATE TABLE foo(x INTEGER);
/// CREATE TABLE bar(y TEXT);
/// COMMIT;",
/// )
/// # }
/// ```
///
/// # Failure
///
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
/// or if the underlying SQLite call fails.
pub fn execute_batch<S>(&self, sql: S) -> Result<BatchRows>
where
S: Into<String>,
{
let sql = sql.into();
let mut sql = sql.as_str();
let mut batch_rows = Vec::new();
while !sql.is_empty() {
let stmt = self.prepare(sql)?;
let tail = if !stmt.inner.raw_stmt.is_null() {
let returned_rows = stmt.step()?;
let tail = stmt.tail();
// Check if there are rows to be extracted, we must do this upfront due to the lazy
// nature of sqlite and our somewhat hacked batch command.
if returned_rows {
// Extract columns
let cols = stmt
.columns()
.iter()
.enumerate()
.map(|(i, c)| {
use crate::value::ValueType;
let val = stmt.inner.column_type(i as i32);
let t = match val {
libsql_sys::ffi::SQLITE_INTEGER => ValueType::Integer,
libsql_sys::ffi::SQLITE_FLOAT => ValueType::Real,
libsql_sys::ffi::SQLITE_BLOB => ValueType::Blob,
libsql_sys::ffi::SQLITE_TEXT => ValueType::Text,
libsql_sys::ffi::SQLITE_NULL => ValueType::Null,
_ => unreachable!("unknown column type {} at index {}", val, i),
};
(c.name.to_string(), t)
})
.collect::<Vec<_>>();
let mut rows = Vec::new();
// If returned rows we must extract the rows available right away instead of
// using the `Rows` type we have already. This is due to the step api once its
// returned SQLITE_ROWS we must extract them before we call step again.
{
let row = crate::local::Row { stmt: stmt.clone() };
let mut values = Vec::with_capacity(cols.len());
for i in 0..cols.len() {
let value = row.get_value(i as i32)?;
values.push(value);
}
rows.push(values);
}
// Now we can use the normal rows type to extract any n+1 rows
let rows_sys = Rows::new(stmt);
while let Some(row) = rows_sys.next()? {
let mut values = Vec::with_capacity(cols.len());
for i in 0..cols.len() {
let value = row.get_value(i as i32)?;
values.push(value);
}
rows.push(values);
}
rows.len();
batch_rows.push(Some(crate::Rows::new(BatchedRows::new(cols, rows))));
} else {
batch_rows.push(None);
}
tail
} else {
stmt.tail()
};
if tail == 0 || tail >= sql.len() {
break;
}
sql = &sql[tail..];
}
Ok(BatchRows::new(batch_rows))
}
fn execute_transactional_batch_inner<S>(&self, sql: S) -> Result<()>
where
S: Into<String>,
{
let sql = sql.into();
let mut sql = sql.as_str();
while !sql.is_empty() {
let stmt = self.prepare(sql)?;
let tail = stmt.tail();
let stmt_sql = if tail == 0 || tail >= sql.len() {
sql
} else {
&sql[..tail]
};
let prefix_count = stmt_sql.chars().take_while(|c| c.is_whitespace()).count();
let stmt_sql = &stmt_sql[prefix_count..];
if stmt_sql.starts_with("BEGIN")
|| stmt_sql.starts_with("COMMIT")
|| stmt_sql.starts_with("ROLLBACK")
|| stmt_sql.starts_with("END")
{
return Err(Error::TransactionalBatchError(
"Transactions forbidden inside transactional batch".to_string(),
));
}
if !stmt.inner.raw_stmt.is_null() {
stmt.step()?;
}
if tail == 0 || tail >= sql.len() {
break;
}
sql = &sql[tail..];
}
Ok(())
}
pub fn execute_transactional_batch<S>(&self, sql: S) -> Result<()>
where
S: Into<String>,
{
self.execute("BEGIN TRANSACTION", Params::None)?;
match self.execute_transactional_batch_inner(sql) {
Ok(_) => {
self.execute("COMMIT", Params::None)?;
Ok(())
}
Err(e) => {
self.execute("ROLLBACK", Params::None)?;
Err(e)
}
}
}
/// Execute the SQL statement synchronously.
///
/// If you execute a SQL query statement (e.g. `SELECT` statement) that
/// returns the number of rows changed.
///
/// This method blocks the thread until the SQL statement is executed.
pub fn execute<S, P>(&self, sql: S, params: P) -> Result<u64>
where
S: Into<String>,
P: TryInto<Params>,
P::Error: Into<crate::BoxError>,
{
let stmt = Statement::prepare(self.clone(), self.raw, sql.into().as_str())?;
let params = params
.try_into()
.map_err(|e| Error::ToSqlConversionFailure(e.into()))?;
stmt.execute(¶ms)
}
/// Execute the SQL statement synchronously.
///
/// This method never blocks the thread until, but instead returns a
/// `RowsFuture` object immediately that can be used to deferredly
/// execute the statement.
pub fn execute_async<S, P>(&self, sql: S, params: P) -> RowsFuture
where
S: Into<String>,
P: Into<Params>,
{
RowsFuture {
conn: self.clone(),
sql: sql.into(),
params: params.into(),
}
}
/// Begin a new transaction in DEFERRED mode, which is the default.
pub fn transaction(&self) -> Result<Transaction> {
self.transaction_with_behavior(TransactionBehavior::Deferred)
}
/// Begin a new transaction in the given mode.
pub fn transaction_with_behavior(
&self,
tx_behavior: TransactionBehavior,
) -> Result<Transaction> {
Transaction::begin(self.clone(), tx_behavior)
}
pub fn is_autocommit(&self) -> bool {
unsafe { ffi::sqlite3_get_autocommit(self.raw) != 0 }
}
pub fn changes(&self) -> u64 {
unsafe { ffi::sqlite3_changes64(self.raw) as u64 }
}
pub fn total_changes(&self) -> u64 {
unsafe { ffi::sqlite3_total_changes(self.raw) as u64 }
}
pub fn last_insert_rowid(&self) -> i64 {
unsafe { ffi::sqlite3_last_insert_rowid(self.raw) }
}
#[cfg(feature = "replication")]
pub(crate) fn writer(&self) -> Option<&crate::replication::Writer> {
self.writer.as_ref()
}
#[cfg(feature = "replication")]
pub(crate) fn new_connection_writer(&self) -> Option<crate::replication::Writer> {
self.writer.as_ref().cloned().map(|mut w| {
w.new_client_id();
w
})
}
pub fn enable_load_extension(&self, onoff: bool) -> Result<()> {
// SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION configration verb accepts 2 additional parameters: an on/off flag and a pointer to an c_int where new state of the parameter will be written (or NULL if reporting back the setting is not needed)
// See: https://sqlite.org/c3ref/c_dbconfig_defensive.html#sqlitedbconfigenableloadextension
let err = unsafe {
ffi::sqlite3_db_config(
self.raw,
ffi::SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,
onoff as i32,
std::ptr::null::<c_int>(),
)
};
match err {
ffi::SQLITE_OK => Ok(()),
_ => Err(errors::Error::SqliteFailure(
err,
errors::error_from_code(err),
)),
}
}
pub fn load_extension(&self, dylib_path: &Path, entry_point: Option<&str>) -> Result<()> {
let mut raw_err_msg: *mut std::ffi::c_char = std::ptr::null_mut();
let dylib_path = match dylib_path.to_str() {
Some(dylib_path) => std::ffi::CString::new(dylib_path).unwrap(),
None => {
return Err(crate::Error::Misuse(format!(
"dylib path is not a valid utf8 string"
)))
}
};
let err = match entry_point {
Some(entry_point) => {
let entry_point = std::ffi::CString::new(entry_point).unwrap();
unsafe {
ffi::sqlite3_load_extension(
self.raw,
dylib_path.as_ptr(),
entry_point.as_ptr(),
&mut raw_err_msg,
)
}
}
None => unsafe {
ffi::sqlite3_load_extension(
self.raw,
dylib_path.as_ptr(),
std::ptr::null(),
&mut raw_err_msg,
)
},
};
match err {
ffi::SQLITE_OK => Ok(()),
_ => {
let err_msg = unsafe { std::ffi::CStr::from_ptr(raw_err_msg) };
let err_msg = err_msg.to_string_lossy().to_string();
unsafe { ffi::sqlite3_free(raw_err_msg as *mut std::ffi::c_void) };
Err(errors::Error::SqliteFailure(err, err_msg))
}
}
}
}
impl fmt::Debug for Connection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Connection").finish()
}
}