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

Method getNativeQuery

packages/sql/src/query/QueryBuilder.ts:2151–2237  ·  view source on GitHub ↗
(processVirtualEntity = true)

Source from the content-addressed store, hash-verified

2149 }
2150
2151 getNativeQuery(processVirtualEntity = true): NativeQueryBuilder {
2152 if (this.#state.unionQuery) {
2153 if (!this.#query?.qb) {
2154 this.#query = {} as any;
2155 const nqb = this.platform.createNativeQueryBuilder();
2156 nqb.select('*');
2157 nqb.from(raw(`(${this.#state.unionQuery.sql})`, this.#state.unionQuery.params));
2158 this.#query!.qb = nqb;
2159 }
2160
2161 return this.#query!.qb;
2162 }
2163
2164 if (this.#query?.qb) {
2165 return this.#query.qb;
2166 }
2167
2168 this.#query = {} as any;
2169 this.finalize();
2170 const qb = this.getQueryBase(processVirtualEntity);
2171
2172 for (const cte of this.#state.ctes) {
2173 const query = cte.query;
2174 const opts: CteOptions = { columns: cte.columns, materialized: cte.materialized };
2175
2176 if (cte.recursive) {
2177 qb.withRecursive(cte.name, query, opts);
2178 } else {
2179 qb.with(cte.name, query, opts);
2180 }
2181 }
2182
2183 const schema = this.getSchema(this.mainAlias);
2184 const isNotEmptyObject = (obj: Dictionary) => Utils.hasObjectKeys(obj) || RawQueryFragment.hasObjectFragments(obj);
2185
2186 Utils.runIfNotEmpty(
2187 () => this.helper.appendQueryCondition(this.type, this.#state.cond, qb),
2188 this.#state.cond && !this.#state.onConflict,
2189 );
2190 Utils.runIfNotEmpty(
2191 () => qb.groupBy(this.prepareFields(this.#state.groupBy, 'groupBy', schema)),
2192 isNotEmptyObject(this.#state.groupBy),
2193 );
2194 Utils.runIfNotEmpty(
2195 () => this.helper.appendQueryCondition(this.type, this.#state.having, qb, undefined, 'having'),
2196 isNotEmptyObject(this.#state.having),
2197 );
2198 Utils.runIfNotEmpty(() => {
2199 const queryOrder = this.helper.getQueryOrder(
2200 this.type,
2201 this.#state.orderBy as FlatQueryOrderMap[],
2202 this.#state.populateMap,
2203 this.#state.collation,
2204 );
2205
2206 if (queryOrder.length > 0) {
2207 const sql = Utils.unique(queryOrder).join(', ');
2208 qb.orderBy(sql);

Callers 8

toQueryMethod · 0.95
asMethod · 0.95
joinReferenceMethod · 0.45
fromSubQueryMethod · 0.45
processMethod · 0.45

Calls 15

finalizeMethod · 0.95
getQueryBaseMethod · 0.95
getSchemaMethod · 0.95
prepareFieldsMethod · 0.95
rawFunction · 0.90
runIfNotEmptyMethod · 0.80
appendQueryConditionMethod · 0.80
getQueryOrderMethod · 0.80
getLockSQLMethod · 0.80

Tested by

no test coverage detected