Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-sql-parser

Package Overview
Dependencies
Maintainers
1
Versions
172
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-sql-parser - npm Package Compare versions

Comparing version 4.8.0 to 4.9.0

143

ast/postgresql.ts

@@ -12,5 +12,5 @@

export type create_stmt = create_table_stmt | create_constraint_trigger | create_extension_stmt | create_index_stmt | create_sequence | create_db_stmt;
export type create_stmt = create_table_stmt | create_constraint_trigger | create_extension_stmt | create_index_stmt | create_sequence | create_db_stmt | create_domain_stmt | create_type_stmt | create_view_stmt;
export type alter_stmt = alter_table_stmt;
export type alter_stmt = alter_table_stmt | alter_schema_stmt | alter_domain_type_stmt | alter_function_stmt | alter_aggregate_stmt;

@@ -52,6 +52,6 @@ export type crud_stmt = union_stmt | update_stmt | replace_insert_stmt | insert_no_columns_stmt | delete_stmt | cmd_stmt | proc_stmts;

type: 'create',
keyword: 'database',
keyword: 'database' | 'schema',
if_not_exists?: 'if not exists',
database: string,
create_definition?: create_db_definition
create_definitions?: create_db_definition
}

@@ -61,2 +61,45 @@

export type view_with = string;
export type with_view_option = {type: string; value: string; symbol: string; };
export type with_view_options = with_view_option[];
export type create_view_stmt = {
type: 'create',
keyword: 'view',
replace?: 'or replace',
temporary?: 'temporary' | 'temp',
recursive?: 'recursive',
view: table_name,
columns?: column_list,
select: select_stmt_nake,
with_options?: with_options,
with?: string,
}
export type create_view_stmt = AstStatement<create_view_stmt>;
export type create_type_stmt = {
type: 'create',
keyword: 'type',
name: { schema: string; name: string },
as?: string,
resource?: string,
create_definitions?: any
}
export type create_type_stmt = AstStatement<create_type_stmt>;
export type create_domain_stmt = {
type: 'create',
keyword: 'domain',
domain: { schema: string; name: string },
as?: string,
target: data_type,
create_definitions?: any[]
}
export type create_domain_stmt = AstStatement<create_domain_stmt>;
export type create_table_stmt_node = create_table_stmt_node_simple | create_table_stmt_node_like;

@@ -74,3 +117,3 @@ export interface create_table_stmt_node_base {

query_expr?: union_stmt_node;
create_definition?: create_table_definition;
create_definitions?: create_table_definition;
table_options?: table_options;

@@ -91,3 +134,3 @@ }

table: table_ref_list,
create_definition?: create_sequence_definition_list
create_definitions?: create_sequence_definition_list
}

@@ -230,2 +273,26 @@

export type aggregate_signature = { name: ”*“ } | alter_func_args;
export type alter_func_argmode = ignore;
export type alter_func_arg_item = { mode?: string; name?: string; type: data_type; };
export type alter_func_args = alter_func_arg_item[];
export type alter_aggregate_stmt = AstStatement<alter_resource_stmt_node>;
export type alter_function_stmt = AstStatement<alter_resource_stmt_node>;
export interface alter_resource_stmt_node {
type: 'alter';
keyword: 'domain' | 'type',
name: string | { schema: string, name: string };
args?: { parentheses: true; expr?: alter_func_args; orderby?: alter_func_args; };
expr: alter_rename_owner;
}
export type alter_domain_type_stmt = AstStatement<alter_resource_stmt_node>;
export type alter_schema_stmt = AstStatement<alter_resource_stmt_node>;
export interface alter_table_stmt_node {

@@ -241,3 +308,3 @@ type: 'alter';

export type alter_action = ALTER_ADD_COLUMN | ALTER_ADD_CONSTRAINT | ALTER_DROP_COLUMN | ALTER_ADD_INDEX_OR_KEY | ALTER_ADD_FULLETXT_SPARITAL_INDEX | ALTER_RENAME_TABLE | ALTER_ALGORITHM | ALTER_LOCK;
export type alter_action = ALTER_ADD_COLUMN | ALTER_ADD_CONSTRAINT | ALTER_DROP_COLUMN | ALTER_ADD_INDEX_OR_KEY | ALTER_ADD_FULLETXT_SPARITAL_INDEX | ALTER_RENAME | ALTER_ALGORITHM | ALTER_LOCK;

@@ -279,14 +346,18 @@

export interface alter_rename_owner {
action: string;
type: 'alter';
resource: string;
keyword?: 'to' | 'as';
[key: string]: ident;
}
export type ALTER_RENAME = AstStatement<alter_rename>;
export type ALTER_RENAME_TABLE = {
action: 'rename';
type: 'alter';
resource: 'table';
keyword?: 'to' | 'as';
table: ident;
};
export type ALTER_OWNER_TO = AstStatement<alter_rename_owner>;
export type ALTER_SET_SCHEMA = AstStatement<alter_rename_owner>;
export type ALTER_ALGORITHM = {

@@ -331,3 +402,3 @@ type: 'alter';

export type create_constraint_definition = create_constraint_primary | create_constraint_unique | create_constraint_foreign;
export type create_constraint_definition = create_constraint_primary | create_constraint_unique | create_constraint_foreign | create_constraint_check;

@@ -338,2 +409,12 @@ export type constraint_name = { keyword: 'constraint'; constraint: ident; };

export type create_constraint_check = {
constraint?: constraint_name['constraint'];
definition: or_and_where_expr;
keyword?: constraint_name['keyword'];
constraint_type: 'check';
resource: 'constraint';
};
export type create_constraint_primary = {

@@ -343,2 +424,3 @@ constraint?: constraint_name['constraint'];

constraint_type: 'primary key';
keyword?: constraint_name['keyword'];
index_type?: index_type;

@@ -355,2 +437,3 @@ resource: 'constraint';

constraint_type: 'unique key' | 'unique' | 'unique index';
keyword?: constraint_name['keyword'];
index_type?: index_type;

@@ -389,3 +472,3 @@ resource: 'constraint';

export type reference_option = 'restrict' | 'cascade' | 'set null' | 'no action' | 'set default';
export type reference_option = { type: 'function'; name: string; args: expr_list; } | 'restrict' | 'cascade' | 'set null' | 'no action' | 'set default' | 'current_timestamp';

@@ -501,3 +584,3 @@

export type with_clause = cte_definition[] | [cte_definition & {recursive: true; }];
export type with_clause = cte_definition[] | [cte_definition & { recursive: true; }];

@@ -844,4 +927,6 @@ export type cte_definition = { name: { type: 'default'; value: string; }; stmt: crud_stmt; columns?: cte_column_definition; };

export type primary = cast_expr | literal | aggr_func | window_func | func_call | case_expr | interval_expr | column_ref | param | or_and_where_expr | var_decl | { type: 'origin'; value: string; };
export type column_ref_array_index = column_ref;
export type primary = cast_expr | or_and_where_expr | var_decl | { type: 'origin'; value: string; };
export type string_constants_escape = { type: 'origin'; value: string; };

@@ -962,3 +1047,3 @@

export type func_call = trim_func_clause | { type: 'function'; name: string; args: expr_list; suffix: literal_string; } | { type: 'function'; name: string; args: expr_list; over?: over_partition; } | { type: 'function'; name: string; args: expr_list; } | extract_func | { type: 'function'; name: string; over?: on_update_current_timestamp; };
export type func_call = trim_func_clause | { type: 'function'; name: string; args: expr_list; suffix: literal_string; } | { type: 'function'; name: string; args: expr_list; over?: over_partition; } | extract_func | { type: 'function'; name: string; over?: on_update_current_timestamp; } | { type: 'function'; name: string; args: expr_list; };

@@ -975,14 +1060,20 @@ export type extract_filed = 'string';

export type cast_double_colon = {
as?: alias_clause,
symbol: '::' | 'as',
target: data_type;
arrows?: ('->>' | '->')[];
property?: (literal_string | literal_numeric)[];
};
export type cast_expr = {
as?: alias_clause,
type: 'cast';
expr: literal | aggr_func | func_call | case_expr | interval_expr | column_ref | param
| expr;
symbol: '::' | 'as',
keyword: 'cast';
target: data_type;
arrows?: ('->>' | '->')[];
property?: (literal_string | literal_numeric)[];
...cast_double_colon;
};

@@ -1101,3 +1192,3 @@

type KW_SCHEME = never;
type KW_SCHEMA = never;

@@ -1342,2 +1433,4 @@ type KW_SEQUENCE = never;

type KW_VIEW = never;
type KW_VAR__PRE_AT = never;

@@ -1344,0 +1437,0 @@

167

lib/alter.js

@@ -22,48 +22,2 @@ (function (global, factory) {

function alterTableToSQL(stmt) {
const {
type,
table,
expr = []
} = stmt;
const action = (0, _util.toUpper)(type);
const tableName = (0, _tables.tablesToSQL)(table);
const exprList = expr.map(_expr.exprToSQL);
const result = [action, 'TABLE', tableName, exprList.join(', ')];
return result.filter(_util.hasVal).join(' ');
}
function alterViewToSQL(stmt) {
const {
type,
columns,
attributes,
select,
view,
with: withExpr
} = stmt;
const action = (0, _util.toUpper)(type);
const viewName = (0, _tables.tableToSQL)(view);
const result = [action, 'VIEW', viewName];
if (columns) result.push(`(${columns.map(_column.columnRefToSQL).join(', ')})`);
if (attributes) result.push(`WITH ${attributes.map(_util.toUpper).join(', ')}`);
result.push('AS', (0, _select.selectToSQL)(select));
if (withExpr) result.push((0, _util.toUpper)(withExpr));
return result.filter(_util.hasVal).join(' ');
}
function alterToSQL(stmt) {
const {
keyword = 'table'
} = stmt;
switch (keyword) {
case 'table':
return alterTableToSQL(stmt);
case 'view':
return alterViewToSQL(stmt);
}
}
function alterExprToSQL(expr) {

@@ -96,5 +50,13 @@ if (!expr) return '';

case 'table':
case 'schema':
name = (0, _util.identifierToSql)(expr[resource]);
break;
case 'aggregate':
case 'function':
case 'domain':
case 'type':
name = (0, _util.identifierToSql)(expr[resource]);
break;
case 'algorithm':

@@ -123,2 +85,115 @@ case 'lock':

}
function alterTableToSQL(stmt) {
const {
type,
table,
expr = []
} = stmt;
const action = (0, _util.toUpper)(type);
const tableName = (0, _tables.tablesToSQL)(table);
const exprList = expr.map(_expr.exprToSQL);
const result = [action, 'TABLE', tableName, exprList.join(', ')];
return result.filter(_util.hasVal).join(' ');
}
function alterViewToSQL(stmt) {
const {
type,
columns,
attributes,
select,
view,
with: withExpr
} = stmt;
const action = (0, _util.toUpper)(type);
const viewName = (0, _tables.tableToSQL)(view);
const result = [action, 'VIEW', viewName];
if (columns) result.push(`(${columns.map(_column.columnRefToSQL).join(', ')})`);
if (attributes) result.push(`WITH ${attributes.map(_util.toUpper).join(', ')}`);
result.push('AS', (0, _select.selectToSQL)(select));
if (withExpr) result.push((0, _util.toUpper)(withExpr));
return result.filter(_util.hasVal).join(' ');
}
function alterArgsToSQL(arg) {
return [(0, _util.toUpper)(arg.mode), arg.name, (0, _util.dataTypeToSQL)(arg.type)].filter(_util.hasVal).join(' ');
}
function alterSchemaToSQL(stmt) {
const {
expr,
keyword,
schema,
type
} = stmt;
const result = [(0, _util.toUpper)(type), (0, _util.toUpper)(keyword), (0, _util.identifierToSql)(schema), alterExprToSQL(expr)];
return result.filter(_util.hasVal).join(' ');
}
function alterDomainTypeToSQL(stmt) {
const {
expr,
keyword,
name,
type
} = stmt;
const result = [(0, _util.toUpper)(type), (0, _util.toUpper)(keyword), [(0, _util.identifierToSql)(name.schema), (0, _util.identifierToSql)(name.name)].filter(_util.hasVal).join('.'), alterExprToSQL(expr)];
return result.filter(_util.hasVal).join(' ');
}
function alterFunctionToSQL(stmt) {
const {
args,
expr,
keyword,
name,
type
} = stmt;
const result = [(0, _util.toUpper)(type), (0, _util.toUpper)(keyword), [[(0, _util.identifierToSql)(name.schema), (0, _util.identifierToSql)(name.name)].filter(_util.hasVal).join('.'), args && `(${args.expr ? args.expr.map(alterArgsToSQL).join(', ') : ''})`].filter(_util.hasVal).join(''), alterExprToSQL(expr)];
return result.filter(_util.hasVal).join(' ');
}
function alterAggregateToSQL(stmt) {
const {
args,
expr,
keyword,
name,
type
} = stmt;
const {
expr: argsExpr,
orderby
} = args;
const result = [(0, _util.toUpper)(type), (0, _util.toUpper)(keyword), [[(0, _util.identifierToSql)(name.schema), (0, _util.identifierToSql)(name.name)].filter(_util.hasVal).join('.'), `(${argsExpr.map(alterArgsToSQL).join(', ')}${orderby ? [' ORDER', 'BY', orderby.map(alterArgsToSQL).join(', ')].join(' ') : ''})`].filter(_util.hasVal).join(''), alterExprToSQL(expr)];
return result.filter(_util.hasVal).join(' ');
}
function alterToSQL(stmt) {
const {
keyword = 'table'
} = stmt;
switch (keyword) {
case 'aggregate':
return alterAggregateToSQL(stmt);
case 'table':
return alterTableToSQL(stmt);
case 'schema':
return alterSchemaToSQL(stmt);
case 'domain':
case 'type':
return alterDomainTypeToSQL(stmt);
case 'function':
return alterFunctionToSQL(stmt);
case 'view':
return alterViewToSQL(stmt);
}
}
});

@@ -101,3 +101,3 @@ (function (global, factory) {

reference.push((0, _util.toUpper)(match));
onAction.map(onRef => reference.push(...(0, _util.commonTypeValue)(onRef)));
onAction.map(onRef => reference.push((0, _util.toUpper)(onRef.type), (0, _expr.exprToSQL)(onRef.value)));
return reference.filter(_util.hasVal);

@@ -104,0 +104,0 @@ }

@@ -219,8 +219,11 @@ (function (global, factory) {

keyword,
recursive,
replace,
select,
sql_security: sqlSecurity,
temporary,
type,
view,
with: withClause
with: withClause,
with_options: withOptions
} = stmt;

@@ -232,6 +235,70 @@ const {

const viewName = [(0, _util.identifierToSql)(db), (0, _util.identifierToSql)(name)].filter(_util.hasVal).join('.');
const sql = [(0, _util.toUpper)(type), (0, _util.toUpper)(replace), algorithm && `ALGORITHM = ${(0, _util.toUpper)(algorithm)}`, definer, sqlSecurity && `SQL SECURITY ${(0, _util.toUpper)(sqlSecurity)}`, (0, _util.toUpper)(keyword), viewName, columns && `(${columns.map(_util.columnIdentifierToSql).join(', ')})`, 'AS', (0, _union.unionToSQL)(select), (0, _util.toUpper)(withClause)];
const sql = [(0, _util.toUpper)(type), (0, _util.toUpper)(replace), (0, _util.toUpper)(temporary), (0, _util.toUpper)(recursive), algorithm && `ALGORITHM = ${(0, _util.toUpper)(algorithm)}`, definer, sqlSecurity && `SQL SECURITY ${(0, _util.toUpper)(sqlSecurity)}`, (0, _util.toUpper)(keyword), viewName, columns && `(${columns.map(_util.columnIdentifierToSql).join(', ')})`, withOptions && ['WITH', `(${withOptions.map(withOpt => (0, _util.commonTypeValue)(withOpt).join(' ')).join(', ')})`].join(' '), 'AS', (0, _union.unionToSQL)(select), (0, _util.toUpper)(withClause)];
return sql.filter(_util.hasVal).join(' ');
}
function createDomainToSQL(stmt) {
const {
as,
domain,
type,
keyword,
target,
create_definitions: createDefinition
} = stmt;
const sql = [(0, _util.toUpper)(type), (0, _util.toUpper)(keyword), [(0, _util.identifierToSql)(domain.schema), (0, _util.identifierToSql)(domain.name)].filter(_util.hasVal).join('.'), (0, _util.toUpper)(as), (0, _util.dataTypeToSQL)(target)];
if (createDefinition && createDefinition.length > 0) {
const definitionSQL = [];
for (const definition of createDefinition) {
const definitionType = definition.type;
switch (definitionType) {
case 'collate':
definitionSQL.push((0, _util.commonTypeValue)(definition).join(' '));
break;
case 'default':
definitionSQL.push((0, _util.toUpper)(definitionType), (0, _expr.exprToSQL)(definition.value));
break;
case 'constraint':
definitionSQL.push((0, _constrain.constraintDefinitionToSQL)(definition));
break;
}
}
sql.push(definitionSQL.filter(_util.hasVal).join(' '));
}
return sql.filter(_util.hasVal).join(' ');
}
function createTypeToSQL(stmt) {
const {
as,
create_definitions: createDefinition,
keyword,
name,
resource,
type
} = stmt;
const sql = [(0, _util.toUpper)(type), (0, _util.toUpper)(keyword), [(0, _util.identifierToSql)(name.schema), (0, _util.identifierToSql)(name.name)].filter(_util.hasVal).join('.'), (0, _util.toUpper)(as), (0, _util.toUpper)(resource)];
if (createDefinition) {
const definitionSQL = [];
switch (resource) {
case 'enum':
definitionSQL.push((0, _expr.exprToSQL)(createDefinition));
break;
}
sql.push(definitionSQL.filter(_util.hasVal).join(' '));
}
return sql.filter(_util.hasVal).join(' ');
}
function createToSQL(stmt) {

@@ -272,2 +339,10 @@ const {

case 'domain':
sql = createDomainToSQL(stmt);
break;
case 'type':
sql = createTypeToSQL(stmt);
break;
default:

@@ -274,0 +349,0 @@ throw new Error(`unknown create resource ${keyword}`);

(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "./binary", "./column", "./expr", "./insert", "./util"], factory);
define(["exports", "./binary", "./column", "./expr", "./insert", "./interval", "./util"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("./binary"), require("./column"), require("./expr"), require("./insert"), require("./util"));
factory(exports, require("./binary"), require("./column"), require("./expr"), require("./insert"), require("./interval"), require("./util"));
} else {

@@ -10,6 +10,6 @@ var mod = {

};
factory(mod.exports, global.binary, global.column, global.expr, global.insert, global.util);
factory(mod.exports, global.binary, global.column, global.expr, global.insert, global.interval, global.util);
global.tables = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _binary, _column, _expr, _insert, _util) {
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _binary, _column, _expr, _insert, _interval, _util) {
"use strict";

@@ -24,2 +24,3 @@

_exports.tableToSQL = tableToSQL;
_exports.tableTumbleToSQL = tableTumbleToSQL;
_exports.tablesToSQL = tablesToSQL;

@@ -101,2 +102,14 @@ _exports.unnestToSQL = unnestToSQL;

function tableTumbleToSQL(tumble) {
if (!tumble) return '';
const {
data: tableInfo,
timecol,
size
} = tumble;
const fullTableName = [(0, _util.identifierToSql)(tableInfo.db), (0, _util.identifierToSql)(tableInfo.table)].filter(_util.hasVal).join('.');
const result = ['TABLE(TUMBLE(TABLE', fullTableName, `DESCRIPTOR(${(0, _column.columnRefToSQL)(timecol)})`, `${(0, _interval.intervalToSQL)(size)}))`];
return result.filter(_util.hasVal).join(' ');
}
function tableToSQL(tableInfo) {

@@ -119,16 +132,28 @@ if ((0, _util.toUpper)(tableInfo.type) === 'UNNEST') return unnestToSQL(tableInfo);

if (expr && expr.type === 'values') {
const {
parentheses,
values,
prefix
} = expr;
const valueSQL = [parentheses && '(', '', parentheses && ')'];
let valuesExpr = (0, _insert.valuesToSQL)(values);
if (prefix) valuesExpr = valuesExpr.split('(').slice(1).map(val => `${(0, _util.toUpper)(prefix)}(${val}`).join('');
valueSQL[1] = `VALUES ${valuesExpr}`;
tableName = valueSQL.filter(_util.hasVal).join('');
if (expr) {
const exprType = expr.type;
switch (exprType) {
case 'values':
const {
parentheses,
values,
prefix
} = expr;
const valueSQL = [parentheses && '(', '', parentheses && ')'];
let valuesExpr = (0, _insert.valuesToSQL)(values);
if (prefix) valuesExpr = valuesExpr.split('(').slice(1).map(val => `${(0, _util.toUpper)(prefix)}(${val}`).join('');
valueSQL[1] = `VALUES ${valuesExpr}`;
tableName = valueSQL.filter(_util.hasVal).join('');
break;
case 'tumble':
tableName = tableTumbleToSQL(expr);
break;
default:
tableName = (0, _expr.exprToSQL)(expr);
}
}
if (expr && expr.type !== 'values') tableName = (0, _expr.exprToSQL)(expr);
tableName = [(0, _util.toUpper)(prefixStr), tableName].filter(_util.hasVal).join(' ');

@@ -135,0 +160,0 @@ let str = [database, schemaStr, tableName].filter(_util.hasVal).join('.');

{
"name": "node-sql-parser",
"version": "4.8.0",
"version": "4.9.0",
"description": "simple node sql parser",

@@ -5,0 +5,0 @@ "main": "index.js",

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc