Skip to content

Commit 792cf2d

Browse files
Merge pull request taozhi8833998#173 from taozhi8833998/feature-pg-returning
Feature pg returning
2 parents 0f599ae + d7c69b9 commit 792cf2d

8 files changed

Lines changed: 46 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sql-parser",
3-
"version": "1.6.16",
3+
"version": "1.6.17",
44
"description": "simple node sql parser",
55
"main": "index.js",
66
"types": "index.d.ts",

pegjs/mysql.pegjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2102,7 +2102,6 @@ var_decl
21022102
without_prefix_var_decl
21032103
= name:ident_name m:mem_chain {
21042104
//push for analysis
2105-
console.log('name===', name)
21062105
varList.push(name);
21072106
return {
21082107
type: 'var',

pegjs/postgresql.pegjs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,8 @@ update_stmt
10741074
t:table_ref_list __
10751075
KW_SET __
10761076
l:set_list __
1077-
w:where_clause? {
1077+
w:where_clause? __
1078+
r:returning_stmt? {
10781079
if (t) t.forEach(tableInfo => {
10791080
const { db, as, table } = tableInfo
10801081
tableList.add(`update::${db}::${table}`)
@@ -1089,7 +1090,8 @@ update_stmt
10891090
type: 'update',
10901091
table: t,
10911092
set: l,
1092-
where: w
1093+
where: w,
1094+
returning: r,
10931095
}
10941096
};
10951097
}
@@ -1137,7 +1139,13 @@ set_item
11371139
= tbl:(ident __ DOT)? __ c:column __ '=' __ v:additive_expr {
11381140
return { column: c, value: v, table: tbl && tbl[0] };
11391141
}
1140-
1142+
returning_stmt
1143+
= k:KW_RETURNING __ c:column_ref_list {
1144+
return {
1145+
type: k && k.toLowerCase() || 'returning',
1146+
columns: c
1147+
}
1148+
}
11411149
insert_value_clause
11421150
= value_clause
11431151
/ select_stmt_nake
@@ -1155,7 +1163,8 @@ replace_insert_stmt
11551163
KW_INTO? __
11561164
t:table_name __
11571165
p:insert_partition? __ LPAREN __ c:column_list __ RPAREN __
1158-
v:insert_value_clause {
1166+
v:insert_value_clause __
1167+
r:returning_stmt? {
11591168
if (t) {
11601169
tableList.add(`insert::${t.db}::${t.table}`)
11611170
t.as = null
@@ -1173,6 +1182,7 @@ replace_insert_stmt
11731182
columns: c,
11741183
values: v,
11751184
partition: p,
1185+
returning: r,
11761186
}
11771187
};
11781188
}
@@ -1182,7 +1192,8 @@ insert_no_columns_stmt
11821192
KW_INTO __
11831193
t:table_name __
11841194
p:insert_partition? __
1185-
v:insert_value_clause {
1195+
v:insert_value_clause __
1196+
r:returning_stmt? {
11861197
if (t) {
11871198
tableList.add(`insert::${t.db}::${t.table}`)
11881199
columnList.add(`insert::${t.table}::(.*)`);
@@ -1197,6 +1208,7 @@ insert_no_columns_stmt
11971208
columns: null,
11981209
values: v,
11991210
partition: p,
1211+
returning: r,
12001212
}
12011213
};
12021214
}
@@ -1783,6 +1795,7 @@ KW_DELETE = "DELETE"i !ident_start
17831795
KW_INSERT = "INSERT"i !ident_start
17841796
KW_RECURSIVE= "RECURSIVE" !ident_start
17851797
KW_REPLACE = "REPLACE"i !ident_start
1798+
KW_RETURNING = "RETURNING"i !ident_start { return 'RETURNING' }
17861799
KW_RENAME = "RENAME"i !ident_start
17871800
KW_IGNORE = "IGNORE"i !ident_start
17881801
KW_EXPLAIN = "EXPLAIN"i !ident_start

src/insert.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { tablesToSQL } from './tables'
22
import { exprToSQL } from './expr'
3-
import { identifierToSql, commonOptionConnector, hasVal, toUpper } from './util'
3+
import { identifierToSql, commonOptionConnector, hasVal, toUpper, returningToSQL } from './util'
44
import { selectToSQL } from './select'
55

66
/**
@@ -34,11 +34,13 @@ function insertToSQL(stmt) {
3434
values,
3535
where,
3636
partition,
37+
returning,
3738
} = stmt
3839
const clauses = ['INSERT', toUpper(prefix), tablesToSQL(table), partitionToSQL(partition)]
3940
if (Array.isArray(columns)) clauses.push(`(${columns.map(identifierToSql).join(', ')})`)
4041
clauses.push(commonOptionConnector(Array.isArray(values) ? 'VALUES' : '', valuesToSQL, values))
4142
clauses.push(commonOptionConnector('WHERE', exprToSQL, where))
43+
clauses.push(returningToSQL(returning))
4244
return clauses.filter(hasVal).join(' ')
4345
}
4446

src/update.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { tablesToSQL } from './tables'
22
import { exprToSQL } from './expr'
3-
import { hasVal, identifierToSql, commonOptionConnector } from './util'
3+
import { hasVal, identifierToSql, commonOptionConnector, returningToSQL } from './util'
44

55
/**
66
* @param {Array} sets
@@ -20,12 +20,13 @@ function setToSQL(sets) {
2020
}
2121

2222
function updateToSQL(stmt) {
23-
const { table, set, where } = stmt
23+
const { table, set, where, returning } = stmt
2424
const clauses = [
2525
'UPDATE',
2626
tablesToSQL(table),
2727
commonOptionConnector('SET', setToSQL, set),
2828
commonOptionConnector('WHERE', exprToSQL, where),
29+
returningToSQL(returning),
2930
]
3031
return clauses.filter(hasVal).join(' ')
3132
}

src/util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ function commentToSQL(comment) {
209209
return result.join(' ')
210210
}
211211

212+
function returningToSQL(returning) {
213+
if (!returning) return ''
214+
const { columns } = returning
215+
return ['RETURNING', columns.map(columnRefToSQL).filter(hasVal).join(', ')].join(' ')
216+
}
217+
212218
export {
213219
commonOptionConnector,
214220
connector,
@@ -222,6 +228,7 @@ export {
222228
literalToSQL,
223229
identifierToSql,
224230
replaceParams,
231+
returningToSQL,
225232
hasVal,
226233
setParserOpt,
227234
toUpper,

test/insert.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,11 @@ describe('insert', () => {
126126
const backSQL = parser.sqlify(ast)
127127
expect(backSQL).to.be.equal("INSERT OVERWRITE TABLE `account` PARTITION(`date`, `id`) SELECT * FROM `tmp`")
128128
})
129+
130+
it('should support parse pg insert returning', () => {
131+
const sql = 'INSERT into account (date, id) values("2019-12-23", 123) returning id'
132+
const ast = parser.astify(sql, { database: 'postgresql' })
133+
const backSQL = parser.sqlify(ast)
134+
expect(backSQL).to.be.equal("INSERT INTO `account` (`date`, `id`) VALUES ('2019-12-23',123) RETURNING `id`")
135+
})
129136
});

test/update.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,11 @@ describe('update', () => {
193193
}
194194
});
195195
})
196+
197+
it('should support parse pg update returning', () => {
198+
const sql = 'update account set id = 1 where name = "abc" returning id'
199+
const ast = parser.astify(sql, { database: 'postgresql' })
200+
const backSQL = parser.sqlify(ast)
201+
expect(backSQL).to.be.equal("UPDATE `account` SET `id` = 1 WHERE `name` = 'abc' RETURNING `id`")
202+
})
196203
});

0 commit comments

Comments
 (0)