Skip to content

Commit

Permalink
WIP [next]: implement generalized query placeholders
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Bonander <austin@launchbadge.com>
  • Loading branch information
abonander committed May 5, 2021
1 parent e7664d8 commit ef0daeb
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ memchr = "2.3"
conquer-once = { version = "0.3.2", optional = true }
parking_lot = { version = "0.11.1", optional = true }
crossbeam = { version = "0.8.0", optional = true }
combine = "4.5.2"

[dev-dependencies]
anyhow = "1"
9 changes: 9 additions & 0 deletions sqlx-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ pub trait Database:

/// The concrete [`TypeId`] implementation for this database.
type TypeId: 'static + PartialEq + Hash + Clone + Copy + Send + Sync;

/// The character used to prefix bind parameter placeholders, e.g. `$` for Postgres, `?` for MySQL, etc.
const PLACEHOLDER_CHAR: char;

/// The indexing type for bind parameters.
///
/// E.g. `Implicit` for MySQL which just does `SELECT 1 FROM foo WHERE bar = ? AND baz = ?`
/// or `OneIndexed` for Postgres which does `SELECT 1 FROM foo WHERE bar = $1 AND baz = $2`
const PARAM_INDEXING: crate::placeholders::ParamIndexing;
}

/// Associates [`Database`] with an `Output` of a generic lifetime.
Expand Down
21 changes: 21 additions & 0 deletions sqlx-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ pub enum Error {
/// An error occurred encoding a value for a specific parameter to
/// be sent to the database.
ParameterEncode { parameter: Either<usize, Box<str>>, source: EncodeError },
/// An error occurred while parsing the query for rewriting the generic placeholder syntax.
QueryParse {
/// The byte position in the query string where the error occurred.
byte_position: usize,
/// The line in the string where the error occurred.
line: i32,
/// The column in the string where the error occurred.
column: i32,
/// The message string, with error and context.
message: String,
/// The context string, which may help with locating the error.
context: String,
},
}

impl Error {
Expand Down Expand Up @@ -179,6 +192,14 @@ impl Display for Error {
Self::ParameterEncode { parameter: Either::Right(name), source } => {
write!(f, "Encode parameter `{}`: {}", name, source)
}

Self::QueryParse { line, column, message, context, .. } => {
write!(
f,
"Error parsing query at line {}, column {}: {} near {:?}",
line, column, message, context
)
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions sqlx-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub mod io;
#[doc(hidden)]
pub mod net;

#[doc(hidden)]
pub mod placeholders;

#[doc(hidden)]
#[cfg(feature = "_mock")]
pub mod mock;
Expand Down

0 comments on commit ef0daeb

Please sign in to comment.