node-sql-parser
Advanced tools
Comparing version 1.5.0 to 1.5.2
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,10 +6,8 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.aggrToSQL = undefined; | ||
exports.aggrToSQL = aggrToSQL; | ||
var _has = require('has'); | ||
var _has = _interopRequireDefault(require("has")); | ||
var _has2 = _interopRequireDefault(_has); | ||
var _expr = require("./expr"); | ||
var _expr = require('./expr'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -19,3 +17,5 @@ | ||
/** @type {Object} */ | ||
const { args } = expr; | ||
const { | ||
args | ||
} = expr; | ||
let str = (0, _expr.exprToSQL)(args.expr); | ||
@@ -25,8 +25,6 @@ const fnName = expr.name; | ||
if (fnName === 'COUNT') { | ||
if ((0, _has2.default)(args, 'distinct') && args.distinct !== null) str = `DISTINCT ${str}`; | ||
if ((0, _has.default)(args, 'distinct') && args.distinct !== null) str = `DISTINCT ${str}`; | ||
} | ||
return `${fnName}(${str})`; | ||
} | ||
exports.aggrToSQL = aggrToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,39 +6,54 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.alterExprToSQL = exports.alterToSQL = undefined; | ||
exports.alterToSQL = alterToSQL; | ||
exports.alterExprToSQL = alterExprToSQL; | ||
var _column = require('./column'); | ||
var _column = require("./column"); | ||
var _indexDefinition = require('./index-definition'); | ||
var _indexDefinition = require("./index-definition"); | ||
var _tables = require('./tables'); | ||
var _tables = require("./tables"); | ||
var _expr = require('./expr'); | ||
var _expr = require("./expr"); | ||
var _util = require('./util'); | ||
var _util = require("./util"); | ||
function alterToSQL(stmt) { | ||
const { type, table, expr = [] } = stmt; | ||
const { | ||
type, | ||
table, | ||
expr = [] | ||
} = stmt; | ||
const action = type && type.toUpperCase(); | ||
const tableName = (0, _tables.tablesToSQL)(table); | ||
const exprList = expr.map(_expr.exprToSQL); | ||
return `${action} TABLE ${tableName} ${exprList.join(', ')}`; | ||
const result = [action, 'TABLE', tableName, exprList.join(', ')]; | ||
return result.filter(_util.hasVal).join(' '); | ||
} | ||
function alterExprToSQL(expr) { | ||
const { action, keyword, resource } = expr; | ||
const { | ||
action, | ||
keyword, | ||
resource | ||
} = expr; | ||
const actionUpper = action && action.toUpperCase(); | ||
const keyWordUpper = keyword && keyword.toUpperCase(); | ||
const name = expr[resource]; | ||
let name = ''; | ||
let dataType = ''; | ||
switch (resource) { | ||
case 'column': | ||
dataType = (0, _column.columnDataType)(expr.definition); | ||
dataType = (0, _column.columnDefinitionToSQL)(expr); | ||
break; | ||
case 'index': | ||
dataType = (0, _indexDefinition.indexTypeAndOptionToSQL)(expr); | ||
dataType = dataType.filter(_util.hasVal).join(' '); | ||
name = expr[resource]; | ||
break; | ||
default: | ||
break; | ||
} | ||
const alterArray = [actionUpper]; | ||
@@ -49,5 +64,2 @@ alterArray.push(keyWordUpper); | ||
return alterArray.filter(_util.hasVal).join(' '); | ||
} | ||
exports.alterToSQL = alterToSQL; | ||
exports.alterExprToSQL = alterExprToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,9 +6,14 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.assignToSQL = undefined; | ||
exports.assignToSQL = assignToSQL; | ||
var _expr = require('./expr'); | ||
var _expr = require("./expr"); | ||
function assignToSQL(expr) { | ||
/** @type {Object} */ | ||
const { left, right, symbol, keyword } = expr; | ||
const { | ||
left, | ||
right, | ||
symbol, | ||
keyword | ||
} = expr; | ||
left.keyword = keyword; | ||
@@ -18,4 +23,2 @@ const leftVar = (0, _expr.exprToSQL)(left); | ||
return `${leftVar} ${symbol} ${rightVal}`; | ||
} | ||
exports.assignToSQL = assignToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,21 +6,38 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.binaryToSQL = undefined; | ||
exports.binaryToSQL = binaryToSQL; | ||
var _expr = require('./expr'); | ||
var _expr = require("./expr"); | ||
function binaryToSQL(expr) { | ||
let { operator } = expr; | ||
let { | ||
operator | ||
} = expr; | ||
let rstr = (0, _expr.exprToSQL)(expr.right); | ||
let isBetween = false; | ||
if (Array.isArray(rstr)) { | ||
if (operator === '=') operator = 'IN'; | ||
if (operator === '!=') operator = 'NOT IN'; | ||
if (operator === 'BETWEEN' || operator === 'NOT BETWEEN') rstr = `${rstr[0]} AND ${rstr[1]}`;else rstr = `(${rstr.join(', ')})`; | ||
switch (operator) { | ||
case '=': | ||
operator = 'IN'; | ||
break; | ||
case '!=': | ||
operator = 'NOT IN'; | ||
break; | ||
case 'BETWEEN': | ||
case 'NOT BETWEEN': | ||
isBetween = true; | ||
rstr = `${rstr[0]} AND ${rstr[1]}`; | ||
break; | ||
default: | ||
break; | ||
} | ||
if (!isBetween) rstr = `(${rstr.join(', ')})`; | ||
} | ||
const str = `${(0, _expr.exprToSQL)(expr.left)} ${operator} ${rstr}`; | ||
return expr.parentheses ? `(${str})` : str; | ||
} | ||
exports.binaryToSQL = binaryToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,5 +6,5 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.caseToSQL = undefined; | ||
exports.caseToSQL = caseToSQL; | ||
var _expr = require('./expr'); | ||
var _expr = require("./expr"); | ||
@@ -14,3 +14,2 @@ function caseToSQL(expr) { | ||
const conditions = expr.args; | ||
if (expr.expr) res.push((0, _expr.exprToSQL)(expr.expr)); | ||
@@ -20,2 +19,3 @@ | ||
res.push(conditions[i].type.toUpperCase()); | ||
if (conditions[i].cond) { | ||
@@ -25,2 +25,3 @@ res.push((0, _expr.exprToSQL)(conditions[i].cond)); | ||
} | ||
res.push((0, _expr.exprToSQL)(conditions[i].result)); | ||
@@ -30,6 +31,3 @@ } | ||
res.push('END'); | ||
return res.join(' '); | ||
} | ||
exports.caseToSQL = caseToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,9 +6,12 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.columnReferenceDefinitionToSQL = exports.columnDataType = exports.columnsToSQL = exports.columnDefinitionToSQL = undefined; | ||
exports.columnDefinitionToSQL = columnDefinitionToSQL; | ||
exports.columnsToSQL = columnsToSQL; | ||
exports.columnDataType = columnDataType; | ||
exports.columnReferenceDefinitionToSQL = columnReferenceDefinitionToSQL; | ||
var _expr = require('./expr'); | ||
var _expr = require("./expr"); | ||
var _tables = require('./tables'); | ||
var _tables = require("./tables"); | ||
var _util = require('./util'); | ||
var _util = require("./util"); | ||
@@ -45,21 +48,22 @@ function columnDataType(definition) { | ||
nullable, | ||
comment, | ||
collate, | ||
storage, | ||
default_val: defaultOpt, | ||
auto_increment: autoIncrement, | ||
unique_or_primary: uniquePrimary, | ||
comment, | ||
collate, | ||
column_format: columnFormat, | ||
storage, | ||
reference_definition: referenceDefinition | ||
} = definition; | ||
columnOpt.push((0, _util.toUpper)(nullable && nullable.value)); | ||
columnOpt.push((0, _util.toUpper)(nullable && nullable.value)); | ||
if (defaultOpt) { | ||
const { type, value } = defaultOpt; | ||
columnOpt.push(type.toUpperCase()); | ||
columnOpt.push((0, _expr.exprToSQL)(value)); | ||
const { | ||
type, | ||
value | ||
} = defaultOpt; | ||
columnOpt.push(type.toUpperCase(), (0, _expr.exprToSQL)(value)); | ||
} | ||
columnOpt.push((0, _util.toUpper)(autoIncrement)); | ||
columnOpt.push((0, _util.toUpper)(uniquePrimary)); | ||
columnOpt.push((0, _util.commentToSQL)(comment)); | ||
columnOpt.push((0, _util.toUpper)(autoIncrement), (0, _util.toUpper)(uniquePrimary), (0, _util.commentToSQL)(comment)); | ||
columnOpt.push(...(0, _util.commonTypeValue)(collate)); | ||
@@ -82,3 +86,2 @@ columnOpt.push(...(0, _util.commonTypeValue)(columnFormat)); | ||
} | ||
/** | ||
@@ -90,4 +93,7 @@ * Stringify column expressions | ||
*/ | ||
function columnsToSQL(columns, tables) { | ||
if (!columns) return; | ||
if (columns === '*') return columns; | ||
const baseTable = Array.isArray(tables) && tables[0]; | ||
@@ -97,5 +103,8 @@ let isDual = false; | ||
return columns.map(column => { | ||
const { expr } = column; | ||
const { | ||
expr | ||
} = column; | ||
if (isDual) expr.isDual = isDual; | ||
let str = (0, _expr.exprToSQL)(expr); | ||
if (column.as !== null) { | ||
@@ -108,7 +117,2 @@ str = `${str} AS `; | ||
}).join(', '); | ||
} | ||
exports.columnDefinitionToSQL = columnDefinitionToSQL; | ||
exports.columnsToSQL = columnsToSQL; | ||
exports.columnDataType = columnDataType; | ||
exports.columnReferenceDefinitionToSQL = columnReferenceDefinitionToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,55 +6,48 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.setVarToSQL = exports.callToSQL = exports.useToSQL = exports.renameToSQL = exports.truncateToSQL = exports.dropToSQL = undefined; | ||
exports.dropToSQL = dropToSQL; | ||
exports.truncateToSQL = truncateToSQL; | ||
exports.renameToSQL = renameToSQL; | ||
exports.useToSQL = useToSQL; | ||
exports.callToSQL = callToSQL; | ||
exports.setVarToSQL = setVarToSQL; | ||
var _has = require('has'); | ||
var _util = require("./util"); | ||
var _has2 = _interopRequireDefault(_has); | ||
var _expr = require("./expr"); | ||
var _tables = require('./tables'); | ||
var _tables = require("./tables"); | ||
var _util = require('./util'); | ||
var _expr = require('./expr'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function dropToSQL(stmt) { | ||
const clauses = ['DROP TABLE']; | ||
let str = ''; | ||
if ((0, _has2.default)(stmt, 'table')) str = (0, _tables.tablesToSQL)(stmt.table); | ||
if ((0, _has2.default)(stmt, 'db') && stmt.db !== null) str = `${(0, _util.identifierToSql)(stmt.db)}.${str}`; | ||
clauses.push(str); | ||
return `${clauses.join(' ')}`; | ||
const clauses = ['DROP TABLE', (0, _tables.tablesToSQL)(stmt.table)]; | ||
return clauses.join(' '); | ||
} | ||
function truncateToSQL(stmt) { | ||
const clauses = ['TRUNCATE']; | ||
if (stmt.keyword) clauses.push(stmt.keyword); | ||
let str = ''; | ||
if ((0, _has2.default)(stmt, 'table')) str = (0, _tables.tablesToSQL)(stmt.table); | ||
if ((0, _has2.default)(stmt, 'db') && stmt.db !== null) str = `${(0, _util.identifierToSql)(stmt.db)}.${str}`; | ||
clauses.push(str); | ||
return `${clauses.join(' ')}`; | ||
const clauses = ['TRUNCATE', stmt.keyword, (0, _tables.tablesToSQL)(stmt.table)]; | ||
return clauses.filter(_util.hasVal).join(' '); | ||
} | ||
function renameToSQL(stmt) { | ||
const type = `${stmt.type && stmt.type.toUpperCase()} TABLE `; | ||
const { | ||
type, | ||
table | ||
} = stmt; | ||
const clauses = []; | ||
if ((0, _has2.default)(stmt, 'table') && stmt.table !== null) { | ||
for (const tables of stmt.table) { | ||
const renameInfo = []; | ||
for (const tableInfo of tables) { | ||
let str = ''; | ||
if ((0, _has2.default)(tableInfo, 'db') && tableInfo.db !== null) str += `${(0, _util.identifierToSql)(tableInfo.db)}.`; | ||
if ((0, _has2.default)(tableInfo, 'table') && tableInfo.table !== null) str += (0, _util.identifierToSql)(tableInfo.table, false); | ||
renameInfo.push(str); | ||
} | ||
const prefix = `${type && type.toUpperCase()} TABLE`; | ||
if (table) { | ||
for (const tables of table) { | ||
const renameInfo = tables.map(_tables.tableToSQL); | ||
clauses.push(renameInfo.join(' TO ')); | ||
} | ||
} | ||
return type + clauses.join(', '); | ||
return `${prefix} ${clauses.join(', ')}`; | ||
} | ||
function useToSQL(stmt) { | ||
const { type, db } = stmt; | ||
const { | ||
type, | ||
db | ||
} = stmt; | ||
const action = type && type.toUpperCase(); | ||
@@ -72,13 +65,8 @@ const database = (0, _util.identifierToSql)(db); | ||
function setVarToSQL(stmt) { | ||
const { expr } = stmt; | ||
const { | ||
expr | ||
} = stmt; | ||
const action = 'SET'; | ||
const val = (0, _expr.exprToSQL)(expr); | ||
return `${action} ${val}`; | ||
} | ||
exports.dropToSQL = dropToSQL; | ||
exports.truncateToSQL = truncateToSQL; | ||
exports.renameToSQL = renameToSQL; | ||
exports.useToSQL = useToSQL; | ||
exports.callToSQL = callToSQL; | ||
exports.setVarToSQL = setVarToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.constraintDefinitionToSQL = undefined; | ||
exports.constraintDefinitionToSQL = constraintDefinitionToSQL; | ||
var _util = require('./util'); | ||
var _util = require("./util"); | ||
var _indexDefinition = require('./index-definition'); | ||
var _indexDefinition = require("./index-definition"); | ||
var _column = require('./column'); | ||
var _column = require("./column"); | ||
@@ -31,4 +31,2 @@ function constraintDefinitionToSQL(constraintDefinition) { | ||
return constraintSQL.filter(_util.hasVal).join(' '); | ||
} | ||
exports.constraintDefinitionToSQL = constraintDefinitionToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,23 +6,32 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.createToSQL = undefined; | ||
exports.createToSQL = createToSQL; | ||
var _indexDefinition = require('./index-definition'); | ||
var _indexDefinition = require("./index-definition"); | ||
var _column = require('./column'); | ||
var _column = require("./column"); | ||
var _constrain = require('./constrain'); | ||
var _constrain = require("./constrain"); | ||
var _tables = require('./tables'); | ||
var _tables = require("./tables"); | ||
var _union = require('./union'); | ||
var _union = require("./union"); | ||
var _util = require("./util"); | ||
function createDefinitionToSQL(definition) { | ||
const { resource } = definition; | ||
if (!definition) return []; | ||
const { | ||
resource | ||
} = definition; | ||
switch (resource) { | ||
case 'column': | ||
return (0, _column.columnDefinitionToSQL)(definition); | ||
case 'index': | ||
return (0, _indexDefinition.indexDefinitionToSQL)(definition); | ||
case 'constraint': | ||
return (0, _constrain.constraintDefinitionToSQL)(definition); | ||
default: | ||
@@ -37,40 +46,35 @@ throw new Error(`unknow resource = ${resource} type`); | ||
keyword, | ||
table, | ||
like, | ||
as, | ||
temporary, | ||
if_not_exists: ifNotExists, | ||
table, | ||
like, | ||
create_definitions: createDefinition, | ||
table_options: tableOptions, | ||
ignore_replace: ignoreReplace, | ||
as, | ||
query_expr: queryExpr | ||
} = stmt; | ||
const action = type.toUpperCase(); | ||
const sql = [action]; | ||
if (temporary) sql.push(temporary.toUpperCase()); | ||
sql.push(keyword.toUpperCase()); | ||
if (ifNotExists) sql.push(ifNotExists.toUpperCase()); | ||
const tableName = (0, _tables.tablesToSQL)(table); | ||
sql.push(tableName); | ||
const sql = [(0, _util.toUpper)(type), (0, _util.toUpper)(temporary), (0, _util.toUpper)(keyword), (0, _util.toUpper)(ifNotExists), (0, _tables.tablesToSQL)(table)]; | ||
if (like) { | ||
const { type: likeType, table: likeTable } = like; | ||
sql.push(likeType.toUpperCase()); | ||
const { | ||
type: likeType, | ||
table: likeTable | ||
} = like; | ||
const likeTableName = (0, _tables.tablesToSQL)(likeTable); | ||
sql.push(likeTableName); | ||
return sql.join(' '); | ||
sql.push((0, _util.toUpper)(likeType), likeTableName); | ||
return sql.filter(_util.hasVal).join(' '); | ||
} | ||
if (createDefinition) { | ||
const createDefinitionList = createDefinition.map(createDefinitionToSQL); | ||
sql.push(`(${createDefinitionList.join(', ')})`); | ||
sql.push(`(${createDefinition.map(createDefinitionToSQL).join(', ')})`); | ||
} | ||
if (tableOptions) { | ||
const tableOptionList = tableOptions.map(_tables.tableOptionToSQL); | ||
sql.push(tableOptionList.join(' ')); | ||
sql.push(tableOptions.map(_tables.tableOptionToSQL).join(' ')); | ||
} | ||
if (ignoreReplace) sql.push(ignoreReplace.toUpperCase()); | ||
if (as) sql.push(as.toUpperCase()); | ||
sql.push((0, _util.toUpper)(ignoreReplace), (0, _util.toUpper)(as)); | ||
if (queryExpr) sql.push((0, _union.unionToSQL)(queryExpr)); | ||
return sql.join(' '); | ||
} | ||
exports.createToSQL = createToSQL; | ||
return sql.filter(_util.hasVal).join(' '); | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,22 +6,22 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.deleteToSQL = undefined; | ||
exports.deleteToSQL = deleteToSQL; | ||
var _has = require('has'); | ||
var _column = require("./column"); | ||
var _has2 = _interopRequireDefault(_has); | ||
var _tables = require("./tables"); | ||
var _column = require('./column'); | ||
var _expr = require("./expr"); | ||
var _tables = require('./tables'); | ||
var _util = require("./util"); | ||
var _expr = require('./expr'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function deleteToSQL(stmt) { | ||
const clauses = ['DELETE']; | ||
const { columns, from, table, where } = stmt; | ||
const { | ||
columns, | ||
from, | ||
table, | ||
where | ||
} = stmt; | ||
const columnInfo = (0, _column.columnsToSQL)(columns, from); | ||
// if (columns === '*') clauses.push('*') | ||
if (columnInfo) clauses.push(columnInfo); | ||
clauses.push(columnInfo); | ||
@@ -31,8 +31,6 @@ if (Array.isArray(table)) { | ||
} | ||
if (Array.isArray(from)) clauses.push('FROM', (0, _tables.tablesToSQL)(from)); | ||
if ((0, _has2.default)(stmt, 'where') && where !== null) clauses.push(`WHERE ${(0, _expr.exprToSQL)(where)}`); | ||
return clauses.join(' '); | ||
} | ||
exports.deleteToSQL = deleteToSQL; | ||
clauses.push((0, _util.commonOptionConnector)('FROM', _tables.tablesToSQL, from)); | ||
clauses.push((0, _util.commonOptionConnector)('WHERE', _expr.exprToSQL, where)); | ||
return clauses.filter(_util.hasVal).join(' '); | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,23 +6,26 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.getExprListSQL = exports.exprToSQL = exports.exprToSQLConvertFn = undefined; | ||
exports.exprToSQL = exprToSQL; | ||
exports.getExprListSQL = getExprListSQL; | ||
exports.varToSQL = varToSQL; | ||
exports.exprToSQLConvertFn = void 0; | ||
var _util = require('./util'); | ||
var _util = require("./util"); | ||
var _alter = require('./alter'); | ||
var _alter = require("./alter"); | ||
var _aggregation = require('./aggregation'); | ||
var _aggregation = require("./aggregation"); | ||
var _assign = require('./assign'); | ||
var _assign = require("./assign"); | ||
var _binary = require('./binary'); | ||
var _binary = require("./binary"); | ||
var _case = require('./case'); | ||
var _case = require("./case"); | ||
var _func = require('./func'); | ||
var _func = require("./func"); | ||
var _interval = require('./interval'); | ||
var _interval = require("./interval"); | ||
var _select = require('./select'); | ||
var _select = require("./select"); | ||
var _union = require('./union'); | ||
var _union = require("./union"); | ||
@@ -40,4 +43,11 @@ const exprToSQLConvertFn = { | ||
}; | ||
exports.exprToSQLConvertFn = exprToSQLConvertFn; | ||
function varToSQL(expr) { | ||
const { prefix = '@', name, members, keyword } = expr; | ||
const { | ||
prefix = '@', | ||
name, | ||
members, | ||
keyword | ||
} = expr; | ||
const val = []; | ||
@@ -54,5 +64,9 @@ if (keyword) val.push(keyword); | ||
const expr = exprOrigin; | ||
if (exprOrigin.ast) { | ||
const { ast } = expr; | ||
const { | ||
ast | ||
} = expr; | ||
Reflect.deleteProperty(expr, ast); | ||
for (const key of Object.keys(ast)) { | ||
@@ -62,2 +76,3 @@ expr[key] = ast[key]; | ||
} | ||
return exprToSQLConvertFn[expr.type] ? exprToSQLConvertFn[expr.type](expr) : (0, _util.literalToSQL)(expr); | ||
@@ -86,6 +101,2 @@ } | ||
exprToSQLConvertFn.unary_expr = unaryToSQL; | ||
exports.exprToSQLConvertFn = exprToSQLConvertFn; | ||
exports.exprToSQL = exprToSQL; | ||
exports.getExprListSQL = getExprListSQL; | ||
exprToSQLConvertFn.unary_expr = unaryToSQL; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,5 +6,6 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.funcToSQL = exports.castToSQL = undefined; | ||
exports.castToSQL = castToSQL; | ||
exports.funcToSQL = funcToSQL; | ||
var _expr = require('./expr'); | ||
var _expr = require("./expr"); | ||
@@ -20,5 +21,2 @@ function castToSQL(expr) { | ||
return expr.parentheses ? `(${str})` : str; | ||
} | ||
exports.castToSQL = castToSQL; | ||
exports.funcToSQL = funcToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,9 +6,15 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.indexTypeAndOptionToSQL = exports.indexOptionToSQL = exports.indexTypeToSQL = exports.indexDefinitionToSQL = undefined; | ||
exports.indexDefinitionToSQL = indexDefinitionToSQL; | ||
exports.indexTypeToSQL = indexTypeToSQL; | ||
exports.indexOptionToSQL = indexOptionToSQL; | ||
exports.indexTypeAndOptionToSQL = indexTypeAndOptionToSQL; | ||
var _util = require('./util'); | ||
var _util = require("./util"); | ||
function indexTypeToSQL(indexType) { | ||
if (!indexType) return; | ||
const { keyword, type } = indexType; | ||
const { | ||
keyword, | ||
type | ||
} = indexType; | ||
return `${keyword.toUpperCase()} ${type.toUpperCase()}`; | ||
@@ -19,7 +25,12 @@ } | ||
if (!indexOpt) return; | ||
const { type, expr, symbol } = indexOpt; | ||
const { | ||
type, | ||
expr, | ||
symbol | ||
} = indexOpt; | ||
const upperType = type.toUpperCase(); | ||
const indexOptArray = []; | ||
indexOptArray.push(upperType); | ||
switch (upperType.toUpperCase()) { | ||
switch (upperType) { | ||
case 'KEY_BLOCK_SIZE': | ||
@@ -29,2 +40,3 @@ if (symbol) indexOptArray.push(symbol); | ||
break; | ||
case 'BTREE': | ||
@@ -35,8 +47,11 @@ case 'HASH': | ||
break; | ||
case 'WITH PARSER': | ||
indexOptArray.push(expr); | ||
break; | ||
case 'VISIBLE': | ||
case 'INVISIBLE': | ||
break; | ||
case 'COMMENT': | ||
@@ -46,5 +61,7 @@ indexOptArray.shift(); | ||
break; | ||
default: | ||
break; | ||
} | ||
return indexOptArray.join(' '); | ||
@@ -76,7 +93,2 @@ } | ||
return indexSQL.filter(_util.hasVal).join(' '); | ||
} | ||
exports.indexDefinitionToSQL = indexDefinitionToSQL; | ||
exports.indexTypeToSQL = indexTypeToSQL; | ||
exports.indexOptionToSQL = indexOptionToSQL; | ||
exports.indexTypeAndOptionToSQL = indexTypeAndOptionToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,16 +6,10 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.insertToSQL = undefined; | ||
exports.insertToSQL = insertToSQL; | ||
var _has = require('has'); | ||
var _tables = require("./tables"); | ||
var _has2 = _interopRequireDefault(_has); | ||
var _expr = require("./expr"); | ||
var _tables = require('./tables'); | ||
var _util = require("./util"); | ||
var _expr = require('./expr'); | ||
var _util = require('./util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -31,11 +25,13 @@ * @param {Array} values | ||
function insertToSQL(stmt) { | ||
const clauses = ['INSERT INTO']; | ||
if ((0, _has2.default)(stmt, 'table')) clauses.push((0, _tables.tablesToSQL)(stmt.table)); | ||
if (Array.isArray(stmt.columns)) clauses.push(`(${stmt.columns.map(_util.identifierToSql).join(', ')})`); | ||
if (Array.isArray(stmt.values)) clauses.push('VALUES', valuesToSQL(stmt.values)); | ||
if (stmt.where) clauses.push(`WHERE ${(0, _expr.exprToSQL)(stmt.where)}`); | ||
return clauses.join(' '); | ||
} | ||
exports.insertToSQL = insertToSQL; | ||
const { | ||
table, | ||
columns, | ||
values, | ||
where | ||
} = stmt; | ||
const clauses = ['INSERT INTO', (0, _tables.tablesToSQL)(table)]; | ||
if (Array.isArray(columns)) clauses.push(`(${columns.map(_util.identifierToSql).join(', ')})`); | ||
clauses.push((0, _util.commonOptionConnector)('VALUES', valuesToSQL, values)); | ||
clauses.push((0, _util.commonOptionConnector)('WHERE', _expr.exprToSQL, where)); | ||
return clauses.filter(_util.hasVal).join(' '); | ||
} |
@@ -6,7 +6,7 @@ "use strict"; | ||
}); | ||
exports.intervalToSQL = intervalToSQL; | ||
function intervalToSQL(expr) { | ||
const [intervalNum, unit] = expr.value; | ||
return `INTERVAL ${intervalNum} ${unit}`; | ||
} | ||
exports.intervalToSQL = intervalToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,9 +6,8 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _pegjsParser = require('../build/pegjs-parser'); | ||
var _pegjsParser = require("../build/pegjs-parser"); | ||
var _sql = require('./sql'); | ||
var _sql = _interopRequireDefault(require("./sql")); | ||
var _sql2 = _interopRequireDefault(_sql); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -23,3 +22,3 @@ | ||
sqlify(ast) { | ||
return (0, _sql2.default)(ast); | ||
return (0, _sql.default)(ast); | ||
} | ||
@@ -38,6 +37,9 @@ | ||
let denyInfo = ''; | ||
for (const authority of authorityList) { | ||
let hasCorrespondingAuthority = false; | ||
for (const whiteAuthority of whiteList) { | ||
const regex = new RegExp(whiteAuthority, 'i'); | ||
if (regex.test(authority)) { | ||
@@ -48,2 +50,3 @@ hasCorrespondingAuthority = true; | ||
} | ||
if (!hasCorrespondingAuthority) { | ||
@@ -55,2 +58,3 @@ denyInfo = authority; | ||
} | ||
if (!hasAuthority) throw new Error(`authority = '${denyInfo}' is required in ${type} whiteList to execute SQL = '${sql}'`); | ||
@@ -68,4 +72,6 @@ } | ||
} | ||
} | ||
exports.default = Parser; | ||
var _default = Parser; | ||
exports.default = _default; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,18 +6,14 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.selectToSQL = undefined; | ||
exports.selectToSQL = selectToSQL; | ||
var _has = require('has'); | ||
var _expr = require("./expr"); | ||
var _has2 = _interopRequireDefault(_has); | ||
var _column = require("./column"); | ||
var _expr = require('./expr'); | ||
var _with = require("./with"); | ||
var _column = require('./column'); | ||
var _tables = require("./tables"); | ||
var _with = require('./with'); | ||
var _util = require("./util"); | ||
var _tables = require('./tables'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -38,27 +34,30 @@ * @param {Object} stmt | ||
function selectToSQL(stmt) { | ||
const clauses = ['SELECT']; | ||
const { | ||
with: withInfo, | ||
options, | ||
distinct, | ||
columns, | ||
from, | ||
where, | ||
groupby, | ||
having, | ||
orderby, | ||
limit | ||
} = stmt; | ||
const clauses = [(0, _with.withToSql)(withInfo), 'SELECT']; | ||
if (Array.isArray(options)) clauses.push(options.join(' ')); | ||
clauses.push(distinct, (0, _column.columnsToSQL)(columns, from)); // FROM + joins | ||
if ((0, _has2.default)(stmt, 'with') && Array.isArray(stmt.with)) clauses.unshift((0, _with.withToSql)(stmt.with)); | ||
if ((0, _has2.default)(stmt, 'options') && Array.isArray(stmt.options)) clauses.push(stmt.options.join(' ')); | ||
if ((0, _has2.default)(stmt, 'distinct') && stmt.distinct !== null) clauses.push(stmt.distinct); | ||
clauses.push((0, _util.commonOptionConnector)('FROM', _tables.tablesToSQL, from)); | ||
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)); | ||
if (stmt.columns === '*') clauses.push('*');else clauses.push((0, _column.columnsToSQL)(stmt.columns, stmt.from)); | ||
// FROM + joins | ||
if (Array.isArray(stmt.from)) clauses.push('FROM', (0, _tables.tablesToSQL)(stmt.from)); | ||
if ((0, _has2.default)(stmt, 'where') && stmt.where !== null) clauses.push(`WHERE ${(0, _expr.exprToSQL)(stmt.where)}`); | ||
if (Array.isArray(stmt.groupby) && stmt.groupby.length > 0) clauses.push('GROUP BY', (0, _expr.getExprListSQL)(stmt.groupby).join(', ')); | ||
if ((0, _has2.default)(stmt, 'having') && stmt.having !== null) clauses.push(`HAVING ${(0, _expr.exprToSQL)(stmt.having)}`); | ||
if (Array.isArray(stmt.orderby) && stmt.orderby.length > 0) { | ||
const orderExpressions = stmt.orderby.map(expr => `${(0, _expr.exprToSQL)(expr.expr)} ${expr.type}`); | ||
clauses.push('ORDER BY', orderExpressions.join(', ')); | ||
if (Array.isArray(orderby)) { | ||
const orderExpressions = orderby.map(expr => `${(0, _expr.exprToSQL)(expr.expr)} ${expr.type}`); | ||
clauses.push((0, _util.connector)('ORDER BY', orderExpressions.join(', '))); | ||
} | ||
if (Array.isArray(stmt.limit)) clauses.push('LIMIT', stmt.limit.map(_expr.exprToSQL)); | ||
return clauses.join(' '); | ||
} | ||
exports.selectToSQL = selectToSQL; | ||
if (Array.isArray(limit)) clauses.push((0, _util.connector)('LIMIT', limit.map(_expr.exprToSQL))); | ||
return clauses.filter(_util.hasVal).join(' '); | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -8,3 +8,3 @@ Object.defineProperty(exports, "__esModule", { | ||
var _union = require('./union'); | ||
var _union = require("./union"); | ||
@@ -23,4 +23,5 @@ const surportedTypes = ['select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'use', 'alter', 'set', 'create']; | ||
} | ||
checkSupported(ast); | ||
return (0, _union.unionToSQL)(ast); | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,14 +6,23 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.tableOptionToSQL = exports.tablesToSQL = undefined; | ||
exports.tablesToSQL = tablesToSQL; | ||
exports.tableOptionToSQL = tableOptionToSQL; | ||
exports.tableToSQL = tableToSQL; | ||
var _has = require('has'); | ||
var _util = require("./util"); | ||
var _has2 = _interopRequireDefault(_has); | ||
var _expr = require("./expr"); | ||
var _util = require('./util'); | ||
var _expr = require('./expr'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function tableToSQL(tableInfo) { | ||
const { | ||
table, | ||
db, | ||
as, | ||
expr | ||
} = tableInfo; | ||
const database = (0, _util.identifierToSql)(db); | ||
const tableName = table ? (0, _util.identifierToSql)(table) : (0, _expr.exprToSQL)(expr); | ||
const str = database ? `${database}.${tableName}` : tableName; | ||
if (as) return `${str} AS ${(0, _util.identifierToSql)(as)}`; | ||
return str; | ||
} | ||
/** | ||
@@ -23,2 +32,4 @@ * @param {Array} tables | ||
*/ | ||
function tablesToSQL(tables) { | ||
@@ -28,33 +39,28 @@ const baseTable = tables[0]; | ||
if (baseTable.type === 'dual') return 'DUAL'; | ||
let str = baseTable.table ? (0, _util.identifierToSql)(baseTable.table) : (0, _expr.exprToSQL)(baseTable.expr); | ||
clauses.push(tableToSQL(baseTable)); | ||
if (baseTable.db && baseTable.db !== null) str = `${(0, _util.identifierToSql)(baseTable.db)}.${str}`; | ||
if (baseTable.as !== null) str = `${str} AS ${(0, _util.identifierToSql)(baseTable.as)}`; | ||
clauses.push(str); | ||
for (let i = 1; i < tables.length; ++i) { | ||
const joinExpr = tables[i]; | ||
str = joinExpr.join && joinExpr.join !== null ? ` ${joinExpr.join} ` : str = ', '; | ||
if (joinExpr.table) { | ||
if (joinExpr.db !== null) str = `${str}${(0, _util.identifierToSql)(joinExpr.db)}.`; | ||
str = `${str}${(0, _util.identifierToSql)(joinExpr.table)}`; | ||
} else { | ||
str = `${str}${(0, _expr.exprToSQL)(joinExpr.expr)}`; | ||
} | ||
if (joinExpr.as !== null) str = `${str} AS ${(0, _util.identifierToSql)(joinExpr.as)}`; | ||
if ((0, _has2.default)(joinExpr, 'on') && joinExpr.on !== null) str = `${str} ON ${(0, _expr.exprToSQL)(joinExpr.on)}`; | ||
if ((0, _has2.default)(joinExpr, 'using')) str = `${str} USING (${joinExpr.using.map(_util.identifierToSql).join(', ')})`; | ||
clauses.push(str); | ||
const { | ||
on, | ||
using, | ||
join | ||
} = joinExpr; | ||
const str = []; | ||
str.push(join ? ` ${join}` : ','); | ||
str.push(tableToSQL(joinExpr)); | ||
str.push((0, _util.commonOptionConnector)('ON', _expr.exprToSQL, on)); | ||
if (using) str.push(`USING (${using.map(_util.identifierToSql).join(', ')})`); | ||
clauses.push(str.filter(_util.hasVal).join(' ')); | ||
} | ||
return clauses.join(''); | ||
return clauses.filter(_util.hasVal).join(''); | ||
} | ||
function tableOptionToSQL(tableOption) { | ||
const { keyword, symbol, value } = tableOption; | ||
const { | ||
keyword, | ||
symbol, | ||
value | ||
} = tableOption; | ||
const sql = [keyword.toUpperCase()]; | ||
@@ -64,5 +70,2 @@ if (symbol) sql.push(symbol); | ||
return sql.join(' '); | ||
} | ||
exports.tablesToSQL = tablesToSQL; | ||
exports.tableOptionToSQL = tableOptionToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,17 +6,18 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.multipleToSQL = exports.unionToSQL = undefined; | ||
exports.unionToSQL = unionToSQL; | ||
exports.multipleToSQL = multipleToSQL; | ||
var _create = require('./create'); | ||
var _create = require("./create"); | ||
var _alter = require('./alter'); | ||
var _alter = require("./alter"); | ||
var _select = require('./select'); | ||
var _select = require("./select"); | ||
var _delete = require('./delete'); | ||
var _delete = require("./delete"); | ||
var _update = require('./update'); | ||
var _update = require("./update"); | ||
var _insert = require('./insert'); | ||
var _insert = require("./insert"); | ||
var _command = require('./command'); | ||
var _command = require("./command"); | ||
@@ -41,2 +42,3 @@ const typeToSQLFn = { | ||
const res = [fun(stmt)]; | ||
while (stmt._next) { | ||
@@ -46,2 +48,3 @@ res.push('UNION', fun(stmt._next)); | ||
} | ||
return res.join(' '); | ||
@@ -52,2 +55,3 @@ } | ||
const res = []; | ||
for (let i = 0, len = stmt.length; i < len; ++i) { | ||
@@ -58,6 +62,4 @@ let astInfo = stmt[i] && stmt[i].ast; | ||
} | ||
return res.join(' ; '); | ||
} | ||
exports.unionToSQL = unionToSQL; | ||
exports.multipleToSQL = multipleToSQL; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,14 +6,10 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.updateToSQL = undefined; | ||
exports.updateToSQL = updateToSQL; | ||
var _has = require('has'); | ||
var _tables = require("./tables"); | ||
var _has2 = _interopRequireDefault(_has); | ||
var _expr = require("./expr"); | ||
var _tables = require('./tables'); | ||
var _util = require("./util"); | ||
var _expr = require('./expr'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
@@ -26,9 +22,15 @@ * @param {Array} sets | ||
const clauses = []; | ||
for (const set of sets) { | ||
let str = ''; | ||
const { table, column, value } = set; | ||
if (column) str = table ? `\`${table}\`.\`${column}\`` : `\`${column}\``; | ||
const { | ||
table, | ||
column, | ||
value | ||
} = set; | ||
str = [table, column].filter(_util.hasVal).map(info => (0, _util.identifierToSql)(info)).join('.'); | ||
if (value) str = `${str} = ${(0, _expr.exprToSQL)(value)}`; | ||
clauses.push(str); | ||
} | ||
return clauses.join(', '); | ||
@@ -38,13 +40,9 @@ } | ||
function updateToSQL(stmt) { | ||
const clauses = ['UPDATE']; | ||
// cross-table update | ||
if ((0, _has2.default)(stmt, 'table') && stmt.table !== null) clauses.push((0, _tables.tablesToSQL)(stmt.table)); | ||
if (Array.isArray(stmt.set)) clauses.push('SET', setToSQL(stmt.set)); | ||
if ((0, _has2.default)(stmt, 'where') && stmt.where !== null) clauses.push(`WHERE ${(0, _expr.exprToSQL)(stmt.where)}`); | ||
return clauses.join(' '); | ||
} | ||
exports.updateToSQL = updateToSQL; | ||
const { | ||
table, | ||
set, | ||
where | ||
} = stmt; | ||
const clauses = ['UPDATE', (0, _tables.tablesToSQL)(table), (0, _util.commonOptionConnector)('SET', setToSQL, set), (0, _util.commonOptionConnector)('WHERE', _expr.exprToSQL, where)]; | ||
return clauses.filter(_util.hasVal).join(' '); | ||
} |
179
lib/util.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,8 +6,18 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.toUpper = exports.hasVal = exports.replaceParams = exports.identifierToSql = exports.literalToSQL = exports.escape = exports.createValueExpr = exports.createBinaryExpr = exports.commentToSQL = exports.columnRefToSQL = exports.commonTypeValue = undefined; | ||
exports.commonOptionConnector = commonOptionConnector; | ||
exports.connector = connector; | ||
exports.commonTypeValue = commonTypeValue; | ||
exports.columnRefToSQL = columnRefToSQL; | ||
exports.commentToSQL = commentToSQL; | ||
exports.createBinaryExpr = createBinaryExpr; | ||
exports.createValueExpr = createValueExpr; | ||
exports.escape = escape; | ||
exports.literalToSQL = literalToSQL; | ||
exports.identifierToSql = identifierToSql; | ||
exports.replaceParams = replaceParams; | ||
exports.hasVal = hasVal; | ||
exports.toUpper = toUpper; | ||
var _has = require('has'); | ||
var _has = _interopRequireDefault(require("has")); | ||
var _has2 = _interopRequireDefault(_has); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -25,16 +35,53 @@ | ||
'\\': '\\\\' | ||
}; | ||
/** | ||
* @param {(Array|boolean|string|number|null)} value | ||
* @return {Object} | ||
*/ | ||
};function createValueExpr(value) { | ||
let expr = {}; | ||
function commonOptionConnector(keyword, action, opt) { | ||
if (!opt) return; | ||
return `${keyword.toUpperCase()} ${action(opt)}`; | ||
} | ||
function connector(keyword, str) { | ||
if (!str) return; | ||
return `${keyword.toUpperCase()} ${str}`; | ||
} | ||
/** | ||
* @param {(Array|boolean|string|number|null)} value | ||
* @return {Object} | ||
*/ | ||
function createValueExpr(value) { | ||
const type = typeof value; | ||
if (Array.isArray(value)) return { | ||
type: 'expr_list', | ||
value: value.map(createValueExpr) | ||
}; | ||
if (value === null) return { | ||
type: 'null', | ||
value: null | ||
}; | ||
if (Array.isArray(value)) expr = { type: 'expr_list', value: value.map(createValueExpr) };else if (type === 'boolean') expr = { type: 'bool', value };else if (type === 'string') expr = { type: 'string', value };else if (type === 'number') expr = { type: 'number', value };else if (value === null) expr = { type: 'null', value: null };else throw new Error(`Cannot convert value "${value}" to SQL`); | ||
switch (type) { | ||
case 'boolean': | ||
return { | ||
type: 'bool', | ||
value | ||
}; | ||
return expr; | ||
case 'string': | ||
return { | ||
type: 'string', | ||
value | ||
}; | ||
case 'number': | ||
return { | ||
type: 'number', | ||
value | ||
}; | ||
default: | ||
throw new Error(`Cannot convert value "${type}" to SQL`); | ||
} | ||
} | ||
/** | ||
@@ -46,7 +93,11 @@ * @param operator | ||
*/ | ||
function createBinaryExpr(operator, left, right) { | ||
const expr = { operator, type: 'binary_expr' }; | ||
const expr = { | ||
operator, | ||
type: 'binary_expr' | ||
}; | ||
expr.left = (0, _has.default)(left, 'type') ? left : createValueExpr(left); | ||
expr.left = left && left.type ? expr.left = left : createValueExpr(left); | ||
if (operator === 'BETWEEN' || operator === 'NOT BETWEEN') { | ||
@@ -57,9 +108,8 @@ expr.right = { | ||
}; | ||
} else { | ||
expr.right = right && right.type ? expr.right = right : expr.right = createValueExpr(right); | ||
return expr; | ||
} | ||
expr.right = (0, _has.default)(right, 'type') ? right : createValueExpr(right); | ||
return expr; | ||
} | ||
/** | ||
@@ -72,2 +122,4 @@ * Replace param expressions | ||
*/ | ||
function replaceParamsInner(ast, keys) { | ||
@@ -79,5 +131,3 @@ Object.keys(ast).filter(key => { | ||
const expr = ast[key]; | ||
if (!(typeof expr === 'object' && expr.type === 'param')) return replaceParamsInner(expr, keys); | ||
if (typeof keys[expr.value] === 'undefined') throw new Error(`no value for parameter :${expr.value} found`); | ||
@@ -87,3 +137,2 @@ ast[key] = createValueExpr(keys[expr.value]); | ||
}); | ||
return ast; | ||
@@ -107,2 +156,3 @@ } | ||
if (isDual === true) return `'${ident}'`; | ||
if (!ident) return; | ||
return `\`${ident}\``; | ||
@@ -112,10 +162,42 @@ } | ||
function literalToSQL(literal) { | ||
const { type } = literal; | ||
let { value } = literal; | ||
const { | ||
type, | ||
parentheses, | ||
value | ||
} = literal; | ||
let str = value; | ||
if (type === 'number') { | ||
/* nothing */ | ||
} else if (type === 'string') value = `'${escape(value)}'`;else if (type === 'bool') value = value ? 'TRUE' : 'FALSE';else if (type === 'null') value = 'NULL';else if (type === 'star') value = '*';else if (['time', 'date', 'timestamp'].includes(type)) value = `${type.toUpperCase()} '${value}'`;else if (type === 'param') value = `:${value}`; | ||
switch (type) { | ||
case 'string': | ||
str = `'${escape(value)}'`; | ||
break; | ||
return literal.parentheses ? `(${value})` : value; | ||
case 'boolean': | ||
case 'bool': | ||
str = value ? 'TRUE' : 'FALSE'; | ||
break; | ||
case 'null': | ||
str = 'NULL'; | ||
break; | ||
case 'star': | ||
str = '*'; | ||
break; | ||
case 'param': | ||
str = `:${value}`; | ||
break; | ||
case 'time': | ||
case 'date': | ||
case 'timestamp': | ||
str = `${type.toUpperCase()} '${value}'`; | ||
break; | ||
default: | ||
break; | ||
} | ||
return parentheses ? `(${str})` : str; | ||
} | ||
@@ -128,5 +210,11 @@ | ||
function columnRefToSQL(expr) { | ||
let str = expr.column === '*' ? '*' : identifierToSql(expr.column, expr.isDual); | ||
if ((0, _has2.default)(expr, 'table') && expr.table !== null) str = `${identifierToSql(expr.table)}.${str}`; | ||
return expr.parentheses ? `(${str})` : str; | ||
const { | ||
column, | ||
isDual, | ||
table, | ||
parentheses | ||
} = expr; | ||
let str = column === '*' ? '*' : identifierToSql(column, isDual); | ||
if (table) str = `${identifierToSql(table)}.${str}`; | ||
return parentheses ? `(${str})` : str; | ||
} | ||
@@ -146,3 +234,6 @@ | ||
if (!opt) return result; | ||
const { type, value } = opt; | ||
const { | ||
type, | ||
value | ||
} = opt; | ||
result.push(type.toUpperCase()); | ||
@@ -156,19 +247,11 @@ result.push(value.toUpperCase()); | ||
const result = []; | ||
const { keyword, symobl, value } = comment; | ||
const { | ||
keyword, | ||
symbol, | ||
value | ||
} = comment; | ||
result.push(keyword.toUpperCase()); | ||
if (symobl) result.push(symobl); | ||
if (symbol) result.push(symbol); | ||
result.push(literalToSQL(value)); | ||
return result.join(' '); | ||
} | ||
exports.commonTypeValue = commonTypeValue; | ||
exports.columnRefToSQL = columnRefToSQL; | ||
exports.commentToSQL = commentToSQL; | ||
exports.createBinaryExpr = createBinaryExpr; | ||
exports.createValueExpr = createValueExpr; | ||
exports.escape = escape; | ||
exports.literalToSQL = literalToSQL; | ||
exports.identifierToSql = identifierToSql; | ||
exports.replaceParams = replaceParams; | ||
exports.hasVal = hasVal; | ||
exports.toUpper = toUpper; | ||
} |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,5 +6,5 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.withToSql = undefined; | ||
exports.withToSql = withToSql; | ||
var _expr = require('./expr'); | ||
var _expr = require("./expr"); | ||
@@ -15,2 +15,3 @@ /** | ||
function withToSql(withExpr) { | ||
if (!withExpr || withExpr.length === 0) return; | ||
const isRecursive = withExpr[0].recursive ? 'RECURSIVE ' : ''; | ||
@@ -20,9 +21,5 @@ const withExprStr = withExpr.map(cte => { | ||
const columns = Array.isArray(cte.columns) ? `(${cte.columns.join(', ')})` : ''; | ||
return `${name}${columns} AS (${(0, _expr.exprToSQL)(cte.stmt)})`; | ||
}).join(', '); | ||
return `WITH ${isRecursive}${withExprStr}`; | ||
} | ||
exports.withToSql = withToSql; | ||
} |
{ | ||
"name": "node-sql-parser", | ||
"version": "1.5.0", | ||
"version": "1.5.2", | ||
"description": "simple node sql parser", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"build": "mkdir -p build && pegjs -o build/pegjs-parser.js sql.pegjs && npm run compile", | ||
"test": "npm run lint && mocha --require babel-register", | ||
"test": "NODE_ENV=test npm run lint && mocha --require @babel/register", | ||
"compile": "babel src -d lib", | ||
@@ -44,9 +44,9 @@ "lint": "npm run build && eslint src/", | ||
"devDependencies": { | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/register": "^7.5.5", | ||
"@types/chai": "^4.1.7", | ||
"@types/mocha": "^5.2.5", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-register": "^6.26.0", | ||
"chai": "^4.2.0", | ||
@@ -53,0 +53,0 @@ "coveralls": "^3.0.6", |
Sorry, the diff of this file is too big to display
17741
518888