node-sql-parser
Advanced tools
Comparing version 3.9.4 to 4.0.0
@@ -470,4 +470,6 @@ | ||
export type distinct_on = {type: string; columns: column_ref_list;} | { type: string | undefined; }; | ||
export type select_stmt_nake = { | ||
@@ -477,3 +479,3 @@ with?: with_clause; | ||
options?: option_clause; | ||
distinct?: 'DISTINCT'; | ||
distinct?: {type: string; columns?: column_list; }; | ||
columns: column_clause; | ||
@@ -486,2 +488,3 @@ from?: from_clause; | ||
limit?: limit_clause; | ||
window?: window_clause; | ||
}; | ||
@@ -572,6 +575,12 @@ | ||
export type as_window_specification = { window_specification: window_specification; parentheses: boolean }; | ||
export type window_clause = { keyword: 'window'; type: 'window', expr: named_window_expr_list; }; | ||
export type window_specification = { name: null; partitionby: partition_by_clause; orderby: order_by_clause; window_frame_clause: string }; | ||
export type named_window_expr_list = named_window_expr[]; | ||
export type named_window_expr = { name: ident_name; as_window_specification: as_window_specification; }; | ||
export type as_window_specification = ident_name | { window_specification: window_specification; parentheses: boolean }; | ||
export type window_specification = { name: null; partitionby: partition_by_clause; orderby: order_by_clause; window_frame_clause: string | null; }; | ||
export type window_specification_frameless = { name: null; partitionby: partition_by_clause; orderby: order_by_clause; window_frame_clause: null }; | ||
@@ -664,3 +673,3 @@ | ||
export type expr_list = { type: 'expr_list'; value: expr[] }; | ||
export type expr_list = { type: 'expr_list'; value: expr_item[] }; | ||
@@ -1045,2 +1054,4 @@ export type interval_expr = { type: 'interval', expr: expr; unit: interval_unit; }; | ||
type KW_WINDOW = never; | ||
type KW_LIMIT = never; | ||
@@ -1363,2 +1374,8 @@ | ||
export type array_type = data_type; | ||
export type boolean_type = data_type; | ||
@@ -1365,0 +1382,0 @@ |
@@ -38,2 +38,3 @@ (function (global, factory) { | ||
const { | ||
array_index, | ||
arrow, | ||
@@ -53,2 +54,3 @@ as, | ||
if (schema) str = `${(0, _util.identifierToSql)(schema)}.${str}`; | ||
if (array_index) str = `${str}[${array_index.number}]`; | ||
const result = [str, (0, _util.commonOptionConnector)('AS', _expr.exprToSQL, as), (0, _util.commonOptionConnector)(arrow, _util.literalToSQL, property)]; | ||
@@ -163,3 +165,3 @@ if (collate) result.push((0, _util.commonTypeValue)(collate).join(' ')); | ||
if (expr.parentheses && Reflect.has(expr, 'array_index')) str = `(${str})`; | ||
if (expr.array_index) str = `${str}[${expr.array_index.number}]`; | ||
if (expr.array_index && expr.type !== 'column_ref') str = `${str}[${expr.array_index.number}]`; | ||
@@ -166,0 +168,0 @@ if (column.as !== null) { |
@@ -116,7 +116,7 @@ (function (global, factory) { | ||
case 'PARTITION BY': | ||
expressions = expr.map(info => `${(0, _column.columnRefToSQL)(info.expr)}`); | ||
expressions = expr.map(info => `${exprToSQL(info.expr)}`); | ||
break; | ||
default: | ||
expressions = expr.map(info => `${(0, _column.columnRefToSQL)(info.expr)}`); | ||
expressions = expr.map(info => `${exprToSQL(info.expr)}`); | ||
break; | ||
@@ -123,0 +123,0 @@ } |
@@ -23,2 +23,14 @@ (function (global, factory) { | ||
function arrayDimensionToSymbol(target) { | ||
if (!target || !target.array) return ''; | ||
switch (target.array) { | ||
case 'one': | ||
return '[]'; | ||
case 'two': | ||
return '[][]'; | ||
} | ||
} | ||
function castToSQL(expr) { | ||
@@ -53,3 +65,4 @@ const { | ||
if (collate) suffix += ` ${(0, _util.commonTypeValue)(collate).join(' ')}`; | ||
return `${prefix}${symbolChar}${dataType}${str}${suffix}`; | ||
const arrayDimension = arrayDimensionToSymbol(target); | ||
return `${prefix}${symbolChar}${dataType}${arrayDimension}${str}${suffix}`; | ||
} | ||
@@ -56,0 +69,0 @@ |
@@ -27,13 +27,8 @@ (function (global, factory) { | ||
keyword, | ||
orderby, | ||
partitionby, | ||
type | ||
} = over; | ||
const upperType = (0, _util.toUpper)(type); | ||
if (upperType === 'WINDOW') return `OVER ${(0, _window.asWindowSpecToSQL)(asWindowSpec)}`; | ||
if ((0, _util.toUpper)(type) === 'WINDOW') { | ||
const windowSQL = (0, _window.asWindowSpecToSQL)(asWindowSpec); | ||
return `OVER ${windowSQL}`; | ||
} | ||
if ((0, _util.toUpper)(type) === 'ON UPDATE') { | ||
if (upperType === 'ON UPDATE') { | ||
let onUpdate = `${(0, _util.toUpper)(type)} ${(0, _util.toUpper)(keyword)}`; | ||
@@ -45,6 +40,4 @@ const args = (0, _expr.exprToSQL)(expr); | ||
const partition = (0, _expr.orderOrPartitionByToSQL)(partitionby, 'partition by'); | ||
const order = (0, _expr.orderOrPartitionByToSQL)(orderby, 'order by'); | ||
return `OVER (${[partition, order].filter(_util.hasVal).join(' ')})`; | ||
throw new Error('unknown over type'); | ||
} | ||
}); |
@@ -21,2 +21,13 @@ (function (global, factory) { | ||
function distinctToSQL(distinct) { | ||
if (!distinct) return; | ||
if (typeof distinct === 'string') return distinct; | ||
const { | ||
type, | ||
columns | ||
} = distinct; | ||
const result = [(0, _util.toUpper)(type)]; | ||
if (columns) result.push(`(${columns.map(_column.columnRefToSQL).join(', ')})`); | ||
return result.filter(_util.hasVal).join(' '); | ||
} | ||
/** | ||
@@ -36,2 +47,4 @@ * @param {Object} stmt | ||
*/ | ||
function selectToSQL(stmt) { | ||
@@ -59,3 +72,3 @@ const { | ||
if (Array.isArray(options)) clauses.push(options.join(' ')); | ||
clauses.push(distinct, (0, _column.columnsToSQL)(columns, from)); // FROM + joins | ||
clauses.push(distinctToSQL(distinct), (0, _column.columnsToSQL)(columns, from)); // FROM + joins | ||
@@ -62,0 +75,0 @@ clauses.push((0, _util.commonOptionConnector)('FROM', _tables.tablesToSQL, from)); |
@@ -19,3 +19,3 @@ (function (global, factory) { | ||
}); | ||
_exports.default = toSQL; | ||
_exports.default = astToSQL; | ||
const surportedTypes = ['analyze', 'attach', 'select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'bigquery', 'declare', 'show', 'replace']; | ||
@@ -41,2 +41,14 @@ | ||
} | ||
function goToSQL(stmt) { | ||
if (!stmt || stmt.length === 0) return ''; | ||
const res = [toSQL(stmt.ast)]; | ||
if (stmt.go_next) res.push(stmt.go.toUpperCase(), goToSQL(stmt.go_next)); | ||
return res.filter(sqlItem => sqlItem).join(' '); | ||
} | ||
function astToSQL(ast) { | ||
if (ast.go === 'go') return goToSQL(ast); | ||
return toSQL(ast); | ||
} | ||
}); |
@@ -24,3 +24,15 @@ (function (global, factory) { | ||
function unnestToSQL(unnestExpr) { | ||
const { | ||
type, | ||
as, | ||
expr, | ||
with_offset: withOffset | ||
} = unnestExpr; | ||
const result = [`${(0, _util.toUpper)(type)}(${expr && (0, _expr.exprToSQL)(expr) || ''})`, (0, _util.commonOptionConnector)('AS', _util.identifierToSql, as), (0, _util.commonOptionConnector)((0, _util.toUpper)(withOffset && withOffset.keyword), _util.identifierToSql, withOffset && withOffset.as)]; | ||
return result.filter(_util.hasVal).join(' '); | ||
} | ||
function tableToSQL(tableInfo) { | ||
if ((0, _util.toUpper)(tableInfo.type) === 'UNNEST') return unnestToSQL(tableInfo); | ||
const { | ||
@@ -42,13 +54,2 @@ table, | ||
} | ||
function unnestToSQL(unnestExpr) { | ||
const { | ||
type, | ||
as, | ||
expr, | ||
with_offset: withOffset | ||
} = unnestExpr; | ||
const result = [(0, _util.toUpper)(type), `(${expr && (0, _expr.exprToSQL)(expr) || ' '})`, (0, _util.commonOptionConnector)('AS', _util.identifierToSql, as), (0, _util.commonOptionConnector)((0, _util.toUpper)(withOffset && withOffset.keyword), _util.identifierToSql, withOffset && withOffset.as)]; | ||
return result.filter(_util.hasVal).join(' '); | ||
} | ||
/** | ||
@@ -61,6 +62,2 @@ * @param {Array} tables | ||
function tablesToSQL(tables) { | ||
const { | ||
type | ||
} = tables; | ||
if ((0, _util.toUpper)(type) === 'UNNEST') return unnestToSQL(tables); | ||
const baseTable = tables[0]; | ||
@@ -67,0 +64,0 @@ const clauses = []; |
{ | ||
"name": "node-sql-parser", | ||
"version": "3.9.4", | ||
"version": "4.0.0", | ||
"description": "simple node sql parser", | ||
@@ -71,3 +71,3 @@ "main": "index.js", | ||
"eslint": "^7.27.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-strict": "^14.0.1", | ||
@@ -74,0 +74,0 @@ "eslint-plugin-filenames": "^1.3.2", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39086790
22838