session.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Column } from "../column.js";
  2. import { entityKind, is } from "../entity.js";
  3. import { NoopLogger } from "../logger.js";
  4. import { SingleStoreTransaction } from "../singlestore-core/index.js";
  5. import { SingleStorePreparedQuery as PreparedQueryBase, SingleStoreSession } from "../singlestore-core/session.js";
  6. import { fillPlaceholders } from "../sql/sql.js";
  7. import { mapResultRow } from "../utils.js";
  8. class SingleStoreRemoteSession extends SingleStoreSession {
  9. constructor(client, dialect, schema, options) {
  10. super(dialect);
  11. this.client = client;
  12. this.schema = schema;
  13. this.logger = options.logger ?? new NoopLogger();
  14. }
  15. static [entityKind] = "SingleStoreRemoteSession";
  16. logger;
  17. prepareQuery(query, fields, customResultMapper, generatedIds, returningIds) {
  18. return new PreparedQuery(
  19. this.client,
  20. query.sql,
  21. query.params,
  22. this.logger,
  23. fields,
  24. customResultMapper,
  25. generatedIds,
  26. returningIds
  27. );
  28. }
  29. all(query) {
  30. const querySql = this.dialect.sqlToQuery(query);
  31. this.logger.logQuery(querySql.sql, querySql.params);
  32. return this.client(querySql.sql, querySql.params, "all").then(({ rows }) => rows);
  33. }
  34. async transaction(_transaction, _config) {
  35. throw new Error("Transactions are not supported by the SingleStore Proxy driver");
  36. }
  37. }
  38. class SingleStoreProxyTransaction extends SingleStoreTransaction {
  39. static [entityKind] = "SingleStoreProxyTransaction";
  40. async transaction(_transaction) {
  41. throw new Error("Transactions are not supported by the SingleStore Proxy driver");
  42. }
  43. }
  44. class PreparedQuery extends PreparedQueryBase {
  45. constructor(client, queryString, params, logger, fields, customResultMapper, generatedIds, returningIds) {
  46. super();
  47. this.client = client;
  48. this.queryString = queryString;
  49. this.params = params;
  50. this.logger = logger;
  51. this.fields = fields;
  52. this.customResultMapper = customResultMapper;
  53. this.generatedIds = generatedIds;
  54. this.returningIds = returningIds;
  55. }
  56. static [entityKind] = "SingleStoreProxyPreparedQuery";
  57. async execute(placeholderValues = {}) {
  58. const params = fillPlaceholders(this.params, placeholderValues);
  59. const { fields, client, queryString, logger, joinsNotNullableMap, customResultMapper, returningIds, generatedIds } = this;
  60. logger.logQuery(queryString, params);
  61. if (!fields && !customResultMapper) {
  62. const { rows: data } = await client(queryString, params, "execute");
  63. const insertId = data[0].insertId;
  64. const affectedRows = data[0].affectedRows;
  65. if (returningIds) {
  66. const returningResponse = [];
  67. let j = 0;
  68. for (let i = insertId; i < insertId + affectedRows; i++) {
  69. for (const column of returningIds) {
  70. const key = returningIds[0].path[0];
  71. if (is(column.field, Column)) {
  72. if (column.field.primary && column.field.autoIncrement) {
  73. returningResponse.push({ [key]: i });
  74. }
  75. if (column.field.defaultFn && generatedIds) {
  76. returningResponse.push({ [key]: generatedIds[j][key] });
  77. }
  78. }
  79. }
  80. j++;
  81. }
  82. return returningResponse;
  83. }
  84. return data;
  85. }
  86. const { rows } = await client(queryString, params, "all");
  87. if (customResultMapper) {
  88. return customResultMapper(rows);
  89. }
  90. return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
  91. }
  92. iterator(_placeholderValues = {}) {
  93. throw new Error("Streaming is not supported by the SingleStore Proxy driver");
  94. }
  95. }
  96. export {
  97. PreparedQuery,
  98. SingleStoreProxyTransaction,
  99. SingleStoreRemoteSession
  100. };
  101. //# sourceMappingURL=session.js.map