import { type RootOperationNode } from '../operation-node/root-operation-node.js'; import { type QueryId } from '../util/query-id.js'; /** * A SQL query compiled for a specific dialect, together with its parameters * and operation node tree. * * The output type `O` describes a result row. It doesn't validate or convert * values returned by the database driver. * * Query builders create compiled queries with {@link Compilable.compile | compile()}, * and {@link RawBuilder}s with `compile(db)`. Compiling doesn't execute the query. * * See {@link Kysely.executeQuery} for execution and {@link CompiledQuery.raw} * for constructing a query from an existing SQL string. * * ### Examples * * ```ts * const compiledQuery = db * .selectFrom('person') * .select('first_name') * .where('id', '=', 123) * .compile() * * console.log(compiledQuery.sql) // select "first_name" from "person" where "id" = $1 * console.log(compiledQuery.parameters) // [123] * ``` */ export interface CompiledQuery { /** * The operation node tree used to compile the query, after query plugins have run. */ readonly query: RootOperationNode; /** * Identifies the query when passing data between plugin hooks. */ readonly queryId: QueryId; /** * The SQL string, with placeholders in the target dialect's syntax. */ readonly sql: string; /** * The parameter values in the order expected by the placeholders in {@link CompiledQuery.sql | sql}. */ readonly parameters: ReadonlyArray; } export declare function isCompiledQuery(thing: unknown): thing is CompiledQuery; type CompiledQueryFactory = Readonly<{ /** * Creates a compiled query from an existing SQL string and parameter values. * * The SQL is used as provided. This method doesn't escape SQL, generate * placeholders, or check that the parameters match the placeholders. * Use the target dialect's placeholder syntax, such as `$1` for PostgreSQL * or `?` for MySQL and SQLite. For SQL built from interpolated values, * prefer the {@link sql} template tag. * * ### Examples * * Execute a parameterized PostgreSQL query: * * ```ts * import { CompiledQuery } from 'kysely' * * const result = await db.executeQuery<{ first_name: string }>( * CompiledQuery.raw( * 'select "first_name" from "person" where "id" = $1', * [123], * ), * ) * * const firstName: string | undefined = result.rows[0]?.first_name * ``` * * The generated SQL (PostgreSQL): * * ```sql * select "first_name" * from "person" * where "id" = $1 * ``` */ raw(sql: string, parameters?: unknown[]): Readonly; }>; export declare const CompiledQuery: CompiledQueryFactory; export {};