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

Commit 75f48a23 by Takashi Sasaki Committed by Sushant

fix(query): correctly quote identifier for attributes (#9964) (#10118)

1 parent 25071392
...@@ -885,6 +885,14 @@ class QueryGenerator { ...@@ -885,6 +885,14 @@ class QueryGenerator {
return this.quoteIdentifier(identifiers); return this.quoteIdentifier(identifiers);
} }
quoteAttribute(attribute, model) {
if (model && attribute in model.rawAttributes) {
return this.quoteIdentifier(attribute);
} else {
return this.quoteIdentifiers(attribute);
}
}
/** /**
* Quote table name with optional alias and schema attribution * Quote table name with optional alias and schema attribution
* *
...@@ -1389,7 +1397,7 @@ class QueryGenerator { ...@@ -1389,7 +1397,7 @@ class QueryGenerator {
attr = [attr[0], this.quoteIdentifier(attr[1])].join(' AS '); attr = [attr[0], this.quoteIdentifier(attr[1])].join(' AS ');
} else { } else {
attr = !attr.includes(Utils.TICK_CHAR) && !attr.includes('"') attr = !attr.includes(Utils.TICK_CHAR) && !attr.includes('"')
? this.quoteIdentifiers(attr) ? this.quoteAttribute(attr, options.model)
: this.escape(attr); : this.escape(attr);
} }
if (!_.isEmpty(options.include) && !attr.includes('.') && addTable) { if (!_.isEmpty(options.include) && !attr.includes('.') && addTable) {
......
...@@ -131,5 +131,19 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -131,5 +131,19 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
}); });
describe('quote', () => {
it('allows for an attribute with dots', function() {
const User = this.sequelize.define('user', {
'foo.bar.baz': Sequelize.TEXT
});
return this.sequelize.sync({force: true})
.then(() => User.findAll())
.then(result => {
expect(result.length).to.equal(0);
});
});
});
}); });
}); });
...@@ -468,6 +468,18 @@ if (dialect === 'mysql') { ...@@ -468,6 +468,18 @@ if (dialect === 'mysql') {
expectation: 'SELECT `test`.* FROM (SELECT * FROM `myTable` AS `test` HAVING `creationYear` > 2002) AS `test`;', expectation: 'SELECT `test`.* FROM (SELECT * FROM `myTable` AS `test` HAVING `creationYear` > 2002) AS `test`;',
context: QueryGenerator, context: QueryGenerator,
needsSequelize: true needsSequelize: true
}, {
title: 'Contains fields with "." characters.',
arguments: ['myTable', {
attributes: ['foo.bar.baz'],
model: {
rawAttributes: {
'foo.bar.baz': {}
}
}
}],
expectation: 'SELECT `foo.bar.baz` FROM `myTable`;',
context: QueryGenerator
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!