CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
session.js101 linesDownload Raw Back to singlestore-proxy
1import { Column } from "../column.js";2import { entityKind, is } from "../entity.js";3import { NoopLogger } from "../logger.js";4import { SingleStoreTransaction } from "../singlestore-core/index.js";5import { SingleStorePreparedQuery as PreparedQueryBase, SingleStoreSession } from "../singlestore-core/session.js";6import { fillPlaceholders } from "../sql/sql.js";7import { mapResultRow } from "../utils.js";8class 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      returningIds27    );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}38class 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}44class 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}96export {97  PreparedQuery,98  SingleStoreProxyTransaction,99  SingleStoreRemoteSession100};101//# sourceMappingURL=session.js.map