Skip to content

Commit 72f3924

Browse files
mnahkiestaozhi8833998
authored andcommitted
feat(sqlite): support returning on insert, update and delete
1 parent b9ff76a commit 72f3924

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

pegjs/sqlite.pegjs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ column_order
409409
order_by: o && o.toLowerCase(),
410410
}
411411
}
412-
412+
413413
create_index_stmt
414414
= a:KW_CREATE __
415415
kw:(KW_UNIQUE)? __
@@ -436,7 +436,7 @@ create_index_stmt
436436
}
437437
}
438438
}
439-
439+
440440
view_with
441441
= KW_WITH __ c:("CASCADED"i / "LOCAL"i) __ "CHECK"i __ "OPTION" {
442442
return `with ${c.toLowerCase()} check option`
@@ -1709,6 +1709,7 @@ update_stmt
17091709
KW_SET __
17101710
l:set_list __
17111711
w:where_clause? __
1712+
r:returning_stmt? __
17121713
or:order_by_clause? __
17131714
lc:limit_clause? {
17141715
const dbObj = {}
@@ -1737,6 +1738,7 @@ update_stmt
17371738
table: t,
17381739
set: l,
17391740
where: w,
1741+
returning: r,
17401742
orderby: or,
17411743
limit: lc,
17421744
}
@@ -1748,6 +1750,7 @@ delete_stmt
17481750
t: table_ref_list? __
17491751
f:from_clause __
17501752
w:where_clause? __
1753+
r:returning_stmt? __
17511754
or:order_by_clause? __
17521755
l:limit_clause? {
17531756
if(f) f.forEach(tableInfo => {
@@ -1773,6 +1776,7 @@ delete_stmt
17731776
table: t,
17741777
from: f,
17751778
where: w,
1779+
returning: r,
17761780
orderby: or,
17771781
limit: l,
17781782
}
@@ -1796,6 +1800,15 @@ set_item
17961800
return { column: c, value: v, table: tbl && tbl[0], keyword: 'values' };
17971801
}
17981802

1803+
returning_stmt
1804+
= k:KW_RETURNING __ c:(column_clause / select_stmt) {
1805+
// => { type: 'returning'; columns: column_clause | select_stmt; }
1806+
return {
1807+
type: k && k.toLowerCase() || 'returning',
1808+
columns: c === '*' && [{ type: 'expr', expr: { type: 'column_ref', table: null, column: '*' }, as: null }] || c
1809+
}
1810+
}
1811+
17991812
insert_value_clause
18001813
= value_clause
18011814
/ select_stmt_nake
@@ -1814,7 +1827,8 @@ replace_insert_stmt
18141827
t:table_name __
18151828
p:insert_partition? __ LPAREN __ c:column_list __ RPAREN __
18161829
v:insert_value_clause __
1817-
odp:on_duplicate_update_stmt? {
1830+
odp:on_duplicate_update_stmt? __
1831+
r:returning_stmt? {
18181832
if (t) {
18191833
tableList.add(`insert::${t.db}::${t.table}`)
18201834
t.as = null
@@ -1840,6 +1854,7 @@ replace_insert_stmt
18401854
values: v,
18411855
partition: p,
18421856
on_duplicate_update: odp,
1857+
returning: r,
18431858
}
18441859
};
18451860
}
@@ -2695,6 +2710,7 @@ KW_INSERT = "INSERT"i !ident_start
26952710
KW_RECURSIVE= "RECURSIVE"i !ident_start
26962711
KW_REPLACE = "REPLACE"i !ident_start
26972712
KW_RENAME = "RENAME"i !ident_start
2713+
KW_RETURNING = "RETURNING"i !ident_start { return 'RETURNING' }
26982714
KW_IGNORE = "IGNORE"i !ident_start
26992715
KW_EXPLAIN = "EXPLAIN"i !ident_start
27002716
KW_PARTITION = "PARTITION"i !ident_start { return 'PARTITION' }

test/sqlite.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,30 @@ describe('sqlite', () => {
213213
);`
214214
expect(getParsedSql(sql)).to.be.equal(`CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" ("MigrationId" TEXT NOT NULL CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY, "ProductVersion" TEXT NOT NULL)`)
215215
})
216+
217+
it('should support INSERT ... RETURNING *', () => {
218+
const sql = `INSERT INTO users (email) VALUES (?) RETURNING *`
219+
expect(getParsedSql(sql)).to.be.equal(`INSERT INTO "users" (email) VALUES (?) RETURNING *`)
220+
})
221+
it('should support INSERT ... RETURNING specific columns', () => {
222+
const sql = `INSERT INTO users (email) VALUES (?) RETURNING id, email as email_address`
223+
expect(getParsedSql(sql)).to.be.equal(`INSERT INTO "users" (email) VALUES (?) RETURNING "id", "email" AS "email_address"`)
224+
})
225+
it('should support UPDATE ... RETURNING *', () => {
226+
const sql = `UPDATE users SET email = ? RETURNING *`
227+
expect(getParsedSql(sql)).to.be.equal(`UPDATE "users" SET "email" = ? RETURNING *`)
228+
})
229+
it('should support UPDATE ... RETURNING specific columns', () => {
230+
const sql = `UPDATE users SET email = ? RETURNING id, email as email_address`
231+
expect(getParsedSql(sql)).to.be.equal(`UPDATE "users" SET "email" = ? RETURNING "id", "email" AS "email_address"`)
232+
})
233+
it('should support DELETE ... RETURNING *', () => {
234+
const sql = `DELETE FROM users WHERE last_login > ? RETURNING *`
235+
expect(getParsedSql(sql)).to.be.equal(`DELETE FROM "users" WHERE "last_login" > ? RETURNING *`)
236+
})
237+
it('should support DELETE ... RETURNING *', () => {
238+
const sql = `DELETE FROM users WHERE last_login > ? RETURNING id, email as email_address`
239+
expect(getParsedSql(sql)).to.be.equal(`DELETE FROM "users" WHERE "last_login" > ? RETURNING "id", "email" AS "email_address"`)
240+
})
241+
216242
})

0 commit comments

Comments
 (0)