• 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

9.2
/src/driver/sqlite/SqliteQueryRunner.ts
1
import { ConnectionIsNotSetError } from "../../error/ConnectionIsNotSetError"
1✔
2
import { QueryFailedError } from "../../error/QueryFailedError"
1✔
3
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
1✔
4
import { QueryResult } from "../../query-runner/QueryResult"
1✔
5
import { Broadcaster } from "../../subscriber/Broadcaster"
1✔
6
import { BroadcasterResult } from "../../subscriber/BroadcasterResult"
1✔
7
import { AbstractSqliteQueryRunner } from "../sqlite-abstract/AbstractSqliteQueryRunner"
1✔
8
import { SqliteConnectionOptions } from "./SqliteConnectionOptions"
9
import { SqliteDriver } from "./SqliteDriver"
10

11
/**
12
 * Runs queries on a single sqlite database connection.
13
 *
14
 * Does not support compose primary keys with autoincrement field.
15
 * todo: need to throw exception for this case.
16
 */
17
export class SqliteQueryRunner extends AbstractSqliteQueryRunner {
1✔
18
    /**
19
     * Database driver used by connection.
20
     */
21
    driver: SqliteDriver
22

23
    // -------------------------------------------------------------------------
24
    // Constructor
25
    // -------------------------------------------------------------------------
26

27
    constructor(driver: SqliteDriver) {
UNCOV
28
        super()
×
UNCOV
29
        this.driver = driver
×
UNCOV
30
        this.connection = driver.connection
×
UNCOV
31
        this.broadcaster = new Broadcaster(this)
×
32
    }
33

34
    /**
35
     * Called before migrations are run.
36
     */
37
    async beforeMigration(): Promise<void> {
UNCOV
38
        await this.query(`PRAGMA foreign_keys = OFF`)
×
39
    }
40

41
    /**
42
     * Called after migrations are run.
43
     */
44
    async afterMigration(): Promise<void> {
UNCOV
45
        await this.query(`PRAGMA foreign_keys = ON`)
×
46
    }
47

48
    /**
49
     * Executes a given SQL query.
50
     */
51
    async query(
52
        query: string,
53
        parameters?: any[],
54
        useStructuredResult = false,
×
55
    ): Promise<any> {
UNCOV
56
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
×
57

UNCOV
58
        const connection = this.driver.connection
×
UNCOV
59
        const options = connection.options as SqliteConnectionOptions
×
UNCOV
60
        const maxQueryExecutionTime = this.driver.options.maxQueryExecutionTime
×
UNCOV
61
        const broadcaster = this.broadcaster
×
62

UNCOV
63
        if (!connection.isInitialized) {
×
UNCOV
64
            throw new ConnectionIsNotSetError("sqlite")
×
65
        }
66

UNCOV
67
        const databaseConnection = await this.connect()
×
68

UNCOV
69
        this.driver.connection.logger.logQuery(query, parameters, this)
×
UNCOV
70
        await broadcaster.broadcast("BeforeQuery", query, parameters)
×
71

UNCOV
72
        const broadcasterResult = new BroadcasterResult()
×
73

UNCOV
74
        return new Promise(async (ok, fail) => {
×
UNCOV
75
            try {
×
UNCOV
76
                const queryStartTime = Date.now()
×
UNCOV
77
                const isInsertQuery = query.startsWith("INSERT ")
×
UNCOV
78
                const isDeleteQuery = query.startsWith("DELETE ")
×
UNCOV
79
                const isUpdateQuery = query.startsWith("UPDATE ")
×
80

UNCOV
81
                const execute = async () => {
×
UNCOV
82
                    if (isInsertQuery || isDeleteQuery || isUpdateQuery) {
×
UNCOV
83
                        await databaseConnection.run(query, parameters, handler)
×
84
                    } else {
UNCOV
85
                        await databaseConnection.all(query, parameters, handler)
×
86
                    }
87
                }
88

UNCOV
89
                const self = this
×
UNCOV
90
                const handler = function (this: any, err: any, rows: any) {
×
UNCOV
91
                    if (err && err.toString().indexOf("SQLITE_BUSY:") !== -1) {
×
92
                        if (
×
93
                            typeof options.busyErrorRetry === "number" &&
×
94
                            options.busyErrorRetry > 0
95
                        ) {
96
                            setTimeout(execute, options.busyErrorRetry)
×
97
                            return
×
98
                        }
99
                    }
100

101
                    // log slow queries if maxQueryExecution time is set
UNCOV
102
                    const queryEndTime = Date.now()
×
UNCOV
103
                    const queryExecutionTime = queryEndTime - queryStartTime
×
UNCOV
104
                    if (
×
105
                        maxQueryExecutionTime &&
×
106
                        queryExecutionTime > maxQueryExecutionTime
107
                    )
108
                        connection.logger.logQuerySlow(
×
109
                            queryExecutionTime,
110
                            query,
111
                            parameters,
112
                            self,
113
                        )
114

UNCOV
115
                    if (err) {
×
UNCOV
116
                        connection.logger.logQueryError(
×
117
                            err,
118
                            query,
119
                            parameters,
120
                            self,
121
                        )
UNCOV
122
                        broadcaster.broadcastAfterQueryEvent(
×
123
                            broadcasterResult,
124
                            query,
125
                            parameters,
126
                            false,
127
                            undefined,
128
                            undefined,
129
                            err,
130
                        )
131

UNCOV
132
                        return fail(
×
133
                            new QueryFailedError(query, parameters, err),
134
                        )
135
                    } else {
UNCOV
136
                        const result = new QueryResult()
×
137

UNCOV
138
                        if (isInsertQuery) {
×
UNCOV
139
                            result.raw = this["lastID"]
×
140
                        } else {
UNCOV
141
                            result.raw = rows
×
142
                        }
143

UNCOV
144
                        broadcaster.broadcastAfterQueryEvent(
×
145
                            broadcasterResult,
146
                            query,
147
                            parameters,
148
                            true,
149
                            queryExecutionTime,
150
                            result.raw,
151
                            undefined,
152
                        )
153

UNCOV
154
                        if (Array.isArray(rows)) {
×
UNCOV
155
                            result.records = rows
×
156
                        }
157

UNCOV
158
                        result.affected = this["changes"]
×
159

UNCOV
160
                        if (useStructuredResult) {
×
UNCOV
161
                            ok(result)
×
162
                        } else {
UNCOV
163
                            ok(result.raw)
×
164
                        }
165
                    }
166
                }
167

UNCOV
168
                await execute()
×
169
            } catch (err) {
170
                fail(err)
×
171
            } finally {
UNCOV
172
                await broadcasterResult.wait()
×
173
            }
174
        })
175
    }
176
}
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