Skip to content

Parameterized queries #291

Closed
Closed

Description

@Jasperav

This fails to parse, but it can be successfully executed by SQLite (after providing a value for the parameter):

#[test]
fn go() {
    use sqlparser::dialect::SQLiteDialect;
    use sqlparser::parser::Parser;

    let sql = "select * from test_table where test_column = ?;";

    let dialect = SQLiteDialect {}; // or AnsiDialect, or your own dialect ...

    let ast = Parser::parse_sql(&dialect, sql).unwrap();

    println!("AST: {:#?}", ast);
}

Any idea why I get this error message?:

called Result::unwrap() on an Err value: ParserError("Expected an expression, found: ?")
thread 'dynamic_queries::go' panicked at 'called Result::unwrap() on an Err value: ParserError("Expected an expression, found: ?")',

Activity

  1. Dandandan commented on Jan 24, 2021

    @Dandandan
    Contributor

    Thanks @Jasperav .
    In think the ? is not something that's supported yet.

  2. H2CO3 commented on Jan 27, 2021

    @H2CO3

    I was just going to open an issue about the same thing. Support for other flavors of parameterized queries like :named, @named, and $named would be nice as well.

  3. alex-dukhno commented on Jan 28, 2021

    @alex-dukhno
    Contributor

    see discussions in #265

  4. mikoto2000 commented on Aug 16, 2024

    @mikoto2000

    I would also be happy to have this feature.
    I would like to support the MyBatis's #{named} method.

  5. H2CO3 commented on Aug 16, 2024

    @H2CO3

    see discussions in #265

    That issue is closed. Of what use is it? Does that mean that we're decidedly not getting parameter support?

  6. git-hulk commented on Aug 16, 2024

    @git-hulk
    Member

    @H2CO3 It should make sense to support this if it's a valid syntax for SQLite. And PR #265 was closed due to a lack of activity. I will have a look and support this feature if you want.

  7. git-hulk commented on Aug 19, 2024

    @git-hulk
    Member

    @H2CO3 I found this issue has been resolved by #420 after checking the code. Would you mind having a try and close this issue if it works well for you?

  8. H2CO3 commented on Aug 26, 2024

    @H2CO3

    @git-hulk Hi, and thanks for getting back to this!

    The current implementation largely works indeed. Unfortunately, it still seems to have a bug that is blocking in my case. I slightly modified one of the cases in the test suite to reproduce it, and – at least in the case of the SQLite dialect – it appears that $ placeholders are incorrectly recognized as an identifier. The following code:

    fn main() {
        let ast = sqlparser::parser::Parser::parse_sql(
            &sqlparser::dialect::SQLiteDialect {},
            "SELECT * FROM student WHERE id = $foo",
        );
        dbg!(ast);
    }

    prints the following AST (irrelevant parts excluded for clarity):

                            selection: Some(
                                BinaryOp {
                                    left: Identifier(
                                        Ident {
                                            value: "id",
                                            quote_style: None,
                                        },
                                    ),
                                    op: Eq,
                                    right: Identifier(
                                        Ident {
                                            value: "$foo",
                                            quote_style: None,
                                        },
                                    ),
                                },
                            ),
    
  9. git-hulk commented on Aug 26, 2024

    @git-hulk
    Member

    @H2CO3 Thanks for your feedback.

    This should be caused by SQLite dialect marks $ as the identifier start: dialect/sqlite.rs#L44. And the identifier in SQLite must start with ALPHABETIC from the documentation:

    H41130: SQLite shall recognize as an ID token any sequence of characters that begins with an ALPHABETIC character and continue with zero or more ALPHANUMERIC characters and/or "$" (u0024) characters and which is not a keyword token.

    Please refer to: https://www.sqlite.org/draft/tokenreq.html

    So I guess it should be a bug for the SQLite dialect. I can fix this issue if this change sounds good to you. cc @alamb @iffyio

  10. H2CO3 commented on Aug 26, 2024

    @H2CO3

    Perfect, thanks!

  11. git-hulk commented on Sep 2, 2024

    @git-hulk
    Member

    @H2CO3 You now can try again.

  12. LucaCappelletti94 commented on Sep 22, 2026

    @LucaCappelletti94
    Contributor

    Closing as resolved by #1402 #1979 and #420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions