ts-sql-query
Version:
Type-safe SQL query builder like QueryDSL or JOOQ in Java or Linq in .Net for TypeScript with MariaDB, MySql, Oracle, PostgreSql, Sqlite and SqlServer support.
183 lines (182 loc) • 9.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractPoolQueryRunner = void 0;
class AbstractPoolQueryRunner {
constructor() {
this.transactionLevel = 0;
}
execute(fn) {
return this.getQueryRunner().then(queryRunner => queryRunner.execute(fn)).finally(() => this.releaseIfNeeded());
}
executeSelectOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeSelectOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeSelectManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeSelectManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeSelectOneColumnOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeSelectOneColumnOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeSelectOneColumnManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeSelectOneColumnManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeInsert(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeInsert(query, params)).finally(() => this.releaseIfNeeded());
}
executeInsertReturningLastInsertedId(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeInsertReturningLastInsertedId(query, params)).finally(() => this.releaseIfNeeded());
}
executeInsertReturningMultipleLastInsertedId(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeInsertReturningMultipleLastInsertedId(query, params)).finally(() => this.releaseIfNeeded());
}
executeInsertReturningOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeInsertReturningOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeInsertReturningManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeInsertReturningManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeInsertReturningOneColumnOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeInsertReturningOneColumnOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeInsertReturningOneColumnManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeInsertReturningOneColumnManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeUpdate(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeUpdate(query, params)).finally(() => this.releaseIfNeeded());
}
executeUpdateReturningOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeUpdateReturningOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeUpdateReturningManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeUpdateReturningManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeUpdateReturningOneColumnOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeUpdateReturningOneColumnOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeUpdateReturningOneColumnManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeUpdateReturningOneColumnManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeDelete(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeDelete(query, params)).finally(() => this.releaseIfNeeded());
}
executeDeleteReturningOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeDeleteReturningOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeDeleteReturningManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeDeleteReturningManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeDeleteReturningOneColumnOneRow(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeDeleteReturningOneColumnOneRow(query, params)).finally(() => this.releaseIfNeeded());
}
executeDeleteReturningOneColumnManyRows(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeDeleteReturningOneColumnManyRows(query, params)).finally(() => this.releaseIfNeeded());
}
executeProcedure(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeProcedure(query, params)).finally(() => this.releaseIfNeeded());
}
executeFunction(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeFunction(query, params)).finally(() => this.releaseIfNeeded());
}
executeBeginTransaction() {
if (this.transactionLevel <= 0) {
this.transactionLevel = 1;
return this.createResolvedPromise(undefined);
}
else {
return this.getQueryRunner().then(queryRunner => queryRunner.executeBeginTransaction()).then(() => {
this.transactionLevel++;
});
}
}
executeCommit() {
if (this.transactionLevel <= 0) {
throw new Error('You are not in a transaction');
}
if (this.currentQueryRunner) {
return this.currentQueryRunner.executeCommit().then(() => {
// Transaction count and release only modified when commit successful, in case of error there is still an open transaction
this.transactionLevel--;
if (this.transactionLevel < 0) {
this.transactionLevel = 0;
}
this.releaseIfNeeded();
});
}
this.transactionLevel--;
if (this.transactionLevel < 0) {
this.transactionLevel = 0;
}
return this.createResolvedPromise(undefined);
}
executeRollback() {
if (this.transactionLevel <= 0) {
throw new Error('You are not in a transaction');
}
if (this.currentQueryRunner) {
return this.currentQueryRunner.executeRollback().finally(() => {
this.transactionLevel--;
if (this.transactionLevel < 0) {
this.transactionLevel = 0;
}
this.releaseIfNeeded();
});
}
this.transactionLevel--;
if (this.transactionLevel < 0) {
this.transactionLevel = 0;
}
return this.createResolvedPromise(undefined);
}
isTransactionActive() {
return this.transactionLevel > 0;
}
executeDatabaseSchemaModification(query, params = []) {
return this.getQueryRunner().then(queryRunner => queryRunner.executeDatabaseSchemaModification(query, params)).finally(() => this.releaseIfNeeded());
}
executeConnectionConfiguration(query, params = []) {
if (!this.isTransactionActive()) {
throw new Error("You are trying to configure a connection when you didn't request a dedicated connection. Begin a transaction to get a dedicated connection");
}
return this.getQueryRunner().then(queryRunner => queryRunner.executeConnectionConfiguration(query, params)).finally(() => this.releaseIfNeeded());
}
getCurrentNativeTransaction() {
if (!this.currentQueryRunner) {
return undefined;
}
return this.currentQueryRunner.getCurrentNativeTransaction();
}
addOutParam(_params, _name) {
throw new Error('Unsupported output parameters');
}
getQueryRunner() {
if (!this.currentQueryRunner) {
return this.createQueryRunner().then(queryRunner => {
if (this.currentQueryRunner) {
this.releaseQueryRunner(queryRunner);
throw new Error('Forbidden concurrent usage of the query runner was detected when it tried to get a database connection from the pool');
}
this.currentQueryRunner = queryRunner;
if (this.transactionLevel > 0) {
return this.currentQueryRunner.executeBeginTransaction().then(() => {
return queryRunner;
});
}
return queryRunner;
});
}
return this.createResolvedPromise(this.currentQueryRunner);
}
releaseIfNeeded() {
if (this.transactionLevel <= 0 && this.currentQueryRunner) {
this.releaseQueryRunner(this.currentQueryRunner);
this.currentQueryRunner = undefined;
}
}
isMocked() {
return false;
}
lowLevelTransactionManagementSupported() {
return true;
}
}
exports.AbstractPoolQueryRunner = AbstractPoolQueryRunner;