sql-parser-cst
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -68,6 +68,3 @@ "use strict"; | ||
// FROM | ||
from_clause: (node) => [ | ||
line(layout(node.fromKw)), | ||
indent(layout(node.tables)), | ||
], | ||
from_clause: (node) => [line(layout(node.fromKw)), indent(layout(node.expr))], | ||
// Expressions | ||
@@ -74,0 +71,0 @@ binary_expr: ({ left, operator, right }) => layout([left, operator, right]), |
@@ -44,4 +44,4 @@ "use strict"; | ||
// FROM | ||
from_clause: (node) => show([node.fromKw, node.tables]), | ||
join: (node) => show([node.operator, node.table, node.specification]), | ||
from_clause: (node) => show([node.fromKw, node.expr]), | ||
join_expr: (node) => show([node.left, node.operator, node.right, node.specification]), | ||
join_on_specification: (node) => show([node.onKw, node.expr]), | ||
@@ -74,2 +74,3 @@ join_using_specification: (node) => show([node.usingKw, node.expr]), | ||
values_clause: (node) => show([node.valuesKw, node.values]), | ||
row_constructor: (node) => show([node.rowKw, node.row]), | ||
// Window frame | ||
@@ -83,2 +84,4 @@ frame_clause: (node) => show([node.unitKw, node.extent, node.exclusion]), | ||
frame_exclusion: (node) => show([node.excludeKw, node.kindKw]), | ||
// returning clause | ||
returning_clause: (node) => show([node.returningKw, node.columns]), | ||
// CREATE TABLE statement | ||
@@ -93,3 +96,5 @@ create_table_stmt: (node) => show([ | ||
node.options, | ||
node.as, | ||
]), | ||
create_table_as: (node) => show([node.asKw, node.expr]), | ||
column_definition: (node) => show([ | ||
@@ -142,24 +147,32 @@ node.name, | ||
// INSERT INTO statement | ||
insert_stmt: (node) => show([ | ||
insert_stmt: (node) => show(node.clauses), | ||
insert_clause: (node) => show([ | ||
node.insertKw, | ||
node.options, | ||
node.orAction, | ||
node.intoKw, | ||
node.table, | ||
node.columns, | ||
node.source, | ||
]), | ||
insert_option: (node) => show(node.kw), | ||
upsert_option: (node) => show(node.kw), | ||
or_alternate_action: (node) => show([node.orKw, node.actionKw]), | ||
default_values: (node) => show(node.kw), | ||
default: (node) => show(node.kw), | ||
upsert_clause: (node) => show([node.onConflictKw, node.columns, node.where, node.doKw, node.action]), | ||
upsert_action_nothing: (node) => show(node.nothingKw), | ||
upsert_action_update: (node) => show([node.updateKw, node.set, node.where]), | ||
// UPDATE statement | ||
update_stmt: (node) => show([ | ||
node.updateKw, | ||
node.tables, | ||
node.setKw, | ||
node.assignments, | ||
update_stmt: (node) => show(node.clauses), | ||
update_clause: (node) => show([node.updateKw, node.options, node.orAction, node.tables]), | ||
set_clause: (node) => show([node.setKw, node.assignments]), | ||
column_assignment: (node) => show([node.column, "=", node.expr]), | ||
// DELETE FROM statement | ||
delete_stmt: (node) => show([ | ||
node.with, | ||
node.deleteKw, | ||
node.fromKw, | ||
node.table, | ||
node.where, | ||
node.returning, | ||
]), | ||
column_assignment: (node) => show([node.column, "=", node.expr]), | ||
// DELETE FROM statement | ||
delete_stmt: (node) => show([node.deleteKw, node.fromKw, node.table, node.where]), | ||
// CREATE VIEW statement | ||
@@ -250,5 +263,7 @@ create_view_stmt: (node) => show([ | ||
func_call: (node) => show([node.name, node.args, node.filter, node.over]), | ||
table_func_call: (node) => show([node.name, node.args]), | ||
distinct_arg: (node) => show([node.distinctKw, node.value]), | ||
cast_expr: (node) => show([node.castKw, node.args]), | ||
cast_arg: (node) => show([node.expr, node.asKw, node.dataType]), | ||
raise_expr: (node) => show([node.raiseKw, node.args]), | ||
filter_arg: (node) => show([node.filterKw, node.where]), | ||
@@ -269,2 +284,4 @@ over_arg: (node) => show([node.overKw, node.window]), | ||
alias: (node) => show([node.expr, node.asKw, node.alias]), | ||
indexed_table_ref: (node) => show([node.table, node.indexedByKw, node.index]), | ||
not_indexed_table_ref: (node) => show([node.table, node.notIndexedKw]), | ||
all_columns: () => "*", | ||
@@ -278,3 +295,4 @@ // Basic language elements | ||
null: (node) => node.text, | ||
parameter: (node) => node.text, | ||
// Cast to FullTransformMap, so TypeScript ensures all node types are covered | ||
}); |
201
lib/sql.d.ts
@@ -18,3 +18,3 @@ type BaseNode = { | ||
| Keyword | ||
| Join | ||
| JoinExpr | ||
| JoinOnSpecification | ||
@@ -24,2 +24,3 @@ | JoinUsingSpecification | ||
| ColumnDefinition | ||
| CreateTableAs | ||
| Constraint<ColumnConstraint | TableConstraint> | ||
@@ -52,7 +53,13 @@ | ColumnConstraint | ||
| CaseElse | ||
| RowConstructor | ||
| DefaultValues | ||
| Default | ||
| InsertOption | ||
| UpsertOption | ||
| OrAlternateAction | ||
| ColumnAssignment | ||
| UpsertActionNothing | ||
| UpsertActionUpdate | ||
| Alias | ||
| IndexedTableRef | ||
| NotIndexedTableRef | ||
| PragmaAssignment | ||
@@ -94,3 +101,5 @@ | PragmaFuncCall; | ||
| FuncCall | ||
| TableFuncCall | ||
| CastExpr | ||
| RaiseExpr | ||
| BetweenExpr | ||
@@ -103,3 +112,4 @@ | CaseExpr | ||
| TableRef | ||
| Identifier; | ||
| Identifier | ||
| Parameter; | ||
@@ -130,3 +140,13 @@ type Literal = | ||
type: "select_stmt"; | ||
clauses: Clause[]; | ||
clauses: ( | ||
| WithClause | ||
| SelectClause | ||
| FromClause | ||
| WhereClause | ||
| GroupByClause | ||
| HavingClause | ||
| WindowClause | ||
| OrderByClause | ||
| LimitClause | ||
)[]; | ||
}; | ||
@@ -143,5 +163,10 @@ | ||
| OrderByClause | ||
| PartitionByClause | ||
| PartitionByClause // in window definitions | ||
| LimitClause | ||
| ValuesClause; | ||
| InsertClause | ||
| ValuesClause | ||
| UpdateClause // in UPDATE statement | ||
| SetClause // in UPDATE statement | ||
| UpsertClause // in INSERT statement | ||
| ReturningClause; // in UPDATE,INSERT,DELETE | ||
@@ -174,3 +199,3 @@ type WithClause = BaseNode & { | ||
fromKw: Keyword; | ||
tables: (JoinTable | Alias<JoinTable> | Join)[]; | ||
expr: TableOrSubquery | JoinExpr; | ||
}; | ||
@@ -220,3 +245,3 @@ | ||
orderByKw: Keyword[]; | ||
specifications: ExprList<SortSpecification>; | ||
specifications: ExprList<SortSpecification | ColumnRef>; | ||
withRollupKw?: Keyword[]; // WITH ROLLUP | ||
@@ -239,11 +264,31 @@ }; | ||
type Join = BaseNode & { | ||
type: "join"; | ||
type JoinExpr = BaseNode & { | ||
type: "join_expr"; | ||
left: JoinExpr | TableOrSubquery; | ||
operator: Keyword[] | ","; | ||
table: JoinTable | Alias<JoinTable>; | ||
right: TableOrSubquery; | ||
specification?: JoinOnSpecification | JoinUsingSpecification; | ||
}; | ||
type JoinTable = TableRef | ParenExpr<TableRef | SubSelect>; | ||
type TableOrSubquery = | ||
| TableRef | ||
| TableFuncCall | ||
| IndexedTableRef | ||
| NotIndexedTableRef | ||
| ParenExpr<SubSelect | TableOrSubquery | JoinExpr> | ||
| Alias<TableOrSubquery>; | ||
// SQLite only | ||
type IndexedTableRef = BaseNode & { | ||
type: "indexed_table_ref"; | ||
table: TableRef | Alias<TableRef>; | ||
indexedByKw: Keyword[]; // INDEXED BY | ||
index: Identifier; | ||
}; | ||
type NotIndexedTableRef = BaseNode & { | ||
type: "not_indexed_table_ref"; | ||
table: TableRef | Alias<TableRef>; | ||
notIndexedKw: Keyword[]; // NOT INDEXED | ||
}; | ||
type JoinOnSpecification = BaseNode & { | ||
@@ -268,2 +313,8 @@ type: "join_on_specification"; | ||
type ReturningClause = BaseNode & { | ||
type: "returning_clause"; | ||
returningKw: Keyword; // RETURNING | ||
columns: ExprList<Expr | Alias<Expr>>; | ||
}; | ||
// CREATE TABLE | ||
@@ -277,12 +328,19 @@ type CreateTableStmt = BaseNode & { | ||
table: TableRef; | ||
columns: ParenExpr< | ||
columns?: ParenExpr< | ||
ExprList<ColumnDefinition | TableConstraint | Constraint<TableConstraint>> | ||
>; | ||
options?: ExprList<TableOption>; | ||
as?: CreateTableAs; | ||
}; | ||
type CreateTableAs = BaseNode & { | ||
type: "create_table_as"; | ||
asKw: Keyword; // AS | ||
expr: SubSelect; | ||
}; | ||
type ColumnDefinition = BaseNode & { | ||
type: "column_definition"; | ||
name: ColumnRef; | ||
dataType: DataType; | ||
dataType?: DataType; | ||
constraints: (ColumnConstraint | Constraint<ColumnConstraint>)[]; | ||
@@ -344,3 +402,3 @@ }; | ||
primaryKeyKw: Keyword[]; | ||
columns?: ParenExpr<ExprList<ColumnRef>>; | ||
columns?: ParenExpr<ExprList<SortSpecification | ColumnRef>>; | ||
onConflict?: OnConflictClause; | ||
@@ -531,21 +589,47 @@ }; | ||
type: "insert_stmt"; | ||
clauses: ( | ||
| WithClause | ||
| InsertClause | ||
| (ValuesClause | SubSelect | DefaultValues) | ||
| UpsertClause | ||
| ReturningClause | ||
)[]; | ||
}; | ||
type InsertClause = BaseNode & { | ||
type: "insert_clause"; | ||
insertKw: Keyword; // INSERT | REPLACE | ||
options: InsertOption[]; | ||
intoKw?: Keyword; | ||
options: UpsertOption[]; | ||
orAction?: OrAlternateAction; | ||
intoKw?: Keyword; // INTO | ||
table: TableRef | Alias<TableRef>; | ||
columns?: ParenExpr<ExprList<ColumnRef>>; | ||
source: ValuesClause | SubSelect | DefaultValues; | ||
}; | ||
type InsertOption = BaseNode & { | ||
type: "insert_option"; | ||
kw: Keyword | Keyword[]; | ||
// Only in MySQL INSERT & UPDATE clauses | ||
type UpsertOption = BaseNode & { | ||
type: "upsert_option"; | ||
kw: Keyword; // LOW_PRIORITY | DELAYED | HIGH_PRIORITY | IGNORE | ||
}; | ||
// Only in SQLite | ||
type OrAlternateAction = BaseNode & { | ||
type: "or_alternate_action"; | ||
orKw: Keyword; // OR | ||
actionKw: Keyword; // ABORT | FAIL | IGNORE | REPLACE | ROLLBACK | ||
}; | ||
type ValuesClause = BaseNode & { | ||
type: "values_clause"; | ||
valuesKw: Keyword; // VALUES | VALUE | ||
values: ExprList<ParenExpr<ExprList<Expr | Default>>>; | ||
values: ExprList<ParenExpr<ExprList<Expr | Default>> | RowConstructor>; | ||
}; | ||
// only in MySQL | ||
type RowConstructor = BaseNode & { | ||
type: "row_constructor"; | ||
rowKw: Keyword; // ROW | ||
row: ParenExpr<ExprList<Expr | Default>>; | ||
}; | ||
type DefaultValues = BaseNode & { | ||
@@ -561,5 +645,28 @@ type: "default_values"; | ||
// only in SQLite | ||
type UpsertClause = BaseNode & { | ||
type: "upsert_clause"; | ||
onConflictKw: Keyword[]; // ON CONFLICT | ||
columns?: ParenExpr<ExprList<SortSpecification | ColumnRef>>; | ||
where?: WhereClause; | ||
doKw: Keyword; // DO | ||
action: UpsertActionNothing | UpsertActionUpdate; | ||
}; | ||
type UpsertActionNothing = BaseNode & { | ||
type: "upsert_action_nothing"; | ||
nothingKw: Keyword; // NOTHING | ||
}; | ||
type UpsertActionUpdate = BaseNode & { | ||
type: "upsert_action_update"; | ||
updateKw: Keyword; // UPDATE | ||
set: SetClause; | ||
where?: WhereClause; | ||
}; | ||
// DELETE FROM | ||
type DeleteStmt = BaseNode & { | ||
type: "delete_stmt"; | ||
with?: WithClause; | ||
deleteKw: Keyword; | ||
@@ -569,2 +676,3 @@ fromKw: Keyword; | ||
where?: WhereClause; | ||
returning?: ReturningClause; | ||
}; | ||
@@ -575,7 +683,26 @@ | ||
type: "update_stmt"; | ||
clauses: ( | ||
| WithClause | ||
| UpdateClause | ||
| SetClause | ||
| WhereClause | ||
| FromClause | ||
| OrderByClause | ||
| LimitClause | ||
| ReturningClause | ||
)[]; | ||
}; | ||
type UpdateClause = BaseNode & { | ||
type: "update_clause"; | ||
updateKw: Keyword; | ||
tables: ExprList<TableRef>; | ||
options: UpsertOption[]; | ||
orAction?: OrAlternateAction; | ||
tables: ExprList<TableRef | Alias<TableRef>>; | ||
}; | ||
type SetClause = BaseNode & { | ||
type: "set_clause"; | ||
setKw: Keyword; | ||
assignments: ExprList<ColumnAssignment>; | ||
where?: WhereClause; | ||
}; | ||
@@ -585,3 +712,3 @@ | ||
type: "column_assignment"; | ||
column: ColumnRef; | ||
column: ColumnRef | ParenExpr<ExprList<ColumnRef>>; | ||
expr: Expr | Default; | ||
@@ -622,3 +749,3 @@ }; | ||
table: TableRef; | ||
columns: ParenExpr<ExprList<ColumnRef>>; | ||
columns: ParenExpr<ExprList<SortSpecification | ColumnRef>>; | ||
where?: WhereClause; | ||
@@ -867,3 +994,3 @@ }; | ||
interface Alias<T = Expr> extends BaseNode { | ||
interface Alias<T = Node> extends BaseNode { | ||
type: "alias"; | ||
@@ -916,2 +1043,8 @@ expr: T; | ||
type TableFuncCall = BaseNode & { | ||
type: "table_func_call"; | ||
name: TableRef; | ||
args: ParenExpr<ExprList<Expr>>; | ||
}; | ||
type FilterArg = BaseNode & { | ||
@@ -948,2 +1081,8 @@ type: "filter_arg"; | ||
type RaiseExpr = BaseNode & { | ||
type: "raise_expr"; | ||
raiseKw: Keyword; // RAISE | ||
args: ParenExpr<ExprList<Keyword | StringLiteral>>; | ||
}; | ||
type BetweenExpr = BaseNode & { | ||
@@ -1041,2 +1180,9 @@ type: "between_expr"; | ||
type Parameter = BaseNode & { | ||
type: "parameter"; | ||
text: string; | ||
}; | ||
type ParamType = "?" | "?nr" | ":name" | "$name" | "@name"; | ||
export type ParserOptions = { | ||
@@ -1047,4 +1193,5 @@ preserveComments?: boolean; | ||
includeRange?: boolean; | ||
paramTypes?: ParamType[]; | ||
}; | ||
export function parse(str: string, options?: ParserOptions): Program; |
@@ -5,3 +5,3 @@ { | ||
"license": "GPL-2.0-or-later", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"main": "lib/parser.js", | ||
@@ -25,10 +25,14 @@ "types": "lib/parser.d.ts", | ||
"watch:generate": "npm-watch", | ||
"copy": "cp -R src/keywords lib/keywords && cp -R src/dialects lib/dialects && cp src/sql.d.ts lib/sql.d.ts", | ||
"build": "yarn generate && tsc && yarn copy", | ||
"clean": "rm -rf lib; rm -f src/dialects/*.js", | ||
"copy": "cp -R src/keywords lib/ && cp -R src/dialects lib/ && cp src/sql.d.ts lib/", | ||
"build": "yarn clean && yarn generate && tsc && yarn copy", | ||
"test:mysql": "jest --config test/config/mysql.json", | ||
"test:sqlite": "jest --config test/config/sqlite.json", | ||
"test": "yarn generate && yarn test:mysql && yarn test:sqlite" | ||
"test": "yarn generate && yarn test:mysql && yarn test:sqlite", | ||
"perf": "yarn ts-node perf/perf-test.ts" | ||
}, | ||
"devDependencies": { | ||
"@types/benchmark": "^2.1.2", | ||
"@types/jest": "^29.1.2", | ||
"benchmark": "^2.1.4", | ||
"dedent-js": "^1.0.1", | ||
@@ -35,0 +39,0 @@ "jest": "^29.1.2", |
@@ -11,6 +11,8 @@ # SQL Parser CST [![npm version](https://img.shields.io/npm/v/sql-parser-cst)](https://www.npmjs.com/package/sql-parser-cst) ![example workflow](https://github.com/nene/sql-parser-cst/actions/workflows/build.yml/badge.svg) | ||
- **SQLite** - supports all SQLite statements (but some parts of syntax are still unsupported). | ||
- **SQLite** - full support. | ||
- **MySQL** - basic support for the most common SQL statements. | ||
**Note:** This is pre-alpha quality software in early development stages. | ||
**Note:** This software is still in very active development. | ||
The syntax tree structure is constantly evolving and changing | ||
even between patch versions. | ||
@@ -17,0 +19,0 @@ ## Usage |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
197
1617269
11
29
59674