不要怂,就是干,撸起袖子干!

Commit 9b204af6 by Muhammad AbdulMoiz Committed by Sushant

try catch block added for formatting parameters process (#6999)

* try catch block added for formatting parameters process

* new line space added before braces

* query function processing wrapped to Promise.try

* processQuery method removed from var

* binding context changed to arrow function

* promise.try chaining added

* extra Promise.resolve removed code refactored

* space added

* test cases for 'query' method updated with respect to new rejection structure instead of throwing error

* it block messages corrected

* return added and code structured

* line space added

* line space added

* code style fixed
1 parent cc09a3c7
......@@ -455,112 +455,107 @@ class Sequelize {
*
* @see {@link Model#build} for more information about instance option.
*/
query(sql, options) {
if (arguments.length > 2) {
// TODO: Remove this note in the next major version (4.0)
throw new Error('Sequelize.query was refactored to only use the parameters `sql` and `options`. Please read the changelog about BC.');
}
options = _.assign({}, this.options.query, options);
if (options.instance && !options.model) {
options.model = options.instance.constructor;
}
query(sql, options) {
let bindParameters;
// Map raw fields to model field names using the `fieldAttributeMap`
if (options.model && options.mapToModel && !Utils._.isEmpty(options.model.fieldAttributeMap)) {
options.fieldMap = options.model.fieldAttributeMap;
}
return Promise.try(() => {
options = _.assign({}, this.options.query, options);
if (typeof sql === 'object') {
if (sql.values !== undefined) {
if (options.replacements !== undefined) {
throw new Error('Both `sql.values` and `options.replacements` cannot be set at the same time');
}
if (options.instance && !options.model) {
options.model = options.instance.constructor;
}
options.replacements = sql.values;
// Map raw fields to model field names using the `fieldAttributeMap`
if (options.model && options.mapToModel && !Utils._.isEmpty(options.model.fieldAttributeMap)) {
options.fieldMap = options.model.fieldAttributeMap;
}
if (sql.bind !== undefined) {
if (options.bind !== undefined) {
throw new Error('Both `sql.bind` and `options.bind` cannot be set at the same time');
if (typeof sql === 'object') {
if (sql.values !== undefined) {
if (options.replacements !== undefined) {
throw new Error('Both `sql.values` and `options.replacements` cannot be set at the same time');
}
options.replacements = sql.values;
}
options.bind = sql.bind;
}
if (sql.bind !== undefined) {
if (options.bind !== undefined) {
throw new Error('Both `sql.bind` and `options.bind` cannot be set at the same time');
}
options.bind = sql.bind;
}
if (sql.query !== undefined) {
sql = sql.query;
if (sql.query !== undefined) {
sql = sql.query;
}
}
}
sql = sql.trim();
sql = sql.trim();
if (!options.instance && !options.model) {
options.raw = true;
}
if (!options.instance && !options.model) {
options.raw = true;
}
if (options.replacements && options.bind) {
throw new Error('Both `replacements` and `bind` cannot be set at the same time');
}
if (options.replacements) {
if (Array.isArray(options.replacements)) {
sql = Utils.format([sql].concat(options.replacements), this.options.dialect);
if (options.replacements && options.bind) {
throw new Error('Both `replacements` and `bind` cannot be set at the same time');
}
else {
sql = Utils.formatNamedParameters(sql, options.replacements, this.options.dialect);
if (options.replacements) {
if (Array.isArray(options.replacements)) {
sql = Utils.format([sql].concat(options.replacements), this.options.dialect);
} else {
sql = Utils.formatNamedParameters(sql, options.replacements, this.options.dialect);
}
}
}
let bindParameters;
if (options.bind) {
const bindSql = this.dialect.Query.formatBindParameters(sql, options.bind, this.options.dialect);
sql = bindSql[0];
bindParameters = bindSql[1];
}
options = _.defaults(options, {
logging: this.options.hasOwnProperty('logging') ? this.options.logging : console.log,
searchPath: this.options.hasOwnProperty('searchPath') ? this.options.searchPath : 'DEFAULT'
});
if (options.bind) {
const bindSql = this.dialect.Query.formatBindParameters(sql, options.bind, this.options.dialect);
sql = bindSql[0];
bindParameters = bindSql[1];
}
if (options.transaction === undefined && Sequelize._cls) {
options.transaction = Sequelize._cls.get('transaction');
}
options = _.defaults(options, {
logging: this.options.hasOwnProperty('logging') ? this.options.logging : console.log,
searchPath: this.options.hasOwnProperty('searchPath') ? this.options.searchPath : 'DEFAULT'
});
if (!options.type) {
if (options.model || options.nest || options.plain) {
options.type = QueryTypes.SELECT;
} else {
options.type = QueryTypes.RAW;
if (options.transaction === undefined && Sequelize._cls) {
options.transaction = Sequelize._cls.get('transaction');
}
}
if (options.transaction && options.transaction.finished) {
const error = new Error(options.transaction.finished+' has been called on this transaction('+options.transaction.id+'), you can no longer use it. (The rejected query is attached as the \'sql\' property of this error)');
error.sql = sql;
return Promise.reject(error);
}
if (this.test._trackRunningQueries) {
this.test._runningQueries++;
}
if (!options.type) {
if (options.model || options.nest || options.plain) {
options.type = QueryTypes.SELECT;
} else {
options.type = QueryTypes.RAW;
}
}
//if dialect doesn't support search_path or dialect option
//to prepend searchPath is not true delete the searchPath option
if (!this.dialect.supports.searchPath || !this.options.dialectOptions || !this.options.dialectOptions.prependSearchPath ||
options.supportsSearchPath === false) {
delete options.searchPath;
} else if (!options.searchPath) {
//if user wants to always prepend searchPath (dialectOptions.preprendSearchPath = true)
//then set to DEFAULT if none is provided
options.searchPath = 'DEFAULT';
}
if (options.transaction && options.transaction.finished) {
const error = new Error(options.transaction.finished+' has been called on this transaction('+options.transaction.id+'), you can no longer use it. (The rejected query is attached as the \'sql\' property of this error)');
error.sql = sql;
return Promise.reject(error);
}
return Promise.resolve(
options.transaction ? options.transaction.connection : this.connectionManager.getConnection(options)
).then(connection => {
if (this.test._trackRunningQueries) {
this.test._runningQueries++;
}
//if dialect doesn't support search_path or dialect option
//to prepend searchPath is not true delete the searchPath option
if (!this.dialect.supports.searchPath || !this.options.dialectOptions || !this.options.dialectOptions.prependSearchPath ||
options.supportsSearchPath === false) {
delete options.searchPath;
} else if (!options.searchPath) {
//if user wants to always prepend searchPath (dialectOptions.preprendSearchPath = true)
//then set to DEFAULT if none is provided
options.searchPath = 'DEFAULT';
}
return options.transaction ? options.transaction.connection : this.connectionManager.getConnection(options);
}).then(connection => {
const query = new this.dialect.Query(connection, this, options);
return retry(() => query.run(sql, bindParameters).finally(() => {
if (!options.transaction) {
return this.connectionManager.releaseConnection(connection);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!