Skip to content

Commit d5ccbf2

Browse files
committed
Update Sqlite driver
1 parent 18473ab commit d5ccbf2

File tree

14 files changed

+71
-65
lines changed

14 files changed

+71
-65
lines changed

ci.db

0 Bytes
Binary file not shown.

sqlx-sqlite/src/any.rs

Lines changed: 6 additions & 5 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 10 additions & 11 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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;
@@ -225,7 +226,7 @@ impl Connection for SqliteConnection {
225226
write!(pragma_string, "PRAGMA analysis_limit = {limit}; ").ok();
226227
}
227228
pragma_string.push_str("PRAGMA optimize;");
228-
self.execute(&*pragma_string).await?;
229+
self.execute(AssertSqlSafe(pragma_string)).await?;
229230
}
230231
let shutdown = self.worker.shutdown();
231232
// Drop the statement worker, which should

sqlx-sqlite/src/connection/worker.rs

Lines changed: 22 additions & 28 deletions
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;
@@ -40,15 +40,15 @@ pub(crate) struct WorkerSharedState {
4040

4141
enum Command {
4242
Prepare {
43-
query: Box<str>,
43+
query: SqlStr,
4444
tx: oneshot::Sender<Result<SqliteStatement, Error>>,
4545
},
4646
Describe {
47-
query: Box<str>,
47+
query: SqlStr,
4848
tx: oneshot::Sender<Result<Describe<Sqlite>, Error>>,
4949
},
5050
Execute {
51-
query: Box<str>,
51+
query: SqlStr,
5252
arguments: Option<SqliteArguments<'static>>,
5353
persistent: bool,
5454
tx: flume::Sender<Result<Either<SqliteQueryResult, SqliteRow>, Error>>,
@@ -120,7 +120,7 @@ impl ConnectionWorker {
120120
let _guard = span.enter();
121121
match cmd {
122122
Command::Prepare { query, tx } => {
123-
tx.send(prepare(&mut conn, &query).map(|prepared| {
123+
tx.send(prepare(&mut conn, query).map(|prepared| {
124124
update_cached_statements_size(
125125
&conn,
126126
&shared.cached_statements_size,
@@ -130,7 +130,7 @@ impl ConnectionWorker {
130130
.ok();
131131
}
132132
Command::Describe { query, tx } => {
133-
tx.send(describe(&mut conn, &query)).ok();
133+
tx.send(describe(&mut conn, query)).ok();
134134
}
135135
Command::Execute {
136136
query,
@@ -139,7 +139,7 @@ impl ConnectionWorker {
139139
tx,
140140
limit
141141
} => {
142-
let iter = match execute::iter(&mut conn, &query, arguments, persistent)
142+
let iter = match execute::iter(&mut conn, query, arguments, persistent)
143143
{
144144
Ok(iter) => iter,
145145
Err(e) => {
@@ -186,7 +186,7 @@ impl ConnectionWorker {
186186
let depth = conn.transaction_depth;
187187
let res =
188188
conn.handle
189-
.exec(begin_ansi_transaction_sql(depth))
189+
.exec(begin_ansi_transaction_sql(depth).as_str())
190190
.map(|_| {
191191
conn.transaction_depth += 1;
192192
});
@@ -199,7 +199,7 @@ impl ConnectionWorker {
199199
// immediately otherwise it would remain started forever.
200200
if let Err(error) = conn
201201
.handle
202-
.exec(rollback_ansi_transaction_sql(depth + 1))
202+
.exec(rollback_ansi_transaction_sql(depth + 1).as_str())
203203
.map(|_| {
204204
conn.transaction_depth -= 1;
205205
})
@@ -217,7 +217,7 @@ impl ConnectionWorker {
217217

218218
let res = if depth > 0 {
219219
conn.handle
220-
.exec(commit_ansi_transaction_sql(depth))
220+
.exec(commit_ansi_transaction_sql(depth).as_str())
221221
.map(|_| {
222222
conn.transaction_depth -= 1;
223223
})
@@ -243,7 +243,7 @@ impl ConnectionWorker {
243243

244244
let res = if depth > 0 {
245245
conn.handle
246-
.exec(rollback_ansi_transaction_sql(depth))
246+
.exec(rollback_ansi_transaction_sql(depth).as_str())
247247
.map(|_| {
248248
conn.transaction_depth -= 1;
249249
})
@@ -290,25 +290,19 @@ impl ConnectionWorker {
290290
establish_rx.await.map_err(|_| Error::WorkerCrashed)?
291291
}
292292

293-
pub(crate) async fn prepare(&mut self, query: &str) -> Result<SqliteStatement, Error> {
294-
self.oneshot_cmd(|tx| Command::Prepare {
295-
query: query.into(),
296-
tx,
297-
})
298-
.await?
293+
pub(crate) async fn prepare(&mut self, query: SqlStr) -> Result<SqliteStatement, Error> {
294+
self.oneshot_cmd(|tx| Command::Prepare { query, tx })
295+
.await?
299296
}
300297

301-
pub(crate) async fn describe(&mut self, query: &str) -> Result<Describe<Sqlite>, Error> {
302-
self.oneshot_cmd(|tx| Command::Describe {
303-
query: query.into(),
304-
tx,
305-
})
306-
.await?
298+
pub(crate) async fn describe(&mut self, query: SqlStr) -> Result<Describe<Sqlite>, Error> {
299+
self.oneshot_cmd(|tx| Command::Describe { query, tx })
300+
.await?
307301
}
308302

309303
pub(crate) async fn execute(
310304
&mut self,
311-
query: &str,
305+
query: SqlStr,
312306
args: Option<SqliteArguments<'_>>,
313307
chan_size: usize,
314308
persistent: bool,
@@ -319,7 +313,7 @@ impl ConnectionWorker {
319313
self.command_tx
320314
.send_async((
321315
Command::Execute {
322-
query: query.into(),
316+
query,
323317
arguments: args.map(SqliteArguments::into_static),
324318
persistent,
325319
tx,
@@ -424,9 +418,9 @@ impl ConnectionWorker {
424418
}
425419
}
426420

427-
fn prepare(conn: &mut ConnectionState, query: &str) -> Result<SqliteStatement, Error> {
421+
fn prepare(conn: &mut ConnectionState, query: SqlStr) -> Result<SqliteStatement, Error> {
428422
// prepare statement object (or checkout from cache)
429-
let statement = conn.statements.get(query, true)?;
423+
let statement = conn.statements.get(query.as_str(), true)?;
430424

431425
let mut parameters = 0;
432426
let mut columns = None;
@@ -443,7 +437,7 @@ fn prepare(conn: &mut ConnectionState, query: &str) -> Result<SqliteStatement, E
443437
}
444438

445439
Ok(SqliteStatement {
446-
sql: AssertSqlSafe(query).into_sql_str(),
440+
sql: query,
447441
columns: columns.unwrap_or_default(),
448442
column_names: column_names.unwrap_or_default(),
449443
parameters,

sqlx-sqlite/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub use options::{
5656
};
5757
pub use query_result::SqliteQueryResult;
5858
pub use row::SqliteRow;
59+
use sqlx_core::sql_str::{AssertSqlSafe, SqlSafeStr};
5960
pub use statement::SqliteStatement;
6061
pub use transaction::SqliteTransactionManager;
6162
pub use type_info::SqliteTypeInfo;
@@ -131,9 +132,10 @@ pub fn describe_blocking(query: &str, database_url: &str) -> Result<Describe<Sql
131132
let mut conn = params.establish()?;
132133

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

136-
connection::describe::describe(&mut conn, query)
138+
connection::describe::describe(&mut conn, AssertSqlSafe(query.to_string()).into_sql_str())
137139

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

0 commit comments

Comments
 (0)