pgsql-ast-parser
Advanced tools
Comparing version 7.0.2 to 7.1.0
@@ -79,2 +79,3 @@ import * as a from './syntax/ast'; | ||
alterSequence?(seq: a.AlterSequenceStatement): a.Statement | nil; | ||
begin?(begin: a.BeginStatement): a.Statement | nil; | ||
} | ||
@@ -144,2 +145,3 @@ export declare type IAstFullMapper = { | ||
alterSequence(seq: a.AlterSequenceStatement): a.Statement | nil; | ||
begin(begin: a.BeginStatement): a.Statement | nil; | ||
createSequence(seq: a.CreateSequenceStatement): a.Statement | nil; | ||
@@ -146,0 +148,0 @@ tablespace(val: a.TablespaceStatement): a.Statement | nil; |
{ | ||
"name": "pgsql-ast-parser", | ||
"version": "7.0.2", | ||
"version": "7.1.0", | ||
"description": "Yet another simple Postgres SQL parser/modifier", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -82,2 +82,3 @@ import * as a from './syntax/ast'; | ||
alterSequence?(seq: a.AlterSequenceStatement): a.Statement | nil | ||
begin?(begin: a.BeginStatement): a.Statement | nil | ||
} | ||
@@ -244,2 +245,4 @@ | ||
return this.alterSequence(val); | ||
case 'begin': | ||
return this.begin(val); | ||
case 'drop index': | ||
@@ -380,2 +383,6 @@ return this.dropIndex(val); | ||
begin(begin: a.BeginStatement): a.Statement | nil { | ||
return begin; | ||
} | ||
createSequence(seq: a.CreateSequenceStatement): a.Statement | nil { | ||
@@ -382,0 +389,0 @@ if (seq.options.as) { |
@@ -93,3 +93,3 @@ import { compile, keywords } from 'moo'; | ||
codeblock: { | ||
match: /\$\$(?:.|[\s\t\n\v\f\r])*\$\$/s, | ||
match: /\$\$(?:.|[\s\t\n\v\f\r])*?\$\$/s, | ||
lineBreaks: true, | ||
@@ -96,0 +96,0 @@ value: (x: string) => x.substr(2, x.length - 4), |
@@ -42,2 +42,3 @@ // import { IType } from '../../interfaces'; | ||
| DoStatement | ||
| BeginStatement | ||
| StartTransactionStatement; | ||
@@ -53,2 +54,9 @@ | ||
export interface BeginStatement extends PGNode { | ||
type: 'begin'; | ||
isolationLevel?: 'serializable' | 'repeatable read' | 'read committed' | 'read uncommitted'; | ||
writeable?: 'read write' | 'read only'; | ||
deferrable?: boolean; | ||
} | ||
export interface DoStatement extends PGNode { | ||
@@ -55,0 +63,0 @@ type: 'do'; |
import 'mocha'; | ||
import 'chai'; | ||
import { checkTreeExpr, checkInvalidExpr, checkInvalid, checkTreeExprLoc, starCol, star, col, ref } from './spec-utils'; | ||
import { toSql } from '../to-sql'; | ||
import { expect } from 'chai'; | ||
@@ -889,2 +891,20 @@ | ||
it('does not wrap list expressions in parenthesis', () => { | ||
const generated = toSql.expr({ | ||
type: 'binary', | ||
op: 'IN', | ||
left: { type: 'ref', name: 'a' }, | ||
right: { | ||
type: 'list', | ||
expressions: [ | ||
{ type: 'ref', name: 'a' }, | ||
{ type: 'ref', name: 'b' }, | ||
{ type: 'ref', name: 'c' }, | ||
] | ||
}, | ||
}); | ||
expect(generated).to.equal('("a" IN ("a", "b", "c"))'); | ||
}) | ||
checkTreeExpr(['a in (b)', 'a in ( b )'], { | ||
@@ -891,0 +911,0 @@ type: 'binary', |
@@ -5,2 +5,4 @@ import 'mocha'; | ||
import { CreateFunctionStatement } from './ast'; | ||
import { parse } from '../parser'; | ||
import { expect } from 'chai'; | ||
@@ -155,2 +157,9 @@ describe('Create function', () => { | ||
}); | ||
it ('is not greedy', () => { | ||
const parsed = parse(`create function foo() returns text as $$ select 'hi'; $$ language sql; | ||
create function bar() returns text as $$ select 'there'; $$ language sql;`); | ||
expect(parsed.length).to.equal(2); | ||
}); | ||
}); |
@@ -9,3 +9,3 @@ import 'mocha'; | ||
checkStatement(['start transaction', 'begin'], { | ||
checkStatement(['start transaction'], { | ||
type: 'start transaction', | ||
@@ -222,2 +222,19 @@ }); | ||
checkStatement(`begin isolation level read uncommitted isolation level read committed read write`, { | ||
type: 'begin', | ||
isolationLevel: 'read committed', | ||
writeable: 'read write', | ||
}); | ||
checkStatement(`begin deferrable`, { | ||
type: 'begin', | ||
deferrable: true, | ||
}) | ||
checkStatement(`begin not deferrable`, { | ||
type: 'begin', | ||
deferrable: false, | ||
}) | ||
}); |
@@ -339,2 +339,8 @@ import { IAstPartialMapper, AstDefaultMapper } from './ast-mapper'; | ||
} | ||
// lists can become incorrect with an additional set of parentheses | ||
if (e.type === 'list') { | ||
m.super().expr(e); | ||
return; | ||
} | ||
// this forces to respect precedence | ||
@@ -501,2 +507,18 @@ // (however, it will introduce lots of unecessary parenthesis) | ||
begin: beg => { | ||
ret.push('BEGIN '); | ||
if (beg.isolationLevel) { | ||
ret.push('ISOLATION LEVEL ', beg.isolationLevel.toUpperCase(), ' '); | ||
} | ||
if (beg.writeable) { | ||
ret.push(beg.writeable.toUpperCase(), ' '); | ||
} | ||
if (typeof beg.deferrable === 'boolean') { | ||
if (!beg.deferrable) { | ||
ret.push('NOT '); | ||
} | ||
ret.push('DEFERRABLE '); | ||
} | ||
}, | ||
alterSequence: cs => { | ||
@@ -503,0 +525,0 @@ ret.push('ALTER SEQUENCE '); |
import { nil } from '../utils'; | ||
export declare function locationOf(node: PGNode): NodeLocation; | ||
export declare type Statement = SelectStatement | CreateTableStatement | CreateSequenceStatement | CreateIndexStatement | CreateExtensionStatement | CommitStatement | InsertStatement | UpdateStatement | ShowStatement | PrepareStatement | DeleteStatement | WithStatement | RollbackStatement | TablespaceStatement | CreateViewStatement | CreateMaterializedViewStatement | AlterTableStatement | AlterSequenceStatement | SetGlobalStatement | SetTimezone | CreateEnumType | TruncateTableStatement | DropTableStatement | DropSequenceStatement | DropIndexStatement | CommentStatement | CreateSchemaStatement | RaiseStatement | CreateFunctionStatement | DoStatement | StartTransactionStatement; | ||
export declare type Statement = SelectStatement | CreateTableStatement | CreateSequenceStatement | CreateIndexStatement | CreateExtensionStatement | CommitStatement | InsertStatement | UpdateStatement | ShowStatement | PrepareStatement | DeleteStatement | WithStatement | RollbackStatement | TablespaceStatement | CreateViewStatement | CreateMaterializedViewStatement | AlterTableStatement | AlterSequenceStatement | SetGlobalStatement | SetTimezone | CreateEnumType | TruncateTableStatement | DropTableStatement | DropSequenceStatement | DropIndexStatement | CommentStatement | CreateSchemaStatement | RaiseStatement | CreateFunctionStatement | DoStatement | BeginStatement | StartTransactionStatement; | ||
export interface PGNode { | ||
@@ -10,2 +10,8 @@ _location?: NodeLocation; | ||
} | ||
export interface BeginStatement extends PGNode { | ||
type: 'begin'; | ||
isolationLevel?: 'serializable' | 'repeatable read' | 'read committed' | 'read uncommitted'; | ||
writeable?: 'read write' | 'read only'; | ||
deferrable?: boolean; | ||
} | ||
export interface DoStatement extends PGNode { | ||
@@ -12,0 +18,0 @@ type: 'do'; |
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
1404959
15301