session.cjs 5.2 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. SQLiteDOPreparedQuery: () => SQLiteDOPreparedQuery,
  22. SQLiteDOSession: () => SQLiteDOSession,
  23. SQLiteDOTransaction: () => SQLiteDOTransaction
  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_session2 = require("../sqlite-core/session.cjs");
  32. var import_utils = require("../utils.cjs");
  33. class SQLiteDOSession extends import_session.SQLiteSession {
  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] = "SQLiteDOSession";
  41. logger;
  42. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper) {
  43. return new SQLiteDOPreparedQuery(
  44. this.client,
  45. query,
  46. this.logger,
  47. fields,
  48. executeMethod,
  49. isResponseInArrayMode,
  50. customResultMapper
  51. );
  52. }
  53. transaction(transaction, _config) {
  54. const tx = new SQLiteDOTransaction("sync", this.dialect, this, this.schema);
  55. return this.client.transactionSync(() => transaction(tx));
  56. }
  57. }
  58. class SQLiteDOTransaction extends import_sqlite_core.SQLiteTransaction {
  59. static [import_entity.entityKind] = "SQLiteDOTransaction";
  60. transaction(transaction) {
  61. const tx = new SQLiteDOTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  62. return this.session.transaction(() => transaction(tx));
  63. }
  64. }
  65. class SQLiteDOPreparedQuery extends import_session2.SQLitePreparedQuery {
  66. constructor(client, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  67. super("sync", executeMethod, query, void 0, void 0, void 0);
  68. this.client = client;
  69. this.logger = logger;
  70. this.fields = fields;
  71. this._isResponseInArrayMode = _isResponseInArrayMode;
  72. this.customResultMapper = customResultMapper;
  73. }
  74. static [import_entity.entityKind] = "SQLiteDOPreparedQuery";
  75. run(placeholderValues) {
  76. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  77. this.logger.logQuery(this.query.sql, params);
  78. params.length > 0 ? this.client.sql.exec(this.query.sql, ...params) : this.client.sql.exec(this.query.sql);
  79. }
  80. all(placeholderValues) {
  81. const { fields, joinsNotNullableMap, query, logger, client, customResultMapper } = this;
  82. if (!fields && !customResultMapper) {
  83. const params = (0, import_sql.fillPlaceholders)(query.params, placeholderValues ?? {});
  84. logger.logQuery(query.sql, params);
  85. return params.length > 0 ? client.sql.exec(query.sql, ...params).toArray() : client.sql.exec(query.sql).toArray();
  86. }
  87. const rows = this.values(placeholderValues);
  88. if (customResultMapper) {
  89. return customResultMapper(rows);
  90. }
  91. return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  92. }
  93. get(placeholderValues) {
  94. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  95. this.logger.logQuery(this.query.sql, params);
  96. const { fields, client, joinsNotNullableMap, customResultMapper, query } = this;
  97. if (!fields && !customResultMapper) {
  98. return (params.length > 0 ? client.sql.exec(query.sql, ...params) : client.sql.exec(query.sql)).next().value;
  99. }
  100. const rows = this.values(placeholderValues);
  101. const row = rows[0];
  102. if (!row) {
  103. return void 0;
  104. }
  105. if (customResultMapper) {
  106. return customResultMapper(rows);
  107. }
  108. return (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap);
  109. }
  110. values(placeholderValues) {
  111. const params = (0, import_sql.fillPlaceholders)(this.query.params, placeholderValues ?? {});
  112. this.logger.logQuery(this.query.sql, params);
  113. const res = params.length > 0 ? this.client.sql.exec(this.query.sql, ...params) : this.client.sql.exec(this.query.sql);
  114. return res.raw().toArray();
  115. }
  116. /** @internal */
  117. isResponseInArrayMode() {
  118. return this._isResponseInArrayMode;
  119. }
  120. }
  121. // Annotate the CommonJS export names for ESM import in node:
  122. 0 && (module.exports = {
  123. SQLiteDOPreparedQuery,
  124. SQLiteDOSession,
  125. SQLiteDOTransaction
  126. });
  127. //# sourceMappingURL=session.cjs.map