import { QueryResult } from '../driver/database-connection.js' ; import { AliasNode } from '../operation-node/alias-node.js' ; import { OperationNode } from '../operation-node/operation-node.js' ; import { isOperationNodeSource , OperationNodeSource , } from '../operation-node/operation-node-source.js' ; import { RawNode } from '../operation-node/raw-node.js' ; import { ValueNode } from '../operation-node/value-node.js' ; import { parseStringReference } from '../parser/reference-parser.js' ; import { CompiledQuery } from '../query-compiler/compiled-query.js' ; import { preventAwait } from '../util/prevent-await.js' ; import { QueryExecutor } from '../query-executor/query-executor.js' ; import { QueryId } from '../util/query-id.js' ; import { freeze } from '../util/object-utils.js' ; import { KyselyPlugin } from '../plugin/kysely-plugin.js' ; export class RawBuilder < O = unknown > implements OperationNodeSource { readonly #props : RawBuilderProps ; constructor ( props ) { this . #props = freeze ( props ) ; } as < A extends string > ( alias ) { return new AliasedRawBuilder ( this , alias ) ; } /**
     * Change the output type of the raw expression.
     *
     * This doesn't produce any SQL. This methods simply returns a copy
     * of this `RawBuilder` with a new output type.
     */ castTo < T > ( ) { return new RawBuilder ( { ... this . #props , } ) ; } withPlugin ( plugin ) { return new RawBuilder ( { ... this . #props , executor : this . #props . executor . withPlugin ( plugin ) , } ) ; } toOperationNode ( ) { const parameterPlaceholderRegex = /(\?\??)/g ; const sql = this . #props . sql ; const parameters = this . #props . parameters ?? [ ] ; const sqlFragments = [ ] ; const argNodes = [ ] ; let paramIdx = 0 ; let sqlIdx = 0 ; let match = null ; while ( ( match = parameterPlaceholderRegex . exec ( sql ) ) ) { const str = match [ 1 ] ; if ( paramIdx >= parameters . length ) { throw new Error ( `value not provided for all placeholders in string ${ sql }` ) ; } if ( match . index > 0 && sql [ match . index - 1 ] === '\\' ) { continue ; } sqlFragments . push ( sql . slice ( sqlIdx , match . index ) . replace ( /\\\?/g , '?' ) ) ; argNodes . push ( parseRawArg ( str , parameters [ paramIdx ] ) ) ; sqlIdx = match . index + str . length ; ++ paramIdx ; } sqlFragments . push ( sql . slice ( sqlIdx ) ) ; const rawNode = RawNode . create ( sqlFragments , argNodes ) ; return this . #props . executor . transformQuery ( rawNode , this . #props . queryId ) ; } compile ( ) { return this . #props . executor . compileQuery ( this . toOperationNode ( ) , this . #props . queryId ) ; } async execute ( ) { return this . #props . executor . executeQuery < O > ( this . compile ( ) , this . #props . queryId ) ; } } preventAwait ( RawBuilder , "don't await RawBuilder instances directly. To execute the query you need to call `execute`" ) ; /**
 *  RawBuilder with an alias. The result of calling  RawBuilder . as .
  export class AliasedRawBuilder < O = unknown , A extends string = never > implements OperationNodeSource { readonly #rawBuilder : RawBuilder < O > ; readonly #alias : A ; private protected get alias ( ) : A { return this . #alias ; } toOperationNode ( ) { return AliasNode . create ( this . #rawBuilder . toOperationNode ( ) , this . #alias ) ; } constructor ( rawBuilder , alias ) { this . #rawBuilder = rawBuilder ; this . #alias = alias ; } } function parseRawArg ( match , arg ) { if ( isOperationNodeSource ( arg ) ) { return arg . toOperationNode ( ) ; } else if ( match === '??' ) { return parseStringReference ( arg ) ; } else { return ValueNode . create ( arg ) ; } } export interface RawBuilderProps { readonly queryId : QueryId ; readonly executor : QueryExecutor ; readonly sql : string ; readonly parameters ? : ReadonlyArray < unknown > ; }