Skip to content

Commit cfcbfdf

Browse files
committed
feat: support drop view in sqlite
1 parent b58d0a0 commit cfcbfdf

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

pegjs/mariadb.pegjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ drop_stmt
891891
keyword: r.toLowerCase(),
892892
prefix: ife,
893893
name: t,
894-
options: [{ type: 'origin', value: op }],
894+
options: op && [{ type: 'origin', value: op }],
895895
}
896896
};
897897
}

pegjs/mysql.pegjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ drop_stmt
10921092
keyword: r.toLowerCase(),
10931093
prefix: ife,
10941094
name: t,
1095-
options: [{ type: 'origin', value: op }],
1095+
options: op && [{ type: 'origin', value: op }],
10961096
}
10971097
};
10981098
}

pegjs/sqlite.pegjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ if_not_exists_stmt
289289
return 'IF NOT EXISTS'
290290
}
291291

292+
if_exists
293+
= 'if'i __ 'exists'i {
294+
return 'if exists'
295+
}
296+
292297
create_trigger_stmt
293298
= kw: KW_CREATE __
294299
tp:(KW_TEMPORARY / KW_TEMP)? __
@@ -622,6 +627,21 @@ drop_stmt
622627
}
623628
};
624629
}
630+
/ a:KW_DROP __
631+
r:KW_VIEW __
632+
ife:if_exists? __
633+
t:table_ref_list {
634+
return {
635+
tableList: Array.from(tableList),
636+
columnList: columnListTableAlias(columnList),
637+
ast: {
638+
type: a.toLowerCase(),
639+
keyword: r.toLowerCase(),
640+
prefix: ife,
641+
name: t,
642+
}
643+
};
644+
}
625645
/ a:KW_DROP __
626646
r:KW_INDEX __
627647
i:column_ref __

test/sqlite.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ describe('sqlite', () => {
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
})
157157

158-
it('should support create view', () => {
158+
it('should support create or drop view', () => {
159159
let sql = 'create view v1 as select * from t1'
160160
expect(getParsedSql(sql)).to.be.equal('CREATE VIEW `v1` AS SELECT * FROM `t1`')
161161
sql = 'create temp view if not exists s.v1(a, b, c) as select * from t1'
162162
expect(getParsedSql(sql)).to.be.equal('CREATE TEMP VIEW IF NOT EXISTS `s`.`v1` (`a`, `b`, `c`) AS SELECT * FROM `t1`')
163+
sql = 'DROP VIEW IF EXISTS view_name;',
164+
expect(getParsedSql(sql)).to.be.equal('DROP VIEW IF EXISTS `view_name`')
163165
})
164166
})

0 commit comments

Comments
 (0)