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

Commit bbf9139d by Pedro Augusto de Paula Barbosa Committed by GitHub

other(find-all): throw on empty attributes (#11867)

1 parent 0fb4bb11
...@@ -1365,6 +1365,7 @@ class QueryGenerator { ...@@ -1365,6 +1365,7 @@ class QueryGenerator {
} }
if (subQuery) { if (subQuery) {
this._throwOnEmptyAttributes(attributes.main, { modelName: model && model.name, as: mainTable.as });
query = `SELECT ${attributes.main.join(', ')} FROM (${subQueryItems.join('')}) AS ${mainTable.as}${mainJoinQueries.join('')}${mainQueryItems.join('')}`; query = `SELECT ${attributes.main.join(', ')} FROM (${subQueryItems.join('')}) AS ${mainTable.as}${mainJoinQueries.join('')}${mainQueryItems.join('')}`;
} else { } else {
query = mainQueryItems.join(''); query = mainQueryItems.join('');
...@@ -2053,7 +2054,17 @@ class QueryGenerator { ...@@ -2053,7 +2054,17 @@ class QueryGenerator {
return { mainQueryOrder, subQueryOrder }; return { mainQueryOrder, subQueryOrder };
} }
_throwOnEmptyAttributes(attributes, extraInfo = {}) {
if (attributes.length > 0) return;
const asPart = extraInfo.as && `as ${extraInfo.as}` || '';
const namePart = extraInfo.modelName && `for model '${extraInfo.modelName}'` || '';
const message = `Attempted a SELECT query ${namePart} ${asPart} without selecting any columns`;
throw new sequelizeError.QueryError(message.replace(/ +/g, ' '));
}
selectFromTableFragment(options, model, attributes, tables, mainTableAs) { selectFromTableFragment(options, model, attributes, tables, mainTableAs) {
this._throwOnEmptyAttributes(attributes, { modelName: model && model.name, as: mainTableAs });
let fragment = `SELECT ${attributes.join(', ')} FROM ${tables}`; let fragment = `SELECT ${attributes.join(', ')} FROM ${tables}`;
if (mainTableAs) { if (mainTableAs) {
......
...@@ -837,6 +837,8 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { ...@@ -837,6 +837,8 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
} }
selectFromTableFragment(options, model, attributes, tables, mainTableAs, where) { selectFromTableFragment(options, model, attributes, tables, mainTableAs, where) {
this._throwOnEmptyAttributes(attributes, { modelName: model && model.name, as: mainTableAs });
const dbVersion = this.sequelize.options.databaseVersion; const dbVersion = this.sequelize.options.databaseVersion;
const isSQLServer2008 = semver.valid(dbVersion) && semver.lt(dbVersion, '11.0.0'); const isSQLServer2008 = semver.valid(dbVersion) && semver.lt(dbVersion, '11.0.0');
......
...@@ -60,6 +60,38 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -60,6 +60,38 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
it('should throw on an attempt to fetch no attributes', function() {
return expect(this.User.findAll({ attributes: [] })).to.be.rejectedWith(
Sequelize.QueryError,
/^Attempted a SELECT query.+without selecting any columns$/
);
});
it('should not throw if overall attributes are nonempty', function() {
const Post = this.sequelize.define('Post', { foo: DataTypes.STRING });
const Comment = this.sequelize.define('Comment', { bar: DataTypes.STRING });
Post.hasMany(Comment, { as: 'comments' });
return Post.sync({ force: true })
.then(() => Comment.sync({ force: true }))
.then(() => {
// Should not throw in this case, even
// though `attributes: []` is set for the main model
return Post.findAll({
raw: true,
attributes: [],
include: [
{
model: Comment,
as: 'comments',
attributes: [
[Sequelize.fn('COUNT', Sequelize.col('comments.id')), 'commentCount']
]
}
]
});
});
});
describe('special where conditions/smartWhere object', () => { describe('special where conditions/smartWhere object', () => {
beforeEach(function() { beforeEach(function() {
this.buf = Buffer.alloc(16); this.buf = Buffer.alloc(16);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!