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.
1,017 lines (1,016 loc) • 36.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateQueryBuilder = void 0;
const SqlBuilder_1 = require("../sqlBuilders/SqlBuilder");
const ITableOrView_1 = require("../utils/ITableOrView");
const values_1 = require("../expressions/values");
const chained_error_1 = require("chained-error");
const attachSource_1 = require("../utils/attachSource");
const symbols_1 = require("../utils/symbols");
const values_2 = require("../expressions/values");
const ITableOrView_2 = require("../utils/ITableOrView");
const values_3 = require("../expressions/values");
const ComposeSliptQueryBuilder_1 = require("./ComposeSliptQueryBuilder");
const Column_1 = require("../utils/Column");
class UpdateQueryBuilder extends ComposeSliptQueryBuilder_1.ComposeSplitQueryBuilder {
constructor(sqlBuilder, table, allowNoWhere) {
super(sqlBuilder);
this.__sets = {};
this.__withs = [];
// cache
this.__params = [];
this.__query = '';
this.__table = table;
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(sqlBuilder, this.__withs);
this.__allowNoWhere = allowNoWhere;
}
executeUpdate(min, max) {
this.query();
const source = new Error('Query executed at');
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
if (Object.getOwnPropertyNames(this.__sets).length <= 0) {
// Nothing to update, nothing to set
return this.__sqlBuilder._queryRunner.createResolvedPromise(0);
}
let result = this.__sqlBuilder._queryRunner.executeUpdate(this.__query, this.__params).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
if (min !== undefined) {
result = result.then((count) => {
if (count < min) {
throw (0, attachSource_1.attachSource)(new Error("The update operation didn't update the minimum of " + min + " row(s)"), source);
}
if (max !== undefined && count > max) {
throw (0, attachSource_1.attachSource)(new Error("The update operation updated more that the maximum of " + max + " row(s)"), source);
}
return count;
});
}
return result;
}
catch (e) {
throw new chained_error_1.default(e);
}
}
executeUpdateNoneOrOne() {
this.query();
const source = new Error('Query executed at');
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
if (Object.getOwnPropertyNames(this.__sets).length <= 0) {
// Nothing to update, nothing to set
return this.__sqlBuilder._queryRunner.createResolvedPromise(null);
}
this.__sqlBuilder._resetUnique();
let result;
if (this.__oneColumn) {
result = this.__sqlBuilder._queryRunner.executeUpdateReturningOneColumnOneRow(this.__query, this.__params).then((value) => {
const valueSource = this.__columns['result'];
if (!(0, values_1.isValueSource)(valueSource)) {
throw new Error('The result column must be a ValueSource');
}
if (value === undefined) {
return null;
}
return this.__transformValueFromDB(valueSource, value);
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
else {
result = this.__sqlBuilder._queryRunner.executeUpdateReturningOneRow(this.__query, this.__params).then((row) => {
if (row) {
return this.__transformRow(row);
}
else {
return null;
}
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
return this.__applyCompositions(result, source);
}
catch (e) {
throw new chained_error_1.default(e);
}
}
executeUpdateOne() {
this.query();
const source = new Error('Query executed at');
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
if (Object.getOwnPropertyNames(this.__sets).length <= 0) {
// Nothing to update, nothing to set
return this.__sqlBuilder._queryRunner.createResolvedPromise(null).then(() => {
throw (0, attachSource_1.attachSource)(new Error('No values to update due no sets'), source);
});
}
this.__sqlBuilder._resetUnique();
let result;
if (this.__oneColumn) {
result = this.__sqlBuilder._queryRunner.executeUpdateReturningOneColumnOneRow(this.__query, this.__params).then((value) => {
const valueSource = this.__columns['result'];
if (!(0, values_1.isValueSource)(valueSource)) {
throw new Error('The result column must be a ValueSource');
}
if (value === undefined) {
throw new Error('No result returned by the database');
}
return this.__transformValueFromDB(valueSource, value);
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
else {
result = this.__sqlBuilder._queryRunner.executeUpdateReturningOneRow(this.__query, this.__params).then((row) => {
if (row) {
return this.__transformRow(row);
}
else {
throw new Error('No result returned by the database');
}
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
return this.__applyCompositions(result, source);
}
catch (e) {
throw new chained_error_1.default(e);
}
}
executeUpdateMany(min, max) {
const source = new Error('Query executed at');
this.query();
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
if (Object.getOwnPropertyNames(this.__sets).length <= 0) {
// Nothing to update, nothing to set
return this.__sqlBuilder._queryRunner.createResolvedPromise([]);
}
this.__sqlBuilder._resetUnique();
let result;
if (this.__oneColumn) {
result = this.__sqlBuilder._queryRunner.executeUpdateReturningOneColumnManyRows(this.__query, this.__params).then((values) => {
const valueSource = this.__columns['result'];
if (!(0, values_1.isValueSource)(valueSource)) {
throw new Error('The result column must be a ValueSource');
}
return values.map((value) => {
if (value === undefined) {
value = null;
}
return this.__transformValueFromDB(valueSource, value);
});
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
else {
result = this.__sqlBuilder._queryRunner.executeUpdateReturningManyRows(this.__query, this.__params).then((rows) => {
return rows.map((row, index) => {
return this.__transformRow(row, index);
});
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
if (min !== undefined) {
result = result.then((rows) => {
const count = rows.length;
if (count < min) {
throw (0, attachSource_1.attachSource)(new Error("The update operation didn't update the minimum of " + min + " row(s)"), source);
}
if (max !== undefined && count > max) {
throw (0, attachSource_1.attachSource)(new Error("The update operation updated more that the maximum of " + max + " row(s)"), source);
}
return rows;
});
}
return this.__applyCompositions(result, source);
}
catch (e) {
throw new chained_error_1.default(e);
}
}
query() {
if (this.__query) {
return this.__query;
}
try {
this.__query = this.__sqlBuilder._buildUpdate(this, this.__params);
}
catch (e) {
throw new chained_error_1.default(e);
}
return this.__query;
}
params() {
if (!this.__query) {
this.query();
}
return this.__params;
}
__toSql(_sqlBuilder, params) {
return this.__sqlBuilder._buildUpdate(this, params);
}
__toSqlForCondition(sqlBuilder, params) {
return this.__toSql(sqlBuilder, params);
}
shapedAs(shape) {
this.__finishJoin();
this.__query = '';
this.__shape = shape;
return this;
}
extendShape(shape) {
this.__finishJoin();
this.__query = '';
if (!this.__shape) {
this.__shape = shape;
return this;
}
const properties = Object.getOwnPropertyNames(shape);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
const value = shape[property];
// It will review only the properties that can be used as shape, skiiping the other one to allow more complex usages
if (typeof value === 'string' || (0, Column_1.isColumn)(value)) {
const currentShapeValue = this.__shape[property];
if (typeof currentShapeValue === 'string' || (0, Column_1.isColumn)(value)) {
throw new Error('You cannot override the previously defined shape property with name ' + property);
}
}
}
this.__shape = { ...this.__shape, ...shape };
return this;
}
dynamicSet(columns) {
if (columns) {
return this.set(columns);
}
this.__finishJoin();
this.__query = '';
return this;
}
set(columns) {
this.__finishJoin();
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
const value = columns[property];
sets[property] = value;
(0, ITableOrView_2.__addWiths)(this.__sqlBuilder, value, this.__withs);
}
return this;
}
setIfValue(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
const value = columns[property];
if (!this.__sqlBuilder._isValue(value)) {
continue;
}
sets[property] = value;
}
return this;
}
setIfSet(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (!(property in sets)) {
continue;
}
const value = columns[property];
sets[property] = value;
(0, ITableOrView_2.__addWiths)(this.__sqlBuilder, value, this.__withs);
}
return this;
}
setIfSetIfValue(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (!(property in sets)) {
continue;
}
const value = columns[property];
if (!this.__sqlBuilder._isValue(value)) {
continue;
}
sets[property] = value;
}
return this;
}
setIfNotSet(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (property in sets) {
continue;
}
const value = columns[property];
sets[property] = value;
(0, ITableOrView_2.__addWiths)(this.__sqlBuilder, value, this.__withs);
}
return this;
}
setIfNotSetIfValue(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (property in sets) {
continue;
}
const value = columns[property];
if (!this.__sqlBuilder._isValue(value)) {
continue;
}
sets[property] = value;
}
return this;
}
ignoreIfSet(...columns) {
this.__query = '';
let sets = this.__sets;
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
delete sets[column];
}
return this;
}
keepOnly(...columns) {
this.__query = '';
let sets = this.__sets;
const allow = {};
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
allow[column] = true;
}
const properties = Object.getOwnPropertyNames(sets);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (!allow[property]) {
delete sets[property];
}
}
return this;
}
setIfHasValue(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (!this.__sqlBuilder._isValue(sets[property])) {
continue;
}
const value = columns[property];
sets[property] = value;
}
return this;
}
setIfHasValueIfValue(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (!this.__sqlBuilder._isValue(sets[property])) {
continue;
}
const value = columns[property];
if (!this.__sqlBuilder._isValue(value)) {
continue;
}
sets[property] = value;
}
return this;
}
setIfHasNoValue(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (this.__sqlBuilder._isValue(sets[property])) {
continue;
}
const value = columns[property];
sets[property] = value;
}
return this;
}
setIfHasNoValueIfValue(columns) {
this.__query = '';
if (!columns) {
return this;
}
const shape = this.__shape;
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(columns);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (shape && !(property in shape)) {
// property not in the shape
continue;
}
if (this.__sqlBuilder._isValue(sets[property])) {
continue;
}
const value = columns[property];
if (!this.__sqlBuilder._isValue(value)) {
continue;
}
sets[property] = value;
}
return this;
}
ignoreIfHasValue(...columns) {
this.__query = '';
let sets = this.__sets;
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
if (!this.__sqlBuilder._isValue(sets[column])) {
continue;
}
delete sets[column];
}
return this;
}
ignoreIfHasNoValue(...columns) {
this.__query = '';
let sets = this.__sets;
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
if (this.__sqlBuilder._isValue(sets[column])) {
continue;
}
delete sets[column];
}
return this;
}
ignoreAnySetWithNoValue() {
this.__query = '';
let sets = this.__sets;
const properties = Object.getOwnPropertyNames(sets);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (this.__sqlBuilder._isValue(sets[property])) {
continue;
}
delete sets[property];
}
return this;
}
disallowIfSet(error, ...columns) {
this.__query = '';
let sets = this.__sets;
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
if (column in sets) {
if (typeof error === 'string') {
error = new Error(error);
}
error['disallowedPropery'] = column;
throw error;
}
}
return this;
}
disallowIfNotSet(error, ...columns) {
this.__query = '';
let sets = this.__sets;
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
if (!(column in sets)) {
if (typeof error === 'string') {
error = new Error(error);
}
error['disallowedPropery'] = column;
throw error;
}
}
return this;
}
disallowIfValue(error, ...columns) {
this.__query = '';
let sets = this.__sets;
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
if (this.__sqlBuilder._isValue(sets[column])) {
if (typeof error === 'string') {
error = new Error(error);
}
error['disallowedPropery'] = column;
throw error;
}
}
return this;
}
disallowIfNoValue(error, ...columns) {
this.__query = '';
let sets = this.__sets;
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
if (!this.__sqlBuilder._isValue(sets[column])) {
if (typeof error === 'string') {
error = new Error(error);
}
error['disallowedPropery'] = column;
throw error;
}
}
return this;
}
disallowAnyOtherSet(error, ...columns) {
this.__query = '';
let sets = this.__sets;
const allowed = {};
for (let i = 0, length = columns.length; i < length; i++) {
let column = columns[i];
allowed[column] = true;
}
const shape = this.__shape || this.__table;
const properties = Object.getOwnPropertyNames(sets);
for (let i = 0, length = properties.length; i < length; i++) {
const property = properties[i];
if (!(property in shape)) {
// This is not a property that will be included in the update
// Ingoring it allow more complex operations
continue;
}
if (!allowed[property]) {
if (typeof error === 'string') {
error = new Error(error);
}
error['disallowedPropery'] = property;
throw error;
}
}
return this;
}
setWhen(when, columns) {
if (when) {
return this.set(columns);
}
return this;
}
setIfValueWhen(when, columns) {
if (when) {
return this.setIfValue(columns);
}
return this;
}
setIfSetWhen(when, columns) {
if (when) {
return this.setIfSet(columns);
}
return this;
}
setIfSetIfValueWhen(when, columns) {
if (when) {
return this.setIfSetIfValue(columns);
}
return this;
}
setIfNotSetWhen(when, columns) {
if (when) {
return this.setIfNotSet(columns);
}
return this;
}
setIfNotSetIfValueWhen(when, columns) {
if (when) {
return this.setIfNotSetIfValue(columns);
}
return this;
}
ignoreIfSetWhen(when, ...columns) {
if (when) {
return this.ignoreIfSet(...columns);
}
return this;
}
keepOnlyWhen(when, ...columns) {
if (when) {
return this.keepOnly(...columns);
}
return this;
}
setIfHasValueWhen(when, columns) {
if (when) {
return this.setIfHasValue(columns);
}
return this;
}
setIfHasValueIfValueWhen(when, columns) {
if (when) {
return this.setIfHasValueIfValue(columns);
}
return this;
}
setIfHasNoValueWhen(when, columns) {
if (when) {
return this.setIfHasNoValue(columns);
}
return this;
}
setIfHasNoValueIfValueWhen(when, columns) {
if (when) {
return this.setIfHasNoValueIfValue(columns);
}
return this;
}
ignoreIfHasValueWhen(when, ...columns) {
if (when) {
return this.ignoreIfHasValue(...columns);
}
return this;
}
ignoreIfHasNoValueWhen(when, ...columns) {
if (when) {
return this.ignoreIfHasValue(...columns);
}
return this;
}
ignoreAnySetWithNoValueWhen(when) {
if (when) {
return this.ignoreAnySetWithNoValue();
}
return this;
}
disallowIfSetWhen(when, error, ...columns) {
if (when) {
return this.disallowIfSet(error, ...columns);
}
return this;
}
disallowIfNotSetWhen(when, error, ...columns) {
if (when) {
return this.disallowIfNotSet(error, ...columns);
}
return this;
}
disallowIfValueWhen(when, error, ...columns) {
if (when) {
return this.disallowIfValue(error, ...columns);
}
return this;
}
disallowIfNoValueWhen(when, error, ...columns) {
if (when) {
return this.disallowIfNoValue(error, ...columns);
}
return this;
}
disallowAnyOtherSetWhen(when, error, ...columns) {
if (when) {
return this.disallowAnyOtherSet(error, ...columns);
}
return this;
}
dynamicWhere() {
this.__query = '';
return this;
}
where(condition) {
this.__query = '';
if (this.__where) {
throw new Error('Illegal state');
}
this.__where = (0, values_2.asAlwaysIfValueSource)(condition);
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
and(condition) {
this.__query = '';
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
if (this.__lastJoin) {
if (this.__lastJoin.__on) {
this.__lastJoin.__on = this.__lastJoin.__on.and((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__lastJoin.__on = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
if (this.__where) {
this.__where = this.__where.and((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__where = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
or(condition) {
this.__query = '';
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
if (this.__lastJoin) {
if (this.__lastJoin.__on) {
this.__lastJoin.__on = this.__lastJoin.__on.and((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__lastJoin.__on = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
if (this.__where) {
this.__where = this.__where.or((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__where = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
from(table) {
this.__finishJoin();
this.__query = '';
if (!this.__froms) {
this.__froms = [];
}
this.__froms.push(table);
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
join(table) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'join',
__tableOrView: table
};
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
innerJoin(table) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'innerJoin',
__tableOrView: table
};
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
leftJoin(source) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'leftJoin',
__tableOrView: source
};
(0, ITableOrView_1.__getTableOrViewPrivate)(source).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
leftOuterJoin(source) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'leftOuterJoin',
__tableOrView: source
};
(0, ITableOrView_1.__getTableOrViewPrivate)(source).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
dynamicOn() {
this.__query = '';
return this;
}
on(condition) {
this.__query = '';
if (!this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin.__on = (0, values_2.asAlwaysIfValueSource)(condition);
if (!this.__joins) {
this.__joins = [];
}
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
__finishJoin() {
if (this.__lastJoin) {
if (!this.__joins) {
this.__joins = [];
}
this.__joins.push(this.__lastJoin);
this.__lastJoin = undefined;
}
}
customizeQuery(customization) {
this.__customization = customization;
(0, ITableOrView_2.__addWiths)(customization.beforeQuery, this.__sqlBuilder, this.__withs);
(0, ITableOrView_2.__addWiths)(customization.afterUpdateKeyword, this.__sqlBuilder, this.__withs);
(0, ITableOrView_2.__addWiths)(customization.afterQuery, this.__sqlBuilder, this.__withs);
return this;
}
returning(columns) {
this.__query = '';
this.__columns = columns;
this.__registerTableOrViewWithOfColumns(columns, this.__withs);
this.__oldValues = this.__getOldValueOfColumns(columns);
return this;
}
projectingOptionalValuesAsNullable() {
this.__projectOptionalValuesAsNullable = true;
return this;
}
returningOneColumn(column) {
this.__query = '';
this.__oneColumn = true;
this.__columns = { 'result': column };
const columnPrivate = (0, values_3.__getValueSourcePrivate)(column);
columnPrivate.__addWiths(this.__sqlBuilder, this.__withs);
this.__oldValues = columnPrivate.__getOldValues(this.__sqlBuilder);
return this;
}
__addWiths(sqlBuilder, withs) {
const withViews = this.__withs;
for (let i = 0, length = withViews.length; i < length; i++) {
const withView = withViews[i];
(0, ITableOrView_1.__getTableOrViewPrivate)(withView).__addWiths(sqlBuilder, withs);
}
}
__registerTableOrView(_sqlBuilder, _requiredTablesOrViews) {
// do nothing because it is not possible to add external dependency
}
__registerRequiredColumn(_sqlBuilder, _requiredColumns, _onlyForTablesOrViews) {
// do nothing because it is not possible to add external dependency
}
__getOldValues(_sqlBuilder) {
// old values fake table is not possible to be used here
return undefined;
}
__getValuesForInsert(_sqlBuilder) {
// values for insert fake table is not possible to be used here
return undefined;
}
__isAllowed(sqlBuilder) {
let result = (0, ITableOrView_1.__getTableOrViewPrivate)(this.__table).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
const sets = this.__sets;
for (let prop in sets) {
const set = sets[prop];
const result = (0, ITableOrView_1.__isAllowed)(set, sqlBuilder);
if (!result) {
return false;
}
}
const from = this.__froms;
if (from) {
for (let i = 0, length = from.length; i < length; i++) {
result = (0, ITableOrView_1.__getTableOrViewPrivate)(from[i]).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
}
}
const joins = this.__joins;
if (joins) {
for (let i = 0, length = joins.length; i < length; i++) {
const join = joins[i];
result = (0, ITableOrView_1.__getTableOrViewPrivate)(join.__tableOrView).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
if (join.__on) {
result = (0, values_3.__getValueSourcePrivate)(join.__on).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
}
}
}
if (this.__where) {
result = (0, values_3.__getValueSourcePrivate)(this.__where).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
}
if (this.__columns) {
result = (0, SqlBuilder_1.isAllowedQueryColumns)(this.__columns, sqlBuilder);
if (!result) {
return false;
}
}
if (this.__customization) {
result = (0, ITableOrView_1.__isAllowed)(this.__customization.beforeQuery, sqlBuilder);
if (!result) {
return false;
}
result = (0, ITableOrView_1.__isAllowed)(this.__customization.afterUpdateKeyword, sqlBuilder);
if (!result) {
return false;
}
result = (0, ITableOrView_1.__isAllowed)(this.__customization.afterQuery, sqlBuilder);
if (!result) {
return false;
}
}
return true;
}
}
exports.UpdateQueryBuilder = UpdateQueryBuilder;