sql_parse: Parse SQL Statements

Description Usage Arguments Details Value See Also Examples

View source: R/sqlparse_retic.R

Description

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

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  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)
    }
  }

Example output



sqlparseR documentation built on Sept. 20, 2019, 5:05 p.m.