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

typeorm / typeorm / 15089093306

17 May 2025 09:03PM UTC coverage: 50.109% (-26.2%) from 76.346%
15089093306

Pull #11437

github

naorpeled
add comment about vector <#>
Pull Request #11437: feat(postgres): support vector data type

5836 of 12767 branches covered (45.71%)

Branch coverage included in aggregate %.

16 of 17 new or added lines in 4 files covered. (94.12%)

6283 existing lines in 64 files now uncovered.

12600 of 24025 relevant lines covered (52.45%)

28708.0 hits per line

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

69.45
/src/query-builder/UpdateQueryBuilder.ts
1
import { ColumnMetadata } from "../metadata/ColumnMetadata"
2
import { QueryBuilder } from "./QueryBuilder"
6✔
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"
6✔
9
import { ReturningStatementNotSupportedError } from "../error/ReturningStatementNotSupportedError"
6✔
10
import { ReturningResultsEntityUpdator } from "./ReturningResultsEntityUpdator"
6✔
11
import { MysqlDriver } from "../driver/mysql/MysqlDriver"
12
import { OrderByCondition } from "../find-options/OrderByCondition"
13
import { LimitOnUpdateNotSupportedError } from "../error/LimitOnUpdateNotSupportedError"
6✔
14
import { UpdateValuesMissingError } from "../error/UpdateValuesMissingError"
6✔
15
import { QueryDeepPartialEntity } from "./QueryPartialEntity"
16
import { AuroraMysqlDriver } from "../driver/aurora-mysql/AuroraMysqlDriver"
17
import { TypeORMError } from "../error"
6✔
18
import { EntityPropertyNotFoundError } from "../error/EntityPropertyNotFoundError"
6✔
19
import { SqlServerDriver } from "../driver/sqlserver/SqlServerDriver"
20
import { DriverUtils } from "../driver/DriverUtils"
6✔
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>
6✔
26
    extends QueryBuilder<Entity>
27
    implements WhereExpressionBuilder
28
{
29
    readonly "@instanceof" = Symbol.for("UpdateQueryBuilder")
4,022✔
30

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

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

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

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

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

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

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

88
            let declareSql: string | null = null
3,742✔
89
            let selectOutputSql: string | null = null
3,742✔
90

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

98
            const returningColumns: ColumnMetadata[] = []
3,742✔
99

100
            if (
3,742!
101
                Array.isArray(this.expressionMap.returning) &&
3,742!
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

113
            if (
3,742✔
114
                this.expressionMap.updateEntity === true &&
11,160✔
115
                this.expressionMap.mainAlias!.hasMetadata &&
116
                this.expressionMap.whereEntities.length > 0
117
            ) {
118
                this.expressionMap.extraReturningColumns =
601✔
119
                    returningResultsEntityUpdator.getUpdationReturningColumns()
120

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

128
            if (
3,742!
129
                returningColumns.length > 0 &&
3,812✔
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
142
            const [updateSql, parameters] = this.getQueryAndParameters()
3,742✔
143

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

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

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

176
            // close transaction if we started it
177
            if (transactionStartedByUs) await queryRunner.commitTransaction()
3,698!
178

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

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

200
    /**
201
     * Values needs to be updated.
202
     */
203
    set(values: QueryDeepPartialEntity<Entity>): this {
204
        this.expressionMap.valuesSet = values
3,748✔
205
        return this
3,748✔
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 {
223
        this.expressionMap.wheres = [] // don't move this block below since computeWhereParameter can add where expressions
3,264✔
224
        const condition = this.getWhereCondition(where)
3,264✔
225
        if (condition)
3,252✔
226
            this.expressionMap.wheres = [
3,252✔
227
                { type: "simple", condition: condition },
228
            ]
229
        if (parameters) this.setParameters(parameters)
3,252✔
230
        return this
3,252✔
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 {
267
        this.expressionMap.wheres.push({
732✔
268
            type: "or",
269
            condition: this.getWhereCondition(where),
270
        })
271
        if (parameters) this.setParameters(parameters)
732!
272
        return this
732✔
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 {
281
        return this.where(this.getWhereInIdsCondition(ids))
187✔
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 {
295
        return this.orWhere(this.getWhereInIdsCondition(ids))
607✔
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
343
        if (!this.connection.driver.isReturningSqlSupported("update")) {
1!
344
            throw new ReturningStatementNotSupportedError()
×
345
        }
346

347
        this.expressionMap.returning = returning
1✔
348
        return this
1✔
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 {
426
        this.expressionMap.limit = limit
6✔
427
        return this
6✔
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 {
436
        if (!this.expressionMap.mainAlias!.hasMetadata)
607!
437
            throw new TypeORMError(
×
438
                `.whereEntity method can only be used on queries which update real entity table.`,
439
            )
440

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

451
            this.orWhereInIds(entityIdMap)
607✔
452
        })
453

454
        this.expressionMap.whereEntities = entities
607✔
455
        return this
607✔
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 {
464
        this.expressionMap.updateEntity = enabled
2,177✔
465
        return this
2,177✔
466
    }
467

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

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

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

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

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

506
                    columns.forEach((column) => {
4,001✔
507
                        if (
4,490✔
508
                            !column.isUpdate ||
8,955✔
509
                            updatedColumns.includes(column)
510
                        ) {
511
                            return
31✔
512
                        }
513

514
                        updatedColumns.push(column)
4,459✔
515

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

535
                        // todo: duplication zone
536
                        if (typeof value === "function") {
4,459✔
537
                            // support for SQL expressions in update query
538
                            updateColumnAndValues.push(
121✔
539
                                this.escape(column.databaseName) +
540
                                    " = " +
541
                                    value(),
542
                            )
543
                        } else if (
4,338!
544
                            (this.connection.driver.options.type === "sap" ||
8,676!
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 {
553
                            if (
4,338!
554
                                this.connection.driver.options.type === "mssql"
555
                            ) {
UNCOV
556
                                value = (
×
557
                                    this.connection.driver as SqlServerDriver
558
                                ).parametrizeValue(column, value)
559
                            }
560

561
                            const paramName = this.createParameter(value)
4,338✔
562

563
                            let expression = null
4,338✔
564
                            if (
4,338!
565
                                (DriverUtils.isMySQLFamily(
8,676!
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
                                }
587
                            } else if (
4,338✔
588
                                DriverUtils.isPostgresFamily(
5,211✔
589
                                    this.connection.driver,
590
                                ) &&
591
                                this.connection.driver.spatialTypes.indexOf(
592
                                    column.type,
593
                                ) !== -1
594
                            ) {
595
                                if (column.srid != null) {
2!
596
                                    expression = `ST_SetSRID(ST_GeomFromGeoJSON(${paramName}), ${column.srid})::${column.type}`
×
597
                                } else {
598
                                    expression = `ST_GeomFromGeoJSON(${paramName})::${column.type}`
2✔
599
                                }
600
                            } else if (
4,336!
601
                                this.connection.driver.options.type ===
4,336!
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 {
615
                                expression = paramName
4,336✔
616
                            }
617
                            updateColumnAndValues.push(
4,338✔
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`
628
            if (
3,689✔
629
                updateColumnAndValues.length > 0 ||
3,701✔
630
                Object.keys(valuesSetNormalized).length === 0
631
            ) {
632
                if (
3,683✔
633
                    metadata.versionColumn &&
3,719✔
634
                    updatedColumns.indexOf(metadata.versionColumn) === -1
635
                )
636
                    updateColumnAndValues.push(
30✔
637
                        this.escape(metadata.versionColumn.databaseName) +
638
                            " = " +
639
                            this.escape(metadata.versionColumn.databaseName) +
640
                            " + 1",
641
                    )
642
                if (
3,683✔
643
                    metadata.updateDateColumn &&
3,748✔
644
                    updatedColumns.indexOf(metadata.updateDateColumn) === -1
645
                )
646
                    updateColumnAndValues.push(
58✔
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 {
652
            Object.keys(valuesSetNormalized).map((key) => {
54✔
653
                const value = valuesSetNormalized[key]
228✔
654

655
                // todo: duplication zone
656
                if (typeof value === "function") {
228!
657
                    // support for SQL expressions in update query
658
                    updateColumnAndValues.push(
×
659
                        this.escape(key) + " = " + value(),
660
                    )
661
                } else if (
228!
662
                    (this.connection.driver.options.type === "sap" ||
456!
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

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

680
        if (updateColumnAndValues.length <= 0) {
3,743✔
681
            throw new UpdateValuesMissingError()
6✔
682
        }
683

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

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

708
        return `UPDATE ${this.getTableName(
14✔
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() {
719
        const orderBys = this.expressionMap.orderBys
3,737✔
720
        if (Object.keys(orderBys).length > 0)
3,737!
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

744
        return ""
3,737✔
745
    }
746

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

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

764
        return ""
3,731✔
765
    }
766

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

774
        throw new UpdateValuesMissingError()
12✔
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