someProperty:Sequelize.VIRTUAL,// Since we specify the AS part as a part of the literal string, not with sequelize syntax, we have to tell sequelize about the field
it('uses properties `query` and `values` if query is tagged',function(){
it('uses properties `query` and `values` if query is tagged',asyncfunction(){
letlogSql;
returnthis.sequelize.query({query:'select ? as foo, ? as bar',values:[1,2]},{type:this.sequelize.QueryTypes.SELECT,logging(s){logSql=s;}}).then(result=>{
expect(result).to.deep.equal([{foo:1,bar:2}]);
expect(logSql).to.not.include('?');
});
constresult=awaitthis.sequelize.query({query:'select ? as foo, ? as bar',values:[1,2]},{type:this.sequelize.QueryTypes.SELECT,logging(s){logSql=s;}});
expect(result).to.deep.equal([{foo:1,bar:2}]);
expect(logSql).to.not.include('?');
});
it('uses properties `query` and `bind` if query is tagged',function(){
it('uses properties `query` and `bind` if query is tagged',asyncfunction(){
consttypeCast=dialect==='postgres'?'::int':'';
letlogSql;
returnthis.sequelize.query({query:`select $1${typeCast} as foo, $2${typeCast} as bar`,bind:[1,2]},{type:this.sequelize.QueryTypes.SELECT,logging(s){logSql=s;}}).then(result=>{
expect(result).to.deep.equal([{foo:1,bar:2}]);
if(dialect==='postgres'||dialect==='sqlite'){
expect(logSql).to.include('$1');
expect(logSql).to.include('$2');
}elseif(dialect==='mssql'){
expect(logSql).to.include('@0');
expect(logSql).to.include('@1');
}elseif(dialect==='mysql'){
expect(logSql.match(/\?/g).length).to.equal(2);
}
});
constresult=awaitthis.sequelize.query({query:`select $1${typeCast} as foo, $2${typeCast} as bar`,bind:[1,2]},{type:this.sequelize.QueryTypes.SELECT,logging(s){logSql=s;}});
expect(result).to.deep.equal([{foo:1,bar:2}]);
if(dialect==='postgres'||dialect==='sqlite'){
expect(logSql).to.include('$1');
expect(logSql).to.include('$2');
}elseif(dialect==='mssql'){
expect(logSql).to.include('@0');
expect(logSql).to.include('@1');
}elseif(dialect==='mysql'){
expect(logSql.match(/\?/g).length).to.equal(2);
}
});
it('dot separated attributes when doing a raw query without nest',function(){
it('dot separated attributes when doing a raw query without nest',asyncfunction(){
it('replaces named parameters with the passed object using the same key twice',function(){
returnexpect(this.sequelize.query('select :one as foo, :two as bar, :one as baz',{raw:true,replacements:{one:1,two:2}}).then(obj=>obj[0]))
it('replaces named parameters with the passed object using the same key twice',asyncfunction(){
awaitexpect(this.sequelize.query('select :one as foo, :two as bar, :one as baz',{raw:true,replacements:{one:1,two:2}}).then(obj=>obj[0]))
.to.eventually.deep.equal([{foo:1,bar:2,baz:1}]);
});
it('replaces named parameters with the passed object having a null property',function(){
returnexpect(this.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:{one:1,two:null}}).then(obj=>obj[0]))
it('replaces named parameters with the passed object having a null property',asyncfunction(){
awaitexpect(this.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:{one:1,two:null}}).then(obj=>obj[0]))
.to.eventually.deep.equal([{foo:1,bar:null}]);
});
it('reject when key is missing in the passed object',function(){
returnthis.sequelize.query('select :one as foo, :two as bar, :three as baz',{raw:true,replacements:{one:1,two:2}})
it('reject when key is missing in the passed object',asyncfunction(){
awaitthis.sequelize.query('select :one as foo, :two as bar, :three as baz',{raw:true,replacements:{one:1,two:2}})
.should.be.rejectedWith(Error,/Named parameter ":\w+" has no value in the given object\./g);
});
it('reject with the passed number',function(){
returnthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:2})
it('reject with the passed number',asyncfunction(){
awaitthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:2})
.should.be.rejectedWith(Error,/Named parameter ":\w+" has no value in the given object\./g);
});
it('reject with the passed empty object',function(){
returnthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:{}})
it('reject with the passed empty object',asyncfunction(){
awaitthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:{}})
.should.be.rejectedWith(Error,/Named parameter ":\w+" has no value in the given object\./g);
});
it('reject with the passed string',function(){
returnthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:'foobar'})
it('reject with the passed string',asyncfunction(){
awaitthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:'foobar'})
.should.be.rejectedWith(Error,/Named parameter ":\w+" has no value in the given object\./g);
});
it('reject with the passed date',function(){
returnthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:newDate()})
it('reject with the passed date',asyncfunction(){
awaitthis.sequelize.query('select :one as foo, :two as bar',{raw:true,replacements:newDate()})
.should.be.rejectedWith(Error,/Named parameter ":\w+" has no value in the given object\./g);
});
it('binds token with the passed array',function(){
it('binds token with the passed array',asyncfunction(){
consttypeCast=dialect==='postgres'?'::int':'';
letlogSql;
returnthis.sequelize.query(`select $1${typeCast} as foo, $2${typeCast} as bar`,{type:this.sequelize.QueryTypes.SELECT,bind:[1,2],logging(s){logSql=s;}}).then(result=>{
expect(result).to.deep.equal([{foo:1,bar:2}]);
if(dialect==='postgres'||dialect==='sqlite'){
expect(logSql).to.include('$1');
}
});
constresult=awaitthis.sequelize.query(`select $1${typeCast} as foo, $2${typeCast} as bar`,{type:this.sequelize.QueryTypes.SELECT,bind:[1,2],logging(s){logSql=s;}});
expect(result).to.deep.equal([{foo:1,bar:2}]);
if(dialect==='postgres'||dialect==='sqlite'){
expect(logSql).to.include('$1');
}
});
it('binds named parameters with the passed object',function(){
it('binds named parameters with the passed object',asyncfunction(){
consttypeCast=dialect==='postgres'?'::int':'';
letlogSql;
returnthis.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar`,{raw:true,bind:{one:1,two:2},logging(s){logSql=s;}}).then(result=>{
expect(result[0]).to.deep.equal([{foo:1,bar:2}]);
if(dialect==='postgres'){
expect(logSql).to.include('$1');
}
if(dialect==='sqlite'){
expect(logSql).to.include('$one');
}
});
constresult=awaitthis.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar`,{raw:true,bind:{one:1,two:2},logging(s){logSql=s;}});
expect(result[0]).to.deep.equal([{foo:1,bar:2}]);
if(dialect==='postgres'){
expect(logSql).to.include('$1');
}
if(dialect==='sqlite'){
expect(logSql).to.include('$one');
}
});
it('binds named parameters with the passed object using the same key twice',function(){
it('binds named parameters with the passed object using the same key twice',asyncfunction(){
consttypeCast=dialect==='postgres'?'::int':'';
letlogSql;
returnthis.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar, $one${typeCast} as baz`,{raw:true,bind:{one:1,two:2},logging(s){logSql=s;}}).then(result=>{
constresult=awaitthis.sequelize.query(`select $one${typeCast} as foo, $two${typeCast} as bar, $one${typeCast} as baz`,{raw:true,bind:{one:1,two:2},logging(s){logSql=s;}});
returnthis.sequelize.query(`SELECT ${datetime} AS t`).then(([result])=>{
expect(moment(result[0].t).isValid()).to.be.true;
});
const[result]=awaitthis.sequelize.query(`SELECT ${datetime} AS t`);
expect(moment(result[0].t).isValid()).to.be.true;
});
if(Support.getTestDialect()==='postgres'){
it('replaces named parameters with the passed object and ignores casts',function(){
returnexpect(this.sequelize.query('select :one as foo, :two as bar, \'1000\'::integer as baz',{raw:true,replacements:{one:1,two:2}}).then(obj=>obj[0]))
it('replaces named parameters with the passed object and ignores casts',asyncfunction(){
awaitexpect(this.sequelize.query('select :one as foo, :two as bar, \'1000\'::integer as baz',{raw:true,replacements:{one:1,two:2}}).then(obj=>obj[0]))
returnexpect(this.sequelize.query('WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100) SELECT sum(n) FROM t').then(obj=>obj[0]))
it('supports WITH queries',asyncfunction(){
awaitexpect(this.sequelize.query('WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100) SELECT sum(n) FROM t').then(obj=>obj[0]))
.to.eventually.deep.equal([{'sum':'5050'}]);
});
}
if(Support.getTestDialect()==='sqlite'){
it('binds array parameters for upsert are replaced. $$ unescapes only once',function(){
it('binds array parameters for upsert are replaced. $$ unescapes only once',asyncfunction(){
letlogSql;
returnthis.sequelize.query('select $1 as foo, $2 as bar, \'$$$$\' as baz',{type:this.sequelize.QueryTypes.UPSERT,bind:[1,2],logging(s){logSql=s;}}).then(()=>{
// sqlite.exec does not return a result
expect(logSql).to.not.include('$one');
expect(logSql).to.include('\'$$\'');
});
awaitthis.sequelize.query('select $1 as foo, $2 as bar, \'$$$$\' as baz',{type:this.sequelize.QueryTypes.UPSERT,bind:[1,2],logging(s){logSql=s;}});
// sqlite.exec does not return a result
expect(logSql).to.not.include('$one');
expect(logSql).to.include('\'$$\'');
});
it('binds named parameters for upsert are replaced. $$ unescapes only once',function(){
it('binds named parameters for upsert are replaced. $$ unescapes only once',asyncfunction(){
letlogSql;
returnthis.sequelize.query('select $one as foo, $two as bar, \'$$$$\' as baz',{type:this.sequelize.QueryTypes.UPSERT,bind:{one:1,two:2},logging(s){logSql=s;}}).then(()=>{
// sqlite.exec does not return a result
expect(logSql).to.not.include('$one');
expect(logSql).to.include('\'$$\'');
});
awaitthis.sequelize.query('select $one as foo, $two as bar, \'$$$$\' as baz',{type:this.sequelize.QueryTypes.UPSERT,bind:{one:1,two:2},logging(s){logSql=s;}});
@@ -161,28 +166,27 @@ if (current.dialect.supports.transactions) {
/commit has been called on this transaction\([^)]+\), you can no longer use it\.\(The rejected query is attached as the 'sql' property of this error\)/