• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 15219332477

23 May 2025 09:13PM UTC coverage: 17.216% (-59.1%) from 76.346%
15219332477

Pull #11332

github

naorpeled
cr comments - move if block
Pull Request #11332: feat: add new undefined and null behavior flags

1603 of 12759 branches covered (12.56%)

Branch coverage included in aggregate %.

0 of 31 new or added lines in 3 files covered. (0.0%)

14132 existing lines in 166 files now uncovered.

4731 of 24033 relevant lines covered (19.69%)

60.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.22
/src/query-builder/UpdateQueryBuilder.ts
1
import { ColumnMetadata } from "../metadata/ColumnMetadata"
2
import { QueryBuilder } from "./QueryBuilder"
1✔
3
import { ObjectLiteral } from "../common/ObjectLiteral"
4
import { DataSource } from "../data-source/DataSource"
5
import { QueryRunner } from "../query-runner/QueryRunner"
6
import { WhereExpressionBuilder } from "./WhereExpressionBuilder"
7
import { Brackets } from "./Brackets"
8
import { UpdateResult } from "./result/UpdateResult"
1✔
9
import { ReturningStatementNotSupportedError } from "../error/ReturningStatementNotSupportedError"
1✔
10
import { ReturningResultsEntityUpdator } from "./ReturningResultsEntityUpdator"
1✔
11
import { MysqlDriver } from "../driver/mysql/MysqlDriver"
12
import { OrderByCondition } from "../find-options/OrderByCondition"
13
import { LimitOnUpdateNotSupportedError } from "../error/LimitOnUpdateNotSupportedError"
1✔
14
import { UpdateValuesMissingError } from "../error/UpdateValuesMissingError"
1✔
15
import { QueryDeepPartialEntity } from "./QueryPartialEntity"
16
import { AuroraMysqlDriver } from "../driver/aurora-mysql/AuroraMysqlDriver"
17
import { TypeORMError } from "../error"
1✔
18
import { EntityPropertyNotFoundError } from "../error/EntityPropertyNotFoundError"
1✔
19
import { SqlServerDriver } from "../driver/sqlserver/SqlServerDriver"
20
import { DriverUtils } from "../driver/DriverUtils"
1✔
21

22
/**
23
 * Allows to build complex sql queries in a fashion way and execute those queries.
24
 */
25
export class UpdateQueryBuilder<Entity extends ObjectLiteral>
1✔
26
    extends QueryBuilder<Entity>
27
    implements WhereExpressionBuilder
28
{
UNCOV
29
    readonly "@instanceof" = Symbol.for("UpdateQueryBuilder")
×
30

31
    // -------------------------------------------------------------------------
32
    // Constructor
33
    // -------------------------------------------------------------------------
34

35
    constructor(
36
        connectionOrQueryBuilder: DataSource | QueryBuilder<any>,
37
        queryRunner?: QueryRunner,
38
    ) {
UNCOV
39
        super(connectionOrQueryBuilder as any, queryRunner)
×
UNCOV
40
        this.expressionMap.aliasNamePrefixingEnabled = false
×
41
    }
42

43
    // -------------------------------------------------------------------------
44
    // Public Implemented Methods
45
    // -------------------------------------------------------------------------
46

47
    /**
48
     * Gets generated SQL query without parameters being replaced.
49
     */
50
    getQuery(): string {
UNCOV
51
        let sql = this.createComment()
×
UNCOV
52
        sql += this.createCteExpression()
×
UNCOV
53
        sql += this.createUpdateExpression()
×
UNCOV
54
        sql += this.createOrderByExpression()
×
UNCOV
55
        sql += this.createLimitExpression()
×
UNCOV
56
        return this.replacePropertyNamesForTheWholeQuery(sql.trim())
×
57
    }
58

59
    /**
60
     * Executes sql generated by query builder and returns raw database results.
61
     */
62
    async execute(): Promise<UpdateResult> {
UNCOV
63
        const queryRunner = this.obtainQueryRunner()
×
UNCOV
64
        let transactionStartedByUs: boolean = false
×
65

UNCOV
66
        try {
×
67
            // start transaction if it was enabled
UNCOV
68
            if (
×
69
                this.expressionMap.useTransaction === true &&
×
70
                queryRunner.isTransactionActive === false
71
            ) {
72
                await queryRunner.startTransaction()
×
73
                transactionStartedByUs = true
×
74
            }
75

76
            // call before updation methods in listeners and subscribers
UNCOV
77
            if (
×
78
                this.expressionMap.callListeners === true &&
×
79
                this.expressionMap.mainAlias!.hasMetadata
80
            ) {
UNCOV
81
                await queryRunner.broadcaster.broadcast(
×
82
                    "BeforeUpdate",
83
                    this.expressionMap.mainAlias!.metadata,
84
                    this.expressionMap.valuesSet,
85
                )
86
            }
87

UNCOV
88
            let declareSql: string | null = null
×
UNCOV
89
            let selectOutputSql: string | null = null
×
90

91
            // if update entity mode is enabled we may need extra columns for the returning statement
92
            const returningResultsEntityUpdator =
UNCOV
93
                new ReturningResultsEntityUpdator(
×
94
                    queryRunner,
95
                    this.expressionMap,
96
                )
97

UNCOV
98
            const returningColumns: ColumnMetadata[] = []
×
99

UNCOV
100
            if (
×
101
                Array.isArray(this.expressionMap.returning) &&
×
102
                this.expressionMap.mainAlias!.hasMetadata
103
            ) {
UNCOV
104
                for (const columnPath of this.expressionMap.returning) {
×
UNCOV
105
                    returningColumns.push(
×
106
                        ...this.expressionMap.mainAlias!.metadata.findColumnsWithPropertyPath(
107
                            columnPath,
108
                        ),
109
                    )
110
                }
111
            }
112

UNCOV
113
            if (
×
114
                this.expressionMap.updateEntity === true &&
×
115
                this.expressionMap.mainAlias!.hasMetadata &&
116
                this.expressionMap.whereEntities.length > 0
117
            ) {
UNCOV
118
                this.expressionMap.extraReturningColumns =
×
119
                    returningResultsEntityUpdator.getUpdationReturningColumns()
120

UNCOV
121
                returningColumns.push(
×
122
                    ...this.expressionMap.extraReturningColumns.filter(
UNCOV
123
                        (c) => !returningColumns.includes(c),
×
124
                    ),
125
                )
126
            }
127

UNCOV
128
            if (
×
129
                returningColumns.length > 0 &&
×
130
                this.connection.driver.options.type === "mssql"
131
            ) {
UNCOV
132
                declareSql = (
×
133
                    this.connection.driver as SqlServerDriver
134
                ).buildTableVariableDeclaration(
135
                    "@OutputTable",
136
                    returningColumns,
137
                )
UNCOV
138
                selectOutputSql = `SELECT * FROM @OutputTable`
×
139
            }
140

141
            // execute update query
UNCOV
142
            const [updateSql, parameters] = this.getQueryAndParameters()
×
143

UNCOV
144
            const statements = [declareSql, updateSql, selectOutputSql]
×
UNCOV
145
            const queryResult = await queryRunner.query(
×
UNCOV
146
                statements.filter((sql) => sql != null).join(";\n\n"),
×
147
                parameters,
148
                true,
149
            )
UNCOV
150
            const updateResult = UpdateResult.from(queryResult)
×
151

152
            // if we are updating entities and entity updation is enabled we must update some of entity columns (like version, update date, etc.)
UNCOV
153
            if (
×
154
                this.expressionMap.updateEntity === true &&
×
155
                this.expressionMap.mainAlias!.hasMetadata &&
156
                this.expressionMap.whereEntities.length > 0
157
            ) {
UNCOV
158
                await returningResultsEntityUpdator.update(
×
159
                    updateResult,
160
                    this.expressionMap.whereEntities,
161
                )
162
            }
163

164
            // call after updation methods in listeners and subscribers
UNCOV
165
            if (
×
166
                this.expressionMap.callListeners === true &&
×
167
                this.expressionMap.mainAlias!.hasMetadata
168
            ) {
UNCOV
169
                await queryRunner.broadcaster.broadcast(
×
170
                    "AfterUpdate",
171
                    this.expressionMap.mainAlias!.metadata,
172
                    this.expressionMap.valuesSet,
173
                )
174
            }
175

176
            // close transaction if we started it
UNCOV
177
            if (transactionStartedByUs) await queryRunner.commitTransaction()
×
178

UNCOV
179
            return updateResult
×
180
        } catch (error) {
181
            // rollback transaction if we started it
UNCOV
182
            if (transactionStartedByUs) {
×
183
                try {
×
184
                    await queryRunner.rollbackTransaction()
×
185
                } catch (rollbackError) {}
186
            }
UNCOV
187
            throw error
×
188
        } finally {
UNCOV
189
            if (queryRunner !== this.queryRunner) {
×
190
                // means we created our own query runner
UNCOV
191
                await queryRunner.release()
×
192
            }
193
        }
194
    }
195

196
    // -------------------------------------------------------------------------
197
    // Public Methods
198
    // -------------------------------------------------------------------------
199

200
    /**
201
     * Values needs to be updated.
202
     */
203
    set(values: QueryDeepPartialEntity<Entity>): this {
UNCOV
204
        this.expressionMap.valuesSet = values
×
UNCOV
205
        return this
×
206
    }
207

208
    /**
209
     * Sets WHERE condition in the query builder.
210
     * If you had previously WHERE expression defined,
211
     * calling this function will override previously set WHERE conditions.
212
     * Additionally you can add parameters used in where expression.
213
     */
214
    where(
215
        where:
216
            | string
217
            | ((qb: this) => string)
218
            | Brackets
219
            | ObjectLiteral
220
            | ObjectLiteral[],
221
        parameters?: ObjectLiteral,
222
    ): this {
UNCOV
223
        this.expressionMap.wheres = [] // don't move this block below since computeWhereParameter can add where expressions
×
UNCOV
224
        const condition = this.getWhereCondition(where)
×
UNCOV
225
        if (condition)
×
UNCOV
226
            this.expressionMap.wheres = [
×
227
                { type: "simple", condition: condition },
228
            ]
UNCOV
229
        if (parameters) this.setParameters(parameters)
×
UNCOV
230
        return this
×
231
    }
232

233
    /**
234
     * Adds new AND WHERE condition in the query builder.
235
     * Additionally you can add parameters used in where expression.
236
     */
237
    andWhere(
238
        where:
239
            | string
240
            | ((qb: this) => string)
241
            | Brackets
242
            | ObjectLiteral
243
            | ObjectLiteral[],
244
        parameters?: ObjectLiteral,
245
    ): this {
UNCOV
246
        this.expressionMap.wheres.push({
×
247
            type: "and",
248
            condition: this.getWhereCondition(where),
249
        })
UNCOV
250
        if (parameters) this.setParameters(parameters)
×
UNCOV
251
        return this
×
252
    }
253

254
    /**
255
     * Adds new OR WHERE condition in the query builder.
256
     * Additionally you can add parameters used in where expression.
257
     */
258
    orWhere(
259
        where:
260
            | string
261
            | ((qb: this) => string)
262
            | Brackets
263
            | ObjectLiteral
264
            | ObjectLiteral[],
265
        parameters?: ObjectLiteral,
266
    ): this {
UNCOV
267
        this.expressionMap.wheres.push({
×
268
            type: "or",
269
            condition: this.getWhereCondition(where),
270
        })
UNCOV
271
        if (parameters) this.setParameters(parameters)
×
UNCOV
272
        return this
×
273
    }
274

275
    /**
276
     * Sets WHERE condition in the query builder with a condition for the given ids.
277
     * If you had previously WHERE expression defined,
278
     * calling this function will override previously set WHERE conditions.
279
     */
280
    whereInIds(ids: any | any[]): this {
UNCOV
281
        return this.where(this.getWhereInIdsCondition(ids))
×
282
    }
283

284
    /**
285
     * Adds new AND WHERE with conditions for the given ids.
286
     */
287
    andWhereInIds(ids: any | any[]): this {
288
        return this.andWhere(this.getWhereInIdsCondition(ids))
×
289
    }
290

291
    /**
292
     * Adds new OR WHERE with conditions for the given ids.
293
     */
294
    orWhereInIds(ids: any | any[]): this {
UNCOV
295
        return this.orWhere(this.getWhereInIdsCondition(ids))
×
296
    }
297
    /**
298
     * Optional returning/output clause.
299
     * This will return given column values.
300
     */
301
    output(columns: string[]): this
302

303
    /**
304
     * Optional returning/output clause.
305
     * Returning is a SQL string containing returning statement.
306
     */
307
    output(output: string): this
308

309
    /**
310
     * Optional returning/output clause.
311
     */
312
    output(output: string | string[]): this
313

314
    /**
315
     * Optional returning/output clause.
316
     */
317
    output(output: string | string[]): this {
318
        return this.returning(output)
×
319
    }
320

321
    /**
322
     * Optional returning/output clause.
323
     * This will return given column values.
324
     */
325
    returning(columns: string[]): this
326

327
    /**
328
     * Optional returning/output clause.
329
     * Returning is a SQL string containing returning statement.
330
     */
331
    returning(returning: string): this
332

333
    /**
334
     * Optional returning/output clause.
335
     */
336
    returning(returning: string | string[]): this
337

338
    /**
339
     * Optional returning/output clause.
340
     */
341
    returning(returning: string | string[]): this {
342
        // not all databases support returning/output cause
UNCOV
343
        if (!this.connection.driver.isReturningSqlSupported("update")) {
×
344
            throw new ReturningStatementNotSupportedError()
×
345
        }
346

UNCOV
347
        this.expressionMap.returning = returning
×
UNCOV
348
        return this
×
349
    }
350

351
    /**
352
     * Sets ORDER BY condition in the query builder.
353
     * If you had previously ORDER BY expression defined,
354
     * calling this function will override previously set ORDER BY conditions.
355
     *
356
     * Calling order by without order set will remove all previously set order bys.
357
     */
358
    orderBy(): this
359

360
    /**
361
     * Sets ORDER BY condition in the query builder.
362
     * If you had previously ORDER BY expression defined,
363
     * calling this function will override previously set ORDER BY conditions.
364
     */
365
    orderBy(
366
        sort: string,
367
        order?: "ASC" | "DESC",
368
        nulls?: "NULLS FIRST" | "NULLS LAST",
369
    ): this
370

371
    /**
372
     * Sets ORDER BY condition in the query builder.
373
     * If you had previously ORDER BY expression defined,
374
     * calling this function will override previously set ORDER BY conditions.
375
     */
376
    orderBy(order: OrderByCondition): this
377

378
    /**
379
     * Sets ORDER BY condition in the query builder.
380
     * If you had previously ORDER BY expression defined,
381
     * calling this function will override previously set ORDER BY conditions.
382
     */
383
    orderBy(
384
        sort?: string | OrderByCondition,
385
        order: "ASC" | "DESC" = "ASC",
×
386
        nulls?: "NULLS FIRST" | "NULLS LAST",
387
    ): this {
388
        if (sort) {
×
389
            if (typeof sort === "object") {
×
390
                this.expressionMap.orderBys = sort as OrderByCondition
×
391
            } else {
392
                if (nulls) {
×
393
                    this.expressionMap.orderBys = {
×
394
                        [sort as string]: { order, nulls },
395
                    }
396
                } else {
397
                    this.expressionMap.orderBys = { [sort as string]: order }
×
398
                }
399
            }
400
        } else {
401
            this.expressionMap.orderBys = {}
×
402
        }
403
        return this
×
404
    }
405

406
    /**
407
     * Adds ORDER BY condition in the query builder.
408
     */
409
    addOrderBy(
410
        sort: string,
411
        order: "ASC" | "DESC" = "ASC",
×
412
        nulls?: "NULLS FIRST" | "NULLS LAST",
413
    ): this {
414
        if (nulls) {
×
415
            this.expressionMap.orderBys[sort] = { order, nulls }
×
416
        } else {
417
            this.expressionMap.orderBys[sort] = order
×
418
        }
419
        return this
×
420
    }
421

422
    /**
423
     * Sets LIMIT - maximum number of rows to be selected.
424
     */
425
    limit(limit?: number): this {
UNCOV
426
        this.expressionMap.limit = limit
×
UNCOV
427
        return this
×
428
    }
429

430
    /**
431
     * Indicates if entity must be updated after update operation.
432
     * This may produce extra query or use RETURNING / OUTPUT statement (depend on database).
433
     * Enabled by default.
434
     */
435
    whereEntity(entity: Entity | Entity[]): this {
UNCOV
436
        if (!this.expressionMap.mainAlias!.hasMetadata)
×
437
            throw new TypeORMError(
×
438
                `.whereEntity method can only be used on queries which update real entity table.`,
439
            )
440

UNCOV
441
        this.expressionMap.wheres = []
×
UNCOV
442
        const entities: Entity[] = Array.isArray(entity) ? entity : [entity]
×
UNCOV
443
        entities.forEach((entity) => {
×
444
            const entityIdMap =
UNCOV
445
                this.expressionMap.mainAlias!.metadata.getEntityIdMap(entity)
×
UNCOV
446
            if (!entityIdMap)
×
447
                throw new TypeORMError(
×
448
                    `Provided entity does not have ids set, cannot perform operation.`,
449
                )
450

UNCOV
451
            this.orWhereInIds(entityIdMap)
×
452
        })
453

UNCOV
454
        this.expressionMap.whereEntities = entities
×
UNCOV
455
        return this
×
456
    }
457

458
    /**
459
     * Indicates if entity must be updated after update operation.
460
     * This may produce extra query or use RETURNING / OUTPUT statement (depend on database).
461
     * Enabled by default.
462
     */
463
    updateEntity(enabled: boolean): this {
UNCOV
464
        this.expressionMap.updateEntity = enabled
×
UNCOV
465
        return this
×
466
    }
467

468
    // -------------------------------------------------------------------------
469
    // Protected Methods
470
    // -------------------------------------------------------------------------
471

472
    /**
473
     * Creates UPDATE express used to perform insert query.
474
     */
475
    protected createUpdateExpression() {
UNCOV
476
        const valuesSet = this.getValueSet()
×
UNCOV
477
        const metadata = this.expressionMap.mainAlias!.hasMetadata
×
478
            ? this.expressionMap.mainAlias!.metadata
479
            : undefined
480

481
        // it doesn't make sense to update undefined properties, so just skip them
UNCOV
482
        const valuesSetNormalized: ObjectLiteral = {}
×
UNCOV
483
        for (const key in valuesSet) {
×
UNCOV
484
            if (valuesSet[key] !== undefined) {
×
UNCOV
485
                valuesSetNormalized[key] = valuesSet[key]
×
486
            }
487
        }
488

489
        // prepare columns and values to be updated
UNCOV
490
        const updateColumnAndValues: string[] = []
×
UNCOV
491
        const updatedColumns: ColumnMetadata[] = []
×
UNCOV
492
        if (metadata) {
×
UNCOV
493
            this.createPropertyPath(metadata, valuesSetNormalized).forEach(
×
494
                (propertyPath) => {
495
                    // todo: make this and other query builder to work with properly with tables without metadata
496
                    const columns =
UNCOV
497
                        metadata.findColumnsWithPropertyPath(propertyPath)
×
498

UNCOV
499
                    if (columns.length <= 0) {
×
UNCOV
500
                        throw new EntityPropertyNotFoundError(
×
501
                            propertyPath,
502
                            metadata,
503
                        )
504
                    }
505

UNCOV
506
                    columns.forEach((column) => {
×
UNCOV
507
                        if (
×
508
                            !column.isUpdate ||
×
509
                            updatedColumns.includes(column)
510
                        ) {
UNCOV
511
                            return
×
512
                        }
513

UNCOV
514
                        updatedColumns.push(column)
×
515

516
                        //
UNCOV
517
                        let value = column.getEntityValue(valuesSetNormalized)
×
UNCOV
518
                        if (
×
519
                            column.referencedColumn &&
×
520
                            typeof value === "object" &&
521
                            !(value instanceof Date) &&
522
                            value !== null &&
523
                            !Buffer.isBuffer(value)
524
                        ) {
525
                            value =
×
526
                                column.referencedColumn.getEntityValue(value)
UNCOV
527
                        } else if (!(typeof value === "function")) {
×
UNCOV
528
                            value =
×
529
                                this.connection.driver.preparePersistentValue(
530
                                    value,
531
                                    column,
532
                                )
533
                        }
534

535
                        // todo: duplication zone
UNCOV
536
                        if (typeof value === "function") {
×
537
                            // support for SQL expressions in update query
UNCOV
538
                            updateColumnAndValues.push(
×
539
                                this.escape(column.databaseName) +
540
                                    " = " +
541
                                    value(),
542
                            )
UNCOV
543
                        } else if (
×
544
                            (this.connection.driver.options.type === "sap" ||
×
545
                                this.connection.driver.options.type ===
546
                                    "spanner") &&
547
                            value === null
548
                        ) {
UNCOV
549
                            updateColumnAndValues.push(
×
550
                                this.escape(column.databaseName) + " = NULL",
551
                            )
552
                        } else {
UNCOV
553
                            if (
×
554
                                this.connection.driver.options.type === "mssql"
555
                            ) {
UNCOV
556
                                value = (
×
557
                                    this.connection.driver as SqlServerDriver
558
                                ).parametrizeValue(column, value)
559
                            }
560

UNCOV
561
                            const paramName = this.createParameter(value)
×
562

UNCOV
563
                            let expression = null
×
UNCOV
564
                            if (
×
565
                                (DriverUtils.isMySQLFamily(
×
566
                                    this.connection.driver,
567
                                ) ||
568
                                    this.connection.driver.options.type ===
569
                                        "aurora-mysql") &&
570
                                this.connection.driver.spatialTypes.indexOf(
571
                                    column.type,
572
                                ) !== -1
573
                            ) {
574
                                const useLegacy = (
575
                                    this.connection.driver as
×
576
                                        | MysqlDriver
577
                                        | AuroraMysqlDriver
578
                                ).options.legacySpatialSupport
579
                                const geomFromText = useLegacy
×
580
                                    ? "GeomFromText"
581
                                    : "ST_GeomFromText"
582
                                if (column.srid != null) {
×
583
                                    expression = `${geomFromText}(${paramName}, ${column.srid})`
×
584
                                } else {
585
                                    expression = `${geomFromText}(${paramName})`
×
586
                                }
UNCOV
587
                            } else if (
×
588
                                DriverUtils.isPostgresFamily(
×
589
                                    this.connection.driver,
590
                                ) &&
591
                                this.connection.driver.spatialTypes.indexOf(
592
                                    column.type,
593
                                ) !== -1
594
                            ) {
UNCOV
595
                                if (column.srid != null) {
×
596
                                    expression = `ST_SetSRID(ST_GeomFromGeoJSON(${paramName}), ${column.srid})::${column.type}`
×
597
                                } else {
UNCOV
598
                                    expression = `ST_GeomFromGeoJSON(${paramName})::${column.type}`
×
599
                                }
UNCOV
600
                            } else if (
×
601
                                this.connection.driver.options.type ===
×
602
                                    "mssql" &&
603
                                this.connection.driver.spatialTypes.indexOf(
604
                                    column.type,
605
                                ) !== -1
606
                            ) {
UNCOV
607
                                expression =
×
608
                                    column.type +
609
                                    "::STGeomFromText(" +
610
                                    paramName +
611
                                    ", " +
612
                                    (column.srid || "0") +
×
613
                                    ")"
614
                            } else {
UNCOV
615
                                expression = paramName
×
616
                            }
UNCOV
617
                            updateColumnAndValues.push(
×
618
                                this.escape(column.databaseName) +
619
                                    " = " +
620
                                    expression,
621
                            )
622
                        }
623
                    })
624
                },
625
            )
626

627
            // Don't allow calling update only with columns that are `update: false`
UNCOV
628
            if (
×
629
                updateColumnAndValues.length > 0 ||
×
630
                Object.keys(valuesSetNormalized).length === 0
631
            ) {
UNCOV
632
                if (
×
633
                    metadata.versionColumn &&
×
634
                    updatedColumns.indexOf(metadata.versionColumn) === -1
635
                )
UNCOV
636
                    updateColumnAndValues.push(
×
637
                        this.escape(metadata.versionColumn.databaseName) +
638
                            " = " +
639
                            this.escape(metadata.versionColumn.databaseName) +
640
                            " + 1",
641
                    )
UNCOV
642
                if (
×
643
                    metadata.updateDateColumn &&
×
644
                    updatedColumns.indexOf(metadata.updateDateColumn) === -1
645
                )
UNCOV
646
                    updateColumnAndValues.push(
×
647
                        this.escape(metadata.updateDateColumn.databaseName) +
648
                            " = CURRENT_TIMESTAMP",
649
                    ) // todo: fix issue with CURRENT_TIMESTAMP(6) being used, can "DEFAULT" be used?!
650
            }
651
        } else {
UNCOV
652
            Object.keys(valuesSetNormalized).map((key) => {
×
UNCOV
653
                const value = valuesSetNormalized[key]
×
654

655
                // todo: duplication zone
UNCOV
656
                if (typeof value === "function") {
×
657
                    // support for SQL expressions in update query
658
                    updateColumnAndValues.push(
×
659
                        this.escape(key) + " = " + value(),
660
                    )
UNCOV
661
                } else if (
×
662
                    (this.connection.driver.options.type === "sap" ||
×
663
                        this.connection.driver.options.type === "spanner") &&
664
                    value === null
665
                ) {
666
                    updateColumnAndValues.push(this.escape(key) + " = NULL")
×
667
                } else {
668
                    // we need to store array values in a special class to make sure parameter replacement will work correctly
669
                    // if (value instanceof Array)
670
                    //     value = new ArrayParameter(value);
671

UNCOV
672
                    const paramName = this.createParameter(value)
×
UNCOV
673
                    updateColumnAndValues.push(
×
674
                        this.escape(key) + " = " + paramName,
675
                    )
676
                }
677
            })
678
        }
679

UNCOV
680
        if (updateColumnAndValues.length <= 0) {
×
UNCOV
681
            throw new UpdateValuesMissingError()
×
682
        }
683

684
        // get a table name and all column database names
UNCOV
685
        const whereExpression = this.createWhereExpression()
×
UNCOV
686
        const returningExpression = this.createReturningExpression("update")
×
687

UNCOV
688
        if (returningExpression === "") {
×
UNCOV
689
            return `UPDATE ${this.getTableName(
×
690
                this.getMainTableName(),
691
            )} SET ${updateColumnAndValues.join(", ")}${whereExpression}` // todo: how do we replace aliases in where to nothing?
692
        }
UNCOV
693
        if (this.connection.driver.options.type === "mssql") {
×
UNCOV
694
            return `UPDATE ${this.getTableName(
×
695
                this.getMainTableName(),
696
            )} SET ${updateColumnAndValues.join(
697
                ", ",
698
            )} OUTPUT ${returningExpression}${whereExpression}`
699
        }
UNCOV
700
        if (this.connection.driver.options.type === "spanner") {
×
701
            return `UPDATE ${this.getTableName(
×
702
                this.getMainTableName(),
703
            )} SET ${updateColumnAndValues.join(
704
                ", ",
705
            )}${whereExpression} THEN RETURN ${returningExpression}`
706
        }
707

UNCOV
708
        return `UPDATE ${this.getTableName(
×
709
            this.getMainTableName(),
710
        )} SET ${updateColumnAndValues.join(
711
            ", ",
712
        )}${whereExpression} RETURNING ${returningExpression}`
713
    }
714

715
    /**
716
     * Creates "ORDER BY" part of SQL query.
717
     */
718
    protected createOrderByExpression() {
UNCOV
719
        const orderBys = this.expressionMap.orderBys
×
UNCOV
720
        if (Object.keys(orderBys).length > 0)
×
721
            return (
×
722
                " ORDER BY " +
723
                Object.keys(orderBys)
724
                    .map((columnName) => {
725
                        if (typeof orderBys[columnName] === "string") {
×
726
                            return (
×
727
                                this.replacePropertyNames(columnName) +
728
                                " " +
729
                                orderBys[columnName]
730
                            )
731
                        } else {
732
                            return (
×
733
                                this.replacePropertyNames(columnName) +
734
                                " " +
735
                                (orderBys[columnName] as any).order +
736
                                " " +
737
                                (orderBys[columnName] as any).nulls
738
                            )
739
                        }
740
                    })
741
                    .join(", ")
742
            )
743

UNCOV
744
        return ""
×
745
    }
746

747
    /**
748
     * Creates "LIMIT" parts of SQL query.
749
     */
750
    protected createLimitExpression(): string {
UNCOV
751
        const limit: number | undefined = this.expressionMap.limit
×
752

UNCOV
753
        if (limit) {
×
UNCOV
754
            if (
×
755
                DriverUtils.isMySQLFamily(this.connection.driver) ||
×
756
                this.connection.driver.options.type === "aurora-mysql"
757
            ) {
UNCOV
758
                return " LIMIT " + limit
×
759
            } else {
UNCOV
760
                throw new LimitOnUpdateNotSupportedError()
×
761
            }
762
        }
763

UNCOV
764
        return ""
×
765
    }
766

767
    /**
768
     * Gets array of values need to be inserted into the target table.
769
     */
770
    protected getValueSet(): ObjectLiteral {
UNCOV
771
        if (typeof this.expressionMap.valuesSet === "object")
×
UNCOV
772
            return this.expressionMap.valuesSet
×
773

UNCOV
774
        throw new UpdateValuesMissingError()
×
775
    }
776
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc