Skip to content

refactor(sqlite): make background thread responsible for all FFI calls #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 79 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ paste = "1.0.1"
serde = { version = "1.0.111", features = ["derive"] }
serde_json = "1.0.53"
url = "2.1.1"

rand = "0.8.4"
rand_xoshiro = "0.6.0"
hex = "0.4"
#
# Any
#
Expand Down
5 changes: 4 additions & 1 deletion sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mysql = [
"rand",
"rsa",
]
sqlite = ["libsqlite3-sys"]
sqlite = ["libsqlite3-sys", "futures-executor", "flume"]
mssql = ["uuid", "encoding_rs", "regex"]
any = []

Expand Down Expand Up @@ -122,6 +122,9 @@ futures-channel = { version = "0.3.5", default-features = false, features = ["si
futures-core = { version = "0.3.5", default-features = false }
futures-intrusive = "0.4.0"
futures-util = { version = "0.3.5", default-features = false, features = ["alloc", "sink"] }
# used by the SQLite worker thread to block on the async mutex that locks the database handle
futures-executor = { version = "0.3.17", optional = true }
flume = { version = "0.10.9", optional = true, default-features = false, features = ["async"] }
generic-array = { version = "0.14.4", default-features = false, optional = true }
hex = "0.4.2"
hmac = { version = "0.11.0", default-features = false, optional = true }
Expand Down
25 changes: 25 additions & 0 deletions sqlx-core/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
mod statement_cache;

pub(crate) use statement_cache::StatementCache;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};

/// A wrapper for `Fn`s that provides a debug impl that just says "Function"
pub(crate) struct DebugFn<F: ?Sized>(pub F);

impl<F: ?Sized> Deref for DebugFn<F> {
type Target = F;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<F: ?Sized> DerefMut for DebugFn<F> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<F: ?Sized> Debug for DebugFn<F> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Function").finish()
}
}
2 changes: 1 addition & 1 deletion sqlx-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum Error {
Database(#[source] Box<dyn DatabaseError>),

/// Error communicating with the database backend.
#[error("error communicating with the server: {0}")]
#[error("error communicating with database: {0}")]
Io(#[from] io::Error),

/// Error occurred while attempting to establish a TLS connection.
Expand Down
27 changes: 25 additions & 2 deletions sqlx-core/src/sqlite/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ impl<'q> SqliteArguments<'q> {
self.values.push(SqliteArgumentValue::Null);
}
}

pub(crate) fn into_static(self) -> SqliteArguments<'static> {
SqliteArguments {
values: self
.values
.into_iter()
.map(SqliteArgumentValue::into_static)
.collect(),
}
}
}

impl<'q> Arguments<'q> for SqliteArguments<'q> {
Expand All @@ -49,7 +59,7 @@ impl<'q> Arguments<'q> for SqliteArguments<'q> {
}

impl SqliteArguments<'_> {
pub(super) fn bind(&self, handle: &StatementHandle, offset: usize) -> Result<usize, Error> {
pub(super) fn bind(&self, handle: &mut StatementHandle, offset: usize) -> Result<usize, Error> {
let mut arg_i = offset;
// for handle in &statement.handles {

Expand Down Expand Up @@ -95,7 +105,20 @@ impl SqliteArguments<'_> {
}

impl SqliteArgumentValue<'_> {
fn bind(&self, handle: &StatementHandle, i: usize) -> Result<(), Error> {
fn into_static(self) -> SqliteArgumentValue<'static> {
use SqliteArgumentValue::*;

match self {
Null => Null,
Text(text) => Text(text.into_owned().into()),
Blob(blob) => Blob(blob.into_owned().into()),
Int(v) => Int(v),
Int64(v) => Int64(v),
Double(v) => Double(v),
}
}

fn bind(&self, handle: &mut StatementHandle, i: usize) -> Result<(), Error> {
use SqliteArgumentValue::*;

let status = match self {
Expand Down
Loading