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 123 124 125 126 127 128 129 130 131 132 133 | 148x 148x 6471x 16x 6455x 118x 1363x 1x 375x 4598x 957x 6208x 309x 7261x 5483x 5483x 1363x 2x 1361x 375x 262x 113x 2x 111x 48x 686x 63x 4923x 3x 4920x 4920x | import { SqlCompiler, type CompiledQuery } from '../sql/compile.ts';
import type { ExprNode } from '../sql/ast.ts';
/** Table reference accepted by QueryInterface: a name or `{ tableName, schema }`. */
export type TableName = string | QualifiedTable;
/** Schema-qualified table reference. */
export type QualifiedTable = Readonly<{
/** Table name. */
tableName: string;
/** Schema name. */
schema?: string;
}>;
const compiler = new SqlCompiler();
const PLACEHOLDER = /'(?:[^']|'')*'|"(?:[^"]|"")*"|(?<![\w$])\$(\d+)/g;
/**
* Renders values as PostgreSQL literals and table references as quoted names, for DDL where bind
* parameters are not allowed (ADR-010).
*
* @internal
*/
export class SqlLiteral {
/**
* Renders a value as a PostgreSQL literal. Strings with backslashes use the `E'...'` form.
*
* @param value - Value to render.
* @returns SQL literal.
* @throws Error for non-finite numbers and strings with NUL characters.
*/
escape(value: unknown): string {
if (value === null || value === undefined) {
return 'NULL';
}
switch (typeof value) {
case 'boolean':
return value ? 'true' : 'false';
case 'number':
return this.#escapeNumber(value);
case 'bigint':
return value.toString();
case 'object':
return this.#escapeObject(value);
default:
return this.#escapeString(String(value));
}
}
/**
* Replaces `$n` placeholders of a compiled query with escaped literals.
*
* @param query - Compiled query.
* @returns SQL text without placeholders.
*/
inlineValues(query: CompiledQuery): string {
return query.text.replace(PLACEHOLDER, (token, index?: string) =>
index === undefined ? token : this.escape(query.values[Number(index) - 1]),
);
}
/**
* Compiles an expression node and inlines its values (for DDL).
*
* @param node - Expression node.
* @returns SQL text without placeholders.
*/
inlineExpr(node: ExprNode): string {
return this.inlineValues(compiler.compileExpr(node));
}
/**
* Splits a table reference into name and schema.
*
* @param table - Table name or `{ tableName, schema }`.
* @param defaultSchema - Schema used when the reference has none.
* @returns Table name and schema.
*/
tableParts(
table: TableName,
defaultSchema?: string,
): Readonly<{ name: string; schema: string | undefined }> {
return typeof table === 'string'
? { name: table, schema: defaultSchema }
: { name: table.tableName, schema: table.schema ?? defaultSchema };
}
/**
* Quoted, schema-qualified table name.
*
* @param table - Table name or `{ tableName, schema }`.
* @param defaultSchema - Schema used when the reference has none.
* @returns Quoted table name.
*/
quoteTable(table: TableName, defaultSchema?: string): string {
const { name, schema } = this.tableParts(table, defaultSchema);
return schema === undefined
? SqlCompiler.quoteIdent(name)
: `${SqlCompiler.quoteIdent(schema)}.${SqlCompiler.quoteIdent(name)}`;
}
#escapeNumber(value: number): string {
if (!Number.isFinite(value)) {
throw new Error(`Cannot render non-finite number ${String(value)} as SQL literal`);
}
return String(value);
}
#escapeObject(value: object): string {
if (value instanceof Date) {
return this.#escapeString(value.toISOString());
}
if (Buffer.isBuffer(value)) {
return `'\\x${value.toString('hex')}'::bytea`;
}
if (Array.isArray(value)) {
return value.length === 0
? "'{}'"
: `ARRAY[${value.map((item: unknown) => this.escape(item)).join(', ')}]`;
}
return this.#escapeString(JSON.stringify(value));
}
#escapeString(text: string): string {
if (text.includes('\0')) {
throw new Error('SQL literals cannot contain NUL characters');
}
const quoted = text.replaceAll("'", "''");
return text.includes('\\') ? `E'${quoted.replaceAll('\\', '\\\\')}'` : `'${quoted}'`;
}
}
|