Skip to content

Commit 01da714

Browse files
committed
Update Sqlite driver
1 parent 27b0f83 commit 01da714

File tree

13 files changed

+71
-65
lines changed

13 files changed

+71
-65
lines changed

sqlx-sqlite/src/any.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use sqlx_core::any::{
1010
Any, AnyArguments, AnyColumn, AnyConnectOptions, AnyConnectionBackend, AnyQueryResult, AnyRow,
1111
AnyStatement, AnyTypeInfo, AnyTypeInfoKind, AnyValueKind,
1212
};
13+
use sqlx_core::sql_str::SqlStr;
1314

1415
use crate::type_info::DataType;
1516
use sqlx_core::connection::{ConnectOptions, Connection};
@@ -75,7 +76,7 @@ impl AnyConnectionBackend for SqliteConnection {
7576

7677
fn fetch_many<'q>(
7778
&'q mut self,
78-
query: &'q str,
79+
query: SqlStr,
7980
persistent: bool,
8081
arguments: Option<AnyArguments<'q>>,
8182
) -> BoxStream<'q, sqlx_core::Result<Either<AnyQueryResult, AnyRow>>> {
@@ -98,7 +99,7 @@ impl AnyConnectionBackend for SqliteConnection {
9899

99100
fn fetch_optional<'q>(
100101
&'q mut self,
101-
query: &'q str,
102+
query: SqlStr,
102103
persistent: bool,
103104
arguments: Option<AnyArguments<'q>>,
104105
) -> BoxFuture<'q, sqlx_core::Result<Option<AnyRow>>> {
@@ -123,16 +124,16 @@ impl AnyConnectionBackend for SqliteConnection {
123124

124125
fn prepare_with<'c, 'q: 'c>(
125126
&'c mut self,
126-
sql: &'q str,
127+
sql: SqlStr,
127128
_parameters: &[AnyTypeInfo],
128129
) -> BoxFuture<'c, sqlx_core::Result<AnyStatement>> {
129130
Box::pin(async move {
130-
let statement = Executor::prepare_with(self, sql, &[]).await?;
131+
let statement = Executor::prepare_with(self, sql.clone(), &[]).await?;
131132
AnyStatement::try_from_statement(sql, &statement, statement.column_names.clone())
132133
})
133134
}
134135

135-
fn describe<'q>(&'q mut self, sql: &'q str) -> BoxFuture<'q, sqlx_core::Result<Describe<Any>>> {
136+
fn describe<'q>(&'q mut self, sql: SqlStr) -> BoxFuture<'q, sqlx_core::Result<Describe<Any>>> {
136137
Box::pin(async move { Executor::describe(self, sql).await?.try_into_any() })
137138
}
138139
}

sqlx-sqlite/src/connection/describe.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ use crate::error::Error;
55
use crate::statement::VirtualStatement;
66
use crate::type_info::DataType;
77
use crate::{Sqlite, SqliteColumn};
8+
use sqlx_core::sql_str::SqlStr;
89
use sqlx_core::Either;
910
use std::convert::identity;
1011

11-
pub(crate) fn describe(conn: &mut ConnectionState, query: &str) -> Result<Describe<Sqlite>, Error> {
12+
pub(crate) fn describe(
13+
conn: &mut ConnectionState,
14+
query: SqlStr,
15+
) -> Result<Describe<Sqlite>, Error> {
1216
// describing a statement from SQLite can be involved
1317
// each SQLx statement is comprised of multiple SQL statements
1418

15-
let mut statement = VirtualStatement::new(query, false)?;
19+
let mut statement = VirtualStatement::new(query.as_str(), false)?;
1620

1721
let mut columns = Vec::new();
1822
let mut nullable = Vec::new();

sqlx-sqlite/src/connection/execute.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use crate::error::Error;
33
use crate::logger::QueryLogger;
44
use crate::statement::{StatementHandle, VirtualStatement};
55
use crate::{SqliteArguments, SqliteQueryResult, SqliteRow};
6+
use sqlx_core::sql_str::SqlSafeStr;
67
use sqlx_core::Either;
78

89
pub struct ExecuteIter<'a> {
910
handle: &'a mut ConnectionHandle,
1011
statement: &'a mut VirtualStatement,
11-
logger: QueryLogger<'a>,
12+
logger: QueryLogger,
1213
args: Option<SqliteArguments<'a>>,
1314

1415
/// since a `VirtualStatement` can encompass multiple actual statements,
@@ -20,12 +21,13 @@ pub struct ExecuteIter<'a> {
2021

2122
pub(crate) fn iter<'a>(
2223
conn: &'a mut ConnectionState,
23-
query: &'a str,
24+
query: impl SqlSafeStr,
2425
args: Option<SqliteArguments<'a>>,
2526
persistent: bool,
2627
) -> Result<ExecuteIter<'a>, Error> {
28+
let query = query.into_sql_str();
2729
// fetch the cached statement or allocate a new one
28-
let statement = conn.statements.get(query, persistent)?;
30+
let statement = conn.statements.get(query.as_str(), persistent)?;
2931

3032
let logger = QueryLogger::new(query, conn.log_settings.clone());
3133

sqlx-sqlite/src/connection/executor.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use futures_util::{stream, FutureExt, StreamExt, TryFutureExt, TryStreamExt};
77
use sqlx_core::describe::Describe;
88
use sqlx_core::error::Error;
99
use sqlx_core::executor::{Execute, Executor};
10-
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr};
10+
use sqlx_core::sql_str::SqlSafeStr;
1111
use sqlx_core::Either;
1212
use std::{future, pin::pin};
1313

@@ -24,12 +24,12 @@ impl<'c> Executor<'c> for &'c mut SqliteConnection {
2424
'q: 'e,
2525
E: 'q,
2626
{
27-
let sql = query.sql();
2827
let arguments = match query.take_arguments().map_err(Error::Encode) {
2928
Ok(arguments) => arguments,
3029
Err(error) => return stream::once(future::ready(Err(error))).boxed(),
3130
};
3231
let persistent = query.persistent() && arguments.is_some();
32+
let sql = query.sql();
3333

3434
Box::pin(
3535
self.worker
@@ -49,14 +49,14 @@ impl<'c> Executor<'c> for &'c mut SqliteConnection {
4949
'q: 'e,
5050
E: 'q,
5151
{
52-
let sql = query.sql();
5352
let arguments = match query.take_arguments().map_err(Error::Encode) {
5453
Ok(arguments) => arguments,
5554
Err(error) => return future::ready(Err(error)).boxed(),
5655
};
5756
let persistent = query.persistent() && arguments.is_some();
5857

5958
Box::pin(async move {
59+
let sql = query.sql();
6060
let mut stream = pin!(self
6161
.worker
6262
.execute(sql, arguments, self.row_channel_size, persistent, Some(1))
@@ -73,29 +73,28 @@ impl<'c> Executor<'c> for &'c mut SqliteConnection {
7373
})
7474
}
7575

76-
fn prepare_with<'e, 'q: 'e>(
76+
fn prepare_with<'e>(
7777
self,
78-
sql: &'q str,
78+
sql: impl SqlSafeStr,
7979
_parameters: &[SqliteTypeInfo],
8080
) -> BoxFuture<'e, Result<SqliteStatement, Error>>
8181
where
8282
'c: 'e,
8383
{
84+
let sql = sql.into_sql_str();
8485
Box::pin(async move {
8586
let statement = self.worker.prepare(sql).await?;
8687

87-
Ok(SqliteStatement {
88-
sql: AssertSqlSafe(sql).into_sql_str(),
89-
..statement
90-
})
88+
Ok(statement)
9189
})
9290
}
9391

9492
#[doc(hidden)]
95-
fn describe<'e, 'q: 'e>(self, sql: &'q str) -> BoxFuture<'e, Result<Describe<Sqlite>, Error>>
93+
fn describe<'e>(self, sql: impl SqlSafeStr) -> BoxFuture<'e, Result<Describe<Sqlite>, Error>>
9694
where
9795
'c: 'e,
9896
{
99-
Box::pin(self.worker.describe(sql))
97+
let sql = sql.into_sql_str();
98+
Box::pin(async move { self.worker.describe(sql).await })
10099
}
101100
}

sqlx-sqlite/src/connection/explain.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::from_row::FromRow;
1212
use crate::logger::{BranchParent, BranchResult, DebugDiff};
1313
use crate::type_info::DataType;
1414
use crate::SqliteTypeInfo;
15+
use sqlx_core::sql_str::AssertSqlSafe;
1516
use sqlx_core::{hash_map, HashMap};
1617
use std::fmt::Debug;
1718
use std::str::from_utf8;
@@ -567,7 +568,7 @@ pub(super) fn explain(
567568
) -> Result<(Vec<SqliteTypeInfo>, Vec<Option<bool>>), Error> {
568569
let root_block_cols = root_block_columns(conn)?;
569570
let program: Vec<(i64, String, i64, i64, i64, Vec<u8>)> =
570-
execute::iter(conn, &format!("EXPLAIN {query}"), None, false)?
571+
execute::iter(conn, AssertSqlSafe(format!("EXPLAIN {query}")), None, false)?
571572
.filter_map(|res| res.map(|either| either.right()).transpose())
572573
.map(|row| FromRow::from_row(&row?))
573574
.collect::<Result<Vec<_>, Error>>()?;

sqlx-sqlite/src/connection/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use sqlx_core::common::StatementCache;
2222
pub(crate) use sqlx_core::connection::*;
2323
use sqlx_core::error::Error;
2424
use sqlx_core::executor::Executor;
25+
use sqlx_core::sql_str::AssertSqlSafe;
2526
use sqlx_core::transaction::Transaction;
2627

2728
use crate::connection::establish::EstablishParams;
@@ -226,7 +227,7 @@ impl Connection for SqliteConnection {
226227
write!(pragma_string, "PRAGMA analysis_limit = {limit}; ").ok();
227228
}
228229
pragma_string.push_str("PRAGMA optimize;");
229-
self.execute(&*pragma_string).await?;
230+
self.execute(AssertSqlSafe(pragma_string)).await?;
230231
}
231232
let shutdown = self.worker.shutdown();
232233
// Drop the statement worker, which should

sqlx-sqlite/src/connection/worker.rs

+22-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::thread;
55

66
use futures_channel::oneshot;
77
use futures_intrusive::sync::{Mutex, MutexGuard};
8-
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr};
8+
use sqlx_core::sql_str::SqlStr;
99
use tracing::span::Span;
1010

1111
use sqlx_core::describe::Describe;
@@ -42,15 +42,15 @@ pub(crate) struct WorkerSharedState {
4242

4343
enum Command {
4444
Prepare {
45-
query: Box<str>,
45+
query: SqlStr,
4646
tx: oneshot::Sender<Result<SqliteStatement, Error>>,
4747
},
4848
Describe {
49-
query: Box<str>,
49+
query: SqlStr,
5050
tx: oneshot::Sender<Result<Describe<Sqlite>, Error>>,
5151
},
5252
Execute {
53-
query: Box<str>,
53+
query: SqlStr,
5454
arguments: Option<SqliteArguments<'static>>,
5555
persistent: bool,
5656
tx: flume::Sender<Result<Either<SqliteQueryResult, SqliteRow>, Error>>,
@@ -132,7 +132,7 @@ impl ConnectionWorker {
132132
let _guard = span.enter();
133133
match cmd {
134134
Command::Prepare { query, tx } => {
135-
tx.send(prepare(&mut conn, &query).map(|prepared| {
135+
tx.send(prepare(&mut conn, query).map(|prepared| {
136136
update_cached_statements_size(
137137
&conn,
138138
&shared.cached_statements_size,
@@ -142,7 +142,7 @@ impl ConnectionWorker {
142142
.ok();
143143
}
144144
Command::Describe { query, tx } => {
145-
tx.send(describe(&mut conn, &query)).ok();
145+
tx.send(describe(&mut conn, query)).ok();
146146
}
147147
Command::Execute {
148148
query,
@@ -151,7 +151,7 @@ impl ConnectionWorker {
151151
tx,
152152
limit
153153
} => {
154-
let iter = match execute::iter(&mut conn, &query, arguments, persistent)
154+
let iter = match execute::iter(&mut conn, query, arguments, persistent)
155155
{
156156
Ok(iter) => iter,
157157
Err(e) => {
@@ -198,7 +198,7 @@ impl ConnectionWorker {
198198
let depth = conn.transaction_depth;
199199
let res =
200200
conn.handle
201-
.exec(begin_ansi_transaction_sql(depth))
201+
.exec(begin_ansi_transaction_sql(depth).as_str())
202202
.map(|_| {
203203
conn.transaction_depth += 1;
204204
});
@@ -211,7 +211,7 @@ impl ConnectionWorker {
211211
// immediately otherwise it would remain started forever.
212212
if let Err(error) = conn
213213
.handle
214-
.exec(rollback_ansi_transaction_sql(depth + 1))
214+
.exec(rollback_ansi_transaction_sql(depth + 1).as_str())
215215
.map(|_| {
216216
conn.transaction_depth -= 1;
217217
})
@@ -229,7 +229,7 @@ impl ConnectionWorker {
229229

230230
let res = if depth > 0 {
231231
conn.handle
232-
.exec(commit_ansi_transaction_sql(depth))
232+
.exec(commit_ansi_transaction_sql(depth).as_str())
233233
.map(|_| {
234234
conn.transaction_depth -= 1;
235235
})
@@ -255,7 +255,7 @@ impl ConnectionWorker {
255255

256256
let res = if depth > 0 {
257257
conn.handle
258-
.exec(rollback_ansi_transaction_sql(depth))
258+
.exec(rollback_ansi_transaction_sql(depth).as_str())
259259
.map(|_| {
260260
conn.transaction_depth -= 1;
261261
})
@@ -308,25 +308,19 @@ impl ConnectionWorker {
308308
establish_rx.await.map_err(|_| Error::WorkerCrashed)?
309309
}
310310

311-
pub(crate) async fn prepare(&mut self, query: &str) -> Result<SqliteStatement, Error> {
312-
self.oneshot_cmd(|tx| Command::Prepare {
313-
query: query.into(),
314-
tx,
315-
})
316-
.await?
311+
pub(crate) async fn prepare(&mut self, query: SqlStr) -> Result<SqliteStatement, Error> {
312+
self.oneshot_cmd(|tx| Command::Prepare { query, tx })
313+
.await?
317314
}
318315

319-
pub(crate) async fn describe(&mut self, query: &str) -> Result<Describe<Sqlite>, Error> {
320-
self.oneshot_cmd(|tx| Command::Describe {
321-
query: query.into(),
322-
tx,
323-
})
324-
.await?
316+
pub(crate) async fn describe(&mut self, query: SqlStr) -> Result<Describe<Sqlite>, Error> {
317+
self.oneshot_cmd(|tx| Command::Describe { query, tx })
318+
.await?
325319
}
326320

327321
pub(crate) async fn execute(
328322
&mut self,
329-
query: &str,
323+
query: SqlStr,
330324
args: Option<SqliteArguments<'_>>,
331325
chan_size: usize,
332326
persistent: bool,
@@ -337,7 +331,7 @@ impl ConnectionWorker {
337331
self.command_tx
338332
.send_async((
339333
Command::Execute {
340-
query: query.into(),
334+
query,
341335
arguments: args.map(SqliteArguments::into_static),
342336
persistent,
343337
tx,
@@ -465,9 +459,9 @@ impl ConnectionWorker {
465459
}
466460
}
467461

468-
fn prepare(conn: &mut ConnectionState, query: &str) -> Result<SqliteStatement, Error> {
462+
fn prepare(conn: &mut ConnectionState, query: SqlStr) -> Result<SqliteStatement, Error> {
469463
// prepare statement object (or checkout from cache)
470-
let statement = conn.statements.get(query, true)?;
464+
let statement = conn.statements.get(query.as_str(), true)?;
471465

472466
let mut parameters = 0;
473467
let mut columns = None;
@@ -484,7 +478,7 @@ fn prepare(conn: &mut ConnectionState, query: &str) -> Result<SqliteStatement, E
484478
}
485479

486480
Ok(SqliteStatement {
487-
sql: AssertSqlSafe(query).into_sql_str(),
481+
sql: query,
488482
columns: columns.unwrap_or_default(),
489483
column_names: column_names.unwrap_or_default(),
490484
parameters,

sqlx-sqlite/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub use options::{
5757
};
5858
pub use query_result::SqliteQueryResult;
5959
pub use row::SqliteRow;
60+
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr};
6061
pub use statement::SqliteStatement;
6162
pub use transaction::SqliteTransactionManager;
6263
pub use type_info::SqliteTypeInfo;
@@ -132,9 +133,10 @@ pub fn describe_blocking(query: &str, database_url: &str) -> Result<Describe<Sql
132133
let mut conn = params.establish()?;
133134

134135
// Execute any ancillary `PRAGMA`s
135-
connection::execute::iter(&mut conn, &opts.pragma_string(), None, false)?.finish()?;
136+
connection::execute::iter(&mut conn, AssertSqlSafe(opts.pragma_string()), None, false)?
137+
.finish()?;
136138

137-
connection::describe::describe(&mut conn, query)
139+
connection::describe::describe(&mut conn, AssertSqlSafe(query.to_string()).into_sql_str())
138140

139141
// SQLite database is closed immediately when `conn` is dropped
140142
}

0 commit comments

Comments
 (0)