}).to.throw(Error,'Both `sql.values` and `options.replacements` cannot be set at the same time');
});
it('throw an exception if `sql.bind` and `options.bind` are both passed',function(){
varself=this;
expect(function(){
returnself.sequelize.query({query:'select $1 + ? as foo, $2 + ? as bar',bind:[1,2]},{raw:true,bind:[1,2]});
}).to.throw(Error,'Both `sql.bind` and `options.bind` cannot be set at the same time');
});
it('throw an exception if `options.replacements` and `options.bind` are both passed',function(){
varself=this;
expect(function(){
returnself.sequelize.query('select $1 + ? as foo, $2 + ? as bar',{raw:true,bind:[1,2],replacements:[1,2]});
}).to.throw(Error,'Both `replacements` and `bind` cannot be set at the same time');
});
it('throw an exception if `sql.bind` and `sql.values` are both passed',function(){
varself=this;
expect(function(){
returnself.sequelize.query({query:'select $1 + ? as foo, $2 + ? as bar',bind:[1,2],values:[1,2]},{raw:true});
}).to.throw(Error,'Both `replacements` and `bind` cannot be set at the same time');
});
it('throw an exception if `sql.bind` and `options.replacements`` are both passed',function(){
varself=this;
expect(function(){
returnself.sequelize.query({query:'select $1 + ? as foo, $2 + ? as bar',bind:[1,2]},{raw:true,replacements:[1,2]});
}).to.throw(Error,'Both `replacements` and `bind` cannot be set at the same time');
});
it('throw an exception if `options.bind` and `sql.replacements` are both passed',function(){
varself=this;
expect(function(){
returnself.sequelize.query({query:'select $1 + ? as foo, $1 _ ? as bar',values:[1,2]},{raw:true,bind:[1,2]});
}).to.throw(Error,'Both `replacements` and `bind` cannot be set at the same time');
});
it('uses properties `query` and `values` if query is tagged',function(){
returnthis.sequelize.query({query:'select ? as foo, ? as bar',values:[1,2]},{type:this.sequelize.QueryTypes.SELECT}).then(function(result){
varlogSql;
returnthis.sequelize.query({query:'select ? as foo, ? as bar',values:[1,2]},{type:this.sequelize.QueryTypes.SELECT,logging:function(s){logSql=s;}}).then(function(result){
expect(result).to.deep.equal([{foo:1,bar:2}]);
expect(logSql.indexOf('?')).to.equal(-1);
});
});
it('uses properties `query` and `bind` if query is tagged',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query({query:'select $1'+typeCast+' as foo, $2'+typeCast+' as bar',bind:[1,2]},{type:this.sequelize.QueryTypes.SELECT,logging:function(s){logSql=s;}}).then(function(result){
}).to.throw(Error,/Named parameter ":\w+" has no value in the given object\./g);
});
it('binds token with the passed array',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query('select $1'+typeCast+' as foo, $2'+typeCast+' as bar',{type:this.sequelize.QueryTypes.SELECT,bind:[1,2],logging:function(s){logSql=s;}}).then(function(result){
expect(result).to.deep.equal([{foo:1,bar:2}]);
if((dialect==='postgres')||(dialect==='sqlite')){
expect(logSql.indexOf('$1')).to.be.above(-1);
}
});
});
it('binds named parameters with the passed object',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query('select $one'+typeCast+' as foo, $two'+typeCast+' as bar',{raw:true,bind:{one:1,two:2},logging:function(s){logSql=s;}}).then(function(result){
expect(result[0]).to.deep.equal([{foo:1,bar:2}]);
if((dialect==='postgres')){
expect(logSql.indexOf('$1')).to.be.above(-1);
}
if((dialect==='sqlite')){
expect(logSql.indexOf('$one')).to.be.above(-1);
}
});
});
it('binds named parameters with the passed object and ignore numeric $1',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query('select $one'+typeCast+' as foo, $two'+typeCast+' as bar, \'$1\' as baz',{raw:true,bind:{one:1,two:2},logging:function(s){logSql=s;}}).then(function(result){
it('binds named parameters with the passed array and ignore $alpha',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query('select $1'+typeCast+' as foo, $2'+typeCast+' as bar, \'$foo\' as baz',{raw:true,bind:[1,2],logging:function(s){logSql=s;}}).then(function(result){
it('binds named parameters with the passed object using the same key twice',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query('select $one'+typeCast+' as foo, $two'+typeCast+' as bar, $one'+typeCast+' as baz',{raw:true,bind:{one:1,two:2},logging:function(s){logSql=s;}}).then(function(result){
it('binds named parameters with the passed object having a null property',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query('select $one'+typeCast+' as foo, $two'+typeCast+' as bar',{raw:true,bind:{one:1,two:null},logging:function(s){logSql=s;}}).then(function(result){
it('binds named parameters object handles escaped $$',function(){
vartypeCast=(dialect==='postgres')?'::int':'';
varlogSql;
returnthis.sequelize.query('select $one'+typeCast+' as foo, \'$$\' as bar',{raw:true,bind:{one:1},logging:function(s){logSql=s;}}).then(function(result){
it('binds array parameters for upsert are replaced. $$ unescapes only once',function(){
varlogSql;
returnthis.sequelize.query('select $1 as foo, $2 as bar, \'$$$$\' as baz',{type:this.sequelize.QueryTypes.UPSERT,bind:[1,2],logging:function(s){logSql=s;}}).then(function(result){
// sqlite.exec does not return a result
expect(logSql.indexOf('$one')).to.equal(-1);
expect(logSql.indexOf('\'$$\'')).to.be.above(-1);
});
});
it('binds named parameters for upsert are replaced. $$ unescapes only once',function(){
varlogSql;
returnthis.sequelize.query('select $one as foo, $two as bar, \'$$$$\' as baz',{type:this.sequelize.QueryTypes.UPSERT,bind:{one:1,two:2},logging:function(s){logSql=s;}}).then(function(result){