pgsql-ast-parser
Advanced tools
Comparing version 1.3.6 to 1.3.7
{ | ||
"name": "pgsql-ast-parser", | ||
"version": "1.3.6", | ||
"version": "1.3.7", | ||
"description": "Yet another simple Postgres SQL parser/modifier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -28,3 +28,3 @@ import { Statement, Expr, LOCATION, QName } from './syntax/ast'; | ||
// always return an array of statements. | ||
if (!entry && !Array.isArray(parsed)) { | ||
if (!entry && !Array.isArray(parsed)) { | ||
parsed = [parsed] | ||
@@ -43,10 +43,11 @@ } | ||
function _parse(sql: string, grammar: Grammar, entry?: string): any { | ||
grammar.start = entry ?? 'main'; | ||
const parser = new Parser(grammar); | ||
parser.feed(sql); | ||
const asts = parser.finish(); | ||
if (!asts.length) { | ||
throw new Error('Unexpected end of input'); | ||
} else if (asts.length !== 1) { | ||
throw new Error(`💀 Ambiguous SQL syntax: Please file an issue stating the request that has failed at https://github.com/oguimbal/pgsql-ast-parser: | ||
try { | ||
grammar.start = entry ?? 'main'; | ||
const parser = new Parser(grammar); | ||
parser.feed(sql); | ||
const asts = parser.finish(); | ||
if (!asts.length) { | ||
throw new Error('Unexpected end of input'); | ||
} else if (asts.length !== 1) { | ||
throw new Error(`💀 Ambiguous SQL syntax: Please file an issue stating the request that has failed at https://github.com/oguimbal/pgsql-ast-parser: | ||
@@ -56,4 +57,24 @@ ${sql} | ||
`); | ||
} | ||
return asts[0]; | ||
} catch (e) { | ||
if (typeof e?.message !== 'string') { | ||
throw e; | ||
} | ||
let msg: string = e.message; | ||
// remove all the stack crap of nearley parser | ||
let begin: string | null = null; | ||
const parts: string[] = []; | ||
const reg = /A (.+) token based on:/g; | ||
let m: RegExpExecArray | null; | ||
while (m = reg.exec(msg)) { | ||
begin = begin ?? msg.substr(0, m.index); | ||
parts.push(` - A "${m[1]}" token`); | ||
} | ||
if (begin) { | ||
msg = begin + parts.join('\n') + '\n\n'; | ||
} | ||
e.message = msg; | ||
throw e; | ||
} | ||
return asts[0]; | ||
} | ||
} |
@@ -28,4 +28,5 @@ // import { IType } from '../../interfaces'; | ||
export interface TruncateTableStatement extends QName { | ||
export interface TruncateTableStatement { | ||
type: 'truncate table'; | ||
tables: QName[]; | ||
} | ||
@@ -32,0 +33,0 @@ export interface DropTableStatement extends QName { |
@@ -330,2 +330,42 @@ import 'mocha'; | ||
}) | ||
// bugfix | ||
checkCreateTable([`CREATE TABLE Post ( | ||
categoryId text, | ||
CONSTRAINT Post_fk_categoryId FOREIGN KEY (categoryId) | ||
REFERENCES Category (id) ON UPDATE CASCADE ON DELETE CASCADE, | ||
CONSTRAINT Post_ck_isPublished CHECK (isPublished IN (0, 1)) | ||
);`], { | ||
type: 'create table', | ||
name: 'post', | ||
columns: [{ | ||
name: 'categoryid', | ||
dataType: { type: 'text' }, | ||
}], | ||
constraints: [{ | ||
type: 'foreign key', | ||
foreignTable: 'category', | ||
foreignColumns: ['id'], | ||
localColumns: ['categoryid'], | ||
onUpdate: 'cascade', | ||
onDelete: 'cascade', | ||
constraintName: 'post_fk_categoryid', | ||
}, { | ||
type: 'check', | ||
constraintName: 'post_ck_ispublished', | ||
expr: { | ||
type: 'binary', | ||
op: 'IN', | ||
left: { type: 'ref', name: 'ispublished' }, | ||
right: { | ||
type: 'list', | ||
expressions: [ | ||
{ type: 'integer', value: 0 }, | ||
{ type: 'integer', value: 1 } | ||
] | ||
} | ||
} | ||
}] | ||
}) | ||
}); |
@@ -21,5 +21,9 @@ import 'mocha'; | ||
type: 'truncate table', | ||
name: 'test', | ||
tables: [{ name: 'test' }] | ||
}); | ||
checkStatement([`truncate ta, "tb"`, `truncate table "ta","tb"`], { | ||
type: 'truncate table', | ||
tables: [{ name: 'ta' }, { name: 'tb' }] | ||
}); | ||
checkDelete([`delete from test`], { | ||
@@ -26,0 +30,0 @@ type: 'delete', |
@@ -13,2 +13,7 @@ import 'mocha'; | ||
// yea... thats a valid query. Try it oO' | ||
checkSelect(['select'], { | ||
type: 'select', | ||
}); | ||
checkSelect(['select 42', 'select(42)'], { | ||
@@ -15,0 +20,0 @@ type: 'select', |
@@ -524,3 +524,10 @@ import { IAstPartialMapper, AstDefaultMapper } from './ast-mapper'; | ||
ret.push('TRUNCATE TABLE '); | ||
m.tableRef(t); | ||
let first = true; | ||
for (const tbl of t.tables) { | ||
if (!first) { | ||
ret.push(', '); | ||
} | ||
first = false; | ||
m.tableRef(tbl); | ||
} | ||
}, | ||
@@ -668,5 +675,3 @@ | ||
ret.push('SELECT '); | ||
if (!s.columns) { | ||
ret.push('*'); | ||
} else { | ||
if (s.columns) { | ||
list(s.columns, c => m.selectionColumn(c), false); | ||
@@ -673,0 +678,0 @@ } |
@@ -6,4 +6,5 @@ import { nil } from '../utils'; | ||
}; | ||
export interface TruncateTableStatement extends QName { | ||
export interface TruncateTableStatement { | ||
type: 'truncate table'; | ||
tables: QName[]; | ||
} | ||
@@ -10,0 +11,0 @@ export interface DropTableStatement extends QName { |
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
822122
9098