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.
236 lines (235 loc) • 9.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractQueryRunner = void 0;
class AbstractQueryRunner {
executeSelectOneRow(query, params = []) {
return this.executeQueryReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
return rows[0];
});
}
executeSelectManyRows(query, params = []) {
return this.executeQueryReturning(query, params);
}
executeSelectOneColumnOneRow(query, params = []) {
return this.executeQueryReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
const row = rows[0];
if (row) {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}
return undefined;
});
}
executeSelectOneColumnManyRows(query, params = []) {
return this.executeQueryReturning(query, params).then((rows) => rows.map((row) => {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}));
}
executeInsert(query, params = []) {
return this.executeMutation(query, params);
}
executeInsertReturningLastInsertedId(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
const row = rows[0];
if (row) {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}
return undefined;
});
}
executeInsertReturningMultipleLastInsertedId(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
return rows.map((row) => {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
});
});
}
executeInsertReturningOneRow(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
return rows[0];
});
}
executeInsertReturningManyRows(query, params = []) {
return this.executeMutationReturning(query, params);
}
executeInsertReturningOneColumnOneRow(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
const row = rows[0];
if (row) {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}
return undefined;
});
}
executeInsertReturningOneColumnManyRows(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => rows.map((row) => {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}));
}
executeUpdate(query, params = []) {
return this.executeMutation(query, params);
}
executeUpdateReturningOneRow(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
return rows[0];
});
}
executeUpdateReturningManyRows(query, params = []) {
return this.executeMutationReturning(query, params);
}
executeUpdateReturningOneColumnOneRow(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
const row = rows[0];
if (row) {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}
return undefined;
});
}
executeUpdateReturningOneColumnManyRows(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => rows.map((row) => {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}));
}
executeDelete(query, params = []) {
return this.executeMutation(query, params);
}
executeDeleteReturningOneRow(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
return rows[0];
});
}
executeDeleteReturningManyRows(query, params = []) {
return this.executeMutationReturning(query, params);
}
executeDeleteReturningOneColumnOneRow(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
const row = rows[0];
if (row) {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}
return undefined;
});
}
executeDeleteReturningOneColumnManyRows(query, params = []) {
return this.executeMutationReturning(query, params).then((rows) => rows.map((row) => {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}));
}
executeProcedure(query, params = []) {
return this.executeMutation(query, params).then(() => undefined);
}
executeFunction(query, params = []) {
return this.executeQueryReturning(query, params).then((rows) => {
if (rows.length > 1) {
throw new Error('Too many rows, expected only zero or one row');
}
const row = rows[0];
if (row) {
const columns = Object.getOwnPropertyNames(row);
if (columns.length > 1) {
throw new Error('Too many columns, expected only one column');
}
return row[columns[0]]; // Value in the row of the first column without care about the name
}
return undefined;
});
}
executeDatabaseSchemaModification(query, params = []) {
return this.executeMutation(query, params).then(() => undefined);
}
executeConnectionConfiguration(query, params = []) {
return this.executeMutation(query, params).then(() => undefined);
}
containsInsertReturningClause(query, params) {
const p = params;
if (p._containsInsertReturningClause === true) {
return true;
}
else if (p._containsInsertReturningClause === false) {
return false;
}
else {
return /\sreturning\s/.test(query);
}
}
executeMutationReturning(query, params) {
return this.executeQueryReturning(query, params);
}
addOutParam(_params, _name) {
throw new Error('Unsupported output parameters');
}
isMocked() {
return false;
}
lowLevelTransactionManagementSupported() {
return true;
}
}
exports.AbstractQueryRunner = AbstractQueryRunner;