query.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { entityKind } from "../../entity.js";
  2. import { QueryPromise } from "../../query-promise.js";
  3. import {
  4. mapRelationalRow
  5. } from "../../relations.js";
  6. class RelationalQueryBuilder {
  7. constructor(mode, fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
  8. this.mode = mode;
  9. this.fullSchema = fullSchema;
  10. this.schema = schema;
  11. this.tableNamesMap = tableNamesMap;
  12. this.table = table;
  13. this.tableConfig = tableConfig;
  14. this.dialect = dialect;
  15. this.session = session;
  16. }
  17. static [entityKind] = "SQLiteAsyncRelationalQueryBuilder";
  18. findMany(config) {
  19. return this.mode === "sync" ? new SQLiteSyncRelationalQuery(
  20. this.fullSchema,
  21. this.schema,
  22. this.tableNamesMap,
  23. this.table,
  24. this.tableConfig,
  25. this.dialect,
  26. this.session,
  27. config ? config : {},
  28. "many"
  29. ) : new SQLiteRelationalQuery(
  30. this.fullSchema,
  31. this.schema,
  32. this.tableNamesMap,
  33. this.table,
  34. this.tableConfig,
  35. this.dialect,
  36. this.session,
  37. config ? config : {},
  38. "many"
  39. );
  40. }
  41. findFirst(config) {
  42. return this.mode === "sync" ? new SQLiteSyncRelationalQuery(
  43. this.fullSchema,
  44. this.schema,
  45. this.tableNamesMap,
  46. this.table,
  47. this.tableConfig,
  48. this.dialect,
  49. this.session,
  50. config ? { ...config, limit: 1 } : { limit: 1 },
  51. "first"
  52. ) : new SQLiteRelationalQuery(
  53. this.fullSchema,
  54. this.schema,
  55. this.tableNamesMap,
  56. this.table,
  57. this.tableConfig,
  58. this.dialect,
  59. this.session,
  60. config ? { ...config, limit: 1 } : { limit: 1 },
  61. "first"
  62. );
  63. }
  64. }
  65. class SQLiteRelationalQuery extends QueryPromise {
  66. constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
  67. super();
  68. this.fullSchema = fullSchema;
  69. this.schema = schema;
  70. this.tableNamesMap = tableNamesMap;
  71. this.table = table;
  72. this.tableConfig = tableConfig;
  73. this.dialect = dialect;
  74. this.session = session;
  75. this.config = config;
  76. this.mode = mode;
  77. }
  78. static [entityKind] = "SQLiteAsyncRelationalQuery";
  79. /** @internal */
  80. mode;
  81. /** @internal */
  82. getSQL() {
  83. return this.dialect.buildRelationalQuery({
  84. fullSchema: this.fullSchema,
  85. schema: this.schema,
  86. tableNamesMap: this.tableNamesMap,
  87. table: this.table,
  88. tableConfig: this.tableConfig,
  89. queryConfig: this.config,
  90. tableAlias: this.tableConfig.tsName
  91. }).sql;
  92. }
  93. /** @internal */
  94. _prepare(isOneTimeQuery = false) {
  95. const { query, builtQuery } = this._toSQL();
  96. return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
  97. builtQuery,
  98. void 0,
  99. this.mode === "first" ? "get" : "all",
  100. true,
  101. (rawRows, mapColumnValue) => {
  102. const rows = rawRows.map(
  103. (row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection, mapColumnValue)
  104. );
  105. if (this.mode === "first") {
  106. return rows[0];
  107. }
  108. return rows;
  109. }
  110. );
  111. }
  112. prepare() {
  113. return this._prepare(false);
  114. }
  115. _toSQL() {
  116. const query = this.dialect.buildRelationalQuery({
  117. fullSchema: this.fullSchema,
  118. schema: this.schema,
  119. tableNamesMap: this.tableNamesMap,
  120. table: this.table,
  121. tableConfig: this.tableConfig,
  122. queryConfig: this.config,
  123. tableAlias: this.tableConfig.tsName
  124. });
  125. const builtQuery = this.dialect.sqlToQuery(query.sql);
  126. return { query, builtQuery };
  127. }
  128. toSQL() {
  129. return this._toSQL().builtQuery;
  130. }
  131. /** @internal */
  132. executeRaw() {
  133. if (this.mode === "first") {
  134. return this._prepare(false).get();
  135. }
  136. return this._prepare(false).all();
  137. }
  138. async execute() {
  139. return this.executeRaw();
  140. }
  141. }
  142. class SQLiteSyncRelationalQuery extends SQLiteRelationalQuery {
  143. static [entityKind] = "SQLiteSyncRelationalQuery";
  144. sync() {
  145. return this.executeRaw();
  146. }
  147. }
  148. export {
  149. RelationalQueryBuilder,
  150. SQLiteRelationalQuery,
  151. SQLiteSyncRelationalQuery
  152. };
  153. //# sourceMappingURL=query.js.map