UNPKG

ts-db-helper

Version:
208 lines 7.68 kB
import { QueryError } from '../../errors/query.error'; import { QueryPart } from './query-part.model'; import { QueryManager } from '../../managers/query-manager'; import { ModelManager } from '../../managers/model-manager'; import { DbQuery } from '../db-query.model'; import { ClauseGroup } from './clause-group.model'; import { Clause } from './clause.model'; /** * @public * @class QueryUpdate * * @description * For design reasons this class should not be used directly. Use this class with {@link Update} function. * * @param T @extends DbHelperModel a model declared with table and column annotations * * @example * ```typescript * // update todo object * Update(todo).exec().subscribe((qr: QueryResult<any>) => { * // do something with the result... * }, (err) => { * // do something with the error... * }); * ``` * * @author Olivier Margarit * @since 0.1 */ var QueryUpdate = /** @class */ (function () { /** * @public * @constructor should not be use directly, see class header * * @param {T | {new (): T}} model model instance or model */ function QueryUpdate(model) { this.model = model; /** * @private * @constant {string} type the type of statement, should not be modified */ this.type = 'UPDATE'; } /** * @public * @method where is the method to add clauses to the where statement of the query * see {@link Clause} or {@link ClauseGroup} * * @param {Clause|Clause[]|CompositeClause|ClauseGroup|{[index: string]: any}} clauses where clauses * * @return {QueryUpdate<T>} this instance to chain query instructions */ QueryUpdate.prototype.where = function (clauses) { if (!this.whereClauses) { this.whereClauses = new ClauseGroup(); } this.whereClauses.add(clauses); return this; }; /** * @public * @method where is the method to add clauses to the where statement of the query * see {@link Clause} or {@link ClauseGroup} * * @throws {@link QueryError} on set on single model update. set method is for updating * many entries of a specific table target from its class. * * @param {{[index: string]: any}} dict map of column and values to update. * * @return {QueryUpdate<T>} this instance to chain query instructions */ QueryUpdate.prototype.set = function (dict) { if (this.model.$$isDbHelperModel) { throw (new QueryError('Try to set values on Update query' + ' already containing a model. This is not supported', '', '')); } if (this.valueSet) { // merge values for (var key in dict) { if (dict.hasOwnProperty(key)) { this.valueSet[key] = dict[key]; } } } else { this.valueSet = dict; } return this; }; /** * @private * @method getValuesFromModel build values part of the query from the model. * * @return {QueryPart} the values query part of update statement */ QueryUpdate.prototype.getValuesFromModel = function () { var table = ModelManager.getInstance().getModel(this.model); var queryPart = new QueryPart(); var columnsToUpdate = []; for (var _i = 0, _a = table.columnList; _i < _a.length; _i++) { var column = _a[_i]; var value = this.model.getColumnValue(column.name); value = value === undefined ? null : value; if (this.model.$$partialWithProjection) { if (this.model.$$partialWithProjection.indexOf(column.name) >= 0 || this.model[column.field]) { columnsToUpdate.push(column.name); if (value && value.$$isDbHelperModel) { queryPart.params.push(value[column.field]); } else { queryPart.params.push(value === undefined ? null : value); } } } else { columnsToUpdate.push(column.name); if (value && value.$$isDbHelperModel) { queryPart.params.push(value[column.field]); } else { queryPart.params.push(value === undefined ? null : value); } } } queryPart.content = 'SET `' + columnsToUpdate.join('` = (?), `') + '` = (?)'; return queryPart; }; /** * @private * @method getValuesFromSet build values part of the query from the set dict. * * @return {QueryPart} the values query part of update statement */ QueryUpdate.prototype.getValuesFromSet = function () { var queryPart = new QueryPart(); for (var key in this.valueSet) { if (this.valueSet.hasOwnProperty(key)) { queryPart.content += queryPart.content ? ', (?)' : '(?)'; queryPart.params.push(this.valueSet[key]); } } return queryPart; }; /** * @public * @method build should be removed to be a part of the private API * * @return {DbQuery} of the query with the string part and * clauses params. */ QueryUpdate.prototype.build = function () { var table = ModelManager.getInstance().getModel(this.model); var dbQuery = new DbQuery(); dbQuery.table = table.name; dbQuery.type = this.type; var rowid = this.model['$$rowid']; if (this.model.$$isDbHelperModel && (rowid || rowid === 0)) { var clause = new Clause(); clause.key = 'rowid'; clause.value = rowid; this.where(clause); } else { for (var _i = 0, _a = table.columnList; _i < _a.length; _i++) { var column = _a[_i]; if (column.primaryKey) { var clause = new Clause(); clause.key = column.name; clause.value = this.model[column.field]; this.where(clause); } } } // setup values to update dbQuery.query += this.type + ' ' + dbQuery.table; var queryPart; if (this.model.$$isDbHelperModel) { queryPart = this.getValuesFromModel(); } else if (this.valueSet && Object.getOwnPropertyNames(this.valueSet).length) { queryPart = this.getValuesFromSet(); } else { throw (new QueryError('No values to update on Update query build, ' + 'please use set method or call Update with a single model.', '', '')); } dbQuery.append(queryPart); if (this.whereClauses) { dbQuery.query += ' WHERE'; dbQuery.append(this.whereClauses.build()); } return dbQuery; }; /** * @public * @method exec to execute the query and asynchronously retreive result. * * @return {Observable<QueryResult<any>>} observable to subscribe */ QueryUpdate.prototype.exec = function () { return QueryManager.getInstance().query(this.build()); }; return QueryUpdate; }()); export { QueryUpdate }; //# sourceMappingURL=query-update.model.js.map