Skip to content

Commit 2d44f68

Browse files
committed
fix: support insert or action in sqlite
1 parent 63c5685 commit 2d44f68

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

pegjs/sqlite.pegjs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,7 @@ replace_insert_stmt
18481848
tableList: Array.from(tableList),
18491849
columnList: columnListTableAlias(columnList),
18501850
ast: {
1851-
type: ri,
1851+
...ri,
18521852
table: [t],
18531853
columns: c,
18541854
values: v,
@@ -1877,7 +1877,7 @@ insert_no_columns_stmt
18771877
tableList: Array.from(tableList),
18781878
columnList: columnListTableAlias(columnList),
18791879
ast: {
1880-
type: ri,
1880+
...ri,
18811881
table: [t],
18821882
columns: null,
18831883
values: v,
@@ -1905,7 +1905,7 @@ insert_into_set
19051905
tableList: Array.from(tableList),
19061906
columnList: columnListTableAlias(columnList),
19071907
ast: {
1908-
type: ri,
1908+
...ri,
19091909
table: [t],
19101910
columns: null,
19111911
partition: p,
@@ -1924,8 +1924,28 @@ on_duplicate_update_stmt
19241924
}
19251925

19261926
replace_insert
1927-
= KW_INSERT { return 'insert'; }
1928-
/ KW_REPLACE { return 'replace'; }
1927+
= KW_INSERT tail:(__ KW_OR __ ('ABORT'i / 'FAIL'i / 'IGNORE'i / 'REPLACE'i / 'ROLLBACK'i))? {
1928+
const result = {
1929+
type: 'insert',
1930+
}
1931+
if (!tail || tail.length === 0) {
1932+
return result;
1933+
}
1934+
result.or = [
1935+
{
1936+
type: 'origin',
1937+
value: 'or',
1938+
},
1939+
{
1940+
type: 'origin',
1941+
value: tail[3],
1942+
}
1943+
]
1944+
return result
1945+
}
1946+
/ KW_REPLACE {
1947+
return { type: 'replace' }
1948+
}
19291949

19301950
value_clause
19311951
= KW_VALUES __ l:value_list { return l; }

src/insert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function insertToSQL(stmt) {
6363
const {
6464
table,
6565
type,
66+
or: orExpr = [],
6667
prefix = 'into',
6768
columns,
6869
conflict,
@@ -74,7 +75,7 @@ function insertToSQL(stmt) {
7475
set,
7576
} = stmt
7677
const { keyword, set: duplicateSet } = onDuplicateUpdate || {}
77-
const clauses = [toUpper(type), toUpper(prefix), tablesToSQL(table), partitionToSQL(partition)]
78+
const clauses = [toUpper(type), orExpr.map(literalToSQL).join(' '), toUpper(prefix), tablesToSQL(table), partitionToSQL(partition)]
7879
if (Array.isArray(columns)) clauses.push(`(${columns.map(literalToSQL).join(', ')})`)
7980
clauses.push(commonOptionConnector(Array.isArray(values) ? 'VALUES' : '', valuesToSQL, values))
8081
clauses.push(commonOptionConnector('ON CONFLICT', conflictToSQL, conflict))

test/sqlite.spec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,11 @@ describe('sqlite', () => {
238238
const sql = `DELETE FROM users WHERE last_login > ? RETURNING id, email as email_address`
239239
expect(getParsedSql(sql)).to.be.equal(`DELETE FROM "users" WHERE "last_login" > ? RETURNING "id", "email" AS "email_address"`)
240240
})
241-
241+
it('should support insert or replace', () => {
242+
const keywords = ['ABORT', 'FAIL', 'IGNORE', 'REPLACE', 'ROLLBACK']
243+
keywords.forEach(keyword => {
244+
const sql = `INSERT OR ${keyword} INTO test (category) VALUES ('Infra, Layer1, DePIN')`
245+
expect(getParsedSql(sql)).to.be.equal(`INSERT OR ${keyword} INTO "test" (category) VALUES ('Infra, Layer1, DePIN')`)
246+
})
247+
})
242248
})

0 commit comments

Comments
 (0)