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

Commit a58c3873 by Jan Aagaard Meier

Support for setting columns with the value of another column or a function

1 parent 987145ba
var Utils = require("../../utils") var Utils = require("../../utils")
, SqlString = require("../../sql-string")
module.exports = (function() { module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
...@@ -318,8 +319,12 @@ module.exports = (function() { ...@@ -318,8 +319,12 @@ module.exports = (function() {
/* /*
Escape a value (e.g. a string, number or date) Escape a value (e.g. a string, number or date)
*/ */
escape: function(value) { escape: function(value, field) {
throwMethodUndefined('escape') if (value instanceof Utils.fn || value instanceof Utils.col) {
return value.toString(this)
} else {
return SqlString.escape(value, false, null, this.dialect, field)
}
} }
} }
......
...@@ -605,12 +605,7 @@ module.exports = (function() { ...@@ -605,12 +605,7 @@ module.exports = (function() {
quoteIdentifiers: function(identifiers, force) { quoteIdentifiers: function(identifiers, force) {
return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.') return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.')
},
escape: function(value) {
return SqlString.escape(value, false, null, "mysql")
} }
} }
return Utils._.extend(Utils._.clone(require("../abstract/query-generator")), QueryGenerator) return Utils._.extend(Utils._.clone(require("../abstract/query-generator")), QueryGenerator)
......
...@@ -858,12 +858,7 @@ module.exports = (function() { ...@@ -858,12 +858,7 @@ module.exports = (function() {
quoteIdentifiers: function(identifiers, force) { quoteIdentifiers: function(identifiers, force) {
return identifiers.split('.').map(function(t) { return this.quoteIdentifier(t, force) }.bind(this)).join('.') return identifiers.split('.').map(function(t) { return this.quoteIdentifier(t, force) }.bind(this)).join('.')
},
escape: function(value, field) {
return SqlString.escape(value, false, null, "postgres", field)
} }
} }
// Private // Private
......
...@@ -486,12 +486,7 @@ module.exports = (function() { ...@@ -486,12 +486,7 @@ module.exports = (function() {
quoteIdentifiers: function(identifiers, force) { quoteIdentifiers: function(identifiers, force) {
return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.') return identifiers.split('.').map(function(v) { return this.quoteIdentifier(v, force) }.bind(this)).join('.')
},
escape: function(value) {
return SqlString.escape(value, false, null, "sqlite")
} }
} }
return Utils._.extend({}, MySqlQueryGenerator, QueryGenerator) return Utils._.extend({}, MySqlQueryGenerator, QueryGenerator)
......
...@@ -333,6 +333,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -333,6 +333,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('create', function() { describe('create', function() {
it('is possible to use funtions when creating an instance', function (done) {
var self = this
this.User.create({
secretValue: this.sequelize.fn('upper', 'sequelize')
}).success(function (user) {
self.User.find(user.id).success(function (user) {
expect(user.secretValue).to.equal('SEQUELIZE')
done()
})
})
})
it("casts empty arrays correctly for postgresql insert", function(done) { it("casts empty arrays correctly for postgresql insert", function(done) {
if (dialect !== "postgres" && dialect !== "postgresql-native") { if (dialect !== "postgres" && dialect !== "postgresql-native") {
expect('').to.equal('') expect('').to.equal('')
...@@ -955,6 +966,21 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -955,6 +966,21 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('updates with function and column value', function (done) {
var self = this
this.User.create({
username: 'John'
}).success(function(user) {
self.User.update({username: self.sequelize.fn('upper', self.sequelize.col('username'))}, {username: 'John'}).success(function () {
self.User.all().success(function(users) {
expect(users[0].username).to.equal('JOHN')
done()
})
})
})
})
it('sets updatedAt to the current timestamp', function(done) { it('sets updatedAt to the current timestamp', function(done) {
var self = this var self = this
, data = [{ username: 'Peter', secretValue: '42' }, , data = [{ username: 'Peter', secretValue: '42' },
......
...@@ -641,6 +641,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -641,6 +641,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}, 1000) }, 1000)
}) })
it('updates with function and column value', function (done) {
var self = this
this.User.create({
aNumber: 42
}).success(function(user) {
user.bNumber = self.sequelize.col('aNumber')
user.username = self.sequelize.fn('upper', 'sequelize')
user.save().success(function(){
self.User.find(user.id).success(function(user2) {
expect(user2.username).to.equal('SEQUELIZE')
expect(user2.bNumber).to.equal(42)
done()
})
})
})
})
describe('without timestamps option', function() { describe('without timestamps option', function() {
it("doesn't update the updatedAt column", function(done) { it("doesn't update the updatedAt column", function(done) {
var User2 = this.sequelize.define('User2', { var User2 = this.sequelize.define('User2', {
......
...@@ -308,6 +308,14 @@ if (dialect.match(/^mysql/)) { ...@@ -308,6 +308,14 @@ if (dialect.match(/^mysql/)) {
}, { }, {
arguments: ['myTable', {foo: true}], arguments: ['myTable', {foo: true}],
expectation: "INSERT INTO `myTable` (`foo`) VALUES (true);" expectation: "INSERT INTO `myTable` (`foo`) VALUES (true);"
}, {
arguments: ['myTable', function (sequelize) {
return {
foo: sequelize.fn('NOW')
}
}],
expectation: "INSERT INTO `myTable` (`foo`) VALUES (NOW());",
needsSequelize: true
} }
], ],
...@@ -375,6 +383,22 @@ if (dialect.match(/^mysql/)) { ...@@ -375,6 +383,22 @@ if (dialect.match(/^mysql/)) {
}, { }, {
arguments: ['myTable', {bar: true}, {name: 'foo'}], arguments: ['myTable', {bar: true}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=true WHERE `name`='foo'" expectation: "UPDATE `myTable` SET `bar`=true WHERE `name`='foo'"
}, {
arguments: ['myTable', function (sequelize) {
return {
bar: sequelize.fn('NOW')
}
}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=NOW() WHERE `name`='foo'",
needsSequelize: true
}, {
arguments: ['myTable', function (sequelize) {
return {
bar: sequelize.col('foo')
}
}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=`foo` WHERE `name`='foo'",
needsSequelize: true
} }
], ],
......
...@@ -451,6 +451,14 @@ if (dialect.match(/^postgres/)) { ...@@ -451,6 +451,14 @@ if (dialect.match(/^postgres/)) {
}, { }, {
arguments: ['mySchema.myTable', {name: "foo';DROP TABLE mySchema.myTable;"}], arguments: ['mySchema.myTable', {name: "foo';DROP TABLE mySchema.myTable;"}],
expectation: "INSERT INTO \"mySchema\".\"myTable\" (\"name\") VALUES ('foo'';DROP TABLE mySchema.myTable;') RETURNING *;" expectation: "INSERT INTO \"mySchema\".\"myTable\" (\"name\") VALUES ('foo'';DROP TABLE mySchema.myTable;') RETURNING *;"
}, {
arguments: ['myTable', function (sequelize) {
return {
foo: sequelize.fn('NOW')
}
}],
expectation: "INSERT INTO \"myTable\" (\"foo\") VALUES (NOW()) RETURNING *;",
needsSequelize: true
}, },
// Variants when quoteIdentifiers is false // Variants when quoteIdentifiers is false
...@@ -630,6 +638,22 @@ if (dialect.match(/^postgres/)) { ...@@ -630,6 +638,22 @@ if (dialect.match(/^postgres/)) {
}, { }, {
arguments: ['mySchema.myTable', {name: "foo';DROP TABLE mySchema.myTable;"}, {name: 'foo'}], arguments: ['mySchema.myTable', {name: "foo';DROP TABLE mySchema.myTable;"}, {name: 'foo'}],
expectation: "UPDATE \"mySchema\".\"myTable\" SET \"name\"='foo'';DROP TABLE mySchema.myTable;' WHERE \"name\"='foo' RETURNING *" expectation: "UPDATE \"mySchema\".\"myTable\" SET \"name\"='foo'';DROP TABLE mySchema.myTable;' WHERE \"name\"='foo' RETURNING *"
}, {
arguments: ['myTable', function (sequelize) {
return {
bar: sequelize.fn('NOW')
}
}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=NOW() WHERE \"name\"='foo' RETURNING *",
needsSequelize: true
}, {
arguments: ['myTable', function (sequelize) {
return {
bar: sequelize.col('foo')
}
}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=\"foo\" WHERE \"name\"='foo' RETURNING *",
needsSequelize: true
}, },
// Variants when quoteIdentifiers is false // Variants when quoteIdentifiers is false
......
...@@ -294,6 +294,14 @@ if (dialect === 'sqlite') { ...@@ -294,6 +294,14 @@ if (dialect === 'sqlite') {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}], arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);", expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}} context: {options: {omitNull: true}}
}, {
arguments: ['myTable', function (sequelize) {
return {
foo: sequelize.fn('NOW')
}
}],
expectation: "INSERT INTO `myTable` (`foo`) VALUES (NOW());",
needsSequelize: true
} }
], ],
...@@ -373,6 +381,22 @@ if (dialect === 'sqlite') { ...@@ -373,6 +381,22 @@ if (dialect === 'sqlite') {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}], arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'", expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'",
context: {options: {omitNull: true}} context: {options: {omitNull: true}}
}, {
arguments: ['myTable', function (sequelize) {
return {
bar: sequelize.fn('NOW')
}
}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=NOW() WHERE `name`='foo'",
needsSequelize: true
}, {
arguments: ['myTable', function (sequelize) {
return {
bar: sequelize.col('foo')
}
}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=`foo` WHERE `name`='foo'",
needsSequelize: true
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!