Skip to content

Commit 889540c

Browse files
committed
Improve ON DELETE/ON UPDATE constraints formatting
Properly handle the SET NULL and SET DEFAULT after it
1 parent 5cddff5 commit 889540c

20 files changed

+40
-28
lines changed

src/languages/db2/db2.formatter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ const reservedJoins = expandPhrases([
166166
'{INNER | CROSS} JOIN',
167167
]);
168168

169-
const reservedPhrases = expandPhrases(['ON DELETE', 'ON UPDATE', '{ROWS | RANGE} BETWEEN']);
169+
const reservedPhrases = expandPhrases([
170+
'ON DELETE',
171+
'ON UPDATE',
172+
'SET NULL',
173+
'{ROWS | RANGE} BETWEEN',
174+
]);
170175

171176
// https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/db2/rbafzintro.htm
172177
export default class Db2Formatter extends Formatter {

src/languages/mariadb/mariadb.formatter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ const reservedJoins = expandPhrases([
254254
]);
255255

256256
const reservedPhrases = expandPhrases([
257-
'ON DELETE',
258-
'ON UPDATE',
257+
'ON {UPDATE | DELETE} [SET NULL | SET DEFAULT]',
259258
'CHARACTER SET',
260259
'{ROWS | RANGE} BETWEEN',
261260
]);

src/languages/mysql/mysql.formatter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ const reservedJoins = expandPhrases([
222222
]);
223223

224224
const reservedPhrases = expandPhrases([
225-
'ON DELETE',
226-
'ON UPDATE',
225+
'ON {UPDATE | DELETE} [SET NULL]',
227226
'CHARACTER SET',
228227
'{ROWS | RANGE} BETWEEN',
229228
]);

src/languages/plsql/plsql.formatter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ const reservedJoins = expandPhrases([
7373
]);
7474

7575
const reservedPhrases = expandPhrases([
76-
'ON DELETE',
77-
'ON UPDATE',
76+
'ON {UPDATE | DELETE} [SET NULL]',
7877
'ON COMMIT',
7978
'{ROWS | RANGE} BETWEEN',
8079
]);

src/languages/postgresql/postgresql.formatter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ const reservedJoins = expandPhrases([
239239
]);
240240

241241
const reservedPhrases = expandPhrases([
242-
'ON DELETE',
243-
'ON UPDATE',
242+
'ON {UPDATE | DELETE} [SET NULL | SET DEFAULT]',
244243
'{ROWS | RANGE | GROUPS} BETWEEN',
245244
// https://www.postgresql.org/docs/current/datatype-datetime.html
246245
'{TIMESTAMP | TIME} {WITH | WITHOUT} TIME ZONE',

src/languages/snowflake/snowflake.formatter.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ const reservedJoins = expandPhrases([
310310

311311
const reservedPhrases = expandPhrases([
312312
'{ROWS | RANGE} BETWEEN',
313-
'MATCH {FULL | SIMPLE | PARTIAL}',
314-
'ON {UPDATE | DELETE} {CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION}',
315-
'INITIALLY {DEFERRED | IMMEDIATE}',
313+
'ON {UPDATE | DELETE} [SET NULL | SET DEFAULT]',
316314
]);
317315

318316
export default class SnowflakeFormatter extends Formatter {

src/languages/sql/sql.formatter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ const reservedJoins = expandPhrases([
6565
'NATURAL {LEFT | RIGHT | FULL} [OUTER] JOIN',
6666
]);
6767

68-
const reservedPhrases = expandPhrases(['ON DELETE', 'ON UPDATE', '{ROWS | RANGE} BETWEEN']);
68+
const reservedPhrases = expandPhrases([
69+
'ON {UPDATE | DELETE} [SET NULL | SET DEFAULT]',
70+
'{ROWS | RANGE} BETWEEN',
71+
]);
6972

7073
export default class SqlFormatter extends Formatter {
7174
tokenizer() {

src/languages/sqlite/sqlite.formatter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ const reservedJoins = expandPhrases([
5555
]);
5656

5757
const reservedPhrases = expandPhrases([
58-
'ON DELETE',
59-
'ON UPDATE',
58+
'ON {UPDATE | DELETE} [SET NULL | SET DEFAULT]',
6059
'{ROWS | RANGE | GROUPS} BETWEEN',
6160
]);
6261

src/languages/transactsql/transactsql.formatter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ const reservedJoins = expandPhrases([
211211
'{CROSS | OUTER} APPLY',
212212
]);
213213

214-
const reservedPhrases = expandPhrases(['ON DELETE', 'ON UPDATE', '{ROWS | RANGE} BETWEEN']);
214+
const reservedPhrases = expandPhrases([
215+
'ON {UPDATE | DELETE} [SET NULL | SET DEFAULT]',
216+
'{ROWS | RANGE} BETWEEN',
217+
]);
215218

216219
// https://docs.microsoft.com/en-us/sql/t-sql/language-reference?view=sql-server-ver15
217220
export default class TransactSqlFormatter extends Formatter {

test/behavesLikeMariaDbFormatter.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import behavesLikeSqlFormatter from './behavesLikeSqlFormatter';
55

66
import supportsDropTable from './features/dropTable';
77
import supportsBetween from './features/between';
8-
import supportsConstraints from './features/constraints';
98
import supportsDeleteFrom from './features/deleteFrom';
109
import supportsComments from './features/comments';
1110
import supportsStrings from './features/strings';
@@ -23,7 +22,6 @@ export default function behavesLikeMariaDbFormatter(format: FormatFn) {
2322
supportsStrings(format, ["''-qq", "''-bs", '""-qq', '""-bs', "X''"]);
2423
supportsIdentifiers(format, ['``']);
2524
supportsDropTable(format, { ifExists: true });
26-
supportsConstraints(format);
2725
supportsDeleteFrom(format);
2826
supportsInsertInto(format, { withoutInto: true });
2927
supportsUpdate(format);

0 commit comments

Comments
 (0)