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

Method getQueryBase

packages/sql/src/query/QueryBuilder.ts:3386–3457  ·  view source on GitHub ↗
(processVirtualEntity: boolean)

Source from the content-addressed store, hash-verified

3384 }
3385
3386 private getQueryBase(processVirtualEntity: boolean): NativeQueryBuilder {
3387 const qb = this.platform.createNativeQueryBuilder().setFlags(this.#state.flags);
3388 const { subQuery, aliasName, entityName, meta, rawTableName } = this.mainAlias;
3389 const requiresAlias =
3390 this.#state.finalized && (this.#state.explicitAlias || this.helper.isTableNameAliasRequired(this.type));
3391 const alias = requiresAlias ? aliasName : undefined;
3392 const schema = this.getSchema(this.mainAlias);
3393 const tableName = rawTableName
3394 ? rawTableName
3395 : subQuery instanceof NativeQueryBuilder
3396 ? subQuery.as(aliasName)
3397 : subQuery
3398 ? raw(`(${subQuery.sql}) as ${this.platform.quoteIdentifier(aliasName)}`, subQuery.params)
3399 : this.helper.getTableName(entityName);
3400 const joinSchema = this.#state.schema ?? this.em?.schema ?? schema;
3401 const schemaOverride = this.#state.schema ?? this.em?.schema;
3402
3403 if (meta.virtual && processVirtualEntity) {
3404 qb.from(raw(this.fromVirtual(meta)), { indexHint: this.#state.indexHint });
3405 } else {
3406 qb.from(tableName, {
3407 schema: rawTableName ? undefined : schema,
3408 alias,
3409 indexHint: this.#state.indexHint,
3410 });
3411 }
3412
3413 switch (this.type) {
3414 case QueryType.SELECT:
3415 qb.select(this.prepareFields(this.#state.fields!, 'where', schema));
3416
3417 if (this.#state.distinctOn) {
3418 qb.distinctOn(this.prepareFields(this.#state.distinctOn, 'where', schema) as string[]);
3419 } else if (this.#state.flags.has(QueryFlag.DISTINCT)) {
3420 qb.distinct();
3421 }
3422
3423 this.helper.processJoins(qb, this.#state.joins, joinSchema, schemaOverride);
3424 break;
3425 case QueryType.COUNT: {
3426 const fields = this.#state.fields!.map(f =>
3427 this.helper.mapper(f as string, this.type, undefined, undefined, schema),
3428 );
3429 qb.count(fields, this.#state.flags.has(QueryFlag.DISTINCT));
3430 this.helper.processJoins(qb, this.#state.joins, joinSchema, schemaOverride);
3431 break;
3432 }
3433 case QueryType.INSERT:
3434 if (this.#state.insertSubQuery) {
3435 const columns = this.resolveInsertFromColumns();
3436 const compiled = this.#state.insertSubQuery.toQuery();
3437 qb.insertSelect(columns, raw(compiled.sql, compiled.params));
3438 } else {
3439 qb.insert(this.#state.data!);
3440 }
3441
3442 break;
3443 case QueryType.UPDATE:

Callers 1

getNativeQueryMethod · 0.95

Calls 15

getSchemaMethod · 0.95
fromVirtualMethod · 0.95
prepareFieldsMethod · 0.95
rawFunction · 0.90
setFlagsMethod · 0.80
processJoinsMethod · 0.80
mapperMethod · 0.80
toQueryMethod · 0.80
insertSelectMethod · 0.80
updateVersionPropertyMethod · 0.80

Tested by

no test coverage detected