pgsql-ast-parser
Advanced tools
Comparing version 11.2.0 to 12.0.0
@@ -15,2 +15,3 @@ import * as a from './syntax/ast'; | ||
createEnum?(val: a.CreateEnumType): a.Statement | nil; | ||
alterEnum?(val: a.AlterEnumType): a.Statement | nil; | ||
createCompositeType?(val: a.CreateCompositeType): a.Statement | nil; | ||
@@ -152,2 +153,3 @@ drop?: (val: a.DropStatement) => a.Statement | nil; | ||
createEnum(val: a.CreateEnumType): a.Statement | nil; | ||
alterEnum(val: a.AlterEnumType): a.Statement | nil; | ||
createCompositeType(val: a.CreateCompositeType): a.Statement | nil; | ||
@@ -154,0 +156,0 @@ drop(val: a.DropStatement): a.Statement | nil; |
{ | ||
"name": "pgsql-ast-parser", | ||
"version": "11.2.0", | ||
"version": "12.0.0", | ||
"description": "Yet another simple Postgres SQL parser/modifier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,2 +18,3 @@ import * as a from './syntax/ast'; | ||
createEnum?(val: a.CreateEnumType): a.Statement | nil | ||
alterEnum?(val: a.AlterEnumType): a.Statement | nil | ||
createCompositeType?(val: a.CreateCompositeType): a.Statement | nil | ||
@@ -270,2 +271,4 @@ drop?: (val: a.DropStatement) => a.Statement | nil | ||
return this.createEnum(val); | ||
case 'alter enum': | ||
return this.alterEnum(val); | ||
case 'create composite type': | ||
@@ -404,2 +407,6 @@ return this.createCompositeType(val); | ||
alterEnum(val: a.AlterEnumType): a.Statement | nil { | ||
return val; | ||
} | ||
createCompositeType(val: a.CreateCompositeType): a.Statement | nil { | ||
@@ -1246,2 +1253,4 @@ const attributes = arrayNilMap(val.attributes, a => assignChanged(a, { | ||
const filter = this.expr(val.filter); | ||
const withinGroupList = val.withinGroup ? [val.withinGroup] : undefined | ||
const withinGroup = this.orderBy(withinGroupList)?.[0]; | ||
return assignChanged(val, { | ||
@@ -1251,2 +1260,3 @@ args, | ||
filter, | ||
withinGroup, | ||
}); | ||
@@ -1253,0 +1263,0 @@ } |
@@ -38,2 +38,3 @@ // import { IType } from '../../interfaces'; | ||
| CreateCompositeType | ||
| AlterEnumType | ||
| TruncateTableStatement | ||
@@ -175,2 +176,23 @@ | DropStatement | ||
export interface AlterEnumType extends PGNode { | ||
type: 'alter enum', | ||
name: QName, | ||
change: EnumAlteration | ||
} | ||
export type EnumAlteration | ||
= EnumAlterationRename | ||
| EnumAlterationAddValue | ||
export interface EnumAlterationRename { | ||
type: 'rename'; | ||
to: QName; | ||
} | ||
export interface EnumAlterationAddValue { | ||
type: 'add value'; | ||
add: Literal; | ||
} | ||
export interface CompositeTypeAttribute extends PGNode { | ||
@@ -415,2 +437,3 @@ name: Name; | ||
ifNotExists?: true; | ||
concurrently?: true; | ||
indexName?: Name; | ||
@@ -699,2 +722,3 @@ tablespace?: string; | ||
join?: JoinClause | nil; | ||
lateral?: true; | ||
withOrdinality?: boolean; | ||
@@ -723,2 +747,3 @@ }; | ||
name: QNameMapped; | ||
lateral?: true; | ||
join?: JoinClause | nil; | ||
@@ -731,2 +756,3 @@ } | ||
alias: string; | ||
lateral?: true; | ||
columnNames?: Name[] | nil; | ||
@@ -908,2 +934,4 @@ db?: null | nil; | ||
filter?: Expr | nil; | ||
/** [AGGREGATION FUNCTIONS] WITHIN GROUP clause */ | ||
withinGroup?: OrderByStatement | nil; | ||
/** [AGGREGATION FUNCTIONS] OVER clause */ | ||
@@ -910,0 +938,0 @@ over?: CallOver | nil; |
@@ -218,2 +218,29 @@ import 'mocha'; | ||
}); | ||
checkCreateIndex(['create index concurrently blah on test(col)'], { | ||
type: 'create index', | ||
indexName: { name: 'blah' }, | ||
table: { name: 'test' }, | ||
concurrently: true, | ||
expressions: [{ | ||
expression: { type: 'ref', name: 'col' }, | ||
}], | ||
}); | ||
checkCreateIndex(['create index concurrently on test(col)'], { | ||
type: 'create index', | ||
table: { name: 'test', }, | ||
concurrently: true, | ||
expressions: [{ | ||
expression: { type: 'ref', name: 'col' }, | ||
}], | ||
}); | ||
checkCreateIndex(['create unique index concurrently on test(col)'], { | ||
type: 'create index', | ||
table: { name: 'test', }, | ||
concurrently: true, | ||
unique: true, | ||
expressions: [{ | ||
expression: { type: 'ref', name: 'col' }, | ||
}], | ||
}); | ||
}); |
import 'mocha'; | ||
import 'chai'; | ||
import { checkTreeExpr, checkInvalidExpr, checkInvalid, checkTreeExprLoc, starCol, star, col, ref, tbl, name } from './spec-utils'; | ||
import { checkTreeExpr, checkInvalidExpr, checkInvalid, checkTreeExprLoc, starCol, star, col, ref, tbl, name, numeric } from './spec-utils'; | ||
import { toSql } from '../to-sql'; | ||
@@ -1250,2 +1250,16 @@ import { expect } from 'chai'; | ||
checkTreeExprLoc([`left('foo')`], { | ||
_location: { start: 0, end: 11 }, | ||
type: 'call', | ||
function: { | ||
_location: { start: 0, end: 4 }, | ||
name: 'left' | ||
}, | ||
args: [{ | ||
_location: { start: 5, end: 10 }, | ||
type: 'string', | ||
value: 'foo' | ||
}], | ||
}); | ||
checkTreeExprLoc([`pg_catalog.col_description(23208,4)`], { | ||
@@ -1578,2 +1592,9 @@ _location: { start: 0, end: 35 }, | ||
}); | ||
checkTreeExpr(`PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY a)`, { | ||
type: 'call', | ||
function: { name: 'percentile_disc' }, | ||
args: [numeric(0.5)], | ||
withinGroup: { by: ref('a') } | ||
}); | ||
}) | ||
@@ -1580,0 +1601,0 @@ |
@@ -586,2 +586,119 @@ import 'mocha'; | ||
checkSelect(` | ||
select * from test | ||
inner join lateral ( | ||
select * from test2 | ||
where test2.foo = test1.bar | ||
) test2_inner on true | ||
`, { | ||
type: 'select', | ||
columns: [{ expr: star }], | ||
from: [tbl('test'), | ||
{ | ||
alias: "test2_inner", | ||
join: { | ||
on: { | ||
type: "boolean", | ||
value: true | ||
}, | ||
type: "INNER JOIN", | ||
}, | ||
statement: { | ||
columns: [ | ||
{ | ||
expr: { | ||
name: "*", | ||
type: "ref" | ||
} | ||
} | ||
], | ||
from: [ | ||
{ | ||
name: { | ||
name: "test2" | ||
}, | ||
type: "table" | ||
} | ||
], | ||
type: "select", | ||
where: { | ||
left: { | ||
name: "foo", | ||
table: { | ||
name: "test2" | ||
}, | ||
type: "ref" | ||
}, | ||
op: "=", | ||
right: { | ||
name: "bar", | ||
table: { | ||
name: "test1" | ||
}, | ||
type: "ref", | ||
}, | ||
type: "binary" | ||
} | ||
}, | ||
type: "statement", | ||
lateral: true, | ||
} | ||
] | ||
}); | ||
checkSelect(` | ||
SELECT m.name AS mname, pname | ||
FROM manufacturers m, LATERAL get_product_names(m.id) pname; | ||
`, { | ||
"columns": [ | ||
{ | ||
"expr": { | ||
"type": "ref", | ||
"table": { | ||
"name": "m" | ||
}, | ||
"name": "name" | ||
}, | ||
"alias": { | ||
"name": "mname" | ||
} | ||
}, | ||
{ | ||
"expr": { | ||
"type": "ref", | ||
"name": "pname" | ||
} | ||
} | ||
], | ||
"from": [ | ||
{ | ||
"type": "table", | ||
"name": { | ||
"name": "manufacturers", | ||
"alias": "m" | ||
} | ||
}, | ||
{ | ||
"type": "call", | ||
"function": { | ||
"name": "get_product_names" | ||
}, | ||
"args": [ | ||
{ | ||
"type": "ref", | ||
"table": { | ||
"name": "m" | ||
}, | ||
"name": "id" | ||
} | ||
], | ||
"lateral": true, | ||
"alias": { | ||
"name": "pname" | ||
} | ||
} | ||
], | ||
"type": "select" | ||
}); | ||
checkSelect(['select current_schema()'], { | ||
@@ -588,0 +705,0 @@ type: 'select', |
@@ -5,3 +5,3 @@ import { Parser, Grammar } from 'nearley'; | ||
import { trimNullish } from '../utils'; | ||
import { Expr, SelectStatement, CreateTableStatement, CreateIndexStatement, Statement, InsertStatement, UpdateStatement, AlterTableStatement, DeleteStatement, CreateExtensionStatement, CreateSequenceStatement, AlterSequenceStatement, SelectedColumn, Interval, BinaryOperator, ExprBinary, Name, ExprInteger, FromTable, QName, AlterIndexStatement } from './ast'; | ||
import { Expr, SelectStatement, CreateTableStatement, CreateIndexStatement, Statement, InsertStatement, UpdateStatement, AlterTableStatement, DeleteStatement, CreateExtensionStatement, CreateSequenceStatement, AlterSequenceStatement, SelectedColumn, Interval, BinaryOperator, ExprBinary, Name, ExprInteger, FromTable, QName, AlterIndexStatement, ExprNumeric } from './ast'; | ||
import { astMapper, IAstMapper } from '../ast-mapper'; | ||
@@ -332,1 +332,4 @@ import { toSql, IAstToSql } from '../to-sql'; | ||
} | ||
export function numeric(value: number): ExprNumeric { | ||
return { type: 'numeric', value }; | ||
} |
@@ -319,2 +319,13 @@ import { IAstPartialMapper, AstDefaultMapper } from './ast-mapper'; | ||
alterEnum: t => { | ||
ret.push('ALTER TYPE '); | ||
visitQualifiedName(t.name); | ||
if (t.change.type === 'rename') { | ||
ret.push(' RENAME TO '); | ||
visitQualifiedName(t.change.to); | ||
} else { | ||
ret.push(' ADD VALUE ', literal(t.change.add.value)); | ||
} | ||
}, | ||
createCompositeType: c => { | ||
@@ -494,2 +505,7 @@ ret.push('CREATE TYPE '); | ||
} | ||
if (v.withinGroup) { | ||
ret.push('WITHIN GROUP ('); | ||
visitOrderBy(m, [v.withinGroup]); | ||
ret.push(') '); | ||
} | ||
if (v.over) { | ||
@@ -885,2 +901,5 @@ ret.push('over ('); | ||
ret.push(c.unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX '); | ||
if (c.concurrently) { | ||
ret.push('CONCURRENTLY '); | ||
} | ||
if (c.ifNotExists) { | ||
@@ -1053,2 +1072,5 @@ ret.push(' IF NOT EXISTS '); | ||
join(m, s.join, () => { | ||
if (s.lateral) { | ||
ret.push("LATERAL ") | ||
} | ||
m.call(s); | ||
@@ -1081,2 +1103,5 @@ if (s.withOrdinality) { | ||
join(m, s.join, () => { | ||
if (s.lateral) { | ||
ret.push("LATERAL ") | ||
} | ||
ret.push('('); | ||
@@ -1083,0 +1108,0 @@ m.select(s.statement); |
import { nil } from '../utils'; | ||
export declare function locationOf(node: PGNode): NodeLocation; | ||
export type Statement = SelectStatement | CreateTableStatement | CreateSequenceStatement | CreateIndexStatement | CreateExtensionStatement | CommitStatement | InsertStatement | UpdateStatement | ShowStatement | PrepareStatement | DeallocateStatement | DeleteStatement | WithStatement | RollbackStatement | TablespaceStatement | CreateViewStatement | CreateMaterializedViewStatement | RefreshMaterializedViewStatement | AlterTableStatement | AlterIndexStatement | AlterSequenceStatement | SetGlobalStatement | SetTimezone | SetNames | CreateEnumType | CreateCompositeType | TruncateTableStatement | DropStatement | CommentStatement | CreateSchemaStatement | WithRecursiveStatement | RaiseStatement | ValuesStatement | CreateFunctionStatement | DropFunctionStatement | DoStatement | BeginStatement | StartTransactionStatement; | ||
export type Statement = SelectStatement | CreateTableStatement | CreateSequenceStatement | CreateIndexStatement | CreateExtensionStatement | CommitStatement | InsertStatement | UpdateStatement | ShowStatement | PrepareStatement | DeallocateStatement | DeleteStatement | WithStatement | RollbackStatement | TablespaceStatement | CreateViewStatement | CreateMaterializedViewStatement | RefreshMaterializedViewStatement | AlterTableStatement | AlterIndexStatement | AlterSequenceStatement | SetGlobalStatement | SetTimezone | SetNames | CreateEnumType | CreateCompositeType | AlterEnumType | TruncateTableStatement | DropStatement | CommentStatement | CreateSchemaStatement | WithRecursiveStatement | RaiseStatement | ValuesStatement | CreateFunctionStatement | DropFunctionStatement | DoStatement | BeginStatement | StartTransactionStatement; | ||
export interface PGNode { | ||
@@ -108,2 +108,16 @@ _location?: NodeLocation; | ||
} | ||
export interface AlterEnumType extends PGNode { | ||
type: 'alter enum'; | ||
name: QName; | ||
change: EnumAlteration; | ||
} | ||
export type EnumAlteration = EnumAlterationRename | EnumAlterationAddValue; | ||
export interface EnumAlterationRename { | ||
type: 'rename'; | ||
to: QName; | ||
} | ||
export interface EnumAlterationAddValue { | ||
type: 'add value'; | ||
add: Literal; | ||
} | ||
export interface CompositeTypeAttribute extends PGNode { | ||
@@ -295,2 +309,3 @@ name: Name; | ||
ifNotExists?: true; | ||
concurrently?: true; | ||
indexName?: Name; | ||
@@ -510,2 +525,3 @@ tablespace?: string; | ||
join?: JoinClause | nil; | ||
lateral?: true; | ||
withOrdinality?: boolean; | ||
@@ -526,2 +542,3 @@ } | ||
name: QNameMapped; | ||
lateral?: true; | ||
join?: JoinClause | nil; | ||
@@ -533,2 +550,3 @@ } | ||
alias: string; | ||
lateral?: true; | ||
columnNames?: Name[] | nil; | ||
@@ -639,2 +657,4 @@ db?: null | nil; | ||
filter?: Expr | nil; | ||
/** [AGGREGATION FUNCTIONS] WITHIN GROUP clause */ | ||
withinGroup?: OrderByStatement | nil; | ||
/** [AGGREGATION FUNCTIONS] OVER clause */ | ||
@@ -641,0 +661,0 @@ over?: CallOver | nil; |
@@ -1,2 +0,2 @@ | ||
import { Expr, SelectStatement, CreateTableStatement, CreateIndexStatement, Statement, InsertStatement, UpdateStatement, AlterTableStatement, DeleteStatement, CreateExtensionStatement, CreateSequenceStatement, AlterSequenceStatement, SelectedColumn, Interval, BinaryOperator, ExprBinary, Name, ExprInteger, FromTable, QName, AlterIndexStatement } from './ast'; | ||
import { Expr, SelectStatement, CreateTableStatement, CreateIndexStatement, Statement, InsertStatement, UpdateStatement, AlterTableStatement, DeleteStatement, CreateExtensionStatement, CreateSequenceStatement, AlterSequenceStatement, SelectedColumn, Interval, BinaryOperator, ExprBinary, Name, ExprInteger, FromTable, QName, AlterIndexStatement, ExprNumeric } from './ast'; | ||
export declare function checkSelect(value: string | string[], expected: SelectStatement): void; | ||
@@ -34,2 +34,3 @@ export declare function checkCreateSequence(value: string | string[], expected: CreateSequenceStatement): void; | ||
export declare function tbl(nm: string): FromTable; | ||
export declare function numeric(value: number): ExprNumeric; | ||
//# sourceMappingURL=spec-utils.d.ts.map |
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 not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1704920
184
18618