CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

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