Title: | Wrapper for 'Python' Module 'sqlparse': Parse, Split, and Format 'SQL' |
---|---|
Description: | Wrapper for the non-validating 'SQL' parser 'Python' module 'sqlparse' <https://github.com/andialbrecht/sqlparse>. It allows parsing, splitting, and formatting 'SQL' statements. |
Authors: | Michael Simmler [aut, cre] |
Maintainer: | Michael Simmler <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0 |
Built: | 2024-11-01 11:23:01 UTC |
Source: | https://github.com/cran/sqlparseR |
Install the sqlparse Python package into a virtual environment or conda environment.
install_sqlparse_py(method = "auto", conda = "auto", envname = NULL, skip_if_available = FALSE)
install_sqlparse_py(method = "auto", conda = "auto", envname = NULL, skip_if_available = FALSE)
method |
Installation method passed to |
conda |
Path to conda executable passed to |
envname |
The name, or full path, of the environment in which the sqlparse Python package is to be installed. Alternatively, |
skip_if_available |
Boolean; if |
0 on successful installation or 1 in case an error was raised
## Not run: install_sqlparse_py() ## End(Not run)
## Not run: install_sqlparse_py() ## End(Not run)
Beautifies SQL statements according to numerous formatting settings.
sql_format(sql, keyword_case = NULL, identifier_case = NULL, strip_comments = TRUE, reindent = FALSE, indent_width = 2, indent_tabs = FALSE, indent_after_first = FALSE, indent_columns = FALSE, reindent_aligned = FALSE, use_space_around_operators = FALSE, wrap_after = NULL, comma_first = FALSE, truncate_strings = NULL, truncate_char = "[...]", encoding = NULL)
sql_format(sql, keyword_case = NULL, identifier_case = NULL, strip_comments = TRUE, reindent = FALSE, indent_width = 2, indent_tabs = FALSE, indent_after_first = FALSE, indent_columns = FALSE, reindent_aligned = FALSE, use_space_around_operators = FALSE, wrap_after = NULL, comma_first = FALSE, truncate_strings = NULL, truncate_char = "[...]", encoding = NULL)
sql |
Character string containing one or more SQL statements to be formatted. |
keyword_case |
Character string specifying how keywords are formatted. Options: |
identifier_case |
Character string specifying how identifiers are formatted. Options: |
strip_comments |
Boolean; if |
reindent |
Boolean; if |
indent_width |
Positive integer specifying the width of the indentation. Default: |
indent_tabs |
Boolean; if |
indent_after_first |
Boolean; if |
indent_columns |
Boolean; if |
reindent_aligned |
Boolean; if |
use_space_around_operators |
Boolean; if |
wrap_after |
Positive integer specifying the column limit (in characters) for wrapping comma-separated lists. If |
comma_first |
Boolean; if |
truncate_strings |
Positive integer; string literals longer than the given value are truncated. Default: |
truncate_char |
Character string appended if long string literals are truncated. Default: |
encoding |
Character string specifying the input encoding. Default: |
This function is a wrapper to the sqlparse.format() function from the sqlparse Python module, which is a non-validating SQL parser.
Character string containing the formatted SQL statements.
if (reticulate::py_module_available("sqlparse")) { library("sqlparseR") raw <- "SELECT * FROM FOO WHERE BAR > 4500;" formatted <- sql_format(raw, keyword_case = "capitalize", identifier_case = "lower", reindent = TRUE, indent_after_first = TRUE) cat(formatted) }
if (reticulate::py_module_available("sqlparse")) { library("sqlparseR") raw <- "SELECT * FROM FOO WHERE BAR > 4500;" formatted <- sql_format(raw, keyword_case = "capitalize", identifier_case = "lower", reindent = TRUE, indent_after_first = TRUE) cat(formatted) }
Parse one or several SQL statements (non-validating).
sql_parse(sql, encoding = NULL)
sql_parse(sql, encoding = NULL)
sql |
Character string containing one or more SQL statements to be formatted. |
encoding |
Character string specifying the input encoding. Default: |
This function is a wrapper to the sqlparse.parse() function from the sqlparse Python module, which is a non-validating SQL parser.
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/.
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) } }
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) } }
Split a string with (one or) several SQL statements into single statements.
sql_split(sql, encoding = NULL)
sql_split(sql, encoding = NULL)
sql |
Character string containing (one or) several SQL statements |
encoding |
Character string specifying the input encoding. Default: |
This function is a wrapper to the sqlparse.split() function from the sqlparse python module, which is a non-validating SQL parser.
Character vector with the single SQL statements.
if (reticulate::py_module_available("sqlparse")) { library("sqlparseR") raw <- "select*from foo; select*from bar;" statements <- sql_split(raw) print(statements) }
if (reticulate::py_module_available("sqlparse")) { library("sqlparseR") raw <- "select*from foo; select*from bar;" statements <- sql_split(raw) print(statements) }