crate / cratedb-sqlparse

Parsing utilities to validate and split SQL statements for CrateDB.
Apache License 2.0
5 stars 1 forks source link

Get parameters `WITH` parameters in a query. #31

Closed surister closed 2 months ago

surister commented 3 months ago

Example:

stmt = sqlparse('CREATE TABLE test (A text) WITH ("allocation.max_retries" = $1, "myawesomepassword = $2")')[0]
stmt.with_parameters
>>> {"allocation.max_retries": '$1', "myawesomepassword": '$2'}

Todo: Identify in which parts of the SQL dialect the with params exists (maybe only implement from CREATE table for now and add more later?). - Create

How to get the parameters from CREATE statement

    def get_text(self, node):
        if node:
            return node.getText()
        return node

    def visitCreateTable(self, ctx:SqlBaseParser.CreateTableContext):
        properties = ctx.withProperties().genericProperties().genericProperty()

        for property in properties:
            print(self.get_text(property.ident()), self.get_text(property.expr()))

Missing: Javascript target implementation

surister commented 3 months ago
    def visitGenericProperties(self, ctx: SqlBaseParser.GenericPropertiesContext):
        properties = ctx.genericProperty()

        for property in properties:
            print(self.get_text(property.ident()), self.get_text(property.expr()))

Is more generic (the post factum joke is that visitGeneric is more generic, heheheh)