import { OperationNodeSource } from '../operation-node/operation-node-source.js' ; import { CompiledQuery } from '../query-compiler/compiled-query.js' ; import { JoinCallbackExpression , JoinReferenceExpression , parseJoin , } from '../parser/join-parser.js' ; import { TableExpression , TableExpressionDatabase , TableExpressionTables , LeftJoinTableExpressionDatabase , RightJoinTableExpressionDatabase , FullJoinTableExpressionDatabase , } from '../parser/table-parser.js' ; import { parseSelectExpressionOrList , parseSelectAll , SelectExpression , SelectExpressionOrList , } from '../parser/select-parser.js' ; import { ExistsExpression , parseExistFilter , FilterOperator , parseReferenceFilter , parseWhereFilter , parseNotExistFilter , FilterValueExpressionOrList , WhereGrouper , } from '../parser/filter-parser.js' ; import { ReturningRow } from '../parser/returning-parser.js' ; import { ReferenceExpression } from '../parser/reference-parser.js' ; import { QueryNode } from '../operation-node/query-node.js' ; import { AnyRawBuilder , SingleResultType } from '../util/type-utils.js' ; import { preventAwait } from '../util/prevent-await.js' ; import { Compilable } from '../util/compilable.js' ; import { QueryExecutor } from '../query-executor/query-executor.js' ; import { QueryId } from '../util/query-id.js' ; import { freeze } from '../util/object-utils.js' ; import { ParseContext } from '../parser/parse-context.js' ; import { KyselyPlugin } from '../plugin/kysely-plugin.js' ; import { WhereInterface } from './where-interface.js' ; import { JoinInterface } from './join-interface.js' ; import { ReturningInterface } from './returning-interface.js' ; import { NoResultError , NoResultErrorConstructor } from './no-result-error.js' ; import { DeleteResult } from './delete-result.js' ; import { DeleteQueryNode } from '../operation-node/delete-query-node.js' ; import { Selectable } from '../util/column-type.js' ; export class DeleteQueryBuilder < DB , TB extends keyof DB , O > implements WhereInterface < DB , TB > , JoinInterface < DB , TB > , ReturningInterface < DB , TB , O > , OperationNodeSource , Compilable { readonly #props : DeleteQueryBuilderProps ; constructor ( props ) { this . #props = freeze ( props ) ; } where < RE extends ReferenceExpression < DB , TB > > ( lhs , op , rhs ) ; where ( grouper ) ; where ( raw ) ; where ( ... args ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithWhere ( this . #props . queryNode , parseWhereFilter ( this . #props . parseContext , args ) ) , } ) ; } whereRef ( lhs , op , rhs ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithWhere ( this . #props . queryNode , parseReferenceFilter ( this . #props . parseContext , lhs , op , rhs ) ) , } ) ; } orWhere < RE extends ReferenceExpression < DB , TB > > ( lhs , op , rhs ) ; orWhere ( grouper ) ; orWhere ( raw ) ; orWhere ( ... args ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithOrWhere ( this . #props . queryNode , parseWhereFilter ( this . #props . parseContext , args ) ) , } ) ; } orWhereRef ( lhs , op , rhs ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithOrWhere ( this . #props . queryNode , parseReferenceFilter ( this . #props . parseContext , lhs , op , rhs ) ) , } ) ; } whereExists ( arg ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithWhere ( this . #props . queryNode , parseExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } whereNotExists ( arg ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithWhere ( this . #props . queryNode , parseNotExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } orWhereExists ( arg ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithOrWhere ( this . #props . queryNode , parseExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } orWhereNotExists ( arg ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithOrWhere ( this . #props . queryNode , parseNotExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } innerJoin < TE extends TableExpression < DB , TB > , K1 extends JoinReferenceExpression < DB , TB , TE > , K2 extends JoinReferenceExpression < DB , TB , TE > > ( table , k1 , k2 ) ; innerJoin < TE extends TableExpression < DB , TB > , FN extends JoinCallbackExpression < DB , TB , TE > > ( table , callback ) ; innerJoin ( ... args ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithJoin ( this . #props . queryNode , parseJoin ( this . #props . parseContext , 'InnerJoin' , args ) ) , } ) ; } leftJoin < TE extends TableExpression < DB , TB > , K1 extends JoinReferenceExpression < DB , TB , TE > , K2 extends JoinReferenceExpression < DB , TB , TE > > ( table , k1 , k2 ) ; leftJoin < TE extends TableExpression < DB , TB > , FN extends JoinCallbackExpression < DB , TB , TE > > ( table , callback ) ; leftJoin ( ... args ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithJoin ( this . #props . queryNode , parseJoin ( this . #props . parseContext , 'LeftJoin' , args ) ) , } ) ; } rightJoin < TE extends TableExpression < DB , TB > , K1 extends JoinReferenceExpression < DB , TB , TE > , K2 extends JoinReferenceExpression < DB , TB , TE > > ( table , k1 , k2 ) ; rightJoin < TE extends TableExpression < DB , TB > , FN extends JoinCallbackExpression < DB , TB , TE > > ( table , callback ) ; rightJoin ( ... args ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithJoin ( this . #props . queryNode , parseJoin ( this . #props . parseContext , 'RightJoin' , args ) ) , } ) ; } fullJoin < TE extends TableExpression < DB , TB > , K1 extends JoinReferenceExpression < DB , TB , TE > , K2 extends JoinReferenceExpression < DB , TB , TE > > ( table , k1 , k2 ) ; fullJoin < TE extends TableExpression < DB , TB > , FN extends JoinCallbackExpression < DB , TB , TE > > ( table , callback ) ; fullJoin ( ... args ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithJoin ( this . #props . queryNode , parseJoin ( this . #props . parseContext , 'FullJoin' , args ) ) , } ) ; } returning < SE extends SelectExpression < DB , TB > > ( selections ) ; returning < SE extends SelectExpression < DB , TB > > ( selection ) ; returning ( selection ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithReturning ( this . #props . queryNode , parseSelectExpressionOrList ( this . #props . parseContext , selection ) ) , } ) ; } returningAll ( ) { return new DeleteQueryBuilder ( { ... this . #props , queryNode : QueryNode . cloneWithReturning ( this . #props . queryNode , parseSelectAll ( ) ) , } ) ; } /**
     * Simply calls the given function passing `this` as the only argument.
     *
     * This method can be useful when adding optional method calls:
     *
     * ### Examples
     *
     * ```ts
     * db.deleteFrom('person')
     *   .call((qb) => {
     *     if (something) {
     *       return qb.where('something', '=', something)
     *     } else {
     *       return qb.where('somethingElse', '=', somethingElse)
     *     }
     *   })
     *   .execute()
     * ```
     */ call < T > ( func ) { return func ( this ) ; } /**
     * Change the output type of the query.
     *
     * You should only use this method as the last resort if the types
     * don't support your use case.
     */ castTo < T > ( ) { return new DeleteQueryBuilder ( this . #props ) ; } withPlugin ( plugin ) { return new DeleteQueryBuilder ( { ... this . #props , executor : this . #props . executor . withPlugin ( plugin ) , } ) ; } toOperationNode ( ) { return this . #props . executor . transformQuery ( this . #props . queryNode , this . #props . queryId ) ; } compile ( ) { return this . #props . executor . compileQuery ( this . toOperationNode ( ) , this . #props . queryId ) ; } /**
     * Executes the query and returns an array of rows.
     *
     * Also see the  executeTakeFirst and  executeTakeFirstOrThrow methods.
      async execute ( ) { const compildQuery = this . compile ( ) ; const query = compildQuery . query as DeleteQueryNode ; const result = await this . #props . executor . executeQuery < O > ( compildQuery , this . #props . queryId ) ; if ( this . #props . parseContext . adapter . supportsReturning && query . returning ) { return result . rows ; } else { return [ new DeleteResult ( result . numUpdatedOrDeletedRows ! ) as unknown as O ] ; } } /**
     * Executes the query and returns the first result or undefined if
     * the query returned no result.
     */ async executeTakeFirst ( ) { const [ result ] = await this . execute ( ) ; return result as SingleResultType < O > ; } /**
     * Executes the query and returns the first result or throws if
     * the query returned no result.
     *
     * By default an instance of  NoResultError is thrown, but you can
     * provide a custom error class as the only argument to throw a different
     * error.
      async executeTakeFirstOrThrow ( errorConstructor = NoResultError ) { const result = await this . executeTakeFirst ( ) ; if ( result === undefined ) { throw new errorConstructor ( this . toOperationNode ( ) ) ; } return result as O ; } } preventAwait ( DeleteQueryBuilder , "don't await DeleteQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`." ) ; export interface DeleteQueryBuilderProps { readonly queryId : QueryId ; readonly queryNode : DeleteQueryNode ; readonly executor : QueryExecutor ; readonly parseContext : ParseContext ; }