Skip to content

Commit 950bd75

Browse files
committed
refactor: support replace ast to sql, fix string constants with c-style escape in pg
1 parent 4be1e38 commit 950bd75

7 files changed

Lines changed: 17 additions & 5 deletions

File tree

pegjs/postgresql.pegjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,7 @@ single_char
31303130
/ escape_char
31313131

31323132
escape_char
3133-
= "\\'" { return "'"; }
3133+
= "\\'" { return "\\'"; }
31343134
/ '\\"' { return '"'; }
31353135
/ "\\\\" { return "\\"; }
31363136
/ "\\/" { return "/"; }

src/insert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function partitionToSQL(partition) {
3030
function insertToSQL(stmt) {
3131
const {
3232
table,
33+
type,
3334
prefix = 'into',
3435
columns,
3536
values,
@@ -40,7 +41,7 @@ function insertToSQL(stmt) {
4041
set,
4142
} = stmt
4243
const { keyword, set: duplicateSet } = onDuplicateUpdate || {}
43-
const clauses = ['INSERT', toUpper(prefix), tablesToSQL(table), partitionToSQL(partition)]
44+
const clauses = [toUpper(type), toUpper(prefix), tablesToSQL(table), partitionToSQL(partition)]
4445
if (Array.isArray(columns)) clauses.push(`(${columns.map(identifierToSql).join(', ')})`)
4546
clauses.push(commonOptionConnector(Array.isArray(values) ? 'VALUES' : '', valuesToSQL, values))
4647
clauses.push(commonOptionConnector('SET', setToSQL, set))

src/sql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { bigQueryToSQL, unionToSQL, multipleToSQL } from './union'
22

3-
const surportedTypes = ['analyze', 'attach', 'select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'bigquery', 'declare', 'show']
3+
const surportedTypes = ['analyze', 'attach', 'select', 'delete', 'update', 'insert', 'drop', 'rename', 'truncate', 'call', 'desc', 'use', 'alter', 'set', 'create', 'lock', 'unlock', 'bigquery', 'declare', 'show', 'replace']
44

55
function checkSupported(expr) {
66
const ast = expr && expr.ast ? expr.ast : expr

src/union.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const typeToSQLFn = {
3232
insert : insertToSQL,
3333
drop : commonCmdToSQL,
3434
truncate : commonCmdToSQL,
35+
replace : insertToSQL,
3536
declare : declareToSQL,
3637
use : useToSQL,
3738
rename : renameToSQL,

test/insert.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const Parser = require('../src/parser').default
44
describe('insert', () => {
55
const parser = new Parser();
66

7+
function getParsedSql(sql, opt) {
8+
const ast = parser.astify(sql, opt);
9+
return parser.sqlify(ast, opt);
10+
}
11+
712
it('should parse baisc usage', () => {
813
const { tableList, columnList, ast } = parser.parse('INSERT INTO t (col1, col2) VALUES (1, 2)');
914
expect(tableList).to.eql(['insert::null::t']);
@@ -163,6 +168,11 @@ describe('insert', () => {
163168
(X'ax')`))).to.be.equal("INSERT INTO `t` (`a`) VALUES (X'ax')")
164169
})
165170

171+
it('should support replace into', () => {
172+
const sql = "REPLACE INTO test (test_column1, test_column2) VALUES ('testvalue1', 'testvalue2')"
173+
expect(getParsedSql(sql)).to.be.equal("REPLACE INTO `test` (`test_column1`, `test_column2`) VALUES ('testvalue1','testvalue2')")
174+
})
175+
166176
describe('support ascii pnCtrl single-char', () => {
167177
it('should support ascii pnCtrl', () => {
168178
// 0~31及127(共33个)是控制字符或通信专用字符 \0-\x1F和\x7f in ascii, ETX ascii code is 0x03

test/postgres.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ describe('Postgres', () => {
434434
title: 'String Constants with C-Style Escapes',
435435
sql: [
436436
`SELECT E'\\''`,
437-
`SELECT E'\''`
437+
`SELECT E'\\''`
438438
]
439439
},
440440
]

test/select.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('select', () => {
77
function getParsedSql(sql, opt) {
88
const ast = parser.astify(sql, opt);
99
return parser.sqlify(ast, opt);
10-
}
10+
}
1111

1212
it('should be null if empty', () => {
1313
const ast = parser.astify('SELECT a');

0 commit comments

Comments
 (0)