sql_format: Format SQL Statements

Description Usage Arguments Details Value See Also Examples

View source: R/sqlparse_retic.R

Description

Beautifies SQL statements according to numerous formatting settings.

Usage

1
2
3
4
5
6
7
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)

Arguments

sql

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

keyword_case

Character string specifying how keywords are formatted. Options: "upper", "lower"", and "capitalize". Default: NULL

identifier_case

Character string specifying how identifiers are formatted. Options: "upper", "lower"", and "capitalize". Default: NULL

strip_comments

Boolean; if TRUE comments are removed from the SQL statements. Default: TRUE

reindent

Boolean; if TRUE the indentations of the statements are changed. Default: FALSE

indent_width

Positive integer specifying the width of the indentation. Default: 2

indent_tabs

Boolean; if TRUE tabs instead of spaces are used for indentation. Default: FALSE

indent_after_first

Boolean; if TRUE second line of statement is indented (e.g. after SELECT). Default: FALSE

indent_columns

Boolean; if TRUE all columns are indented by indent_width instead of keyword length. Default: FALSE

reindent_aligned

Boolean; if TRUE the statements are reindented to aligned format. Default: FALSE

use_space_around_operators

Boolean; if TRUE spaces are placed around mathematical operators. Default: FALSE

wrap_after

Positive integer specifying the column limit (in characters) for wrapping comma-separated lists. If NULL, every item is put on its own line. Default: NULL

comma_first

Boolean; if TRUE a linebreak is inserted before comma. Default: FALSE

truncate_strings

Positive integer; string literals longer than the given value are truncated. Default: NULL

truncate_char

Character string appended if long string literals are truncated. Default: "[...]"

encoding

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

Details

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

Value

Character string containing the formatted SQL statements.

See Also

sql_split, sql_parse

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  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)
  }

Example output

Warning message:
In system2(command = python, args = shQuote(config_script), stdout = TRUE,  :
  running command ''/usr/bin/python3' '/usr/lib/R/site-library/reticulate/config/config.py' 2>/dev/null' had status 126

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