pgsql-ast-parser
Advanced tools
Comparing version 10.4.0 to 10.5.0
{ | ||
"name": "pgsql-ast-parser", | ||
"version": "10.4.0", | ||
"version": "10.5.0", | ||
"description": "Yet another simple Postgres SQL parser/modifier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,2 +30,11 @@ import 'mocha'; | ||
function testExprs(statement: string, map: MapperBuilder, exprs: Expr[]) { | ||
const toMap = parse(statement); | ||
const mapped = astMapper(map).statement(toMap); | ||
assert.deepEqual(mapped, { | ||
type: 'select', | ||
columns: exprs.map(expr => ({ expr })), | ||
}) | ||
} | ||
it('maps a select constant', () => { | ||
@@ -127,2 +136,18 @@ testExpr('select 42', b => ({ | ||
it('maps multiple calls', () => { | ||
testExprs('select fn(a), fn(b), fn(c)', () => ({ | ||
call: c => c.args[0], | ||
}), [ | ||
{ | ||
type: 'ref', | ||
name: 'a', | ||
}, { | ||
type: 'ref', | ||
name: 'b', | ||
}, { | ||
type: 'ref', | ||
name: 'c', | ||
} | ||
]); | ||
}) | ||
@@ -145,5 +170,4 @@ it('maps array literal', () => { | ||
}); | ||
}) | ||
}); | ||
it('maps deep', () => { | ||
@@ -150,0 +174,0 @@ // create a mapper |
@@ -170,3 +170,3 @@ import * as a from './syntax/ast'; | ||
const val = mapper(orig); | ||
if (!val || val !== orig) { | ||
if (!changed && (!val || val !== orig)) { | ||
changed = true; | ||
@@ -597,2 +597,11 @@ ret = collection.slice(0, i); | ||
} | ||
case 'reference': { | ||
const foreignTable = this.tableRef(c.foreignTable); | ||
if (!foreignTable) { | ||
return null; | ||
} | ||
return assignChanged(c, { | ||
foreignTable, | ||
}); | ||
} | ||
default: | ||
@@ -599,0 +608,0 @@ throw NotSupported.never(c); |
@@ -192,2 +192,3 @@ // import { IType } from '../../interfaces'; | ||
identity?: 'restart' | 'continue'; | ||
cascade?: 'cascade' | 'restrict'; | ||
} | ||
@@ -505,2 +506,3 @@ | ||
| AlterColumnAddGenerated | ||
| ColumnConstraintReference | ||
| ColumnConstraintCheck; | ||
@@ -516,2 +518,6 @@ | ||
export interface ColumnConstraintReference extends TableReference, PGNode { | ||
type: 'reference'; | ||
} | ||
export interface ColumnConstraintDefault extends PGNode { | ||
@@ -523,5 +529,8 @@ type: 'default'; | ||
export interface ColumnConstraintForeignKey extends PGNode { | ||
export interface ColumnConstraintForeignKey extends TableReference, PGNode { | ||
type: 'foreign key'; | ||
constraintName?: Name; | ||
} | ||
export interface TableReference { | ||
foreignTable: QName; | ||
@@ -528,0 +537,0 @@ foreignColumns: Name[]; |
@@ -732,2 +732,31 @@ import 'mocha'; | ||
}); | ||
checkCreateTable(`CREATE TABLE foo ( | ||
id BIGSERIAL PRIMARY KEY, | ||
bar_id INTEGER NOT NULL REFERENCES users(id) | ||
)`, { | ||
type: 'create table', | ||
name: { name: 'foo' }, | ||
columns: [ | ||
{ | ||
kind: 'column', | ||
name: { name: 'id' }, | ||
dataType: { name: 'bigserial' }, | ||
constraints: [{ type: 'primary key' }], | ||
}, | ||
{ | ||
kind: 'column', | ||
name: { name: 'bar_id' }, | ||
dataType: { name: 'integer' }, | ||
constraints: [ | ||
{ type: 'not null' }, | ||
{ | ||
type: 'reference', | ||
foreignTable: { name: 'users' }, | ||
foreignColumns: [{ name: 'id' }], | ||
} | ||
], | ||
}, | ||
] | ||
}) | ||
}); |
@@ -36,2 +36,20 @@ import 'mocha'; | ||
checkStatement([`TRUNCATE TABLE t1,t2 RESTART IDENTITY CASCADE`], { | ||
type: 'truncate table', | ||
tables: [{ name: 't1' }, { name: 't2' }], | ||
identity: 'restart', | ||
cascade: 'cascade', | ||
}); | ||
checkStatement([`TRUNCATE TABLE t1,t2 CASCADE`], { | ||
type: 'truncate table', | ||
tables: [{ name: 't1' }, { name: 't2' }], | ||
cascade: 'cascade', | ||
}); | ||
checkStatement([`TRUNCATE TABLE t1,t2 restrict`], { | ||
type: 'truncate table', | ||
tables: [{ name: 't1' }, { name: 't2' }], | ||
cascade: 'restrict', | ||
}); | ||
checkStatement([`truncate ta, "tb"`, `truncate table "ta","tb"`], { | ||
@@ -38,0 +56,0 @@ type: 'truncate table', |
@@ -52,8 +52,10 @@ import { IAstPartialMapper, AstDefaultMapper } from './ast-mapper'; | ||
function addConstraint(c: ColumnConstraint | TableConstraint, m: IAstVisitor) { | ||
ret.push(c.type); | ||
switch (c.type) { | ||
case 'foreign key': | ||
ret.push('(' | ||
ret.push(' foreign key (' | ||
, ...c.localColumns.map(name).join(', ') | ||
, ') REFERENCES '); | ||
, ')'); | ||
// 👈 There is no "break" here... that's not an error, we want to fall throught the 'reference' case | ||
case 'reference': | ||
ret.push(' REFERENCES '); | ||
m.tableRef(c.foreignTable); | ||
@@ -75,2 +77,3 @@ ret.push('(' | ||
case 'unique': | ||
ret.push(' ', c.type, ' '); | ||
if ('columns' in c) { | ||
@@ -83,2 +86,3 @@ ret.push('(' | ||
case 'check': | ||
ret.push(' check '); | ||
m.expr(c.expr); | ||
@@ -88,5 +92,6 @@ break; | ||
case 'null': | ||
ret.push(' ', c.type, ' '); | ||
break; | ||
case 'default': | ||
ret.push(' DEFAULT '); | ||
ret.push(' default '); | ||
m.expr(c.default); | ||
@@ -101,2 +106,3 @@ break; | ||
} | ||
ret.push(' '); | ||
} | ||
@@ -644,27 +650,6 @@ function visitQualifiedName(cs: QName, forceDoubleQuote?: boolean) { | ||
constraint: cst => { | ||
if (cst.constraintName) { | ||
if ('constraintName' in cst && cst.constraintName) { | ||
ret.push(' CONSTRAINT ', name(cst.constraintName), ' '); | ||
} | ||
switch (cst.type) { | ||
case 'not null': | ||
case 'null': | ||
case 'primary key': | ||
case 'unique': | ||
ret.push(' ', cst.type, ' '); | ||
return; | ||
case 'default': | ||
ret.push(' DEFAULT '); | ||
m.expr(cst.default); | ||
break; | ||
case 'check': | ||
ret.push(' CHECK '); | ||
m.expr(cst.expr); | ||
break; | ||
case 'add generated': | ||
ret.push(' GENERATED '); | ||
visitGenerated(m, cst); | ||
break; | ||
default: | ||
throw NotSupported.never(cst); | ||
} | ||
addConstraint(cst, m); | ||
}, | ||
@@ -992,2 +977,5 @@ | ||
} | ||
if (t.cascade) { | ||
ret.push(' ', t.cascade, ' '); | ||
} | ||
}, | ||
@@ -994,0 +982,0 @@ |
@@ -124,2 +124,3 @@ import { nil } from '../utils'; | ||
identity?: 'restart' | 'continue'; | ||
cascade?: 'cascade' | 'restrict'; | ||
} | ||
@@ -374,3 +375,3 @@ export interface DropStatement extends PGNode { | ||
} | ||
export declare type ColumnConstraint = ColumnConstraintSimple | ColumnConstraintDefault | AlterColumnAddGenerated | ColumnConstraintCheck; | ||
export declare type ColumnConstraint = ColumnConstraintSimple | ColumnConstraintDefault | AlterColumnAddGenerated | ColumnConstraintReference | ColumnConstraintCheck; | ||
export interface ColumnConstraintSimple extends PGNode { | ||
@@ -380,2 +381,5 @@ type: 'unique' | 'primary key' | 'not null' | 'null'; | ||
} | ||
export interface ColumnConstraintReference extends TableReference, PGNode { | ||
type: 'reference'; | ||
} | ||
export interface ColumnConstraintDefault extends PGNode { | ||
@@ -386,5 +390,7 @@ type: 'default'; | ||
} | ||
export interface ColumnConstraintForeignKey extends PGNode { | ||
export interface ColumnConstraintForeignKey extends TableReference, PGNode { | ||
type: 'foreign key'; | ||
constraintName?: Name; | ||
} | ||
export interface TableReference { | ||
foreignTable: QName; | ||
@@ -391,0 +397,0 @@ foreignColumns: Name[]; |
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
1637499
17736