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
40 changes: 17 additions & 23 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, ReceiverKind, ReturnType, Signature,
Token, TraitItem, Type,
Error, Expr, File, FnArg, Ident, Item, LitBool, LitChar, Pat, ReceiverKind, 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
74 changes: 72 additions & 2 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ pub use self::teradata::TeradataDialect;
/// assert_eq!(d.identifier_quote_style("foo"), None);
/// ```
///
/// # Overriding other methods
///
/// Any other method can be overridden with an expression of the method's return type:
/// ```
/// use sqlparser::derive_dialect;
/// use sqlparser::dialect::{BindPlaceholderStyle, Dialect, GenericDialect};
///
/// derive_dialect!(DollarBindDialect, GenericDialect,
/// overrides = { ordered_bind_placeholder_style = Some(BindPlaceholderStyle::DollarNumbered) }
/// );
/// let d: &dyn Dialect = &DollarBindDialect::new();
/// assert_eq!(
/// d.ordered_bind_placeholder_style(),
/// Some(BindPlaceholderStyle::DollarNumbered)
/// );
/// ```
///
/// # Type Identity
///
/// By default, derived dialects have their own `TypeId`. Set `preserve_type_id = true` to
Expand Down Expand Up @@ -167,6 +184,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 +282,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.
/// Dollar-quoted strings are controlled by [`Dialect::supports_dollar_placeholder`].
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 @@ -1208,8 +1245,7 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if this dialect allows dollar placeholders
/// e.g. `SELECT $var` (SQLite)
/// Enables SQLite `$` placeholders containing `$` characters and disables dollar-quoted strings.
fn supports_dollar_placeholder(&self) -> bool {
false
}
Expand Down Expand Up @@ -2170,13 +2206,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 @@ -2210,6 +2251,27 @@ 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),
),
(
&RedshiftSqlDialect {},
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 @@ -2246,6 +2308,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 @@ -2313,6 +2379,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_eq!(res1.unwrap(), res2.unwrap());
}
}
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, Precedence},
dialect::{BindPlaceholderStyle, Dialect, Precedence},
keywords::Keyword,
parser::{Parser, ParserError},
tokenizer::Token,
Expand Down Expand Up @@ -68,6 +68,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 @@ -77,6 +77,10 @@ impl Dialect for PostgreSqlDialect {
Some('"')
}

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redshift uses the same $1 positional syntax (https://docs.aws.amazon.com/redshift/latest/dg/r_PREPARE.html, "refer to the parameters by position using the notation $1, $2"), so RedshiftSqlDialect should return DollarNumbered too rather than falling through to None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, added Redshift also now.

}

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/redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use crate::dialect::Dialect;
use crate::dialect::{BindPlaceholderStyle, Dialect};
use crate::keywords::Keyword;
use core::iter::Peekable;
use core::str::Chars;
Expand All @@ -35,6 +35,10 @@ pub struct RedshiftSqlDialect {}
// in the Postgres dialect, the query will be parsed as an array, while in the Redshift dialect it will
// be a json path
impl Dialect for RedshiftSqlDialect {
fn ordered_bind_placeholder_style(&self) -> Option<BindPlaceholderStyle> {
Some(BindPlaceholderStyle::DollarNumbered)
}

/// Determine if a character starts a potential nested quoted identifier.
/// Example: RedShift supports the following quote styles to all mean the same thing:
/// ```sql
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() || ch.is_ascii_uppercase() || ch == '_' || ch >= '\u{0080}'
Expand Down
Loading