node-sql-parser
Advanced tools
Comparing version 1.11.0 to 2.0.0-beta
@@ -104,2 +104,23 @@ (function (global, factory) { | ||
} | ||
function columnToSQL(column, isDual) { | ||
const { | ||
expr | ||
} = column; | ||
if (isDual) expr.isDual = isDual; | ||
let str = (0, _expr.exprToSQL)(expr); | ||
if (column.as !== null) { | ||
str = `${str} AS `; | ||
if (column.as.match(/^[a-z_][0-9a-z_]*$/i)) str = `${str}${(0, _util.identifierToSql)(column.as)}`;else str = `${str}\`${column.as}\``; | ||
} | ||
return str; | ||
} | ||
function getDual(tables) { | ||
const baseTable = Array.isArray(tables) && tables[0]; | ||
if (baseTable && baseTable.type === 'dual') return true; | ||
return false; | ||
} | ||
/** | ||
@@ -114,22 +135,16 @@ * Stringify column expressions | ||
function columnsToSQL(columns, tables) { | ||
if (!columns) return; | ||
if (columns === '*') return columns; | ||
const baseTable = Array.isArray(tables) && tables[0]; | ||
let isDual = false; | ||
if (baseTable && baseTable.type === 'dual') isDual = true; | ||
return columns.map(column => { | ||
const { | ||
expr | ||
} = column; | ||
if (isDual) expr.isDual = isDual; | ||
let str = (0, _expr.exprToSQL)(expr); | ||
if (column.as !== null) { | ||
str = `${str} AS `; | ||
if (column.as.match(/^[a-z_][0-9a-z_]*$/i)) str = `${str}${(0, _util.identifierToSql)(column.as)}`;else str = `${str}\`${column.as}\``; | ||
} | ||
return str; | ||
}).join(', '); | ||
if (!columns || columns === '*') return columns; | ||
const isDual = getDual(tables); | ||
const result = []; | ||
const { | ||
expr_list: exprList, | ||
star, | ||
type | ||
} = columns; | ||
result.push(star, (0, _util.toUpper)(type)); | ||
const exprListArr = exprList || columns; | ||
const columnsStr = exprListArr.map(col => columnToSQL(col, isDual)).join(', '); | ||
result.push([type && '(', columnsStr, type && ')'].filter(_util.hasVal).join('')); | ||
return result.filter(_util.hasVal).join(' '); | ||
} | ||
}); |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "./util", "./alter", "./aggregation", "./assign", "./binary", "./case", "./func", "./interval", "./select", "./union"], factory); | ||
define(["exports", "./util", "./alter", "./aggregation", "./assign", "./binary", "./case", "./func", "./interval", "./select", "./array-struct", "./union", "./window"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require("./util"), require("./alter"), require("./aggregation"), require("./assign"), require("./binary"), require("./case"), require("./func"), require("./interval"), require("./select"), require("./union")); | ||
factory(exports, require("./util"), require("./alter"), require("./aggregation"), require("./assign"), require("./binary"), require("./case"), require("./func"), require("./interval"), require("./select"), require("./array-struct"), require("./union"), require("./window")); | ||
} else { | ||
@@ -10,6 +10,6 @@ var mod = { | ||
}; | ||
factory(mod.exports, global.util, global.alter, global.aggregation, global.assign, global.binary, global._case, global.func, global.interval, global.select, global.union); | ||
factory(mod.exports, global.util, global.alter, global.aggregation, global.assign, global.binary, global._case, global.func, global.interval, global.select, global.arrayStruct, global.union, global.window); | ||
global.expr = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _util, _alter, _aggregation, _assign, _binary, _case, _func, _interval, _select, _union) { | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _util, _alter, _aggregation, _assign, _binary, _case, _func, _interval, _select, _arrayStruct, _union, _window) { | ||
"use strict"; | ||
@@ -28,2 +28,3 @@ | ||
aggr_func: _aggregation.aggrToSQL, | ||
'array': _arrayStruct.arrayStructExprToSQL, | ||
assign: _assign.assignToSQL, | ||
@@ -35,3 +36,5 @@ binary_expr: _binary.binaryToSQL, | ||
function: _func.funcToSQL, | ||
interval: _interval.intervalToSQL | ||
interval: _interval.intervalToSQL, | ||
struct: _arrayStruct.arrayStructExprToSQL, | ||
'window': _window.namedWindowExprListToSQL | ||
}; | ||
@@ -38,0 +41,0 @@ _exports.exprToSQLConvertFn = exprToSQLConvertFn; |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "./util", "./expr"], factory); | ||
define(["exports", "./util", "./expr", "./window"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require("./util"), require("./expr")); | ||
factory(exports, require("./util"), require("./expr"), require("./window")); | ||
} else { | ||
@@ -10,6 +10,6 @@ var mod = { | ||
}; | ||
factory(mod.exports, global.util, global.expr); | ||
factory(mod.exports, global.util, global.expr, global.window); | ||
global.over = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _util, _expr) { | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _util, _expr, _window) { | ||
"use strict"; | ||
@@ -25,5 +25,13 @@ | ||
const { | ||
as_window_specification: asWindowSpec, | ||
orderby, | ||
partitionby, | ||
orderby | ||
type | ||
} = over; | ||
if ((0, _util.toUpper)(type) === 'WINDOW') { | ||
const windowSQL = (0, _window.asWindowSpecToSQL)(asWindowSpec); | ||
return `OVER ${windowSQL}`; | ||
} | ||
const partition = (0, _expr.orderOrPartitionByToSQL)(partitionby, 'partition by'); | ||
@@ -30,0 +38,0 @@ const order = (0, _expr.orderOrPartitionByToSQL)(orderby, 'order by'); |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "../build/hive", "../build/mysql", "../build/mariadb", "../build/postgresql", "../build/transactsql", "./sql", "./util"], factory); | ||
define(["exports", "../build/bigquery", "../build/hive", "../build/mysql", "../build/mariadb", "../build/postgresql", "../build/transactsql", "./sql", "./util"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require("../build/hive"), require("../build/mysql"), require("../build/mariadb"), require("../build/postgresql"), require("../build/transactsql"), require("./sql"), require("./util")); | ||
factory(exports, require("../build/bigquery"), require("../build/hive"), require("../build/mysql"), require("../build/mariadb"), require("../build/postgresql"), require("../build/transactsql"), require("./sql"), require("./util")); | ||
} else { | ||
@@ -10,6 +10,6 @@ var mod = { | ||
}; | ||
factory(mod.exports, global.hive, global.mysql, global.mariadb, global.postgresql, global.transactsql, global.sql, global.util); | ||
factory(mod.exports, global.bigquery, global.hive, global.mysql, global.mariadb, global.postgresql, global.transactsql, global.sql, global.util); | ||
global.parser = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _hive, _mysql, _mariadb, _postgresql, _transactsql, _sql, _util) { | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _bigquery, _hive, _mysql, _mariadb, _postgresql, _transactsql, _sql, _util) { | ||
"use strict"; | ||
@@ -26,2 +26,3 @@ | ||
const parser = { | ||
bigquery: _bigquery.parse, | ||
hive: _hive.parse, | ||
@@ -28,0 +29,0 @@ mysql: _mysql.parse, |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "./expr", "./column", "./with", "./tables", "./util"], factory); | ||
define(["exports", "./expr", "./column", "./limit", "./with", "./tables", "./util"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require("./expr"), require("./column"), require("./with"), require("./tables"), require("./util")); | ||
factory(exports, require("./expr"), require("./column"), require("./limit"), require("./with"), require("./tables"), require("./util")); | ||
} else { | ||
@@ -10,6 +10,6 @@ var mod = { | ||
}; | ||
factory(mod.exports, global.expr, global.column, global._with, global.tables, global.util); | ||
factory(mod.exports, global.expr, global.column, global.limit, global._with, global.tables, global.util); | ||
global.select = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _expr, _column, _with, _tables, _util) { | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _expr, _column, _limit, _with, _tables, _util) { | ||
"use strict"; | ||
@@ -38,15 +38,18 @@ | ||
const { | ||
with: withInfo, | ||
options, | ||
as_struct_val: asStructVal, | ||
columns, | ||
distinct, | ||
columns, | ||
from, | ||
where, | ||
for_sys_time_as_of: forSystem = {}, | ||
groupby, | ||
having, | ||
limit, | ||
options, | ||
orderby, | ||
limit, | ||
top | ||
top, | ||
window: windowInfo, | ||
with: withInfo, | ||
where | ||
} = stmt; | ||
const clauses = [(0, _with.withToSql)(withInfo), 'SELECT']; | ||
const clauses = [(0, _with.withToSql)(withInfo), 'SELECT', (0, _util.toUpper)(asStructVal)]; | ||
clauses.push((0, _util.topToSQL)(top)); | ||
@@ -57,17 +60,15 @@ if (Array.isArray(options)) clauses.push(options.join(' ')); | ||
clauses.push((0, _util.commonOptionConnector)('FROM', _tables.tablesToSQL, from)); | ||
const { | ||
keyword, | ||
expr | ||
} = forSystem || {}; | ||
clauses.push((0, _util.commonOptionConnector)(keyword, _expr.exprToSQL, expr)); | ||
clauses.push((0, _util.commonOptionConnector)('WHERE', _expr.exprToSQL, where)); | ||
clauses.push((0, _util.connector)('GROUP BY', (0, _expr.getExprListSQL)(groupby).join(', '))); | ||
clauses.push((0, _util.commonOptionConnector)('HAVING', _expr.exprToSQL, having)); | ||
clauses.push((0, _util.commonOptionConnector)('WINDOW', _expr.exprToSQL, windowInfo)); | ||
clauses.push((0, _expr.orderOrPartitionByToSQL)(orderby, 'order by')); | ||
if (limit) { | ||
const { | ||
seperator, | ||
value | ||
} = limit; | ||
clauses.push((0, _util.connector)('LIMIT', value.map(_expr.exprToSQL).join(`${seperator === 'offset' ? ' ' : ''}${seperator.toUpperCase()} `))); | ||
} | ||
clauses.push((0, _limit.limitToSQL)(limit)); | ||
return clauses.filter(_util.hasVal).join(' '); | ||
} | ||
}); |
@@ -20,3 +20,3 @@ (function (global, factory) { | ||
_exports.default = toSQL; | ||
const surportedTypes = ['select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'use', 'alter', 'set', 'create', 'lock', 'unlock']; | ||
const surportedTypes = ['select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'bigquery']; | ||
@@ -35,4 +35,8 @@ function checkSupported(expr) { | ||
checkSupported(ast); | ||
const { | ||
type | ||
} = ast; | ||
if (type === 'bigquery') return (0, _union.bigQueryToSQL)(ast); | ||
return (0, _union.unionToSQL)(ast); | ||
} | ||
}); |
@@ -22,2 +22,3 @@ (function (global, factory) { | ||
_exports.tableToSQL = tableToSQL; | ||
_exports.unnestToSQL = unnestToSQL; | ||
@@ -37,2 +38,13 @@ function tableToSQL(tableInfo) { | ||
} | ||
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(' '); | ||
} | ||
/** | ||
@@ -45,2 +57,6 @@ * @param {Array} tables | ||
function tablesToSQL(tables) { | ||
const { | ||
type | ||
} = tables; | ||
if ((0, _util.toUpper)(type) === 'UNNEST') return unnestToSQL(tables); | ||
const baseTable = tables[0]; | ||
@@ -47,0 +63,0 @@ const clauses = []; |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports", "./create", "./alter", "./select", "./delete", "./update", "./insert", "./command"], factory); | ||
define(["exports", "./create", "./alter", "./select", "./delete", "./update", "./insert", "./command", "./expr", "./limit", "./with"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports, require("./create"), require("./alter"), require("./select"), require("./delete"), require("./update"), require("./insert"), require("./command")); | ||
factory(exports, require("./create"), require("./alter"), require("./select"), require("./delete"), require("./update"), require("./insert"), require("./command"), require("./expr"), require("./limit"), require("./with")); | ||
} else { | ||
@@ -10,6 +10,6 @@ var mod = { | ||
}; | ||
factory(mod.exports, global.create, global.alter, global.select, global._delete, global.update, global.insert, global.command); | ||
factory(mod.exports, global.create, global.alter, global.select, global._delete, global.update, global.insert, global.command, global.expr, global.limit, global._with); | ||
global.union = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _create, _alter, _select, _delete, _update, _insert, _command) { | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _create, _alter, _select, _delete, _update, _insert, _command, _expr, _limit, _with) { | ||
"use strict"; | ||
@@ -20,2 +20,3 @@ | ||
}); | ||
_exports.bigQueryToSQL = bigQueryToSQL; | ||
_exports.unionToSQL = unionToSQL; | ||
@@ -53,2 +54,18 @@ _exports.multipleToSQL = multipleToSQL; | ||
function bigQueryToSQL(stmt) { | ||
const { | ||
with: withExpr, | ||
select, | ||
left_parenthesis: lp, | ||
right_parenthesis: rp, | ||
orderby, | ||
limit | ||
} = stmt; | ||
const result = [(0, _with.withToSql)(withExpr), lp, unionToSQL(select), rp]; // process with, orderby and limit | ||
result.push((0, _expr.orderOrPartitionByToSQL)(orderby, 'order by')); | ||
result.push((0, _limit.limitToSQL)(limit)); | ||
return result.filter(val => val).join(' '); | ||
} | ||
function multipleToSQL(stmt) { | ||
@@ -55,0 +72,0 @@ const res = []; |
@@ -19,2 +19,3 @@ (function (global, factory) { | ||
}); | ||
_exports.arrayStructTypeToSQL = arrayStructTypeToSQL; | ||
_exports.autoIncreatementToSQL = autoIncreatementToSQL; | ||
@@ -207,2 +208,5 @@ _exports.commonKeywordArgsToSQL = commonKeywordArgsToSQL; | ||
case 'bigquery': | ||
return ident; | ||
default: | ||
@@ -306,2 +310,23 @@ return `\`${ident}\``; | ||
function arrayStructTypeToSQL(expr) { | ||
if (!expr) return; | ||
const { | ||
dataType, | ||
definition, | ||
anglebracket | ||
} = expr; | ||
const dataTypeUpper = toUpper(dataType); | ||
const isNotArrayOrStruct = dataTypeUpper !== 'ARRAY' && dataTypeUpper !== 'STRUCT'; | ||
if (isNotArrayOrStruct) return dataTypeUpper; | ||
const result = definition && definition.map(field => { | ||
const { | ||
field_name: fieldName, | ||
field_type: fieldType | ||
} = field; | ||
const fieldResult = [fieldName, arrayStructTypeToSQL(fieldType)]; | ||
return fieldResult.filter(hasVal).join(' '); | ||
}).join(', '); | ||
return anglebracket ? `${dataTypeUpper}<${result}>` : `${dataTypeUpper} ${result}`; | ||
} | ||
function commentToSQL(comment) { | ||
@@ -308,0 +333,0 @@ if (!comment) return; |
@@ -28,5 +28,9 @@ (function (global, factory) { | ||
const withExprStr = withExpr.map(cte => { | ||
const name = `"${cte.name}"`; | ||
const columns = Array.isArray(cte.columns) ? `(${cte.columns.join(', ')})` : ''; | ||
return `${name}${columns} AS (${(0, _expr.exprToSQL)(cte.stmt)})`; | ||
const { | ||
name, | ||
stmt, | ||
columns | ||
} = cte; | ||
const column = Array.isArray(columns) ? `(${columns.join(', ')})` : ''; | ||
return `${name}${column} AS (${(0, _expr.exprToSQL)(stmt)})`; | ||
}).join(', '); | ||
@@ -33,0 +37,0 @@ return `WITH ${isRecursive}${withExprStr}`; |
{ | ||
"name": "node-sql-parser", | ||
"version": "1.11.0", | ||
"version": "2.0.0-beta", | ||
"description": "simple node sql parser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -89,2 +89,3 @@ # Nodejs SQL Parser | ||
- TransactSQL | ||
- BigQuery | ||
@@ -91,0 +92,0 @@ - New issue could be made for other new database. |
Sorry, the diff of this file is too big to display
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4038829
40
111986
258
1