Skip to content

Commit

Permalink
[refactoring] Handle naked "values" statement, and make the whole AST…
Browse files Browse the repository at this point in the history
… more coherent

#33
  • Loading branch information
oguimbal committed Apr 21, 2021
1 parent 3bd2a69 commit 2b4105c
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 263 deletions.
37 changes: 19 additions & 18 deletions src/ast-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface IAstPartialMapper {
from?: (from: a.From) => a.From | nil
fromCall?: (from: a.FromCall) => a.From | nil
fromStatement?: (from: a.FromStatement) => a.From | nil
fromValues?: (from: a.FromValues) => a.From | nil;
values?: (from: a.ValuesStatement) => a.SelectStatement | nil;
fromTable?: (from: a.FromTable) => a.From | nil
selectionColumn?: (val: a.SelectedColumn) => a.SelectedColumn | nil
expr?: (val: a.Expr) => a.Expr | nil
Expand All @@ -68,6 +68,7 @@ export interface IAstPartialMapper {
callOverlay?: (val: a.ExprOverlay) => a.Expr | nil
array?: (val: a.ExprList) => a.Expr | nil
constant?: (value: a.ExprLiteral) => a.Expr | nil
default?: (value: a.ExprDefault) => a.Expr | nil;
ref?: (val: a.ExprRef) => a.Expr | nil
unary?: (val: a.ExprUnary) => a.Expr | nil
binary?: (val: a.ExprBinary) => a.Expr | nil
Expand Down Expand Up @@ -272,6 +273,8 @@ export class AstDefaultMapper implements IAstMapper {
return this.do(val);
case 'create function':
return this.createFunction(val);
case 'values':
return this.values(val);
default:
throw NotSupported.never(val);
}
Expand Down Expand Up @@ -432,18 +435,10 @@ export class AstDefaultMapper implements IAstMapper {
if (!into) {
return null; // nowhere to insert into
}
const values = arrayNilMap(val.values, valSet => {
return arrayNilMap(valSet, v => {
if (v === 'default') {
return v;
}
return this.expr(v);
});
});

const select = val.select && this.select(val.select);
const select = val.insert && this.select(val.insert);

if (!values?.length && !select) {
if (!select) {
// nothing to insert
return null;
}
Expand All @@ -462,8 +457,7 @@ export class AstDefaultMapper implements IAstMapper {

return assignChanged(val, {
into,
values,
select,
insert: select,
returning,
onConflict: !ocdo ? val.onConflict : assignChanged(val.onConflict, {
do: ocdo,
Expand Down Expand Up @@ -811,6 +805,8 @@ export class AstDefaultMapper implements IAstMapper {
return this.union(val);
case 'with':
return this.with(val);
case 'values':
return this.values(val);
default:
throw NotSupported.never(val);
}
Expand Down Expand Up @@ -894,8 +890,6 @@ export class AstDefaultMapper implements IAstMapper {
return this.fromTable(from);
case 'statement':
return this.fromStatement(from);
case 'values':
return this.fromValues(from);
case 'call':
return this.fromCall(from);
default:
Expand Down Expand Up @@ -924,7 +918,7 @@ export class AstDefaultMapper implements IAstMapper {
})
}

fromValues(from: a.FromValues): a.From | nil {
values(from: a.ValuesStatement): a.SelectStatement | nil {
const values = arrayNilMap(from.values, x => arrayNilMap(x, y => this.expr(y)));
if (!values?.length) {
return null; // nothing to select from
Expand All @@ -945,13 +939,13 @@ export class AstDefaultMapper implements IAstMapper {
}

fromTable(from: a.FromTable): a.From | nil {
const nfrom = this.tableRef(from);
const nfrom = this.tableRef(from.name);
if (!nfrom) {
return null; // nothing to select from
}
const join = from.join && this.join(from.join);
return assignChanged(from, {
...nfrom,
name: nfrom,
join,
})
}
Expand Down Expand Up @@ -1021,6 +1015,10 @@ export class AstDefaultMapper implements IAstMapper {
return this.callOverlay(val);
case 'substring':
return this.callSubstring(val);
case 'values':
return this.values(val);
case 'default':
return this.default(val);
default:
throw NotSupported.never(val);
}
Expand Down Expand Up @@ -1166,6 +1164,9 @@ export class AstDefaultMapper implements IAstMapper {
return value;
}

default(value: a.ExprDefault): a.Expr | nil {
return value;
}

/** Called when a reference is used */
ref(val: a.ExprRef): a.Expr | nil {
Expand Down
32 changes: 21 additions & 11 deletions src/syntax/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type Statement = SelectStatement
| CommentStatement
| CreateSchemaStatement
| RaiseStatement
| ValuesStatement
| CreateFunctionStatement
| DoStatement
| BeginStatement
Expand Down Expand Up @@ -212,10 +213,7 @@ export interface InsertStatement extends PGNode {
returning?: SelectedColumn[] | nil;
columns?: Name[] | nil;
overriding?: 'system' | 'user';
/** Insert values */
values?: (Expr | 'default')[][] | nil;
/** Insert into select */
select?: SelectStatement | nil;
insert: SelectStatement;
onConflict?: OnConflictAction | nil;
}

Expand Down Expand Up @@ -512,6 +510,7 @@ export interface WithStatement extends PGNode {

export type SelectStatement = SelectFromStatement
| SelectFromUnion
| ValuesStatement
| WithStatement;

export interface SelectFromStatement extends PGNode {
Expand Down Expand Up @@ -560,7 +559,9 @@ export interface SelectedColumn extends PGNode {
alias?: Name;
}

export type From = FromTable | FromStatement | FromValues | FromCall
export type From = FromTable
| FromStatement
| FromCall


export interface FromCall extends ExprCall, PGNode {
Expand All @@ -570,28 +571,32 @@ export interface FromCall extends ExprCall, PGNode {



export interface FromValues {
export interface ValuesStatement extends PGNode {
type: 'values';
alias: Name;
values: Expr[][];
columnNames?: Name[] | nil;
join?: JoinClause | nil;
}



export interface QNameAliased extends QName, PGNode {
alias?: string;
}

export interface FromTable extends QNameAliased, PGNode {
export interface QNameMapped extends QNameAliased {
columnNames?: Name[] | nil;
}

export interface FromTable extends PGNode {
type: 'table',
name: QNameMapped;
join?: JoinClause | nil;
}

export interface FromStatement extends PGNode {
type: 'statement';
statement: SelectStatement;
alias: Name;
alias: string;
columnNames?: Name[] | nil;
db?: null | nil;
join?: JoinClause | nil;
}
Expand All @@ -614,6 +619,7 @@ export type Expr = ExprRef
| ExprNull
| ExprExtract
| ExprInteger
| ExprDefault
| ExprMember
| ExprValueKeyword
| ExprArrayIndex
Expand Down Expand Up @@ -804,6 +810,10 @@ export interface ExprInteger extends PGNode {
value: number;
}

export interface ExprDefault extends PGNode {
type: 'default';
}

export interface ExprNumeric extends PGNode {
type: 'numeric';
value: number;
Expand Down
32 changes: 16 additions & 16 deletions src/syntax/create-view.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'mocha';
import 'chai';
import { checkInvalid, checkStatement, columns } from './spec-utils';
import { checkInvalid, checkStatement, columns, tbl } from './spec-utils';

describe('Create view statements', () => {

Expand All @@ -10,7 +10,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -23,7 +23,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -36,7 +36,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -48,7 +48,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -60,7 +60,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -71,7 +71,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -82,7 +82,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -94,7 +94,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -107,7 +107,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -117,7 +117,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -128,7 +128,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -139,7 +139,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand All @@ -151,7 +151,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
withData: true,
});
Expand All @@ -163,7 +163,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
withData: false,
});
Expand All @@ -178,7 +178,7 @@ describe('Create view statements', () => {
query: {
type: 'select',
columns: columns('*'),
from: [{ type: 'table', name: 'tbl' }],
from: [tbl('tbl')],
},
});

Expand Down
1 change: 1 addition & 0 deletions src/syntax/expr.ne
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ expr_primary
| %kw_null {% x => track(x, { type: 'null' }) %}
| value_keyword {% x => track(x, {type: 'keyword', keyword: toStr(x) }) %}
| %qparam {% x => track(x, { type: 'parameter', name: toStr(x[0]) }) %}
| %kw_default {% x => track(x, { type: 'default'}) %}


# LIKE-kind operators
Expand Down
10 changes: 7 additions & 3 deletions src/syntax/expr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'mocha';
import 'chai';
import { checkTreeExpr, checkInvalidExpr, checkInvalid, checkTreeExprLoc, starCol, star, col, ref } from './spec-utils';
import { checkTreeExpr, checkInvalidExpr, checkInvalid, checkTreeExprLoc, starCol, star, col, ref, tbl, name } from './spec-utils';
import { toSql } from '../to-sql';
import { expect } from 'chai';
import { parse } from '../parser';
Expand Down Expand Up @@ -1332,7 +1332,11 @@ line`,
}],
from: [{
_location: { start: 22, end: 25 },
type: 'table', name: 'tbl'
type: 'table',
name: {
_location: { start: 22, end: 25 },
name: 'tbl'
},
}],
}]
}
Expand All @@ -1346,7 +1350,7 @@ line`,
right: {
type: 'select',
columns: [starCol],
from: [{ type: 'table', name: 'tb' }],
from: [tbl('tb')],
}
});

Expand Down
Loading

0 comments on commit 2b4105c

Please sign in to comment.