Skip to content

Commit

Permalink
sql input with sqlparse and sqloptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed May 23, 2024
1 parent 1ad825a commit 39493c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ List of data input types that can be auto-detected (through the file extension o
| raw | Passes the input directly to transforms and output |
| rawhex | Tries to read the input char by char converting into lines with the hexadecimal representation |
| slon | A SLON format (auto-detected) |
| sql | One or more SQLs statements to AST (Abstract Syntax Tree) |
| sql | One or more SQLs statements to AST (Abstract Syntax Tree) or beautified SQL |
| toml | TOML format |
| xls | A XLSx compatible file (requires file=abc.xlsx) |
| xml | An XML format (auto-detected) |
Expand Down Expand Up @@ -254,6 +254,26 @@ List of options to use when _in=rawhex_:

---

### 🧾 SQL input options

List of options to use when _in=sql_:

| Option | Type | Description |
|--------|------|-------------|
| sqlparse | Boolean | If true instead of returning a SQL AST representation it will beautify the SQL statement(s) |
| sqloptions | String | A JSON/SLON map with options for sqlparse=true |

SQL options available:

* indent: the indentation string (defaults to " ")
* uppercase: if true will uppercase the SQL (defaults to false)
* linesBetweenQueries: number of lines between queries (defaults to 1)
* maxColumnLength: maximum column length (defaults to 50)
* skipWhitespaceNearBlockParentheses: if true will whitespace near block parentheses (defaults to false)
* language: the SQL language dialect (Db2, MariaDb, MySql, N1ql, PlSql, PostgreSql, Redshift, SparkSql, StandardSql and TSql)

---

### 🧾 XLS input options

List of options to use when _in=xls_:
Expand Down
7 changes: 6 additions & 1 deletion src/include/inputFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ var _inputFns = new Map([
}],
["sql", (r, options) => {
if (isString(r)) {
_$o(af.fromSQL(r).ast, options)
if (toBoolean(params.sqlparse)) {
if (isUnDef(ow.format.sqlFormat)) _exit(-1, "SQL parse not available.")
_$o(ow.format.sqlFormat(r, isDef(params.sqloptions) ? _fromJSSLON(params.sqloptions) : __), options)
} else {
_$o(af.fromSQL(r).ast, options)
}
} else {
_$o(r, options)
}
Expand Down

0 comments on commit 39493c4

Please sign in to comment.