import { ColumnNode } from '../operation-node/column-node.js' ; import { IdentifierNode } from '../operation-node/identifier-node.js' ; import { OnConflictNode } from '../operation-node/on-conflict-node.js' ; import { OperationNodeSource } from '../operation-node/operation-node-source.js' ; import { ComplexExpression } from '../parser/complex-expression-parser.js' ; import { WhereGrouper , parseWhereFilter , FilterOperator , FilterValueExpressionOrList , parseReferenceFilter , parseExistFilter , parseNotExistFilter , } from '../parser/filter-parser.js' ; import { ParseContext } from '../parser/parse-context.js' ; import { ReferenceExpression } from '../parser/reference-parser.js' ; import { MutationObject , parseUpdateObject , } from '../parser/update-set-parser.js' ; import { freeze } from '../util/object-utils.js' ; import { preventAwait } from '../util/prevent-await.js' ; import { AnyColumn , AnyRawBuilder } from '../util/type-utils.js' ; import { WhereInterface } from './where-interface.js' ; export class OnConflictBuilder < DB , TB extends keyof DB > implements WhereInterface < DB , TB > { readonly #props : OnConflictBuilderProps ; constructor ( props ) { this . #props = freeze ( props ) ; } /**
     * Specify a single column as the conflict target.
     *
     * Also see the  columns ,  constraint and  expression * methods for alternative ways to specify the conflict target.
      column ( column ) { const columnNode = ColumnNode . create ( column ) ; return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWith ( this . #props . onConflictNode , { columns : this . #props . onConflictNode . columns ? freeze ( [ ... this . #props . onConflictNode . columns , columnNode ] ) : freeze ( [ columnNode ] ) , } ) , } ) ; } /**
     * Specify a list of columns as the conflict target.
     *
     * Also see the  column ,  constraint and  expression * methods for alternative ways to specify the conflict target.
      columns ( columns ) { const columnNodes = columns . map ( ColumnNode . create ) ; return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWith ( this . #props . onConflictNode , { columns : this . #props . onConflictNode . columns ? freeze ( [ ... this . #props . onConflictNode . columns , ... columnNodes ] ) : freeze ( columnNodes ) , } ) , } ) ; } /**
     * Specify a specific constraint by name as the conflict target.
     *
     * Also see the  column ,  columns and  expression * methods for alternative ways to specify the conflict target.
      constraint ( constraintName ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWith ( this . #props . onConflictNode , { constraint : IdentifierNode . create ( constraintName ) , } ) , } ) ; } /**
     * Specify an expression as the conflict target.
     *
     * This can be used if the unique index is an expression index.
     *
     * Also see the  column ,  columns and  constraint * methods for alternative ways to specify the conflict target.
      expression ( expression ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWith ( this . #props . onConflictNode , { indexExpression : expression . toOperationNode ( ) , } ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . where for more info.
      where < RE extends ReferenceExpression < DB , TB > > ( lhs , op , rhs ) ; where ( grouper ) ; where ( raw ) ; where ( ... args ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexWhere ( this . #props . onConflictNode , parseWhereFilter ( this . #props . parseContext , args ) ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . whereRef for more info.
      whereRef ( lhs , op , rhs ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexWhere ( this . #props . onConflictNode , parseReferenceFilter ( this . #props . parseContext , lhs , op , rhs ) ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . orWhere for more info.
      orWhere < RE extends ReferenceExpression < DB , TB > > ( lhs , op , rhs ) ; orWhere ( grouper ) ; orWhere ( raw ) ; orWhere ( ... args ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexOrWhere ( this . #props . onConflictNode , parseWhereFilter ( this . #props . parseContext , args ) ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . orWhereRef for more info.
      orWhereRef ( lhs , op , rhs ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexOrWhere ( this . #props . onConflictNode , parseReferenceFilter ( this . #props . parseContext , lhs , op , rhs ) ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . whereExists for more info.
      whereExists ( arg ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexWhere ( this . #props . onConflictNode , parseExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . whereNotExists for more info.
      whereNotExists ( arg ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexWhere ( this . #props . onConflictNode , parseNotExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . orWhereExists for more info.
      orWhereExists ( arg ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexOrWhere ( this . #props . onConflictNode , parseExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } /**
     * Specify an index predicate for the index target.
     *
     * See  WhereInterface . orWhereNotExists for more info.
      orWhereNotExists ( arg ) { return new OnConflictBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithIndexOrWhere ( this . #props . onConflictNode , parseNotExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } /**
     * Adds the "do nothing" conflict action.
     *
     * ### Examples
     *
     * ```ts
     * await db
     *   .insertInto('person')
     *   .values({ first_name, pic })
     *   .onConflict((oc) => oc
     *     .column('pic')
     *     .doNothing()
     *   )
     * ```
     *
     * The generated SQL (PostgreSQL):
     *
     * ```sql
     * insert into "person" ("first_name", "pic")
     * values ($1, $2)
     * on conflict ("pic") do nothing
     * ```
     */ doNothing ( ) { return new OnConflictDoNothingBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWith ( this . #props . onConflictNode , { doNothing : true , } ) , } ) ; } /**
     * Adds the "do update set" conflict action.
     *
     * ### Examples
     *
     * ```ts
     * await db
     *   .insertInto('person')
     *   .values({ first_name, pic })
     *   .onConflict((oc) => oc
     *     .column('pic')
     *     .doUpdateSet({ first_name })
     *   )
     * ```
     *
     * The generated SQL (PostgreSQL):
     *
     * ```sql
     * insert into "person" ("first_name", "pic")
     * values ($1, $2)
     * on conflict ("pic")
     * do update set "first_name" = $3
     * ```
     */ doUpdateSet ( updates ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWith ( this . #props . onConflictNode , { updates : parseUpdateObject ( this . #props . parseContext , updates ) , } ) , } ) ; } } export interface OnConflictBuilderProps { readonly onConflictNode : OnConflictNode ; readonly parseContext : ParseContext ; } preventAwait ( OnConflictBuilder , "don't await OnConflictBuilder instances." ) ; export type OnConflictDatabase < DB , TB extends keyof DB > = { [ K in keyof DB | 'excluded' ] : K extends keyof DB ? DB [ K ] : DB [ TB ] ; } ; export type OnConflictTables < TB > = TB | 'excluded' ; export class OnConflictDoNothingBuilder < DB , TB extends keyof DB > implements OperationNodeSource { readonly #props : OnConflictBuilderProps ; constructor ( props ) { this . #props = freeze ( props ) ; } toOperationNode ( ) { return this . #props . onConflictNode ; } } preventAwait ( OnConflictDoNothingBuilder , "don't await OnConflictDoNothingBuilder instances." ) ; export class OnConflictUpdateBuilder < DB , TB extends keyof DB > implements WhereInterface < DB , TB > , OperationNodeSource { readonly #props : OnConflictBuilderProps ; constructor ( props ) { this . #props = freeze ( props ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . where for more info.
      where < RE extends ReferenceExpression < DB , TB > > ( lhs , op , rhs ) ; where ( grouper ) ; where ( raw ) ; where ( ... args ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateWhere ( this . #props . onConflictNode , parseWhereFilter ( this . #props . parseContext , args ) ) , } ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . whereRef for more info.
      whereRef ( lhs , op , rhs ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateWhere ( this . #props . onConflictNode , parseReferenceFilter ( this . #props . parseContext , lhs , op , rhs ) ) , } ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . orWhere for more info.
      orWhere < RE extends ReferenceExpression < DB , TB > > ( lhs , op , rhs ) ; orWhere ( grouper ) ; orWhere ( raw ) ; orWhere ( ... args ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateOrWhere ( this . #props . onConflictNode , parseWhereFilter ( this . #props . parseContext , args ) ) , } ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . orWhereRef for more info.
      orWhereRef ( lhs , op , rhs ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateOrWhere ( this . #props . onConflictNode , parseReferenceFilter ( this . #props . parseContext , lhs , op , rhs ) ) , } ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . whereExists for more info.
      whereExists ( arg ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateWhere ( this . #props . onConflictNode , parseExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . whereNotExists for more info.
      whereNotExists ( arg ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateWhere ( this . #props . onConflictNode , parseNotExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . orWhereExists for more info.
      orWhereExists ( arg ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateOrWhere ( this . #props . onConflictNode , parseExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } /**
     * Specify a where condition for the update operation.
     *
     * See  WhereInterface . orWhereNotExists for more info.
      orWhereNotExists ( arg ) { return new OnConflictUpdateBuilder ( { ... this . #props , onConflictNode : OnConflictNode . cloneWithUpdateOrWhere ( this . #props . onConflictNode , parseNotExistFilter ( this . #props . parseContext , arg ) ) , } ) ; } toOperationNode ( ) { return this . #props . onConflictNode ; } } preventAwait ( OnConflictUpdateBuilder , "don't await OnConflictUpdateBuilder instances." ) ;