UNPKG

@uwdata/mosaic-sql

Version:

SQL query construction and analysis.

53 lines 1.61 kB
import { LITERAL } from '../constants.js'; import { ExprNode } from './node.js'; export class LiteralNode extends ExprNode { /** The literal value. */ value; /** * Instantiate an literal node. * @param value The literal value. */ constructor(value) { super(LITERAL); this.value = value; } /** * Generate a SQL query string for this node. */ toString() { return literalToSQL(this.value); } } export function literalToSQL(value) { switch (typeof value) { case 'number': return Number.isFinite(value) ? `${value}` : 'NULL'; case 'string': return `'${value.replaceAll(`'`, `''`)}'`; case 'boolean': return value ? 'TRUE' : 'FALSE'; default: if (value == null) { return 'NULL'; } else if (value instanceof Date) { const ts = +value; if (Number.isNaN(ts)) return 'NULL'; const y = value.getUTCFullYear(); const m = value.getUTCMonth(); const d = value.getUTCDate(); return ts === Date.UTC(y, m, d) ? `DATE '${y}-${m + 1}-${d}'` // utc date : `epoch_ms(${ts})`; // timestamp } else if (value instanceof RegExp) { return `'${value.source}'`; } else { // otherwise rely on string coercion return `${value}`; } } } //# sourceMappingURL=literal.js.map