pgsql-ast-parser
Advanced tools
Comparing version 10.3.1 to 10.4.0
{ | ||
"name": "pgsql-ast-parser", | ||
"version": "10.3.1", | ||
"version": "10.4.0", | ||
"description": "Yet another simple Postgres SQL parser/modifier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -448,2 +448,5 @@ import * as a from './syntax/ast'; | ||
} | ||
const from = val.from && this.from(val.from); | ||
const where = val.where && this.expr(val.where); | ||
@@ -461,2 +464,3 @@ | ||
sets, | ||
from, | ||
returning, | ||
@@ -463,0 +467,0 @@ }); |
@@ -471,2 +471,5 @@ // import { IType } from '../../interfaces'; | ||
export interface TableAliasName extends Name, PGNode { | ||
columns?: Name[]; | ||
} | ||
@@ -622,2 +625,3 @@ export interface QName extends Name, PGNode { | ||
where?: Expr | nil; | ||
from?: From | nil; | ||
returning?: SelectedColumn[] | nil; | ||
@@ -642,4 +646,5 @@ } | ||
export interface FromCall extends ExprCall, PGNode { | ||
alias?: Name; | ||
alias?: TableAliasName; | ||
join?: JoinClause | nil; | ||
withOrdinality?: boolean; | ||
}; | ||
@@ -646,0 +651,0 @@ |
@@ -100,2 +100,105 @@ import 'mocha'; | ||
checkSelect(['select * from unnest(generate_series(1, 10)) AS test(num)'], { | ||
type: 'select', | ||
from: [{ | ||
type: 'call', | ||
function: { name: 'unnest' }, | ||
alias: { | ||
name: 'test', | ||
columns: [ | ||
{ name: 'num' }, | ||
], | ||
}, | ||
args: [ | ||
{ | ||
type: 'call', | ||
function: { name: 'generate_series' }, | ||
args: [ | ||
{ type: 'integer', value: 1 }, | ||
{ type: 'integer', value: 10 }, | ||
], | ||
}, | ||
], | ||
}], | ||
columns: columns({ type: 'ref', name: '*' }), | ||
}); | ||
checkSelect(['select * from unnest(ARRAY[\'foo\', \'bar\', \'baz\']) with ordinality AS test(thing, num)'], { | ||
type: 'select', | ||
from: [{ | ||
type: 'call', | ||
function: { name: 'unnest' }, | ||
withOrdinality: true, | ||
alias: { | ||
name: 'test', | ||
columns: [ | ||
{ name: 'thing' }, | ||
{ name: 'num' }, | ||
], | ||
}, | ||
args: [ | ||
{ | ||
type: 'array', | ||
expressions: [ | ||
{ type: 'string', value: 'foo' }, | ||
{ type: 'string', value: 'bar' }, | ||
{ type: 'string', value: 'baz' }, | ||
] | ||
} | ||
], | ||
}], | ||
columns: columns({ type: 'ref', name: '*' }), | ||
}); | ||
checkSelect(['select t.* from things AS t join unnest(ARRAY[\'foo\', \'bar\']) with ordinality AS f(thing, ord) using (thing) order by f.ord'], { | ||
type: 'select', | ||
from: [ | ||
{ | ||
type: 'table', | ||
name: { name: 'things', alias: 't' } | ||
}, | ||
{ | ||
type: 'call', | ||
function: { name: 'unnest' }, | ||
join: { | ||
type: 'INNER JOIN', | ||
using: [ | ||
{ name: 'thing' } | ||
], | ||
}, | ||
withOrdinality: true, | ||
alias: { | ||
name: 'f', | ||
columns: [ | ||
{ name: 'thing' }, | ||
{ name: 'ord' }, | ||
], | ||
}, | ||
args: [ | ||
{ | ||
type: 'array', | ||
expressions: [ | ||
{ type: 'string', value: 'foo' }, | ||
{ type: 'string', value: 'bar' }, | ||
], | ||
} | ||
], | ||
} | ||
], | ||
columns: columns({ | ||
type: 'ref', | ||
table: { name: 't' }, | ||
name: '*', | ||
}), | ||
orderBy: [ | ||
{ | ||
by: { | ||
type: 'ref', | ||
table: { name: 'f' }, | ||
name: 'ord', | ||
} | ||
} | ||
] | ||
}); | ||
checkSelect(['select * from test limit 0'], { | ||
@@ -251,2 +354,3 @@ type: 'select', | ||
checkInvalid('select * from (select id from test)'); // <== missing alias | ||
checkInvalid('select * from sum(DISTINCT whatever)'); | ||
@@ -253,0 +357,0 @@ checkSelect('select * from (select id from test) d', { |
@@ -304,3 +304,3 @@ import { Parser, Grammar } from 'nearley'; | ||
expr: ref(name), | ||
alias: alias ? { name: alias } : undefined, | ||
... alias ? { name: alias } : undefined, | ||
}; | ||
@@ -307,0 +307,0 @@ } |
import 'mocha'; | ||
import 'chai'; | ||
import { checkUpdate } from './spec-utils'; | ||
import { checkUpdate, col } from './spec-utils'; | ||
@@ -57,2 +57,32 @@ describe('Update', () => { | ||
}) | ||
checkUpdate([`update mytable | ||
set col = subsel.subcol | ||
from (select id, subcol from subtable) as subsel | ||
where subsel.id=mytable.id`], { | ||
type: 'update', | ||
table: { name: 'mytable' }, | ||
sets: [{ | ||
column: { name: 'col' }, | ||
value: { type: 'ref', name: 'subcol', table: { name: 'subsel' } }, | ||
}], | ||
from: { | ||
type: 'statement', | ||
alias: 'subsel', | ||
statement: { | ||
type: 'select', | ||
columns: [col('id'), col('subcol')], | ||
from: [{ | ||
type: 'table', | ||
name: { name: 'subtable' }, | ||
}] | ||
}, | ||
}, | ||
where: { | ||
type: 'binary', | ||
op: '=', | ||
left: { type: 'ref', name: 'id', table: { name: 'subsel' } }, | ||
right: { type: 'ref', name: 'id', table: { name: 'mytable' } }, | ||
} | ||
}) | ||
}); |
@@ -1028,4 +1028,18 @@ import { IAstPartialMapper, AstDefaultMapper } from './ast-mapper'; | ||
m.call(s); | ||
if (s.withOrdinality) { | ||
ret.push(' WITH ORDINALITY') | ||
} | ||
if (s.alias) { | ||
ret.push(' AS ', name(s.alias), ' '); | ||
ret.push(' AS ', name<Name>(s.alias), ' '); | ||
const len = s.alias.columns?.length ?? 0; | ||
if (len > 0) { | ||
ret.push('(') | ||
for (let ix = 0; ix < len; ++ix) { | ||
if (ix !== 0) { | ||
ret.push(', ') | ||
} | ||
ret.push(name(s.alias.columns![ix])); | ||
} | ||
ret.push(')') | ||
} | ||
} | ||
@@ -1426,2 +1440,7 @@ }); | ||
ret.push(' '); | ||
if (u.from) { | ||
ret.push('FROM '); | ||
m.from(u.from); | ||
ret.push(' '); | ||
} | ||
if (u.where) { | ||
@@ -1428,0 +1447,0 @@ ret.push('WHERE '); |
@@ -350,2 +350,5 @@ import { nil } from '../utils'; | ||
} | ||
export interface TableAliasName extends Name, PGNode { | ||
columns?: Name[]; | ||
} | ||
export interface QName extends Name, PGNode { | ||
@@ -455,2 +458,3 @@ schema?: string; | ||
where?: Expr | nil; | ||
from?: From | nil; | ||
returning?: SelectedColumn[] | nil; | ||
@@ -468,4 +472,5 @@ } | ||
export interface FromCall extends ExprCall, PGNode { | ||
alias?: Name; | ||
alias?: TableAliasName; | ||
join?: JoinClause | nil; | ||
withOrdinality?: boolean; | ||
} | ||
@@ -472,0 +477,0 @@ export interface ValuesStatement extends PGNode { |
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
1631562
17651