session.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { entityKind } from "../entity.js";
  2. import { NoopLogger } from "../logger.js";
  3. import { fillPlaceholders } from "../sql/sql.js";
  4. import { SQLiteTransaction } from "../sqlite-core/index.js";
  5. import {
  6. SQLiteSession
  7. } from "../sqlite-core/session.js";
  8. import { SQLitePreparedQuery as PreparedQueryBase } from "../sqlite-core/session.js";
  9. import { mapResultRow } from "../utils.js";
  10. class SQLiteDOSession extends SQLiteSession {
  11. constructor(client, dialect, schema, options = {}) {
  12. super(dialect);
  13. this.client = client;
  14. this.schema = schema;
  15. this.logger = options.logger ?? new NoopLogger();
  16. }
  17. static [entityKind] = "SQLiteDOSession";
  18. logger;
  19. prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper) {
  20. return new SQLiteDOPreparedQuery(
  21. this.client,
  22. query,
  23. this.logger,
  24. fields,
  25. executeMethod,
  26. isResponseInArrayMode,
  27. customResultMapper
  28. );
  29. }
  30. transaction(transaction, _config) {
  31. const tx = new SQLiteDOTransaction("sync", this.dialect, this, this.schema);
  32. return this.client.transactionSync(() => transaction(tx));
  33. }
  34. }
  35. class SQLiteDOTransaction extends SQLiteTransaction {
  36. static [entityKind] = "SQLiteDOTransaction";
  37. transaction(transaction) {
  38. const tx = new SQLiteDOTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);
  39. return this.session.transaction(() => transaction(tx));
  40. }
  41. }
  42. class SQLiteDOPreparedQuery extends PreparedQueryBase {
  43. constructor(client, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {
  44. super("sync", executeMethod, query, void 0, void 0, void 0);
  45. this.client = client;
  46. this.logger = logger;
  47. this.fields = fields;
  48. this._isResponseInArrayMode = _isResponseInArrayMode;
  49. this.customResultMapper = customResultMapper;
  50. }
  51. static [entityKind] = "SQLiteDOPreparedQuery";
  52. run(placeholderValues) {
  53. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  54. this.logger.logQuery(this.query.sql, params);
  55. params.length > 0 ? this.client.sql.exec(this.query.sql, ...params) : this.client.sql.exec(this.query.sql);
  56. }
  57. all(placeholderValues) {
  58. const { fields, joinsNotNullableMap, query, logger, client, customResultMapper } = this;
  59. if (!fields && !customResultMapper) {
  60. const params = fillPlaceholders(query.params, placeholderValues ?? {});
  61. logger.logQuery(query.sql, params);
  62. return params.length > 0 ? client.sql.exec(query.sql, ...params).toArray() : client.sql.exec(query.sql).toArray();
  63. }
  64. const rows = this.values(placeholderValues);
  65. if (customResultMapper) {
  66. return customResultMapper(rows);
  67. }
  68. return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  69. }
  70. get(placeholderValues) {
  71. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  72. this.logger.logQuery(this.query.sql, params);
  73. const { fields, client, joinsNotNullableMap, customResultMapper, query } = this;
  74. if (!fields && !customResultMapper) {
  75. return (params.length > 0 ? client.sql.exec(query.sql, ...params) : client.sql.exec(query.sql)).next().value;
  76. }
  77. const rows = this.values(placeholderValues);
  78. const row = rows[0];
  79. if (!row) {
  80. return void 0;
  81. }
  82. if (customResultMapper) {
  83. return customResultMapper(rows);
  84. }
  85. return mapResultRow(fields, row, joinsNotNullableMap);
  86. }
  87. values(placeholderValues) {
  88. const params = fillPlaceholders(this.query.params, placeholderValues ?? {});
  89. this.logger.logQuery(this.query.sql, params);
  90. const res = params.length > 0 ? this.client.sql.exec(this.query.sql, ...params) : this.client.sql.exec(this.query.sql);
  91. return res.raw().toArray();
  92. }
  93. /** @internal */
  94. isResponseInArrayMode() {
  95. return this._isResponseInArrayMode;
  96. }
  97. }
  98. export {
  99. SQLiteDOPreparedQuery,
  100. SQLiteDOSession,
  101. SQLiteDOTransaction
  102. };
  103. //# sourceMappingURL=session.js.map