query.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, mode) {
  8. this.fullSchema = fullSchema;
  9. this.schema = schema;
  10. this.tableNamesMap = tableNamesMap;
  11. this.table = table;
  12. this.tableConfig = tableConfig;
  13. this.dialect = dialect;
  14. this.session = session;
  15. this.mode = mode;
  16. }
  17. static [entityKind] = "MySqlRelationalQueryBuilder";
  18. findMany(config) {
  19. return new MySqlRelationalQuery(
  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. this.mode
  30. );
  31. }
  32. findFirst(config) {
  33. return new MySqlRelationalQuery(
  34. this.fullSchema,
  35. this.schema,
  36. this.tableNamesMap,
  37. this.table,
  38. this.tableConfig,
  39. this.dialect,
  40. this.session,
  41. config ? { ...config, limit: 1 } : { limit: 1 },
  42. "first",
  43. this.mode
  44. );
  45. }
  46. }
  47. class MySqlRelationalQuery extends QueryPromise {
  48. constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, queryMode, mode) {
  49. super();
  50. this.fullSchema = fullSchema;
  51. this.schema = schema;
  52. this.tableNamesMap = tableNamesMap;
  53. this.table = table;
  54. this.tableConfig = tableConfig;
  55. this.dialect = dialect;
  56. this.session = session;
  57. this.config = config;
  58. this.queryMode = queryMode;
  59. this.mode = mode;
  60. }
  61. static [entityKind] = "MySqlRelationalQuery";
  62. prepare() {
  63. const { query, builtQuery } = this._toSQL();
  64. return this.session.prepareQuery(
  65. builtQuery,
  66. void 0,
  67. (rawRows) => {
  68. const rows = rawRows.map((row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection));
  69. if (this.queryMode === "first") {
  70. return rows[0];
  71. }
  72. return rows;
  73. }
  74. );
  75. }
  76. _getQuery() {
  77. const query = this.mode === "planetscale" ? this.dialect.buildRelationalQueryWithoutLateralSubqueries({
  78. fullSchema: this.fullSchema,
  79. schema: this.schema,
  80. tableNamesMap: this.tableNamesMap,
  81. table: this.table,
  82. tableConfig: this.tableConfig,
  83. queryConfig: this.config,
  84. tableAlias: this.tableConfig.tsName
  85. }) : this.dialect.buildRelationalQuery({
  86. fullSchema: this.fullSchema,
  87. schema: this.schema,
  88. tableNamesMap: this.tableNamesMap,
  89. table: this.table,
  90. tableConfig: this.tableConfig,
  91. queryConfig: this.config,
  92. tableAlias: this.tableConfig.tsName
  93. });
  94. return query;
  95. }
  96. _toSQL() {
  97. const query = this._getQuery();
  98. const builtQuery = this.dialect.sqlToQuery(query.sql);
  99. return { builtQuery, query };
  100. }
  101. /** @internal */
  102. getSQL() {
  103. return this._getQuery().sql;
  104. }
  105. toSQL() {
  106. return this._toSQL().builtQuery;
  107. }
  108. execute() {
  109. return this.prepare().execute();
  110. }
  111. }
  112. export {
  113. MySqlRelationalQuery,
  114. RelationalQueryBuilder
  115. };
  116. //# sourceMappingURL=query.js.map