session.cjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ExpoSQLitePreparedQuery: () => ExpoSQLitePreparedQuery,
  22. ExpoSQLiteSession: () => ExpoSQLiteSession,
  23. ExpoSQLiteTransaction: () => ExpoSQLiteTransaction
  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 ExpoSQLiteSession 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] = "ExpoSQLiteSession";
  40. logger;
  41. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper) {
  42. const stmt = this.client.prepareSync(query.sql);
  43. return new ExpoSQLitePreparedQuery(
  44. stmt,
  45. query,
  46. this.logger,
  47. fields,
  48. executeMethod,
  49. isResponseInArrayMode,
  50. customResultMapper
  51. );
  52. }
  53. transaction(transaction, config = {}) {
  54. const tx = new ExpoSQLiteTransaction("sync", this.dialect, this, this.schema);
  55. this.run(import_sql.sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));
  56. try {
  57. const result = transaction(tx);
  58. this.run(import_sql.sql`commit`);
  59. return result;
  60. } catch (err) {
  61. this.run(import_sql.sql`rollback`);
  62. throw err;
  63. }
  64. }
  65. }
  66. class ExpoSQLiteTransaction extends import_sqlite_core.SQLiteTransaction {
  67. static [import_entity.entityKind] = "ExpoSQLiteTransaction";
  68. transaction(transaction) {
  69. const savepointName = `sp${this.nestedIndex}`;
  70. const tx = new ExpoSQLiteTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  71. this.session.run(import_sql.sql.raw(`savepoint ${savepointName}`));
  72. try {
  73. const result = transaction(tx);
  74. this.session.run(import_sql.sql.raw(`release savepoint ${savepointName}`));
  75. return result;
  76. } catch (err) {
  77. this.session.run(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  78. throw err;
  79. }
  80. }
  81. }
  82. class ExpoSQLitePreparedQuery extends import_session.SQLitePreparedQuery {
  83. constructor(stmt, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  84. super("sync", executeMethod, query);
  85. this.stmt = stmt;
  86. this.logger = logger;
  87. this.fields = fields;
  88. this._isResponseInArrayMode = _isResponseInArrayMode;
  89. this.customResultMapper = customResultMapper;
  90. }
  91. static [import_entity.entityKind] = "ExpoSQLitePreparedQuery";
  92. run(placeholderValues) {
  93. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  94. this.logger.logQuery(this.query.sql, params);
  95. const { changes, lastInsertRowId } = this.stmt.executeSync(params);
  96. return {
  97. changes,
  98. lastInsertRowId
  99. };
  100. }
  101. all(placeholderValues) {
  102. const { fields, joinsNotNullableMap, query, logger, stmt, customResultMapper } = this;
  103. if (!fields && !customResultMapper) {
  104. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  105. logger.logQuery(query.sql, params);
  106. return stmt.executeSync(params).getAllSync();
  107. }
  108. const rows = this.values(placeholderValues);
  109. if (customResultMapper) {
  110. return customResultMapper(rows);
  111. }
  112. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  113. }
  114. get(placeholderValues) {
  115. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  116. this.logger.logQuery(this.query.sql, params);
  117. const { fields, stmt, joinsNotNullableMap, customResultMapper } = this;
  118. if (!fields && !customResultMapper) {
  119. return stmt.executeSync(params).getFirstSync();
  120. }
  121. const rows = this.values(placeholderValues);
  122. const row = rows[0];
  123. if (!row) {
  124. return void 0;
  125. }
  126. if (customResultMapper) {
  127. return customResultMapper(rows);
  128. }
  129. return (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap);
  130. }
  131. values(placeholderValues) {
  132. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  133. this.logger.logQuery(this.query.sql, params);
  134. return this.stmt.executeForRawResultSync(params).getAllSync();
  135. }
  136. /** @internal */
  137. isResponseInArrayMode() {
  138. return this._isResponseInArrayMode;
  139. }
  140. }
  141. // Annotate the CommonJS export names for ESM import in node:
  142. 0 && (module.exports = {
  143. ExpoSQLitePreparedQuery,
  144. ExpoSQLiteSession,
  145. ExpoSQLiteTransaction
  146. });
  147. //# sourceMappingURL=session.cjs.map