| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import { entityKind } from "../../entity.js";
- import { QueryPromise } from "../../query-promise.js";
- import {
- mapRelationalRow
- } from "../../relations.js";
- class RelationalQueryBuilder {
- constructor(mode, fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
- this.mode = mode;
- this.fullSchema = fullSchema;
- this.schema = schema;
- this.tableNamesMap = tableNamesMap;
- this.table = table;
- this.tableConfig = tableConfig;
- this.dialect = dialect;
- this.session = session;
- }
- static [entityKind] = "SQLiteAsyncRelationalQueryBuilder";
- findMany(config) {
- return this.mode === "sync" ? new SQLiteSyncRelationalQuery(
- this.fullSchema,
- this.schema,
- this.tableNamesMap,
- this.table,
- this.tableConfig,
- this.dialect,
- this.session,
- config ? config : {},
- "many"
- ) : new SQLiteRelationalQuery(
- this.fullSchema,
- this.schema,
- this.tableNamesMap,
- this.table,
- this.tableConfig,
- this.dialect,
- this.session,
- config ? config : {},
- "many"
- );
- }
- findFirst(config) {
- return this.mode === "sync" ? new SQLiteSyncRelationalQuery(
- this.fullSchema,
- this.schema,
- this.tableNamesMap,
- this.table,
- this.tableConfig,
- this.dialect,
- this.session,
- config ? { ...config, limit: 1 } : { limit: 1 },
- "first"
- ) : new SQLiteRelationalQuery(
- this.fullSchema,
- this.schema,
- this.tableNamesMap,
- this.table,
- this.tableConfig,
- this.dialect,
- this.session,
- config ? { ...config, limit: 1 } : { limit: 1 },
- "first"
- );
- }
- }
- class SQLiteRelationalQuery extends QueryPromise {
- constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
- super();
- this.fullSchema = fullSchema;
- this.schema = schema;
- this.tableNamesMap = tableNamesMap;
- this.table = table;
- this.tableConfig = tableConfig;
- this.dialect = dialect;
- this.session = session;
- this.config = config;
- this.mode = mode;
- }
- static [entityKind] = "SQLiteAsyncRelationalQuery";
- /** @internal */
- mode;
- /** @internal */
- getSQL() {
- return this.dialect.buildRelationalQuery({
- fullSchema: this.fullSchema,
- schema: this.schema,
- tableNamesMap: this.tableNamesMap,
- table: this.table,
- tableConfig: this.tableConfig,
- queryConfig: this.config,
- tableAlias: this.tableConfig.tsName
- }).sql;
- }
- /** @internal */
- _prepare(isOneTimeQuery = false) {
- const { query, builtQuery } = this._toSQL();
- return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
- builtQuery,
- void 0,
- this.mode === "first" ? "get" : "all",
- true,
- (rawRows, mapColumnValue) => {
- const rows = rawRows.map(
- (row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection, mapColumnValue)
- );
- if (this.mode === "first") {
- return rows[0];
- }
- return rows;
- }
- );
- }
- prepare() {
- return this._prepare(false);
- }
- _toSQL() {
- const query = this.dialect.buildRelationalQuery({
- fullSchema: this.fullSchema,
- schema: this.schema,
- tableNamesMap: this.tableNamesMap,
- table: this.table,
- tableConfig: this.tableConfig,
- queryConfig: this.config,
- tableAlias: this.tableConfig.tsName
- });
- const builtQuery = this.dialect.sqlToQuery(query.sql);
- return { query, builtQuery };
- }
- toSQL() {
- return this._toSQL().builtQuery;
- }
- /** @internal */
- executeRaw() {
- if (this.mode === "first") {
- return this._prepare(false).get();
- }
- return this._prepare(false).all();
- }
- async execute() {
- return this.executeRaw();
- }
- }
- class SQLiteSyncRelationalQuery extends SQLiteRelationalQuery {
- static [entityKind] = "SQLiteSyncRelationalQuery";
- sync() {
- return this.executeRaw();
- }
- }
- export {
- RelationalQueryBuilder,
- SQLiteRelationalQuery,
- SQLiteSyncRelationalQuery
- };
- //# sourceMappingURL=query.js.map
|