Skip to content

Commit 9d71240

Browse files
Merge pull request #1695 from taozhi8833998/feat-create-view-sqlite
feat: support create view stmt in sqlite
2 parents 9998ea8 + 59ece8a commit 9d71240

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

pegjs/postgresql.pegjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ create_view_stmt
428428
type: a[0].toLowerCase(),
429429
keyword: 'view',
430430
replace: or && 'or replace',
431-
temporary: tp && tp.toLowerCase(),
431+
temporary: tp && tp[0].toLowerCase(),
432432
recursive: r && r.toLowerCase(),
433433
columns: c && c[2],
434434
select: s,
@@ -805,7 +805,7 @@ create_table_stmt
805805
ast: {
806806
type: a[0].toLowerCase(),
807807
keyword: 'table',
808-
temporary: tp && tp.toLowerCase(),
808+
temporary: tp && tp[0].toLowerCase(),
809809
if_not_exists:ife,
810810
table: t,
811811
ignore_replace: ir && ir[0].toLowerCase(),
@@ -836,7 +836,7 @@ create_table_stmt
836836
ast: {
837837
type: a[0].toLowerCase(),
838838
keyword: 'table',
839-
temporary: tp && tp.toLowerCase(),
839+
temporary: tp && tp[0].toLowerCase(),
840840
if_not_exists:ife,
841841
table: t,
842842
like: lt
@@ -869,7 +869,7 @@ create_sequence
869869
ast: {
870870
type: a[0].toLowerCase(),
871871
keyword: 'sequence',
872-
temporary: tp && tp.toLowerCase(),
872+
temporary: tp && tp[0].toLowerCase(),
873873
if_not_exists:ife,
874874
sequence: [t],
875875
create_definitions: c,
@@ -4716,8 +4716,8 @@ KW_ALTER = "ALTER"i !ident_start
47164716
KW_SELECT = "SELECT"i !ident_start
47174717
KW_UPDATE = "UPDATE"i !ident_start
47184718
KW_CREATE = "CREATE"i !ident_start
4719-
KW_TEMPORARY = "TEMPORARY"i !ident_start { return 'TEMPORARY'; }
4720-
KW_TEMP = "TEMP"i !ident_start { return 'TEMP'; }
4719+
KW_TEMPORARY = "TEMPORARY"i !ident_start
4720+
KW_TEMP = "TEMP"i !ident_start
47214721
KW_DELETE = "DELETE"i !ident_start
47224722
KW_INSERT = "INSERT"i !ident_start
47234723
KW_RECURSIVE= "RECURSIVE" !ident_start { return 'RECURSIVE'; }

pegjs/snowflake.pegjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ create_view_stmt
421421
type: a[0].toLowerCase(),
422422
keyword: 'view',
423423
replace: or && 'or replace',
424-
temporary: tp && tp.toLowerCase(),
424+
temporary: tp && tp[0].toLowerCase(),
425425
recursive: r && r.toLowerCase(),
426426
columns: c && c[2],
427427
select: s,
@@ -534,7 +534,7 @@ create_table_stmt
534534
ast: {
535535
type: a[0].toLowerCase(),
536536
keyword: 'table',
537-
temporary: tp && tp.toLowerCase(),
537+
temporary: tp && tp[0].toLowerCase(),
538538
if_not_exists:ife,
539539
table: t,
540540
ignore_replace: ir && ir[0].toLowerCase(),
@@ -565,7 +565,7 @@ create_table_stmt
565565
ast: {
566566
type: a[0].toLowerCase(),
567567
keyword: 'table',
568-
temporary: tp && tp.toLowerCase(),
568+
temporary: tp && tp[0].toLowerCase(),
569569
if_not_exists:ife,
570570
table: t,
571571
like: lt
@@ -598,7 +598,7 @@ create_sequence
598598
ast: {
599599
type: a[0].toLowerCase(),
600600
keyword: 'sequence',
601-
temporary: tp && tp.toLowerCase(),
601+
temporary: tp && tp[0].toLowerCase(),
602602
if_not_exists:ife,
603603
sequence: [t],
604604
create_definitions: c,
@@ -4118,8 +4118,8 @@ KW_ALTER = "ALTER"i !ident_start
41184118
KW_SELECT = "SELECT"i !ident_start
41194119
KW_UPDATE = "UPDATE"i !ident_start
41204120
KW_CREATE = "CREATE"i !ident_start
4121-
KW_TEMPORARY = "TEMPORARY"i !ident_start { return 'TEMPORARY'; }
4122-
KW_TEMP = "TEMP"i !ident_start { return 'TEMP'; }
4121+
KW_TEMPORARY = "TEMPORARY"i !ident_start
4122+
KW_TEMP = "TEMP"i !ident_start
41234123
KW_DELETE = "DELETE"i !ident_start
41244124
KW_INSERT = "INSERT"i !ident_start
41254125
KW_RECURSIVE= "RECURSIVE" !ident_start { return 'RECURSIVE'; }

pegjs/sqlite.pegjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ create_stmt
223223
= create_table_stmt
224224
/ create_db_stmt
225225
/ create_trigger_stmt
226+
/ create_view_stmt
226227

227228
alter_stmt
228229
= alter_table_stmt
@@ -375,6 +376,35 @@ create_db_stmt
375376
}
376377
}
377378

379+
view_with
380+
= KW_WITH __ c:("CASCADED"i / "LOCAL"i) __ "CHECK"i __ "OPTION" {
381+
return `with ${c.toLowerCase()} check option`
382+
}
383+
/ KW_WITH __ "CHECK"i __ "OPTION" {
384+
return 'with check option'
385+
}
386+
387+
create_view_stmt
388+
= a:KW_CREATE __ tp:(KW_TEMP / KW_TEMPORARY)? __
389+
KW_VIEW __ ife:if_not_exists_stmt? __ v:table_name __ c:(LPAREN __ column_list __ RPAREN)? __
390+
KW_AS __ s:select_stmt_nake {
391+
v.view = v.table
392+
delete v.table
393+
return {
394+
tableList: Array.from(tableList),
395+
columnList: columnListTableAlias(columnList),
396+
ast: {
397+
type: a[0].toLowerCase(),
398+
keyword: 'view',
399+
if_not_exists: ife,
400+
temporary: tp && tp[0].toLowerCase(),
401+
columns: c && c[2],
402+
select: s,
403+
view: v,
404+
}
405+
}
406+
}
407+
378408
create_table_stmt
379409
= a:KW_CREATE __
380410
tp:KW_TEMPORARY? __
@@ -2494,6 +2524,7 @@ KW_SESSION = "SESSION"i !ident_start { return 'SESSION'; }
24942524
KW_LOCAL = "LOCAL"i !ident_start { return 'LOCAL'; }
24952525
KW_PERSIST = "PERSIST"i !ident_start { return 'PERSIST'; }
24962526
KW_PERSIST_ONLY = "PERSIST_ONLY"i !ident_start { return 'PERSIST_ONLY'; }
2527+
KW_VIEW = "VIEW"i !ident_start { return 'VIEW'; }
24972528

24982529
KW_VAR__PRE_AT = '@'
24992530
KW_VAR__PRE_AT_AT = '@@'

src/create.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function createDatabaseToSQL(stmt) {
199199

200200
function createViewToSQL(stmt) {
201201
const {
202-
algorithm, columns, definer, keyword,
202+
algorithm, columns, definer, if_not_exists: ifNotExists, keyword,
203203
recursive, replace, select, sql_security: sqlSecurity,
204204
temporary, type, view, with: withClause, with_options: withOptions,
205205
} = stmt
@@ -214,6 +214,7 @@ function createViewToSQL(stmt) {
214214
definer,
215215
sqlSecurity && `SQL SECURITY ${toUpper(sqlSecurity)}`,
216216
toUpper(keyword),
217+
toUpper(ifNotExists),
217218
viewName,
218219
columns && `(${columns.map(columnIdentifierToSql).join(', ')})`,
219220
withOptions && ['WITH', `(${withOptions.map(withOpt => commonTypeValue(withOpt).join(' ')).join(', ')})`].join(' '),

test/sqlite.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,11 @@ describe('sqlite', () => {
154154
ast = parser.astify(sql, { database: 'mariadb' })
155155
expect(parser.sqlify(ast, DEFAULT_OPT)).to.be.equal('CREATE TABLE `Test` (`id` INT(11) NOT NULL, `name` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE (`name`))')
156156
})
157+
158+
it('should support create view', () => {
159+
let sql = 'create view v1 as select * from t1'
160+
expect(getParsedSql(sql)).to.be.equal('CREATE VIEW `v1` AS SELECT * FROM `t1`')
161+
sql = 'create temp view if not exists s.v1(a, b, c) as select * from t1'
162+
expect(getParsedSql(sql)).to.be.equal('CREATE TEMP VIEW IF NOT EXISTS `s`.`v1` (`a`, `b`, `c`) AS SELECT * FROM `t1`')
163+
})
157164
})

0 commit comments

Comments
 (0)