Skip to content

Commit

Permalink
Fix formatting of ON CONFLICT clause (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Dec 15, 2022
2 parents 2b430c7 + f2b2f95 commit aa4688a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/languages/mariadb/mariadb.formatter.ts
Expand Up @@ -92,7 +92,6 @@ const onelineClauses = expandPhrases([
'CREATE USER',
'DEALLOCATE PREPARE',
'DESCRIBE',
'DO',
'DROP DATABASE',
'DROP EVENT',
'DROP FUNCTION',
Expand Down
1 change: 0 additions & 1 deletion src/languages/mysql/mysql.formatter.ts
Expand Up @@ -87,7 +87,6 @@ const onelineClauses = expandPhrases([
'CREATE USER',
'DEALLOCATE PREPARE',
'DESCRIBE',
'DO',
'DROP DATABASE',
'DROP EVENT',
'DROP FUNCTION',
Expand Down
3 changes: 2 additions & 1 deletion src/languages/postgresql/postgresql.formatter.ts
Expand Up @@ -36,6 +36,8 @@ const onelineClauses = expandPhrases([
// - update:
'UPDATE [ONLY]',
'WHERE CURRENT OF',
// - insert:
'ON CONFLICT',
// - delete:
'DELETE FROM [ONLY]',
// - drop table:
Expand Down Expand Up @@ -150,7 +152,6 @@ const onelineClauses = expandPhrases([
'DEALLOCATE',
'DECLARE',
'DISCARD',
'DO',
'DROP ACCESS METHOD',
'DROP AGGREGATE',
'DROP CAST',
Expand Down
1 change: 0 additions & 1 deletion src/languages/singlestoredb/singlestoredb.formatter.ts
Expand Up @@ -89,7 +89,6 @@ const onelineClauses = expandPhrases([
'DESCRIBE',
'DETACH DATABASE',
'DETACH PIPELINE',
'DO',
'DROP DATABASE',
'DROP FUNCTION',
'DROP INDEX',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/sqlite/sqlite.formatter.ts
Expand Up @@ -32,6 +32,8 @@ const reservedClauses = expandPhrases([
const onelineClauses = expandPhrases([
// - update:
'UPDATE [OR ABORT | OR FAIL | OR IGNORE | OR REPLACE | OR ROLLBACK]',
// - insert:
'ON CONFLICT',
// - delete:
'DELETE FROM',
// - drop table:
Expand Down
16 changes: 16 additions & 0 deletions test/features/onConflict.ts
@@ -0,0 +1,16 @@
import dedent from 'dedent-js';

import { FormatFn } from '../../src/sqlFormatter.js';

export default function supportsOnConflict(format: FormatFn) {
// Regression test for issue #535
it('supports INSERT .. ON CONFLICT syntax', () => {
expect(format(`INSERT INTO tbl VALUES (1,'Blah') ON CONFLICT DO NOTHING;`)).toBe(dedent`
INSERT INTO
tbl
VALUES
(1, 'Blah')
ON CONFLICT DO NOTHING;
`);
});
}
2 changes: 2 additions & 0 deletions test/postgresql.test.ts
Expand Up @@ -25,6 +25,7 @@ import supportsInsertInto from './features/insertInto.js';
import supportsUpdate from './features/update.js';
import supportsTruncateTable from './features/truncateTable.js';
import supportsCreateView from './features/createView.js';
import supportsOnConflict from './features/onConflict.js';

describe('PostgreSqlFormatter', () => {
const language = 'postgresql';
Expand All @@ -45,6 +46,7 @@ describe('PostgreSqlFormatter', () => {
});
supportsDeleteFrom(format);
supportsInsertInto(format);
supportsOnConflict(format);
supportsUpdate(format, { whereCurrentOf: true });
supportsTruncateTable(format, { withoutTable: true });
supportsStrings(format, ["''-qq", "U&''", "X''", "B''"]);
Expand Down
2 changes: 2 additions & 0 deletions test/sqlite.test.ts
Expand Up @@ -21,6 +21,7 @@ import supportsLimiting from './features/limiting.js';
import supportsInsertInto from './features/insertInto.js';
import supportsUpdate from './features/update.js';
import supportsCreateView from './features/createView.js';
import supportsOnConflict from './features/onConflict.js';

describe('SqliteFormatter', () => {
const language = 'sqlite';
Expand All @@ -40,6 +41,7 @@ describe('SqliteFormatter', () => {
});
supportsDeleteFrom(format);
supportsInsertInto(format);
supportsOnConflict(format);
supportsUpdate(format);
supportsStrings(format, ["''-qq", "X''"]);
supportsIdentifiers(format, [`""-qq`, '``', '[]']);
Expand Down

0 comments on commit aa4688a

Please sign in to comment.