AK-21/Graphite-Industrial-Intelligence
0
1import { NoopCache } from "../cache/core/index.js";2import { entityKind } from "../entity.js";3import { NoopLogger } from "../logger.js";4import { fillPlaceholders, sql } from "../sql/sql.js";5import { SQLiteTransaction } from "../sqlite-core/index.js";6import {7 SQLitePreparedQuery,8 SQLiteSession9} from "../sqlite-core/session.js";10import { mapResultRow } from "../utils.js";11class OPSQLiteSession extends SQLiteSession {12 constructor(client, dialect, schema, options = {}) {13 super(dialect);14 this.client = client;15 this.schema = schema;16 this.logger = options.logger ?? new NoopLogger();17 this.cache = options.cache ?? new NoopCache();18 }19 static [entityKind] = "OPSQLiteSession";20 logger;21 cache;22 prepareQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {23 return new OPSQLitePreparedQuery(24 this.client,25 query,26 this.logger,27 this.cache,28 queryMetadata,29 cacheConfig,30 fields,31 executeMethod,32 isResponseInArrayMode,33 customResultMapper34 );35 }36 transaction(transaction, config = {}) {37 const tx = new OPSQLiteTransaction("async", this.dialect, this, this.schema);38 this.run(sql.raw(`begin${config?.behavior ? " " + config.behavior : ""}`));39 try {40 const result = transaction(tx);41 this.run(sql`commit`);42 return result;43 } catch (err) {44 this.run(sql`rollback`);45 throw err;46 }47 }48}49class OPSQLiteTransaction extends SQLiteTransaction {50 static [entityKind] = "OPSQLiteTransaction";51 transaction(transaction) {52 const savepointName = `sp${this.nestedIndex}`;53 const tx = new OPSQLiteTransaction("async", this.dialect, this.session, this.schema, this.nestedIndex + 1);54 this.session.run(sql.raw(`savepoint ${savepointName}`));55 try {56 const result = transaction(tx);57 this.session.run(sql.raw(`release savepoint ${savepointName}`));58 return result;59 } catch (err) {60 this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));61 throw err;62 }63 }64}65class OPSQLitePreparedQuery extends SQLitePreparedQuery {66 constructor(client, query, logger, cache, queryMetadata, cacheConfig, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {67 super("sync", executeMethod, query, cache, queryMetadata, cacheConfig);68 this.client = client;69 this.logger = logger;70 this.fields = fields;71 this._isResponseInArrayMode = _isResponseInArrayMode;72 this.customResultMapper = customResultMapper;73 }74 static [entityKind] = "OPSQLitePreparedQuery";75 async run(placeholderValues) {76 const params = fillPlaceholders(this.query.params, placeholderValues ?? {});77 this.logger.logQuery(this.query.sql, params);78 return await this.queryWithCache(this.query.sql, params, async () => {79 return this.client.executeAsync(this.query.sql, params);80 });81 }82 async all(placeholderValues) {83 const { fields, joinsNotNullableMap, query, logger, customResultMapper, client } = this;84 if (!fields && !customResultMapper) {85 const params = fillPlaceholders(query.params, placeholderValues ?? {});86 logger.logQuery(query.sql, params);87 return await this.queryWithCache(query.sql, params, async () => {88 return client.execute(query.sql, params).rows?._array || [];89 });90 }91 const rows = await this.values(placeholderValues);92 if (customResultMapper) {93 return customResultMapper(rows);94 }95 return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));96 }97 async get(placeholderValues) {98 const { fields, joinsNotNullableMap, customResultMapper, query, logger, client } = this;99 const params = fillPlaceholders(query.params, placeholderValues ?? {});100 logger.logQuery(query.sql, params);101 if (!fields && !customResultMapper) {102 const rows2 = await this.queryWithCache(query.sql, params, async () => {103 return client.execute(query.sql, params).rows?._array || [];104 });105 return rows2[0];106 }107 const rows = await this.values(placeholderValues);108 const row = rows[0];109 if (!row) {110 return void 0;111 }112 if (customResultMapper) {113 return customResultMapper(rows);114 }115 return mapResultRow(fields, row, joinsNotNullableMap);116 }117 async values(placeholderValues) {118 const params = fillPlaceholders(this.query.params, placeholderValues ?? {});119 this.logger.logQuery(this.query.sql, params);120 return await this.queryWithCache(this.query.sql, params, async () => {121 return await this.client.executeRawAsync(this.query.sql, params);122 });123 }124 /** @internal */125 isResponseInArrayMode() {126 return this._isResponseInArrayMode;127 }128}129export {130 OPSQLitePreparedQuery,131 OPSQLiteSession,132 OPSQLiteTransaction133};134//# sourceMappingURL=session.js.map