@mikro-orm/sql 7.0.3-dev.8 → 7.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AbstractSqlConnection.d.ts +94 -58
- package/AbstractSqlConnection.js +235 -238
- package/AbstractSqlDriver.d.ts +410 -155
- package/AbstractSqlDriver.js +2064 -1937
- package/AbstractSqlPlatform.d.ts +83 -73
- package/AbstractSqlPlatform.js +162 -158
- package/PivotCollectionPersister.d.ts +33 -15
- package/PivotCollectionPersister.js +158 -160
- package/README.md +2 -2
- package/SqlEntityManager.d.ts +67 -22
- package/SqlEntityManager.js +54 -38
- package/SqlEntityRepository.d.ts +14 -14
- package/SqlEntityRepository.js +23 -23
- package/dialects/mssql/MsSqlNativeQueryBuilder.d.ts +12 -12
- package/dialects/mssql/MsSqlNativeQueryBuilder.js +192 -194
- package/dialects/mysql/BaseMySqlPlatform.d.ts +64 -45
- package/dialects/mysql/BaseMySqlPlatform.js +134 -131
- package/dialects/mysql/MySqlExceptionConverter.d.ts +6 -6
- package/dialects/mysql/MySqlExceptionConverter.js +91 -77
- package/dialects/mysql/MySqlNativeQueryBuilder.d.ts +3 -3
- package/dialects/mysql/MySqlNativeQueryBuilder.js +66 -69
- package/dialects/mysql/MySqlSchemaHelper.d.ts +39 -39
- package/dialects/mysql/MySqlSchemaHelper.js +327 -319
- package/dialects/oracledb/OracleDialect.d.ts +81 -52
- package/dialects/oracledb/OracleDialect.js +155 -149
- package/dialects/oracledb/OracleNativeQueryBuilder.d.ts +12 -12
- package/dialects/oracledb/OracleNativeQueryBuilder.js +232 -236
- package/dialects/postgresql/BasePostgreSqlPlatform.d.ts +108 -105
- package/dialects/postgresql/BasePostgreSqlPlatform.js +351 -350
- package/dialects/postgresql/FullTextType.d.ts +10 -6
- package/dialects/postgresql/FullTextType.js +51 -51
- package/dialects/postgresql/PostgreSqlExceptionConverter.d.ts +5 -5
- package/dialects/postgresql/PostgreSqlExceptionConverter.js +55 -43
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.d.ts +1 -1
- package/dialects/postgresql/PostgreSqlNativeQueryBuilder.js +4 -4
- package/dialects/postgresql/PostgreSqlSchemaHelper.d.ts +102 -82
- package/dialects/postgresql/PostgreSqlSchemaHelper.js +733 -683
- package/dialects/sqlite/BaseSqliteConnection.d.ts +3 -5
- package/dialects/sqlite/BaseSqliteConnection.js +21 -19
- package/dialects/sqlite/NodeSqliteDialect.d.ts +1 -1
- package/dialects/sqlite/NodeSqliteDialect.js +23 -23
- package/dialects/sqlite/SqliteDriver.d.ts +1 -1
- package/dialects/sqlite/SqliteDriver.js +3 -3
- package/dialects/sqlite/SqliteExceptionConverter.d.ts +6 -6
- package/dialects/sqlite/SqliteExceptionConverter.js +67 -51
- package/dialects/sqlite/SqliteNativeQueryBuilder.d.ts +2 -2
- package/dialects/sqlite/SqliteNativeQueryBuilder.js +7 -7
- package/dialects/sqlite/SqlitePlatform.d.ts +63 -72
- package/dialects/sqlite/SqlitePlatform.js +139 -139
- package/dialects/sqlite/SqliteSchemaHelper.d.ts +70 -60
- package/dialects/sqlite/SqliteSchemaHelper.js +533 -520
- package/package.json +3 -3
- package/plugin/index.d.ts +42 -35
- package/plugin/index.js +43 -36
- package/plugin/transformer.d.ts +117 -94
- package/plugin/transformer.js +890 -881
- package/query/ArrayCriteriaNode.d.ts +4 -4
- package/query/ArrayCriteriaNode.js +18 -18
- package/query/CriteriaNode.d.ts +35 -25
- package/query/CriteriaNode.js +133 -123
- package/query/CriteriaNodeFactory.d.ts +49 -6
- package/query/CriteriaNodeFactory.js +97 -94
- package/query/NativeQueryBuilder.d.ts +118 -118
- package/query/NativeQueryBuilder.js +484 -480
- package/query/ObjectCriteriaNode.d.ts +12 -12
- package/query/ObjectCriteriaNode.js +298 -282
- package/query/QueryBuilder.d.ts +1546 -904
- package/query/QueryBuilder.js +2294 -2144
- package/query/QueryBuilderHelper.d.ts +153 -72
- package/query/QueryBuilderHelper.js +1079 -1028
- package/query/ScalarCriteriaNode.d.ts +3 -3
- package/query/ScalarCriteriaNode.js +53 -46
- package/query/enums.d.ts +14 -14
- package/query/enums.js +14 -14
- package/query/raw.d.ts +16 -6
- package/query/raw.js +10 -10
- package/schema/DatabaseSchema.d.ts +73 -50
- package/schema/DatabaseSchema.js +331 -307
- package/schema/DatabaseTable.d.ts +96 -73
- package/schema/DatabaseTable.js +1012 -927
- package/schema/SchemaComparator.d.ts +70 -66
- package/schema/SchemaComparator.js +766 -740
- package/schema/SchemaHelper.d.ts +109 -95
- package/schema/SchemaHelper.js +675 -659
- package/schema/SqlSchemaGenerator.d.ts +78 -58
- package/schema/SqlSchemaGenerator.js +535 -501
- package/typings.d.ts +380 -266
package/AbstractSqlConnection.js
CHANGED
|
@@ -1,260 +1,257 @@
|
|
|
1
1
|
import { CompiledQuery, Kysely } from 'kysely';
|
|
2
|
-
import { Connection, EventType, RawQueryFragment, Utils
|
|
2
|
+
import { Connection, EventType, RawQueryFragment, Utils } from '@mikro-orm/core';
|
|
3
3
|
import { NativeQueryBuilder } from './query/NativeQueryBuilder.js';
|
|
4
4
|
/** Base class for SQL database connections, built on top of Kysely. */
|
|
5
5
|
export class AbstractSqlConnection extends Connection {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
6
|
+
#client;
|
|
7
|
+
/** Establishes the database connection and runs the onConnect hook. */
|
|
8
|
+
async connect(options) {
|
|
9
|
+
await this.initClient();
|
|
10
|
+
this.connected = true;
|
|
11
|
+
if (options?.skipOnConnect !== true) {
|
|
12
|
+
await this.onConnect();
|
|
14
13
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (driverOptions instanceof Kysely) {
|
|
22
|
-
this.logger.log('info', 'Reusing Kysely client provided via `driverOptions`');
|
|
23
|
-
this.#client = driverOptions;
|
|
24
|
-
}
|
|
25
|
-
else if ('createDriver' in driverOptions) {
|
|
26
|
-
this.logger.log('info', 'Reusing Kysely dialect provided via `driverOptions`');
|
|
27
|
-
this.#client = new Kysely({ dialect: driverOptions });
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
const dialect = this.createKyselyDialect(driverOptions);
|
|
31
|
-
if (dialect instanceof Promise) {
|
|
32
|
-
return dialect.then(d => {
|
|
33
|
-
this.#client = new Kysely({ dialect: d });
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
this.#client = new Kysely({ dialect });
|
|
37
|
-
}
|
|
14
|
+
}
|
|
15
|
+
/** Initializes the Kysely client from driver options or a user-provided Kysely instance. */
|
|
16
|
+
createKysely() {
|
|
17
|
+
let driverOptions = this.options.driverOptions ?? this.config.get('driverOptions');
|
|
18
|
+
if (typeof driverOptions === 'function') {
|
|
19
|
+
driverOptions = driverOptions();
|
|
38
20
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
21
|
+
if (driverOptions instanceof Kysely) {
|
|
22
|
+
this.logger.log('info', 'Reusing Kysely client provided via `driverOptions`');
|
|
23
|
+
this.#client = driverOptions;
|
|
24
|
+
} else if ('createDriver' in driverOptions) {
|
|
25
|
+
this.logger.log('info', 'Reusing Kysely dialect provided via `driverOptions`');
|
|
26
|
+
this.#client = new Kysely({ dialect: driverOptions });
|
|
27
|
+
} else {
|
|
28
|
+
const dialect = this.createKyselyDialect(driverOptions);
|
|
29
|
+
if (dialect instanceof Promise) {
|
|
30
|
+
return dialect.then(d => {
|
|
31
|
+
this.#client = new Kysely({ dialect: d });
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
this.#client = new Kysely({ dialect });
|
|
47
35
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @inheritDoc
|
|
39
|
+
*/
|
|
40
|
+
async close(force) {
|
|
41
|
+
await super.close(force);
|
|
42
|
+
await this.#client?.destroy();
|
|
43
|
+
this.connected = false;
|
|
44
|
+
this.#client = undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @inheritDoc
|
|
48
|
+
*/
|
|
49
|
+
async isConnected() {
|
|
50
|
+
const check = await this.checkConnection();
|
|
51
|
+
return check.ok;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @inheritDoc
|
|
55
|
+
*/
|
|
56
|
+
async checkConnection() {
|
|
57
|
+
if (!this.connected) {
|
|
58
|
+
return { ok: false, reason: 'Connection not established' };
|
|
54
59
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return { ok: false, reason: 'Connection not established' };
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
await this.getClient().executeQuery(CompiledQuery.raw('select 1'));
|
|
64
|
-
return { ok: true };
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
return { ok: false, reason: error.message, error };
|
|
68
|
-
}
|
|
60
|
+
try {
|
|
61
|
+
await this.getClient().executeQuery(CompiledQuery.raw('select 1'));
|
|
62
|
+
return { ok: true };
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return { ok: false, reason: error.message, error };
|
|
69
65
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
66
|
+
}
|
|
67
|
+
/** Returns the underlying Kysely client, creating it synchronously if needed. */
|
|
68
|
+
getClient() {
|
|
69
|
+
if (!this.#client) {
|
|
70
|
+
const maybePromise = this.createKysely();
|
|
71
|
+
/* v8 ignore next */
|
|
72
|
+
if (maybePromise instanceof Promise) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
'Current driver requires async initialization, use `MikroORM.init()` instead of the constructor',
|
|
75
|
+
);
|
|
76
|
+
}
|
|
80
77
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
78
|
+
return this.#client;
|
|
79
|
+
}
|
|
80
|
+
/** Ensures the Kysely client is initialized, creating it asynchronously if needed. */
|
|
81
|
+
async initClient() {
|
|
82
|
+
if (!this.#client) {
|
|
83
|
+
await this.createKysely();
|
|
86
84
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
85
|
+
}
|
|
86
|
+
/** Executes a callback within a transaction, committing on success and rolling back on error. */
|
|
87
|
+
async transactional(cb, options = {}) {
|
|
88
|
+
const trx = await this.begin(options);
|
|
89
|
+
try {
|
|
90
|
+
const ret = await cb(trx);
|
|
91
|
+
await this.commit(trx, options.eventBroadcaster, options.loggerContext);
|
|
92
|
+
return ret;
|
|
93
|
+
} catch (error) {
|
|
94
|
+
await this.rollback(trx, options.eventBroadcaster, options.loggerContext);
|
|
95
|
+
throw error;
|
|
99
96
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
await this.ensureConnection();
|
|
115
|
-
await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
|
|
116
|
-
let trxBuilder = this.getClient().startTransaction();
|
|
117
|
-
if (options.isolationLevel) {
|
|
118
|
-
trxBuilder = trxBuilder.setIsolationLevel(options.isolationLevel);
|
|
119
|
-
}
|
|
120
|
-
if (options.readOnly) {
|
|
121
|
-
trxBuilder = trxBuilder.setAccessMode('read only');
|
|
122
|
-
}
|
|
123
|
-
const trx = await trxBuilder.execute();
|
|
124
|
-
if (options.ctx) {
|
|
125
|
-
const ctx = options.ctx;
|
|
126
|
-
ctx.index ??= 0;
|
|
127
|
-
const savepointName = `trx${ctx.index + 1}`;
|
|
128
|
-
Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
|
|
129
|
-
Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
|
|
130
|
-
this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext);
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
for (const query of this.platform.getBeginTransactionSQL(options)) {
|
|
134
|
-
this.logQuery(query, options.loggerContext);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
|
|
138
|
-
return trx;
|
|
97
|
+
}
|
|
98
|
+
/** Begins a new transaction or creates a savepoint if a transaction context already exists. */
|
|
99
|
+
async begin(options = {}) {
|
|
100
|
+
if (options.ctx) {
|
|
101
|
+
const ctx = options.ctx;
|
|
102
|
+
await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart, ctx);
|
|
103
|
+
ctx.index ??= 0;
|
|
104
|
+
const savepointName = `trx${ctx.index + 1}`;
|
|
105
|
+
const trx = await options.ctx.savepoint(savepointName).execute();
|
|
106
|
+
Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
|
|
107
|
+
Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
|
|
108
|
+
this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext);
|
|
109
|
+
await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
|
|
110
|
+
return trx;
|
|
139
111
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionCommit, ctx);
|
|
146
|
-
if ('savepointName' in ctx) {
|
|
147
|
-
await ctx.releaseSavepoint(ctx.savepointName).execute();
|
|
148
|
-
this.logQuery(this.platform.getReleaseSavepointSQL(ctx.savepointName), loggerContext);
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
await ctx.commit().execute();
|
|
152
|
-
this.logQuery(this.platform.getCommitTransactionSQL(), loggerContext);
|
|
153
|
-
}
|
|
154
|
-
await eventBroadcaster?.dispatchEvent(EventType.afterTransactionCommit, ctx);
|
|
112
|
+
await this.ensureConnection();
|
|
113
|
+
await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
|
|
114
|
+
let trxBuilder = this.getClient().startTransaction();
|
|
115
|
+
if (options.isolationLevel) {
|
|
116
|
+
trxBuilder = trxBuilder.setIsolationLevel(options.isolationLevel);
|
|
155
117
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionRollback, ctx);
|
|
159
|
-
if ('savepointName' in ctx) {
|
|
160
|
-
await ctx.rollbackToSavepoint(ctx.savepointName).execute();
|
|
161
|
-
this.logQuery(this.platform.getRollbackToSavepointSQL(ctx.savepointName), loggerContext);
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
await ctx.rollback().execute();
|
|
165
|
-
this.logQuery(this.platform.getRollbackTransactionSQL(), loggerContext);
|
|
166
|
-
}
|
|
167
|
-
await eventBroadcaster?.dispatchEvent(EventType.afterTransactionRollback, ctx);
|
|
118
|
+
if (options.readOnly) {
|
|
119
|
+
trxBuilder = trxBuilder.setAccessMode('read only');
|
|
168
120
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
121
|
+
const trx = await trxBuilder.execute();
|
|
122
|
+
if (options.ctx) {
|
|
123
|
+
const ctx = options.ctx;
|
|
124
|
+
ctx.index ??= 0;
|
|
125
|
+
const savepointName = `trx${ctx.index + 1}`;
|
|
126
|
+
Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 });
|
|
127
|
+
Reflect.defineProperty(trx, 'savepointName', { value: savepointName });
|
|
128
|
+
this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext);
|
|
129
|
+
} else {
|
|
130
|
+
for (const query of this.platform.getBeginTransactionSQL(options)) {
|
|
131
|
+
this.logQuery(query, options.loggerContext);
|
|
132
|
+
}
|
|
180
133
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const res = await (ctx ?? this.#client).executeQuery(compiled);
|
|
189
|
-
return this.transformRawResult(res, method);
|
|
190
|
-
}, { ...q, ...loggerContext });
|
|
134
|
+
await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx);
|
|
135
|
+
return trx;
|
|
136
|
+
}
|
|
137
|
+
/** Commits the transaction or releases the savepoint. */
|
|
138
|
+
async commit(ctx, eventBroadcaster, loggerContext) {
|
|
139
|
+
if (ctx.isRolledBack) {
|
|
140
|
+
return;
|
|
191
141
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
query: {
|
|
200
|
-
kind: 'SelectQueryNode',
|
|
201
|
-
},
|
|
202
|
-
sql: q.formatted,
|
|
203
|
-
parameters: [],
|
|
204
|
-
};
|
|
205
|
-
try {
|
|
206
|
-
const res = (ctx ?? this.getClient()).getExecutor().stream(compiled, 1);
|
|
207
|
-
this.logQuery(sql, {
|
|
208
|
-
sql,
|
|
209
|
-
params,
|
|
210
|
-
...loggerContext,
|
|
211
|
-
affected: Utils.isPlainObject(res) ? res.affectedRows : undefined,
|
|
212
|
-
});
|
|
213
|
-
for await (const items of res) {
|
|
214
|
-
for (const row of this.transformRawResult(items, 'all')) {
|
|
215
|
-
yield row;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
catch (e) {
|
|
220
|
-
this.logQuery(sql, { sql, params, ...loggerContext, level: 'error' });
|
|
221
|
-
throw e;
|
|
222
|
-
}
|
|
142
|
+
await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionCommit, ctx);
|
|
143
|
+
if ('savepointName' in ctx) {
|
|
144
|
+
await ctx.releaseSavepoint(ctx.savepointName).execute();
|
|
145
|
+
this.logQuery(this.platform.getReleaseSavepointSQL(ctx.savepointName), loggerContext);
|
|
146
|
+
} else {
|
|
147
|
+
await ctx.commit().execute();
|
|
148
|
+
this.logQuery(this.platform.getCommitTransactionSQL(), loggerContext);
|
|
223
149
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
150
|
+
await eventBroadcaster?.dispatchEvent(EventType.afterTransactionCommit, ctx);
|
|
151
|
+
}
|
|
152
|
+
/** Rolls back the transaction or rolls back to the savepoint. */
|
|
153
|
+
async rollback(ctx, eventBroadcaster, loggerContext) {
|
|
154
|
+
await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionRollback, ctx);
|
|
155
|
+
if ('savepointName' in ctx) {
|
|
156
|
+
await ctx.rollbackToSavepoint(ctx.savepointName).execute();
|
|
157
|
+
this.logQuery(this.platform.getRollbackToSavepointSQL(ctx.savepointName), loggerContext);
|
|
158
|
+
} else {
|
|
159
|
+
await ctx.rollback().execute();
|
|
160
|
+
this.logQuery(this.platform.getRollbackTransactionSQL(), loggerContext);
|
|
235
161
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
if (logger.isEnabled('query-params', context)) {
|
|
242
|
-
return formatted;
|
|
243
|
-
}
|
|
244
|
-
return query;
|
|
162
|
+
await eventBroadcaster?.dispatchEvent(EventType.afterTransactionRollback, ctx);
|
|
163
|
+
}
|
|
164
|
+
prepareQuery(query, params = []) {
|
|
165
|
+
if (query instanceof NativeQueryBuilder) {
|
|
166
|
+
query = query.toRaw();
|
|
245
167
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
168
|
+
if (query instanceof RawQueryFragment) {
|
|
169
|
+
params = query.params;
|
|
170
|
+
query = query.sql;
|
|
171
|
+
}
|
|
172
|
+
query = this.config.get('onQuery')(query, params);
|
|
173
|
+
const formatted = this.platform.formatQuery(query, params);
|
|
174
|
+
return { query, params, formatted };
|
|
175
|
+
}
|
|
176
|
+
/** Executes a SQL query and returns the result based on the method: `'all'` for rows, `'get'` for single row, `'run'` for affected count. */
|
|
177
|
+
async execute(query, params = [], method = 'all', ctx, loggerContext) {
|
|
178
|
+
await this.ensureConnection();
|
|
179
|
+
const q = this.prepareQuery(query, params);
|
|
180
|
+
const sql = this.getSql(q.query, q.formatted, loggerContext);
|
|
181
|
+
return this.executeQuery(
|
|
182
|
+
sql,
|
|
183
|
+
async () => {
|
|
184
|
+
const compiled = CompiledQuery.raw(q.formatted);
|
|
185
|
+
const res = await (ctx ?? this.#client).executeQuery(compiled);
|
|
186
|
+
return this.transformRawResult(res, method);
|
|
187
|
+
},
|
|
188
|
+
{ ...q, ...loggerContext },
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
/** Executes a SQL query and returns an async iterable that yields results row by row. */
|
|
192
|
+
async *stream(query, params = [], ctx, loggerContext) {
|
|
193
|
+
await this.ensureConnection();
|
|
194
|
+
const q = this.prepareQuery(query, params);
|
|
195
|
+
const sql = this.getSql(q.query, q.formatted, loggerContext);
|
|
196
|
+
// construct the compiled query manually with `kind: 'SelectQueryNode'` to avoid sqlite validation for select queries when streaming
|
|
197
|
+
const compiled = {
|
|
198
|
+
query: {
|
|
199
|
+
kind: 'SelectQueryNode',
|
|
200
|
+
},
|
|
201
|
+
sql: q.formatted,
|
|
202
|
+
parameters: [],
|
|
203
|
+
};
|
|
204
|
+
try {
|
|
205
|
+
const res = (ctx ?? this.getClient()).getExecutor().stream(compiled, 1);
|
|
206
|
+
this.logQuery(sql, {
|
|
207
|
+
sql,
|
|
208
|
+
params,
|
|
209
|
+
...loggerContext,
|
|
210
|
+
affected: Utils.isPlainObject(res) ? res.affectedRows : undefined,
|
|
211
|
+
});
|
|
212
|
+
for await (const items of res) {
|
|
213
|
+
for (const row of this.transformRawResult(items, 'all')) {
|
|
214
|
+
yield row;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
} catch (e) {
|
|
218
|
+
this.logQuery(sql, { sql, params, ...loggerContext, level: 'error' });
|
|
219
|
+
throw e;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/** @inheritDoc */
|
|
223
|
+
async executeDump(dump) {
|
|
224
|
+
await this.ensureConnection();
|
|
225
|
+
try {
|
|
226
|
+
const raw = CompiledQuery.raw(dump);
|
|
227
|
+
await this.getClient().executeQuery(raw);
|
|
228
|
+
} catch (e) {
|
|
229
|
+
/* v8 ignore next */
|
|
230
|
+
throw this.platform.getExceptionConverter().convertException(e);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
getSql(query, formatted, context) {
|
|
234
|
+
const logger = this.config.getLogger();
|
|
235
|
+
if (!logger.isEnabled('query', context)) {
|
|
236
|
+
return query;
|
|
237
|
+
}
|
|
238
|
+
if (logger.isEnabled('query-params', context)) {
|
|
239
|
+
return formatted;
|
|
240
|
+
}
|
|
241
|
+
return query;
|
|
242
|
+
}
|
|
243
|
+
transformRawResult(res, method) {
|
|
244
|
+
if (method === 'get') {
|
|
245
|
+
return res.rows[0];
|
|
246
|
+
}
|
|
247
|
+
if (method === 'all') {
|
|
248
|
+
return res.rows;
|
|
259
249
|
}
|
|
250
|
+
return {
|
|
251
|
+
affectedRows: Number(res.numAffectedRows ?? res.rows.length),
|
|
252
|
+
insertId: res.insertId != null ? Number(res.insertId) : res.insertId,
|
|
253
|
+
row: res.rows[0],
|
|
254
|
+
rows: res.rows,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
260
257
|
}
|