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 | 138x 1020x 323x 697x 11x 1x 10x 1019x | import { SqlCompiler } from '../sql/compile.ts';
import { DdlGuards } from './ddl-guards.ts';
import type { IndexField } from './ddl-types.ts';
const guards = new DdlGuards();
/**
* One element of an index or EXCLUDE constraint: a quoted column or a trusted SQL expression with
* collation, operator class, order and nulls ordering.
*
* @internal
*/
export class IndexElementDdl {
/**
* SQL of an element.
*
* @param field - Column name or field object.
* @param operator - Default operator class.
* @returns Element SQL.
* @throws Error for an empty expression or an invalid operator class.
*/
elementSql(field: IndexField, operator: string | undefined): string {
if (typeof field === 'string') {
return `${SqlCompiler.quoteIdent(field)}${this.#operatorClass(operator)}`;
}
return [
field.expression === undefined
? SqlCompiler.quoteIdent(field.name)
: this.#expression(field.expression),
field.collate === undefined ? '' : ` COLLATE ${SqlCompiler.quoteIdent(field.collate)}`,
this.#operatorClass(field.operator ?? operator),
field.order === undefined ? '' : ` ${field.order === 'DESC' ? 'DESC' : 'ASC'}`,
field.nulls === undefined ? '' : ` NULLS ${field.nulls === 'FIRST' ? 'FIRST' : 'LAST'}`,
].join('');
}
#expression(expression: string): string {
if (typeof expression !== 'string' || expression.trim() === '') {
throw new Error('Index expression must be a non-empty SQL string');
}
return `(${expression})`;
}
#operatorClass(operator: string | undefined): string {
return operator === undefined ? '' : ` ${guards.word(operator, 'operator class')}`;
}
}
|