node-sql-parser
Advanced tools
Comparing version 1.4.0 to 1.4.1-beta
@@ -17,3 +17,3 @@ 'use strict' | ||
const surportedTypes = ['select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'use'] | ||
const surportedTypes = ['select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'use', 'alter', 'set'] | ||
@@ -82,2 +82,11 @@ function escape(str) { | ||
function assignToSQL(expr) { | ||
/** @type {Object} */ | ||
const { left, right, symbol, keyword } = expr | ||
left.keyword = keyword | ||
const leftVar = exprToSQL(left) | ||
const rightVal = exprToSQL(right) | ||
return `${leftVar} ${symbol} ${rightVal}` | ||
} | ||
function binaryToSQL(expr) { | ||
@@ -147,4 +156,8 @@ let { operator } = expr | ||
function varToSQL(expr) { | ||
const { prefix = '@', name } = expr | ||
return `${prefix}${name}` | ||
const { prefix = '@', name, members, keyword } = expr | ||
const val = [] | ||
if (keyword) val.push(keyword) | ||
const varName = members && members.length > 0 ? `${name}.${members.join('.')}` : name | ||
val.push(`${prefix || ''}${varName}`) | ||
return val.join(' ') | ||
} | ||
@@ -302,4 +315,5 @@ | ||
const { columns, from, table, where } = stmt | ||
if (columns === '*') clauses.push('*') | ||
else columnsToSQL(columns, from) && clauses.push(columnsToSQL(columns, from)) | ||
const columnInfo = columnsToSQL(columns, from) | ||
// if (columns === '*') clauses.push('*') | ||
if (columnInfo) clauses.push(columnInfo) | ||
@@ -329,6 +343,6 @@ if (Array.isArray(table)) { | ||
const clauses = ['INSERT INTO'] | ||
if (has(stmt, 'table') && stmt.table !== null) clauses.push(identifierToSql(stmt.table, false)) | ||
if (has(stmt, 'table')) clauses.push(tablesToSQL(stmt.table)) | ||
if (Array.isArray(stmt.columns)) clauses.push(`(${stmt.columns.map(identifierToSql).join(', ')})`) | ||
if (Array.isArray(stmt.values)) clauses.push('VALUES', valuesToSQL(stmt.values)) | ||
if (has(stmt, 'where') && stmt.where !== null) clauses.push(`WHERE ${exprToSQL(stmt.where)}`) | ||
if (stmt.where) clauses.push(`WHERE ${exprToSQL(stmt.where)}`) | ||
@@ -369,3 +383,3 @@ return clauses.join(' ') | ||
let str = '' | ||
if (has(stmt, 'table') && stmt.table !== null) str = identifierToSql(stmt.table, false) | ||
if (has(stmt, 'table')) str = tablesToSQL(stmt.table) | ||
if (has(stmt, 'db') && stmt.db !== null) str = `${identifierToSql(stmt.db)}.${str}` | ||
@@ -395,5 +409,6 @@ clauses.push(str) | ||
function useToSQL(stmt) { | ||
const type = stmt.type && stmt.type.toUpperCase() | ||
const database = identifierToSql(stmt.db) | ||
return `${type} ${database}` | ||
const { type, db } = stmt | ||
const action = type && type.toUpperCase() | ||
const database = identifierToSql(db) | ||
return `${action} ${database}` | ||
} | ||
@@ -407,4 +422,35 @@ | ||
function setVarToSQL(stmt) { | ||
const { expr } = stmt | ||
const action = 'SET' | ||
const val = exprToSQL(expr) | ||
return `${action} ${val}` | ||
} | ||
function alterToSQL(stmt) { | ||
const { type, table, expr = [] } = stmt | ||
const action = type && type.toUpperCase() | ||
const tableName = tablesToSQL(table) | ||
const exprList = expr.map(exprToSQL) | ||
return `${action} TABLE ${tableName} ${exprList.join(', ')}` | ||
} | ||
function alterExprToSQL(expr) { | ||
const { action, keyword, resource, definition } = expr | ||
const actionUpper = action && action.toUpperCase() | ||
const keyWordUpper = keyword && keyword.toUpperCase() | ||
const name = expr[resource] | ||
let dataType = definition && definition.dataType | ||
if (definition && definition.length) dataType = `${dataType}(${definition.length})` | ||
const alterArray = [actionUpper] | ||
if (keyWordUpper) alterArray.push(keyWordUpper) | ||
alterArray.push(name) | ||
if (dataType) alterArray.push(dataType) | ||
return alterArray.join(' ') | ||
} | ||
exprToSQLConvertFn = { | ||
alter : alterExprToSQL, | ||
aggr_func : aggrToSQL, | ||
assign : assignToSQL, | ||
binary_expr : binaryToSQL, | ||
@@ -429,2 +475,3 @@ case : caseToSQL, | ||
typeToSQLFn = { | ||
alter : alterToSQL, | ||
select : selectToSQL, | ||
@@ -439,2 +486,3 @@ delete : deleteToSQL, | ||
call : callToSQL, | ||
set : setVarToSQL, | ||
} | ||
@@ -441,0 +489,0 @@ |
{ | ||
"name": "node-sql-parser", | ||
"version": "1.4.0", | ||
"version": "1.4.1-beta", | ||
"description": "simple node sql parser", | ||
@@ -46,3 +46,3 @@ "main": "index.js", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.12.1", | ||
"eslint": "^6.1.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
@@ -49,0 +49,0 @@ "eslint-config-strict": "^14.0.1", |
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
399044
13734
1