sql_parse {sqlparseR}R Documentation

Parse SQL Statements

Description

Parse one or several SQL statements (non-validating).

Usage

sql_parse(sql, encoding = NULL)

Arguments

sql

Character string containing one or more SQL statements to be formatted.

encoding

Character string specifying the input encoding. Default: NULL (assumes UTF-8 or latin-1)

Details

This function is a wrapper to the sqlparse.parse() function from the sqlparse Python module, which is a non-validating SQL parser.

Value

List with reference class objects which are converted instances of the custom Python class Statement. These tree-ish representations of the parsed statements can be analyzed with a set of reference class methods (accessed via $). See the documentation of the corresponding Python methods: https://sqlparse.readthedocs.io/en/stable/analyzing/.

See Also

sql_format, sql_split

Examples

  if (reticulate::py_module_available("sqlparse")) {

    library("sqlparseR")

    raw <- "select*from foo; select*from bar;"

    parsed <- sql_parse(raw)

    ## Analyzing the parsed statements
    # e.g., get name of identifier in second statement
    n <- parsed[[2]]$get_name()
    print(n)

    # e.g., get a (Python) generator yielding ungrouped tokens of the first statement
    token_it <- parsed[[1]]$flatten()
    for (t in reticulate::iterate(token_it)) {
      print(t)
    }
  }

[Package sqlparseR version 0.1.0 Index]