UNPKG

@iarayan/ch-orm

Version:

A Developer-First ClickHouse ORM with Powerful CLI Tools

105 lines 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Raw = void 0; /** * Raw SQL expression class for use in queries * Allows inserting raw SQL into query builder statements */ class Raw { /** * Create a new Raw SQL expression * @param value - The raw SQL value */ constructor(value) { this.value = value; } /** * Get the raw SQL value * @returns Raw SQL string */ toSql() { return this.value; } /** * Create a raw SQL expression * @param value - Raw SQL value * @returns Raw SQL expression instance */ static make(value) { return new Raw(value); } /** * String representation of the raw SQL * @returns Raw SQL string */ toString() { return this.value; } /** * Create a raw SQL expression for a column name * @param name - Column name * @returns Raw SQL expression with properly escaped column name */ static column(name) { // Handle column names with dots (table.column format) if (name.includes(".")) { const parts = name.split("."); return new Raw(parts.map((part) => `\`${part}\``).join(".")); } // Handle normal column names return new Raw(`\`${name}\``); } /** * Create a raw expression for a table name * @param name - Table name * @returns Raw SQL expression with properly escaped table name */ static table(name) { // Handle table names with dots (database.table format) if (name.includes(".")) { const parts = name.split("."); return new Raw(parts.map((part) => `\`${part}\``).join(".")); } // Handle normal table names return new Raw(`\`${name}\``); } /** * Create a raw expression for now() * @returns Raw SQL expression for current timestamp */ static now() { return new Raw("now()"); } /** * Create a raw expression for today() * @returns Raw SQL expression for today's date */ static today() { return new Raw("today()"); } /** * Create a raw expression for a ClickHouse function * @param name - Function name * @param params - Function parameters * @returns Raw SQL expression for function call */ static fn(name, ...params) { const formattedParams = params .map((param) => { if (param instanceof Raw) { return param.toSql(); } if (typeof param === "string") { return `'${param.replace(/'/g, "''")}'`; } if (param === null || param === undefined) { return "NULL"; } return String(param); }) .join(", "); return new Raw(`${name}(${formattedParams})`); } } exports.Raw = Raw; //# sourceMappingURL=Raw.js.map