Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 138x 138x 138x 138x 414x 11x 2x 9x 160x 160x 1x 159x 381x 293x 88x 313x 81x 232x 279x 54x 225x 167x | import { Deferrable, type DeferrableValue } from './ddl-types.ts';
const ACTIONS: ReadonlySet<string> = new Set([
'CASCADE',
'SET NULL',
'SET DEFAULT',
'RESTRICT',
'NO ACTION',
]);
const IDENTIFIER_PATTERN = /^[A-Za-z_][\w]*$/;
const OPERATOR_PATTERN = /^[-+*/<>=~!@#%^&|`?]{1,63}$/;
// Only the constraint modes: SET_DEFERRED / SET_IMMEDIATE are `SET CONSTRAINTS` statements, not DDL clauses.
const DEFERRABLE_SQL: ReadonlySet<string> = new Set(
[Deferrable.INITIALLY_IMMEDIATE, Deferrable.INITIALLY_DEFERRED, Deferrable.NOT].map(
(value) => value.sql,
),
);
/**
* Allowlists for the DDL keywords that cannot be quoted: referential actions, deferrable modes,
* index methods and operator classes (ADR-020).
*
* @internal
*/
export class DdlGuards {
/**
* SQL of a `Deferrable` value; anything that is not one of them is rejected.
*
* @param value - Deferrable value.
* @returns SQL of the mode.
* @throws Error for anything but a `Deferrable` constant.
*/
deferrableSql(value: DeferrableValue): string {
if (!DEFERRABLE_SQL.has((value as { sql?: unknown } | undefined)?.sql as string)) {
throw new Error(`Invalid deferrable: use one of the Deferrable constants`);
}
return value.sql;
}
/**
* Normalized referential action (`ON DELETE` / `ON UPDATE`).
*
* @param action - Action as given.
* @returns Upper-case action.
* @throws Error for an action outside the allowlist.
*/
action(action: string): string {
const normalized = action.trim().toUpperCase();
if (!ACTIONS.has(normalized)) {
throw new Error(`Invalid referential action: ${action}`);
}
return normalized;
}
/**
* A plain identifier word (index method, operator class), rendered unquoted.
*
* @param value - Word.
* @param kind - Kind named in the error.
* @returns The word.
* @throws Error for anything but `[A-Za-z_][\w]*`.
*/
word(value: string, kind: string): string {
if (!IDENTIFIER_PATTERN.test(value)) {
throw new Error(`Invalid ${kind}: ${value}`);
}
return value;
}
/**
* An operator symbol (EXCLUDE `WITH` operator), rendered unquoted.
*
* @param value - Operator such as `&&` or `=`.
* @returns The operator.
* @throws Error for anything but 1-63 operator characters, or one that opens a comment.
*/
operator(value: string): string {
if (!OPERATOR_PATTERN.test(value) || value.includes('--') || value.includes('/*')) {
throw new Error(`Invalid operator: ${value}`);
}
return value;
}
/**
* A whole number rendered into DDL (sequence bounds, cache size); anything else is rejected.
*
* @param value - Number or bigint.
* @param kind - Kind named in the error.
* @returns Decimal text of the number.
* @throws Error for non-integers and unsafe numbers.
*/
integer(value: number | bigint, kind: string): string {
if (typeof value === 'bigint' || Number.isSafeInteger(value)) {
return value.toString();
}
throw new Error(`Invalid ${kind}: expected a whole number`);
}
/**
* ` ON DELETE ... ON UPDATE ... <deferrable>` suffix of a foreign key; empty parts are omitted.
*
* @param onDelete - `ON DELETE` action.
* @param onUpdate - `ON UPDATE` action.
* @param deferrable - Deferrable mode.
* @returns SQL suffix.
*/
foreignKeySuffix(
onDelete: string | undefined,
onUpdate: string | undefined,
deferrable: DeferrableValue | undefined,
): string {
return [
onDelete === undefined ? '' : ` ON DELETE ${this.action(onDelete)}`,
onUpdate === undefined ? '' : ` ON UPDATE ${this.action(onUpdate)}`,
deferrable === undefined ? '' : ` ${this.deferrableSql(deferrable)}`,
].join('');
}
}
|