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

Commit 2eb25d6b by Jan Aagaard Meier

Merge pull request #636 from mweibel/fix-sqlite-addquotes

SQLite addQuotes is not correct
2 parents b9e335d6 a524aeae
...@@ -24,8 +24,17 @@ module.exports = (function() { ...@@ -24,8 +24,17 @@ module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
options: {}, options: {},
addQuotes: function(s, quoteChar) { removeQuotes: function (s, quoteChar) {
return Utils.addTicks(s, quoteChar) quoteChar = quoteChar || '`'
return s.replace(new RegExp(quoteChar, 'g'), '')
},
addQuotes: function (s, quoteChar) {
quoteChar = quoteChar || '`'
return QueryGenerator.removeQuotes(s, quoteChar)
.split('.')
.map(function(e) { return quoteChar + String(e) + quoteChar })
.join('.')
}, },
addSchema: function(opts) { addSchema: function(opts) {
...@@ -149,6 +158,74 @@ module.exports = (function() { ...@@ -149,6 +158,74 @@ module.exports = (function() {
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
selectQuery: function(tableName, options) {
var table = null,
joinQuery = ""
options = options || {}
options.table = table = Array.isArray(tableName) ? tableName.map(function(tbl){ return QueryGenerator.addQuotes(tbl) }).join(", ") : QueryGenerator.addQuotes(tableName)
options.attributes = options.attributes && options.attributes.map(function(attr){
if(Array.isArray(attr) && attr.length == 2) {
return [attr[0], QueryGenerator.addQuotes(attr[1])].join(' as ')
} else {
return attr.indexOf(Utils.TICK_CHAR) < 0 ? QueryGenerator.addQuotes(attr) : attr
}
}).join(", ")
options.attributes = options.attributes || '*'
if (options.include) {
var optAttributes = options.attributes === '*' ? [options.table + '.*'] : [options.attributes]
options.include.forEach(function(include) {
var attributes = Object.keys(include.daoFactory.attributes).map(function(attr) {
return "`" + include.as + "`.`" + attr + "` AS `" + include.as + "." + attr + "`"
})
optAttributes = optAttributes.concat(attributes)
var table = include.daoFactory.tableName
var as = include.as
var tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName)
var attrLeft = 'id'
var tableRight = ((include.association.associationType === 'BelongsTo') ? tableName : include.as)
var attrRight = include.association.identifier
joinQuery += " LEFT OUTER JOIN `" + table + "` AS `" + as + "` ON `" + tableLeft + "`.`" + attrLeft + "` = `" + tableRight + "`.`" + attrRight + "`"
})
options.attributes = optAttributes.join(', ')
}
var query = "SELECT " + options.attributes + " FROM " + options.table
query += joinQuery
if (options.hasOwnProperty('where')) {
options.where = this.getWhereConditions(options.where, tableName)
query += " WHERE " + options.where
}
if (options.group) {
options.group = Array.isArray(options.group) ? options.group.map(function(grp){return QueryGenerator.addQuotes(grp)}).join(', ') : QueryGenerator.addQuotes(options.group)
query += " GROUP BY " + options.group
}
if (options.order) {
query += " ORDER BY " + options.order
}
if (options.limit && !(options.include && (options.limit === 1))) {
if (options.offset) {
query += " LIMIT " + options.offset + ", " + options.limit
} else {
query += " LIMIT " + options.limit
}
}
query += ";"
return query
},
updateQuery: function(tableName, attrValueHash, where) { updateQuery: function(tableName, attrValueHash, where) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull) attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
......
...@@ -1116,7 +1116,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1116,7 +1116,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
done(); done();
}.bind(this)) }.bind(this))
}) })
it("should return raw data when raw is true", function (done) { it("should return raw data when raw is true", function (done) {
this.User.find({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, user) { this.User.find({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, user) {
...@@ -1373,9 +1373,9 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1373,9 +1373,9 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
it("should return a DAO when queryOptions are not set", function (done) { it("should return a DAO when queryOptions are not set", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}).done(function (err, users) { this.User.findAll({ where: { username: 'barfooz'}}).done(function (err, users) {
users.forEach(function (user) { users.forEach(function (user) {
expect(user).toHavePrototype(this.User.DAO.prototype) expect(user).toHavePrototype(this.User.DAO.prototype)
}, this) }, this)
done(); done();
}.bind(this)) }.bind(this))
...@@ -1384,17 +1384,17 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1384,17 +1384,17 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
it("should return a DAO when raw is false", function (done) { it("should return a DAO when raw is false", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}, { raw: false }).done(function (err, users) { this.User.findAll({ where: { username: 'barfooz'}}, { raw: false }).done(function (err, users) {
users.forEach(function (user) { users.forEach(function (user) {
expect(user).toHavePrototype(this.User.DAO.prototype) expect(user).toHavePrototype(this.User.DAO.prototype)
}, this) }, this)
done(); done();
}.bind(this)) }.bind(this))
}) })
it("should return raw data when raw is true", function (done) { it("should return raw data when raw is true", function (done) {
this.User.findAll({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, users) { this.User.findAll({ where: { username: 'barfooz'}}, { raw: true }).done(function (err, users) {
users.forEach(function (user) { users.forEach(function (user) {
expect(user).not.toHavePrototype(this.User.DAO.prototype) expect(user).not.toHavePrototype(this.User.DAO.prototype)
expect(users[0]).toBeObject() expect(users[0]).toBeObject()
}, this) }, this)
...@@ -1563,6 +1563,10 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -1563,6 +1563,10 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
expect(self.UserSpecialSync.getTableName()).toEqual('"special"."UserSpecials"'); expect(self.UserSpecialSync.getTableName()).toEqual('"special"."UserSpecials"');
expect(UserSpecial.indexOf('INSERT INTO "special"."UserSpecials"')).toBeGreaterThan(-1) expect(UserSpecial.indexOf('INSERT INTO "special"."UserSpecials"')).toBeGreaterThan(-1)
expect(UserPublic.indexOf('INSERT INTO "UserPublics"')).toBeGreaterThan(-1) expect(UserPublic.indexOf('INSERT INTO "UserPublics"')).toBeGreaterThan(-1)
} else if (dialect === "sqlite") {
expect(self.UserSpecialSync.getTableName()).toEqual('`special`.`UserSpecials`');
expect(UserSpecial.indexOf('INSERT INTO `special.UserSpecials`')).toBeGreaterThan(-1)
expect(UserPublic.indexOf('INSERT INTO `UserPublics`')).toBeGreaterThan(-1)
} else { } else {
expect(self.UserSpecialSync.getTableName()).toEqual('`special.UserSpecials`'); expect(self.UserSpecialSync.getTableName()).toEqual('`special.UserSpecials`');
expect(UserSpecial.indexOf('INSERT INTO `special.UserSpecials`')).toBeGreaterThan(-1) expect(UserSpecial.indexOf('INSERT INTO `special.UserSpecials`')).toBeGreaterThan(-1)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!