Skip to content
Open
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
38 changes: 16 additions & 22 deletions derive/src/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ use std::collections::HashSet;
use syn::{
braced,
parse::{Parse, ParseStream},
Error, File, FnArg, Ident, Item, LitBool, LitChar, Pat, ReturnType, Signature, Token,
Error, Expr, File, FnArg, Ident, Item, LitBool, LitChar, Pat, ReturnType, Signature, Token,
TraitItem, Type,
};

/// Override value types supported by the macro
pub(crate) enum Override {
Bool(LitBool),
Char(LitChar),
None,
Expr(Expr),
}

/// Parsed input for the `derive_dialect!` macro
Expand Down Expand Up @@ -80,20 +80,8 @@ impl Parse for DeriveDialectInput {
Override::Bool(content.parse()?)
} else if content.peek(LitChar) {
Override::Char(content.parse()?)
} else if content.peek(Ident) {
let ident: Ident = content.parse()?;
if ident == "None" {
Override::None
} else {
return Err(Error::new(
ident.span(),
format!("Expected `true`, `false`, a char, or `None`, found `{ident}`"),
));
}
} else {
return Err(
content.error("Expected `true`, `false`, a char, or `None`")
);
Override::Expr(content.parse()?)
};
overrides.push((key, value));
if content.peek(Token![,]) {
Expand Down Expand Up @@ -136,24 +124,31 @@ fn derive_dialect_inner(input: DeriveDialectInput) -> syn::Result<TokenStream> {
let methods = extract_dialect_methods(&file)?;

// Validate overrides
let method_names: HashSet<_> = methods.iter().map(|m| m.name.to_string()).collect();
let bool_names: HashSet<_> = methods
.iter()
.filter(|m| is_bool_method(&m.signature))
.map(|m| m.name.to_string())
.collect();
for (key, value) in &input.overrides {
let key_str = key.to_string();
if !method_names.contains(&key_str) {
return Err(Error::new(
key.span(),
format!("Unknown method `{key_str}`"),
));
}
match value {
Override::Bool(_) if !bool_names.contains(&key_str) => {
return Err(Error::new(
key.span(),
format!("Unknown boolean method `{key_str}`"),
));
}
Override::Char(_) | Override::None if key_str != "identifier_quote_style" => {
Override::Char(_) if key_str != "identifier_quote_style" => {
return Err(Error::new(
key.span(),
format!("Char/None only valid for `identifier_quote_style`, not `{key_str}`"),
format!("Char only valid for `identifier_quote_style`, not `{key_str}`"),
));
}
_ => {}
Expand Down Expand Up @@ -214,10 +209,9 @@ fn generate_derived_dialect(input: &DeriveDialectInput, methods: &[DialectMethod
fn identifier_quote_style(&self, _: &str) -> Option<char> { Some(#c) }
}
}
Some(Override::None) => {
quote_spanned! { method_name.span() =>
fn identifier_quote_style(&self, _: &str) -> Option<char> { None }
}
Some(Override::Expr(expr)) => {
let sig = &method.signature;
quote_spanned! { method_name.span() => #sig { #expr } }
}
None => delegate(method),
}
Expand All @@ -230,7 +224,7 @@ fn generate_derived_dialect(input: &DeriveDialectInput, methods: &[DialectMethod
use ::core::iter::Peekable;
use ::core::str::Chars;
use sqlparser::ast::{ColumnOption, Expr, GranteesType, Ident, ObjectNamePart, Statement};
use sqlparser::dialect::{Dialect, Precedence};
use sqlparser::dialect::{BindPlaceholderStyle, Dialect, Precedence};
use sqlparser::keywords::Keyword;
use sqlparser::parser::{Parser, ParserError};

Expand Down
57 changes: 55 additions & 2 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ macro_rules! dialect_is {
}
}

/// Placeholder spelling for ordered unnamed bind parameters.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum BindPlaceholderStyle {
/// `?`
QuestionMark,
/// `$1`, `$2`, ...
DollarNumbered,
}

/// Encapsulates the differences between SQL implementations.
///
/// # SQL Dialects
Expand Down Expand Up @@ -254,6 +265,15 @@ pub trait Dialect: Debug + Any {
None
}

/// Return the placeholder syntax this server accepts for ordered unnamed bind parameters.
///
/// This describes the server contract rather than every placeholder the parser can tokenize.
/// `None` means this dialect does not specify an ordered bind syntax.
/// See [`Dialect::supports_dollar_placeholder`] for SQLite-style named `$name` placeholders.
fn ordered_bind_placeholder_style(&self) -> Option<BindPlaceholderStyle> {
None
}

/// Determine if a character is a valid start character for an unquoted identifier
fn is_identifier_start(&self, ch: char) -> bool;

Expand Down Expand Up @@ -1107,8 +1127,11 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if this dialect allows dollar placeholders
/// e.g. `SELECT $var` (SQLite)
/// Returns true if this dialect allows SQLite-style named dollar placeholders,
/// for example `SELECT $name`.
///
/// This does not describe PostgreSQL-style ordered binds such as `SELECT $1`.
/// See [`Dialect::ordered_bind_placeholder_style`].
fn supports_dollar_placeholder(&self) -> bool {
false
}
Expand Down Expand Up @@ -1996,13 +2019,18 @@ mod tests {
supports_order_by_all = true,
supports_nested_comments = true,
supports_triple_quoted_string = true,
ordered_bind_placeholder_style = Some(BindPlaceholderStyle::DollarNumbered),
},
);
let dialect = EnhancedGenericDialect::new();

assert!(dialect.supports_order_by_all());
assert!(dialect.supports_nested_comments());
assert!(dialect.supports_triple_quoted_string());
assert_eq!(
dialect.ordered_bind_placeholder_style(),
Some(BindPlaceholderStyle::DollarNumbered)
);

let d: &dyn Dialect = &dialect;
assert!(d.is::<GenericDialect>());
Expand Down Expand Up @@ -2036,6 +2064,23 @@ mod tests {
}
}

#[test]
fn ordered_bind_placeholder_style() {
let tests: Vec<(&dyn Dialect, Option<BindPlaceholderStyle>)> = vec![
(&GenericDialect {}, None),
(&MySqlDialect {}, Some(BindPlaceholderStyle::QuestionMark)),
(
&PostgreSqlDialect {},
Some(BindPlaceholderStyle::DollarNumbered),
),
(&SQLiteDialect {}, Some(BindPlaceholderStyle::QuestionMark)),
];

for (dialect, expected) in tests {
assert_eq!(dialect.ordered_bind_placeholder_style(), expected);
}
}

#[test]
fn parse_with_wrapped_dialect() {
/// Wrapper for a dialect. In a real-world example, this wrapper
Expand Down Expand Up @@ -2072,6 +2117,10 @@ mod tests {
self.0.identifier_quote_style(identifier)
}

fn ordered_bind_placeholder_style(&self) -> Option<BindPlaceholderStyle> {
self.0.ordered_bind_placeholder_style()
}

fn supports_string_literal_backslash_escape(&self) -> bool {
self.0.supports_string_literal_backslash_escape()
}
Expand Down Expand Up @@ -2139,6 +2188,10 @@ mod tests {
let statement = r#"SELECT 'Wayne\'s World'"#;
let res1 = Parser::parse_sql(&MySqlDialect {}, statement);
let res2 = Parser::parse_sql(&WrappedDialect(MySqlDialect {}), statement);
assert_eq!(
WrappedDialect(MySqlDialect {}).ordered_bind_placeholder_style(),
Some(BindPlaceholderStyle::QuestionMark)
);
assert!(res1.is_ok());
assert_eq!(res1, res2);
}
Expand Down
6 changes: 5 additions & 1 deletion src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use alloc::boxed::Box;

use crate::{
ast::{BinaryOperator, Expr, LockTable, LockTableType, Statement},
dialect::Dialect,
dialect::{BindPlaceholderStyle, Dialect},
keywords::Keyword,
parser::{Parser, ParserError},
};
Expand Down Expand Up @@ -67,6 +67,10 @@ impl Dialect for MySqlDialect {
Some('`')
}

fn ordered_bind_placeholder_style(&self) -> Option<BindPlaceholderStyle> {
Some(BindPlaceholderStyle::QuestionMark)
}

// See https://dev.mysql.com/doc/refman/8.0/en/string-literals.html#character-escape-sequences
fn supports_string_literal_backslash_escape(&self) -> bool {
true
Expand Down
6 changes: 5 additions & 1 deletion src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// limitations under the License.
use log::debug;

use crate::dialect::{Dialect, Precedence};
use crate::dialect::{BindPlaceholderStyle, Dialect, Precedence};
use crate::keywords::Keyword;
use crate::parser::{Parser, ParserError};
use crate::tokenizer::Token;
Expand Down Expand Up @@ -63,6 +63,10 @@ impl Dialect for PostgreSqlDialect {
Some('"')
}

fn ordered_bind_placeholder_style(&self) -> Option<BindPlaceholderStyle> {
Some(BindPlaceholderStyle::DollarNumbered)
}

fn is_delimited_identifier_start(&self, ch: char) -> bool {
ch == '"' // Postgres does not support backticks to quote identifiers
}
Expand Down
6 changes: 5 additions & 1 deletion src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use alloc::boxed::Box;

use crate::ast::BinaryOperator;
use crate::ast::{Expr, Statement};
use crate::dialect::Dialect;
use crate::dialect::{BindPlaceholderStyle, Dialect};
use crate::keywords::Keyword;
use crate::parser::{Parser, ParserError};

Expand All @@ -46,6 +46,10 @@ impl Dialect for SQLiteDialect {
Some('`')
}

fn ordered_bind_placeholder_style(&self) -> Option<BindPlaceholderStyle> {
Some(BindPlaceholderStyle::QuestionMark)
}

fn is_identifier_start(&self, ch: char) -> bool {
// See https://www.sqlite.org/draft/tokenreq.html
ch.is_ascii_lowercase()
Expand Down
Loading