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

Commit 66e40c39 by Jan Aagaard Meier

Merge pull request #2102 from findhit/feature/set

Feature: sequelize.set method
2 parents 71a1faaf 994cce6b
Showing with 96 additions and 0 deletions
......@@ -63,6 +63,7 @@ module.exports = (function() {
* @param {String} [options.protocol='tcp'] The protocol of the relational database.
* @param {Object} [options.define={}] Default options for model definitions. See sequelize.define for options
* @param {Object} [options.query={}] Default options for sequelize.query
* @param {Object} [options.set={}] Default options for sequelize.set
* @param {Object} [options.sync={}] Default options for sequelize.sync
* @param {String} [options.timezone='+00:00'] The timezone used when converting a date from the database into a javascript date. The timezone is also used to SET TIMEZONE when connecting to the server, to ensure that the result of NOW, CURRENT_TIMESTAMP and other time related functions have in the right timezone. For best cross platform performance use the format +/-HH:MM.
* @param {Function} [options.logging=console.log] A function that gets executed everytime Sequelize would log something.
......@@ -562,6 +563,42 @@ module.exports = (function() {
};
/**
* Execute a query which would set an environment or user variable.
* If you want to set variables for a specific query, you should create a transaction and pass it on options.
*
* @method set
* @param {Object} variables Object with multiple variables.
* @param {Object} options={} Query options.
* @param {Transaction} options.transaction=null The transaction that the query should be executed under
* @return {Promise}
*
*/
Sequelize.prototype.set = function( variables, options ) {
var query;
// Prepare options
options = Utils._.extend( {}, this.options.set, typeof options === 'object' && options || {} );
if( ! options.transaction || ! ( options.transaction instanceof Transaction ) ) {
return Promise.rejected( new TypeError("options.transaction is required") );
}
// Override some options, since this isn't a SELECT
options.raw = true;
options.plain = true;
options.type = 'SET';
// Generate SQL Query
query =
'SET '+
Utils._.map( variables, function ( v, k ) {
return '@'+k +' := '+ ( typeof v === 'string' ? '"'+v+'"' : v );
}).join(', ');
return this.query( query, null, options );
};
/**
* Create a new database schema.
*
* Note,that this is a schema in the [postgres sense of the word](http://www.postgresql.org/docs/9.1/static/ddl-schemas.html),
......
......@@ -449,6 +449,65 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}
})
describe('set', function() {
it("should return an promised error if transaction isn't defined", function ( done ) {
var S = this.sequelize;
S.set({ foo: 'bar' })
.done( function ( err ) {
expect( err ).to.be.an.instanceof( TypeError )
done();
});
})
it("one value", function ( done ) {
var S = this.sequelize;
S.transaction().then(function ( t ) {
S
.set({ foo: 'bar' }, { transaction: t })
.then(function () {
return S.query( 'SELECT @foo as `foo`', null, { raw: true, plain: true, transaction: t })
})
.then(function ( data ) {
expect( data ).to.be.ok
expect( data.foo ).to.be.equal( 'bar' )
})
.then(function () {
return t.commit()
})
.then( done )
})
})
it("multiple values", function ( done ) {
var S = this.sequelize;
S.transaction().then(function ( t ) {
S
.set({
foo: 'bar',
foos: 'bars',
}, { transaction: t })
.then(function () {
return S.query( 'SELECT @foo as `foo`, @foos as `foos`', null, { raw: true, plain: true, transaction: t })
})
.then(function ( data ) {
expect( data ).to.be.ok
expect( data.foo ).to.be.equal( 'bar' )
expect( data.foos ).to.be.equal( 'bars' )
})
.then(function () {
return t.commit()
})
.then( done )
})
})
})
describe('define', function() {
it("adds a new dao to the dao manager", function(done) {
expect(this.sequelize.daoFactoryManager.all.length).to.equal(0)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!