(processVirtualEntity = true)
| 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); |
no test coverage detected