import { AnyColumn , Column } from './column' ; import { AnyTable , Table } from './TableType' ; import { CollectionToken , ParameterToken , SeparatorToken , StringToken , Token , createQueryState , } from './tokens' ; import { DbConfig , GetResultType } from './config' ; import { GetReturning , QueryExecutorFn , ResultType } from './types' ; import { Expression } from './expression' ; import { Query } from './query' ; import { ResultSet } from './result-set' ; import { wrapQuotes } from './naming' ; export class UpdateQuery < Config extends DbConfig , T extends AnyTable , Returning = number , TableColumns = T extends Table < Config , any , infer Columns > ? Columns : never > extends Query < Returning > { private _updateQueryBrand : any ; internal getReturningKeys ( ) { return this . returningKeys ; } constructor ( private readonly queryExecutor , private readonly returningKeys , private readonly table , private readonly resultType , private readonly tokens ) { super ( ) ; } then < Result1 , Result2 = never > ( onFulfilled ? , onRejected ? ) { const queryState = createQueryState ( this . tokens ) ; return this . queryExecutor ( queryState . text . join ( ` ` ) , queryState . parameters ) . then ( ( result ) => onFulfilled ? onFulfilled ( this . resultType === `AFFECTED_COUNT` ? result . affectedCount : ( result . rows as any ) ) : ( result as any ) ) . catch ( onRejected ) ; } where ( condition ) { return new UpdateQuery ( this . queryExecutor , this . returningKeys , this . table , this . resultType , [ ... this . tokens , new StringToken ( `WHERE` ) , ... condition . toTokens ( ) , ] ) ; } whereCurrentOf ( cursorName ) { return new UpdateQuery ( this . queryExecutor , this . returningKeys , this . table , this . resultType , [ ... this . tokens , new StringToken ( `WHERE CURRENT OF` ) , new ParameterToken ( cursorName ) , ] ) ; } from ( fromItem ) { return new UpdateQuery ( this . queryExecutor , this . returningKeys , this . table , this . resultType , [ ... this . tokens , new StringToken ( `FROM` ) , fromItem . getOriginalName ( ) ? new StringToken ( `${ wrapQuotes ( fromItem . getOriginalName ( ) ) } ${ wrapQuotes ( fromItem . getName ( ) ) }` ) : new StringToken ( wrapQuotes ( fromItem . getName ( ) ) ) , ] ) ; } returning < C1 extends keyof TableColumns > ( column1 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns > ( column1 , column2 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns > ( column1 , column2 , column3 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns , C4 extends keyof TableColumns > ( column1 , column2 , column3 , column4 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns , C4 extends keyof TableColumns , C5 extends keyof TableColumns > ( column1 , column2 , column3 , column4 , column5 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns , C4 extends keyof TableColumns , C5 extends keyof TableColumns , C6 extends keyof TableColumns > ( column1 , column2 , column3 , column4 , column5 , column6 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns , C4 extends keyof TableColumns , C5 extends keyof TableColumns , C6 extends keyof TableColumns , C7 extends keyof TableColumns > ( column1 , column2 , column3 , column4 , column5 , column6 , column7 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns , C4 extends keyof TableColumns , C5 extends keyof TableColumns , C6 extends keyof TableColumns , C7 extends keyof TableColumns , C8 extends keyof TableColumns > ( column1 , column2 , column3 , column4 , column5 , column6 , column7 , column8 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns , C4 extends keyof TableColumns , C5 extends keyof TableColumns , C6 extends keyof TableColumns , C7 extends keyof TableColumns , C8 extends keyof TableColumns , C9 extends keyof TableColumns > ( column1 , column2 , column3 , column4 , column5 , column6 , column7 , column8 , column9 ) ; returning < C1 extends keyof TableColumns , C2 extends keyof TableColumns , C3 extends keyof TableColumns , C4 extends keyof TableColumns , C5 extends keyof TableColumns , C6 extends keyof TableColumns , C7 extends keyof TableColumns , C8 extends keyof TableColumns , C9 extends keyof TableColumns , C10 extends keyof TableColumns > ( column1 , column2 , column3 , column4 , column5 , column6 , column7 , column8 , column9 , column10 ) ; returning ( ... columnNames ) { return new UpdateQuery ( this . queryExecutor , columnNames , this . table , 'ROWS' , [ ... this . tokens , new StringToken ( `RETURNING` ) , new SeparatorToken ( `,` , columnNames . map ( ( alias ) => { const column = ( this . table as any ) [ alias ] as AnyColumn ; if ( alias !== column . getSnakeCaseName ( ) ) { return new StringToken ( `${ wrapQuotes ( column . getSnakeCaseName ( ) ) } ${ wrapQuotes ( alias ) }` ) ; } else { return new StringToken ( wrapQuotes ( column . getSnakeCaseName ( ) ) ) ; } } ) ) , ] ) as any ; } internal toTokens ( ) { return this . tokens ; } } export const makeUpdate = < Config extends DbConfig > ( queryExecutor ) => < T extends AnyTable > ( table ) => { const getTableStringToken = ( table ) => { if ( table . getOriginalName ( ) ) { return new StringToken ( `${ wrapQuotes ( table . getOriginalName ( ) ) } ${ wrapQuotes ( table . getName ( ) ) }` ) ; } return new StringToken ( wrapQuotes ( table . getName ( ) ) ) ; } ; return { set ( values ) { const keys = Object . keys ( values ) ; return new UpdateQuery ( queryExecutor , [ ] , table , 'AFFECTED_COUNT' , [ new StringToken ( `UPDATE` ) , getTableStringToken ( table ) , new StringToken ( `SET` ) , new SeparatorToken ( `,` , keys . map ( ( key ) => { const column = ( table as any ) [ key ] as AnyColumn ; const value = ( values as any ) [ key ] ; return new CollectionToken ( [ new StringToken ( wrapQuotes ( column . getSnakeCaseName ( ) ) ) , new StringToken ( `=` ) , value && typeof value === `object` && 'toTokens' in value ? value . toTokens ( ) : new ParameterToken ( value ) , ] ) ; } ) ) , ] ) ; } , } ; } ;