Skip to content

Add support for prepared queries. #48

Closed

Description

@Jekshmek

Add support for prepared queries.

let sql = "SELECT * FROM users WHERE name = $1";
AST: Err(
    ParserError(
        "Did not expect Char(\'$\') at the beginning of an expression"
    )
)

Activity

  1. nickolay commented on Apr 20, 2019

    @nickolay
    Contributor

    I'm guessing you want this for a library that will let the user execute SQL in two steps (prepare + run)?

    A quick search indicates that the dollar sign:

    • starts a money literal in MSSQL
    • starts an identifier in MySQL
    • can start a double-dollar quoted string in H2

    Another common character to indicate parameters in prepared queries is colon (WHERE name = :1).

    Perhaps this could be implemented by adding functions that recognize the query parameters (similar to is_identifier_start/is_identifier_part) to Dialect, and additional variants to Token and Value? @andygrove what do you think?

  2. Jekshmek commented on Apr 24, 2019

    @Jekshmek
    Author

    @andygrove Yes, I use sql in two steps (prepare + run)
    Similar to the library (implemented as a compiler plugin) rust-postgres-macros
    it is necessary to parse the sql at the compilation stage in order to exclude a non-valid sql at runtime.
    For database queries I use rust-postgres which uses $1 as a parameter of a prepared request.

    It may be possible to add the same: :1.
    Perhaps you should separate the implementation from parse_sql and add the function parse_sql_prepare?

  3. nickolay commented on Apr 26, 2019

    @nickolay
    Contributor

    Thanks for the pull request (#55). Reading through it gave me another idea: you could implement a dialect (in your own crate) that accepts '$' in its is_identifier_start: this would lead to $1 getting parsed as ASTNode::SQLIdentifier. There's some difference in parser's treatment of identifiers vs literals, but I can't think of a case where it would matter. Would this work for you?

  4. Jekshmek commented on Apr 29, 2019

    @Jekshmek
    Author

    @andygrove Thanks, this solution is right for me.

    use sqlparser::dialect::Dialect;
    use sqlparser::sqlast::SQLStatement;
    use sqlparser::sqlparser::ParserError;
    use sqlparser::sqlparser::Parser;
    
    pub struct MyPostgreSqlDialect {}
    
    impl Dialect for MyPostgreSqlDialect {
        fn is_identifier_start(&self, ch: char) -> bool {
            (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '@') || ch == '$'
        }
    
        fn is_identifier_part(&self, ch: char) -> bool {
            (ch >= 'a' && ch <= 'z')
                || (ch >= 'A' && ch <= 'Z')
                || (ch >= '0' && ch <= '9')
                || (ch == '@')
                || ch == '_'
        }
    }
    
    fn main() {
        let sql = "SELECT a FROM b WHERE b.x=$123";
        let dialect = MyPostgreSqlDialect {};
        let ast:Result<Vec<SQLStatement>,ParserError> = Parser::parse_sql(&dialect, sql.to_string());
        assert!(ast.is_ok());
    }
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