session.cjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var session_exports = {};
  20. __export(session_exports, {
  21. PreparedQuery: () => PreparedQuery,
  22. SingleStoreProxyTransaction: () => SingleStoreProxyTransaction,
  23. SingleStoreRemoteSession: () => SingleStoreRemoteSession
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_column = require("../column.cjs");
  27. var import_entity = require("../entity.cjs");
  28. var import_logger = require("../logger.cjs");
  29. var import_singlestore_core = require("../singlestore-core/index.cjs");
  30. var import_session = require("../singlestore-core/session.cjs");
  31. var import_sql = require("../sql/sql.cjs");
  32. var import_utils = require("../utils.cjs");
  33. class SingleStoreRemoteSession extends import_session.SingleStoreSession {
  34. constructor(client, dialect, schema, options) {
  35. super(dialect);
  36. this.client = client;
  37. this.schema = schema;
  38. this.logger = options.logger ?? new import_logger.NoopLogger();
  39. }
  40. static [import_entity.entityKind] = "SingleStoreRemoteSession";
  41. logger;
  42. prepareQuery(query, fields, customResultMapper, generatedIds, returningIds) {
  43. return new PreparedQuery(
  44. this.client,
  45. query.sql,
  46. query.params,
  47. this.logger,
  48. fields,
  49. customResultMapper,
  50. generatedIds,
  51. returningIds
  52. );
  53. }
  54. all(query) {
  55. const querySql = this.dialect.sqlToQuery(query);
  56. this.logger.logQuery(querySql.sql, querySql.params);
  57. return this.client(querySql.sql, querySql.params, "all").then(({ rows }) => rows);
  58. }
  59. async transaction(_transaction, _config) {
  60. throw new Error("Transactions are not supported by the SingleStore Proxy driver");
  61. }
  62. }
  63. class SingleStoreProxyTransaction extends import_singlestore_core.SingleStoreTransaction {
  64. static [import_entity.entityKind] = "SingleStoreProxyTransaction";
  65. async transaction(_transaction) {
  66. throw new Error("Transactions are not supported by the SingleStore Proxy driver");
  67. }
  68. }
  69. class PreparedQuery extends import_session.SingleStorePreparedQuery {
  70. constructor(client, queryString, params, logger, fields, customResultMapper, generatedIds, returningIds) {
  71. super();
  72. this.client = client;
  73. this.queryString = queryString;
  74. this.params = params;
  75. this.logger = logger;
  76. this.fields = fields;
  77. this.customResultMapper = customResultMapper;
  78. this.generatedIds = generatedIds;
  79. this.returningIds = returningIds;
  80. }
  81. static [import_entity.entityKind] = "SingleStoreProxyPreparedQuery";
  82. async execute(placeholderValues = {}) {
  83. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  84. const { fields, client, queryString, logger, joinsNotNullableMap, customResultMapper, returningIds, generatedIds } = this;
  85. logger.logQuery(queryString, params);
  86. if (!fields && !customResultMapper) {
  87. const { rows: data } = await client(queryString, params, "execute");
  88. const insertId = data[0].insertId;
  89. const affectedRows = data[0].affectedRows;
  90. if (returningIds) {
  91. const returningResponse = [];
  92. let j = 0;
  93. for (let i = insertId; i < insertId + affectedRows; i++) {
  94. for (const column of returningIds) {
  95. const key = returningIds[0].path[0];
  96. if ((0, import_entity.is)(column.field, import_column.Column)) {
  97. if (column.field.primary && column.field.autoIncrement) {
  98. returningResponse.push({ [key]: i });
  99. }
  100. if (column.field.defaultFn && generatedIds) {
  101. returningResponse.push({ [key]: generatedIds[j][key] });
  102. }
  103. }
  104. }
  105. j++;
  106. }
  107. return returningResponse;
  108. }
  109. return data;
  110. }
  111. const { rows } = await client(queryString, params, "all");
  112. if (customResultMapper) {
  113. return customResultMapper(rows);
  114. }
  115. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  116. }
  117. iterator(_placeholderValues = {}) {
  118. throw new Error("Streaming is not supported by the SingleStore Proxy driver");
  119. }
  120. }
  121. // Annotate the CommonJS export names for ESM import in node:
  122. 0 && (module.exports = {
  123. PreparedQuery,
  124. SingleStoreProxyTransaction,
  125. SingleStoreRemoteSession
  126. });
  127. //# sourceMappingURL=session.cjs.map