MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / wrapModifySubQuery

Method wrapModifySubQuery

packages/sql/src/query/QueryBuilder.ts:4248–4266  ·  view source on GitHub ↗
(meta: EntityMetadata)

Source from the content-addressed store, hash-verified

4246 }
4247
4248 private wrapModifySubQuery(meta: EntityMetadata): void {
4249 const subQuery = this.clone();
4250 subQuery.#state.finalized = true;
4251
4252 // wrap one more time to get around MySQL limitations
4253 // https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause
4254 const subSubQuery = this.platform.createNativeQueryBuilder();
4255 const method = this.#state.flags.has(QueryFlag.UPDATE_SUB_QUERY) ? 'update' : 'delete';
4256 const schema = this.getSchema(this.mainAlias);
4257 const pks = this.prepareFields(meta.primaryKeys, 'sub-query', schema) as string[];
4258 this.#state.cond = {}; // otherwise we would trigger validation error
4259 this.#state.joins = {}; // included in the subquery
4260 subSubQuery.select(pks).from(subQuery.as(this.mainAlias.aliasName));
4261
4262 const { sql, params } = subSubQuery.compile();
4263 ((this[method] as (data?: unknown) => RunQueryBuilder<Entity>)(this.#state.data).where as any)({
4264 [Utils.getPrimaryKeyHash(meta.primaryKeys)]: { $in: raw(sql, params) },
4265 });
4266 }
4267
4268 private getSchema(alias: Alias<any>): string | undefined {
4269 const { meta } = alias;

Callers 1

finalizeMethod · 0.95

Calls 11

cloneMethod · 0.95
getSchemaMethod · 0.95
prepareFieldsMethod · 0.95
rawFunction · 0.90
getPrimaryKeyHashMethod · 0.80
hasMethod · 0.65
fromMethod · 0.65
selectMethod · 0.65
asMethod · 0.45
compileMethod · 0.45

Tested by

no test coverage detected