session.cjs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. SQLJsSession: () => SQLJsSession,
  23. SQLJsTransaction: () => SQLJsTransaction
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_entity = require("../entity.cjs");
  27. var import_logger = require("../logger.cjs");
  28. var import_sql = require("../sql/sql.cjs");
  29. var import_sqlite_core = require("../sqlite-core/index.cjs");
  30. var import_session = require("../sqlite-core/session.cjs");
  31. var import_utils = require("../utils.cjs");
  32. class SQLJsSession extends import_session.SQLiteSession {
  33. constructor(client, dialect, schema, options = {}) {
  34. super(dialect);
  35. this.client = client;
  36. this.schema = schema;
  37. this.logger = options.logger ?? new import_logger.NoopLogger();
  38. }
  39. static [import_entity.entityKind] = "SQLJsSession";
  40. logger;
  41. prepareQuery(query, fields, executeMethod, isResponseInArrayMode) {
  42. return new PreparedQuery(this.client, query, this.logger, fields, executeMethod, isResponseInArrayMode);
  43. }
  44. transaction(transaction, config = {}) {
  45. const tx = new SQLJsTransaction("sync", this.dialect, this, this.schema);
  46. this.run(import_sql.sql.raw(`begin${config.behavior ? ` ${config.behavior}` : ""}`));
  47. try {
  48. const result = transaction(tx);
  49. this.run(import_sql.sql`commit`);
  50. return result;
  51. } catch (err) {
  52. this.run(import_sql.sql`rollback`);
  53. throw err;
  54. }
  55. }
  56. }
  57. class SQLJsTransaction extends import_sqlite_core.SQLiteTransaction {
  58. static [import_entity.entityKind] = "SQLJsTransaction";
  59. transaction(transaction) {
  60. const savepointName = `sp${this.nestedIndex + 1}`;
  61. const tx = new SQLJsTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  62. tx.run(import_sql.sql.raw(`savepoint ${savepointName}`));
  63. try {
  64. const result = transaction(tx);
  65. tx.run(import_sql.sql.raw(`release savepoint ${savepointName}`));
  66. return result;
  67. } catch (err) {
  68. tx.run(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  69. throw err;
  70. }
  71. }
  72. }
  73. class PreparedQuery extends import_session.SQLitePreparedQuery {
  74. constructor(client, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  75. super("sync", executeMethod, query);
  76. this.client = client;
  77. this.logger = logger;
  78. this.fields = fields;
  79. this._isResponseInArrayMode = _isResponseInArrayMode;
  80. this.customResultMapper = customResultMapper;
  81. }
  82. static [import_entity.entityKind] = "SQLJsPreparedQuery";
  83. run(placeholderValues) {
  84. const stmt = this.client.prepare(this.query.sql);
  85. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  86. this.logger.logQuery(this.query.sql, params);
  87. const result = stmt.run(params);
  88. stmt.free();
  89. return result;
  90. }
  91. all(placeholderValues) {
  92. const stmt = this.client.prepare(this.query.sql);
  93. const { fields, joinsNotNullableMap, logger, query, customResultMapper } = this;
  94. if (!fields && !customResultMapper) {
  95. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  96. logger.logQuery(query.sql, params);
  97. stmt.bind(params);
  98. const rows2 = [];
  99. while (stmt.step()) {
  100. rows2.push(stmt.getAsObject());
  101. }
  102. stmt.free();
  103. return rows2;
  104. }
  105. const rows = this.values(placeholderValues);
  106. if (customResultMapper) {
  107. return customResultMapper(rows, normalizeFieldValue);
  108. }
  109. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row.map((v) => normalizeFieldValue(v)), joinsNotNullableMap));
  110. }
  111. get(placeholderValues) {
  112. const stmt = this.client.prepare(this.query.sql);
  113. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  114. this.logger.logQuery(this.query.sql, params);
  115. const { fields, joinsNotNullableMap, customResultMapper } = this;
  116. if (!fields && !customResultMapper) {
  117. const result = stmt.getAsObject(params);
  118. stmt.free();
  119. return result;
  120. }
  121. const row = stmt.get(params);
  122. stmt.free();
  123. if (!row || row.length === 0 && fields.length > 0) {
  124. return void 0;
  125. }
  126. if (customResultMapper) {
  127. return customResultMapper([row], normalizeFieldValue);
  128. }
  129. return (0, import_utils.mapResultRow)(fields, row.map((v) => normalizeFieldValue(v)), joinsNotNullableMap);
  130. }
  131. values(placeholderValues) {
  132. const stmt = this.client.prepare(this.query.sql);
  133. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  134. this.logger.logQuery(this.query.sql, params);
  135. stmt.bind(params);
  136. const rows = [];
  137. while (stmt.step()) {
  138. rows.push(stmt.get());
  139. }
  140. stmt.free();
  141. return rows;
  142. }
  143. /** @internal */
  144. isResponseInArrayMode() {
  145. return this._isResponseInArrayMode;
  146. }
  147. }
  148. function normalizeFieldValue(value) {
  149. if (value instanceof Uint8Array) {
  150. if (typeof Buffer !== "undefined") {
  151. if (!(value instanceof Buffer)) {
  152. return Buffer.from(value);
  153. }
  154. return value;
  155. }
  156. if (typeof TextDecoder !== "undefined") {
  157. return new TextDecoder().decode(value);
  158. }
  159. throw new Error("TextDecoder is not available. Please provide either Buffer or TextDecoder polyfill.");
  160. }
  161. return value;
  162. }
  163. // Annotate the CommonJS export names for ESM import in node:
  164. 0 && (module.exports = {
  165. PreparedQuery,
  166. SQLJsSession,
  167. SQLJsTransaction
  168. });
  169. //# sourceMappingURL=session.cjs.map