AK-21/Graphite-Industrial-Intelligence
0
1import { entityKind } from "../entity.js";2import { NoopLogger } from "../logger.js";3import { fillPlaceholders, sql } from "../sql/sql.js";4import { SQLiteTransaction } from "../sqlite-core/index.js";5import { SQLitePreparedQuery as PreparedQueryBase, SQLiteSession } from "../sqlite-core/session.js";6import { mapResultRow } from "../utils.js";7class SQLJsSession extends SQLiteSession {8 constructor(client, dialect, schema, options = {}) {9 super(dialect);10 this.client = client;11 this.schema = schema;12 this.logger = options.logger ?? new NoopLogger();13 }14 static [entityKind] = "SQLJsSession";15 logger;16 prepareQuery(query, fields, executeMethod, isResponseInArrayMode) {17 return new PreparedQuery(this.client, query, this.logger, fields, executeMethod, isResponseInArrayMode);18 }19 transaction(transaction, config = {}) {20 const tx = new SQLJsTransaction("sync", this.dialect, this, this.schema);21 this.run(sql.raw(`begin${config.behavior ? ` ${config.behavior}` : ""}`));22 try {23 const result = transaction(tx);24 this.run(sql`commit`);25 return result;26 } catch (err) {27 this.run(sql`rollback`);28 throw err;29 }30 }31}32class SQLJsTransaction extends SQLiteTransaction {33 static [entityKind] = "SQLJsTransaction";34 transaction(transaction) {35 const savepointName = `sp${this.nestedIndex + 1}`;36 const tx = new SQLJsTransaction("sync", this.dialect, this.session, this.schema, this.nestedIndex + 1);37 tx.run(sql.raw(`savepoint ${savepointName}`));38 try {39 const result = transaction(tx);40 tx.run(sql.raw(`release savepoint ${savepointName}`));41 return result;42 } catch (err) {43 tx.run(sql.raw(`rollback to savepoint ${savepointName}`));44 throw err;45 }46 }47}48class PreparedQuery extends PreparedQueryBase {49 constructor(client, query, logger, fields, executeMethod, _isResponseInArrayMode, customResultMapper) {50 super("sync", executeMethod, query);51 this.client = client;52 this.logger = logger;53 this.fields = fields;54 this._isResponseInArrayMode = _isResponseInArrayMode;55 this.customResultMapper = customResultMapper;56 }57 static [entityKind] = "SQLJsPreparedQuery";58 run(placeholderValues) {59 const stmt = this.client.prepare(this.query.sql);60 const params = fillPlaceholders(this.query.params, placeholderValues ?? {});61 this.logger.logQuery(this.query.sql, params);62 const result = stmt.run(params);63 stmt.free();64 return result;65 }66 all(placeholderValues) {67 const stmt = this.client.prepare(this.query.sql);68 const { fields, joinsNotNullableMap, logger, query, customResultMapper } = this;69 if (!fields && !customResultMapper) {70 const params = fillPlaceholders(query.params, placeholderValues ?? {});71 logger.logQuery(query.sql, params);72 stmt.bind(params);73 const rows2 = [];74 while (stmt.step()) {75 rows2.push(stmt.getAsObject());76 }77 stmt.free();78 return rows2;79 }80 const rows = this.values(placeholderValues);81 if (customResultMapper) {82 return customResultMapper(rows, normalizeFieldValue);83 }84 return rows.map((row) => mapResultRow(fields, row.map((v) => normalizeFieldValue(v)), joinsNotNullableMap));85 }86 get(placeholderValues) {87 const stmt = this.client.prepare(this.query.sql);88 const params = fillPlaceholders(this.query.params, placeholderValues ?? {});89 this.logger.logQuery(this.query.sql, params);90 const { fields, joinsNotNullableMap, customResultMapper } = this;91 if (!fields && !customResultMapper) {92 const result = stmt.getAsObject(params);93 stmt.free();94 return result;95 }96 const row = stmt.get(params);97 stmt.free();98 if (!row || row.length === 0 && fields.length > 0) {99 return void 0;100 }101 if (customResultMapper) {102 return customResultMapper([row], normalizeFieldValue);103 }104 return mapResultRow(fields, row.map((v) => normalizeFieldValue(v)), joinsNotNullableMap);105 }106 values(placeholderValues) {107 const stmt = this.client.prepare(this.query.sql);108 const params = fillPlaceholders(this.query.params, placeholderValues ?? {});109 this.logger.logQuery(this.query.sql, params);110 stmt.bind(params);111 const rows = [];112 while (stmt.step()) {113 rows.push(stmt.get());114 }115 stmt.free();116 return rows;117 }118 /** @internal */119 isResponseInArrayMode() {120 return this._isResponseInArrayMode;121 }122}123function normalizeFieldValue(value) {124 if (value instanceof Uint8Array) {125 if (typeof Buffer !== "undefined") {126 if (!(value instanceof Buffer)) {127 return Buffer.from(value);128 }129 return value;130 }131 if (typeof TextDecoder !== "undefined") {132 return new TextDecoder().decode(value);133 }134 throw new Error("TextDecoder is not available. Please provide either Buffer or TextDecoder polyfill.");135 }136 return value;137}138export {139 PreparedQuery,140 SQLJsSession,141 SQLJsTransaction142};143//# sourceMappingURL=session.js.map