Skip to content

Commit d781fff

Browse files
committed
feat: support * in returning for pg
1 parent ed81938 commit d781fff

4 files changed

Lines changed: 18 additions & 15 deletions

File tree

pegjs/flinksql.pegjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,13 +1757,14 @@ set_item
17571757
return { column: c, value: v, table: tbl && tbl[0] };
17581758
}
17591759
returning_stmt
1760-
= k:KW_RETURNING __ c:column_ref_list {
1761-
// => { type: 'returning'; columns: column_ref_list; }
1760+
= k:KW_RETURNING __ c:(STAR / column_ref_list) {
1761+
// => { type: 'returning'; columns: column_ref_list | column_ref; }
17621762
return {
17631763
type: k && k.toLowerCase() || 'returning',
1764-
columns: c
1764+
columns: c === '*' && [{ type: 'columne_ref', table: null, column: '*' }] || c
17651765
}
17661766
}
1767+
17671768
insert_value_clause
17681769
= value_clause
17691770
/ select_stmt_nake
@@ -3106,4 +3107,3 @@ key_value_type
31063107

31073108
row_type
31083109
= t:KW_ROW { return {dataType: t} }
3109-

pegjs/postgresql.pegjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,13 +1833,14 @@ set_item
18331833
return { column: c, value: v, table: tbl && tbl[0] };
18341834
}
18351835
returning_stmt
1836-
= k:KW_RETURNING __ c:column_ref_list {
1837-
// => { type: 'returning'; columns: column_ref_list; }
1836+
= k:KW_RETURNING __ c:(STAR / column_ref_list) {
1837+
// => { type: 'returning'; columns: column_ref_list | column_ref; }
18381838
return {
18391839
type: k && k.toLowerCase() || 'returning',
1840-
columns: c
1840+
columns: c === '*' && [{ type: 'columne_ref', table: null, column: '*' }] || c
18411841
}
18421842
}
1843+
18431844
insert_value_clause
18441845
= value_clause
18451846
/ select_stmt_nake

src/insert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { setToSQL } from './update'
1111
function valuesToSQL(values) {
1212
if (values.type === 'select') return selectToSQL(values)
1313
const clauses = values.map(exprToSQL)
14-
return `(${clauses.join('),(')})`
14+
return `(${clauses.join('), (')})`
1515
}
1616

1717
function partitionToSQL(partition) {

test/insert.spec.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('insert', () => {
2828
const sql = 'INSERT INTO t1 values("1223", "name"), ("1224", "name2")'
2929
const ast = parser.astify(sql)
3030
const backSQL = parser.sqlify(ast)
31-
expect(backSQL).to.be.equal("INSERT INTO `t1` VALUES ('1223','name'),('1224','name2')")
31+
expect(backSQL).to.be.equal("INSERT INTO `t1` VALUES ('1223','name'), ('1224','name2')")
3232
})
3333

3434
it('should support parse insert from select', () => {
@@ -110,14 +110,14 @@ describe('insert', () => {
110110
const sql = 'INSERT into account partition(date, id) (id, name) values(123, "test"), (124, "test2")'
111111
const ast = parser.astify(sql)
112112
const backSQL = parser.sqlify(ast)
113-
expect(backSQL).to.be.equal("INSERT INTO `account` PARTITION(`date`, `id`) (`id`, `name`) VALUES (123,'test'),(124,'test2')")
113+
expect(backSQL).to.be.equal("INSERT INTO `account` PARTITION(`date`, `id`) (`id`, `name`) VALUES (123,'test'), (124,'test2')")
114114
})
115115

116116
it('should support parse insert on duplicate key update', () => {
117117
const sql = 'INSERT into account partition(date, id) (id, name) values(123, "test"), (124, "test2") on duplicate key update id = 123, name = "test"'
118118
const ast = parser.astify(sql)
119119
const backSQL = parser.sqlify(ast)
120-
expect(backSQL).to.be.equal("INSERT INTO `account` PARTITION(`date`, `id`) (`id`, `name`) VALUES (123,'test'),(124,'test2') ON DUPLICATE KEY UPDATE `id` = 123, `name` = 'test'")
120+
expect(backSQL).to.be.equal("INSERT INTO `account` PARTITION(`date`, `id`) (`id`, `name`) VALUES (123,'test'), (124,'test2') ON DUPLICATE KEY UPDATE `id` = 123, `name` = 'test'")
121121
})
122122

123123
it('should support parse insert set', () => {
@@ -131,7 +131,7 @@ describe('insert', () => {
131131
const sql = 'INSERT into account partition(date = 20191218, id = 2) (id, name) values(123, "test"), (124, "test2")'
132132
const ast = parser.astify(sql)
133133
const backSQL = parser.sqlify(ast)
134-
expect(backSQL).to.be.equal("INSERT INTO `account` PARTITION(`date` = 20191218, `id` = 2) (`id`, `name`) VALUES (123,'test'),(124,'test2')")
134+
expect(backSQL).to.be.equal("INSERT INTO `account` PARTITION(`date` = 20191218, `id` = 2) (`id`, `name`) VALUES (123,'test'), (124,'test2')")
135135
})
136136

137137
it('should support parse insert partition for hive', () => {
@@ -143,9 +143,11 @@ describe('insert', () => {
143143

144144
it('should support parse pg insert returning', () => {
145145
const sql = 'INSERT into account (date, id) values("2019-12-23", 123) returning id'
146-
const ast = parser.astify(sql, { database: 'postgresql' })
147-
const backSQL = parser.sqlify(ast)
148-
expect(backSQL).to.be.equal("INSERT INTO `account` (`date`, `id`) VALUES (\"2019-12-23\",123) RETURNING `id`")
146+
const opt = { database: 'postgresql' }
147+
const ast = parser.astify(sql, opt)
148+
const backSQL = parser.sqlify(ast, opt)
149+
expect(backSQL).to.be.equal('INSERT INTO "account" ("date", "id") VALUES ("2019-12-23",123) RETURNING "id"')
150+
expect(parser.sqlify(parser.astify(`INSERT INTO account (date, id) VALUES ("2019-12-23",123) RETURNING *`, opt), opt)).to.be.equal('INSERT INTO "account" ("date", "id") VALUES ("2019-12-23",123) RETURNING *')
149151
})
150152

151153
describe('support ascii pnCtrl single-char', () => {

0 commit comments

Comments
 (0)