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

Commit 6acb13dd by Joel Trost Committed by Matt Broadstone

Passes "and-or-where" tests

1 parent ba5e688e
...@@ -28,7 +28,6 @@ module.exports = (function() { ...@@ -28,7 +28,6 @@ module.exports = (function() {
}; };
Query.prototype.run = function(sql) { Query.prototype.run = function(sql) {
console.log(sql);
var self = this; var self = this;
this.sql = sql; this.sql = sql;
...@@ -37,15 +36,22 @@ module.exports = (function() { ...@@ -37,15 +36,22 @@ module.exports = (function() {
} }
var promise = new Utils.Promise(function(resolve, reject) { var promise = new Utils.Promise(function(resolve, reject) {
//console.log(self.sql); console.log(self.sql);
self self
.connection .connection
.lib .lib
.execute(self.connection.config, { query: self.sql }) .execute(self.connection.config, { query: self.sql })
.then( .then(
function (data) { resolve(self.formatResults(data.result)); } function (data) {
//function (err) { reject(self.formatError(err)); } promise.emit('sql', self.sql, self.connection.uuid);
resolve(self.formatResults(data.result));
},
function (err) {
console.log('err:', err);
err.sql = sql;
reject(self.formatError(err));
}
); );
}); });
...@@ -71,34 +77,37 @@ module.exports = (function() { ...@@ -71,34 +77,37 @@ module.exports = (function() {
Query.prototype.formatResults = function(data) { Query.prototype.formatResults = function(data) {
var result = this.callee; var result = this.callee;
//console.log(data); //console.log(data);
if (this.isInsertQuery(data)) { if(data){
this.handleInsertQuery(data); if (this.isInsertQuery(data)) {
} else if (this.isShowTableQuery()) { this.handleInsertQuery(data);
result = this.handleShowTableQuery(data); } else if (this.isShowTableQuery()) {
} else if (this.isShowOrDescribeQuery()) { result = this.handleShowTableQuery(data);
result = data; } else if (this.isShowOrDescribeQuery()) {
if (this.sql.toLowerCase().indexOf("select c.name, t.name as 'type', c.is_nullable as isnull") === 0) {
result = {};
data.forEach(function(_result) {
if(_result.Default)
_result.Default = _result.Default.replace('(\'','').replace('\')','').replace(/'/g,'');
result[_result.Name] = {
type: _result.Type.toUpperCase(),
allowNull: _result.IsNull,
defaultValue: _result.Default
};
});
} else if (this.isShowIndexesQuery()) {
result = data; result = data;
if (this.sql.toLowerCase().indexOf("select c.name, t.name as 'type', c.is_nullable as isnull") === 0) {
result = {};
data.forEach(function(_result) {
if(_result.Default)
_result.Default = _result.Default.replace('(\'','').replace('\')','').replace(/'/g,'');
result[_result.Name] = {
type: _result.Type.toUpperCase(),
allowNull: _result.IsNull,
defaultValue: _result.Default
};
});
} else if (this.isShowIndexesQuery()) {
result = data;
}
} else if (this.isSelectQuery()) {
result = this.handleSelectQuery(data);
} else if (this.isCallQuery()) {
result = data[0];
} else if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery()) {
result = data.affectedRows;
} }
} else if (this.isSelectQuery()) { }else{
result = this.handleSelectQuery(data); result = null;
} else if (this.isCallQuery()) {
result = data[0];
} else if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery()) {
result = data.affectedRows;
} }
return result; return result;
}; };
......
...@@ -169,7 +169,12 @@ module.exports = { ...@@ -169,7 +169,12 @@ module.exports = {
set sequelize(seq) { set sequelize(seq) {
_sequelize = seq; _sequelize = seq;
}, },
quoteIdentifier: function(val){
return quoteIdentifier(val);
},
escape: function(value, field) {
return escape(value,field);
},
showTableSql: function(){ showTableSql: function(){
return 'SELECT name FROM sys.Tables;'; return 'SELECT name FROM sys.Tables;';
}, },
...@@ -620,7 +625,8 @@ module.exports = { ...@@ -620,7 +625,8 @@ module.exports = {
} else { } else {
query.push(quoteIdentifier(key)); query.push(quoteIdentifier(key));
} }
console.log('val', typeof val); console.log('where', where);
console.log('here', val);
query.push(operator); query.push(operator);
if(!val){ if(!val){
query.push('NULL'); query.push('NULL');
......
...@@ -35,7 +35,11 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -35,7 +35,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this.User.find({ this.User.find({
where: Sequelize[method]( "1=1", "2=2" ) where: Sequelize[method]( "1=1", "2=2" )
}).on('sql', function(sql) { }).on('sql', function(sql) {
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2) LIMIT 1") if(dialect === 'mssql'){
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2)")
}else{
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2) LIMIT 1")
}
done() done()
}) })
}) })
...@@ -44,7 +48,11 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -44,7 +48,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this.User.find({ this.User.find({
where: Sequelize[method]( ["1=?", 1], ["2=?", 2] ) where: Sequelize[method]( ["1=?", 1], ["2=?", 2] )
}).on('sql', function(sql) { }).on('sql', function(sql) {
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2) LIMIT 1") if(dialect === 'mssql'){
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2)")
}else{
expect(sql).to.contain("WHERE (1=1 " + word + " 2=2) LIMIT 1")
}
done() done()
}) })
}) })
...@@ -52,9 +60,10 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -52,9 +60,10 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it('can handle objects', function(done) { it('can handle objects', function(done) {
this.User.find({ this.User.find({
where: Sequelize[method]( { username: "foo", intVal: 2 }, { secretValue: 'bar' } ) where: Sequelize[method]( { username: "foo", intVal: 2 }, { secretValue: 'bar' } )
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expectation = ({ var expectation = ({
mysql: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')", mysql: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')",
mssql: 'WHERE ("User"."username"=\'foo\' AND "User"."intVal"=2 ' + word + ' "User"."secretValue"=\'bar\')',
sqlite: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')", sqlite: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')",
postgres: 'WHERE ("User"."username"=\'foo\' AND "User"."intVal"=2 ' + word + ' "User"."secretValue"=\'bar\')', postgres: 'WHERE ("User"."username"=\'foo\' AND "User"."intVal"=2 ' + word + ' "User"."secretValue"=\'bar\')',
mariadb: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')" mariadb: "WHERE (`User`.`username`='foo' AND `User`.`intVal`=2 " + word + " `User`.`secretValue`='bar')"
...@@ -74,11 +83,12 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -74,11 +83,12 @@ describe(Support.getTestDialectTeaser("Model"), function () {
it('can handle numbers', function(done) { it('can handle numbers', function(done) {
this.User.find({ this.User.find({
where: Sequelize[method]( 1, 2 ) where: Sequelize[method]( 1, 2 )
}).on('sql', function(sql) { }).on('sql', function(sql) {
var expectation = ({ var expectation = ({
mysql: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)", mysql: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)",
sqlite: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)", sqlite: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)",
postgres: 'WHERE ("User"."id"=1 ' + word + ' "User"."id"=2)', postgres: 'WHERE ("User"."id"=1 ' + word + ' "User"."id"=2)',
mssql: 'WHERE ("User"."id"=1 ' + word + ' "User"."id"=2)',
mariadb: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)" mariadb: "WHERE (`User`.`id`=1 " + word + " `User`.`id`=2)"
})[Support.getTestDialect()] })[Support.getTestDialect()]
...@@ -100,7 +110,11 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -100,7 +110,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this.User.find({ this.User.find({
where: Sequelize.and( Sequelize.or("1=1", "2=2"), Sequelize.or("3=3", "4=4") ) where: Sequelize.and( Sequelize.or("1=1", "2=2"), Sequelize.or("3=3", "4=4") )
}).on('sql', function(sql) { }).on('sql', function(sql) {
expect(sql).to.contain("WHERE ((1=1 OR 2=2) AND (3=3 OR 4=4)) LIMIT 1") if(dialect === 'mssql'){
expect(sql).to.contain("WHERE ((1=1 OR 2=2) AND (3=3 OR 4=4))")
}else{
expect(sql).to.contain("WHERE ((1=1 OR 2=2) AND (3=3 OR 4=4)) LIMIT 1")
}
done() done()
}) })
}) })
...@@ -114,6 +128,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -114,6 +128,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
mysql: "WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1", mysql: "WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1",
sqlite: "WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1", sqlite: "WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1",
postgres: 'WHERE (("User"."username" = \'foo\' OR "User"."username" = \'bar\') AND ("User"."id" = 1 OR "User"."id" = 4)) LIMIT 1', postgres: 'WHERE (("User"."username" = \'foo\' OR "User"."username" = \'bar\') AND ("User"."id" = 1 OR "User"."id" = 4)) LIMIT 1',
mssql: 'WHERE (("User"."username" = \'foo\' OR "User"."username" = \'bar\') AND ("User"."id" = 1 OR "User"."id" = 4))',
mariadb: "WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1" mariadb: "WHERE ((`User`.`username` = 'foo' OR `User`.`username` = 'bar') AND (`User`.`id` = 1 OR `User`.`id` = 4)) LIMIT 1"
})[Support.getTestDialect()] })[Support.getTestDialect()]
...@@ -131,7 +146,11 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -131,7 +146,11 @@ describe(Support.getTestDialectTeaser("Model"), function () {
this.User.find({ this.User.find({
where: Sequelize.or( Sequelize.and("1=1", "2=2"), Sequelize.and("3=3", "4=4") ) where: Sequelize.or( Sequelize.and("1=1", "2=2"), Sequelize.and("3=3", "4=4") )
}).on('sql', function(sql) { }).on('sql', function(sql) {
expect(sql).to.contain("WHERE ((1=1 AND 2=2) OR (3=3 AND 4=4)) LIMIT 1") if(dialect === 'mssql'){
expect(sql).to.contain("WHERE ((1=1 AND 2=2) OR (3=3 AND 4=4))")
}else{
expect(sql).to.contain("WHERE ((1=1 AND 2=2) OR (3=3 AND 4=4)) LIMIT 1")
}
done() done()
}) })
}) })
...@@ -145,6 +164,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -145,6 +164,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
mysql: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1", mysql: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1",
sqlite: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1", sqlite: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1",
postgres: 'WHERE (("User"."username" = \'foo\' AND "User"."username" = \'bar\') OR ("User"."id" = 1 AND "User"."id" = 4)) LIMIT 1', postgres: 'WHERE (("User"."username" = \'foo\' AND "User"."username" = \'bar\') OR ("User"."id" = 1 AND "User"."id" = 4)) LIMIT 1',
mssql: 'WHERE (("User"."username" = \'foo\' AND "User"."username" = \'bar\') OR ("User"."id" = 1 AND "User"."id" = 4))',
mariadb: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1" mariadb: "WHERE ((`User`.`username` = 'foo' AND `User`.`username` = 'bar') OR (`User`.`id` = 1 AND `User`.`id` = 4)) LIMIT 1"
})[Support.getTestDialect()] })[Support.getTestDialect()]
...@@ -186,7 +206,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -186,7 +206,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
) )
] ]
}).on('sql', function(sql) { }).on('sql', function(sql) {
if (Support.getTestDialect() === 'postgres') { if (Support.getTestDialect() === 'postgres' || dialect === 'mssql') {
expect(sql).to.contain( expect(sql).to.contain(
'WHERE (' + [ 'WHERE (' + [
'"User"."id"=42 AND 2=2 AND 1=1 AND "User"."username"=\'foo\' AND ', '"User"."id"=42 AND 2=2 AND 1=1 AND "User"."username"=\'foo\' AND ',
......
...@@ -426,7 +426,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -426,7 +426,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
var keys = Object.keys(fks[0]), var keys = Object.keys(fks[0]),
keys2 = Object.keys(fks[1]), keys2 = Object.keys(fks[1]),
keys3 = Object.keys(fks[2]) keys3 = Object.keys(fks[2])
if (dialect === "postgres" || dialect === "postgres-native") { if (dialect === "postgres" || dialect === "postgres-native" || dialect == 'mssql') {
expect(keys).to.have.length(6) expect(keys).to.have.length(6)
expect(keys2).to.have.length(7) expect(keys2).to.have.length(7)
expect(keys3).to.have.length(7) expect(keys3).to.have.length(7)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!