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

Commit b52f6295 by Mick Hansen

add databaseVersion() method for getting the dialect database version

1 parent 5aef119f
...@@ -50,6 +50,10 @@ module.exports = (function() { ...@@ -50,6 +50,10 @@ module.exports = (function() {
throwMethodUndefined('createTableQuery'); throwMethodUndefined('createTableQuery');
}, },
versionQuery: function(tableName, attributes, options) {
throwMethodUndefined('versionQuery');
},
describeTableQuery: function(tableName, schema, schemaDelimiter) { describeTableQuery: function(tableName, schema, schemaDelimiter) {
var table = this.quoteTable( var table = this.quoteTable(
this.addSchema({ this.addSchema({
......
...@@ -83,6 +83,10 @@ module.exports = (function() { ...@@ -83,6 +83,10 @@ module.exports = (function() {
} }
}; };
AbstractQuery.prototype.isVersionQuery = function () {
return this.options.type === QueryTypes.VERSION;
};
AbstractQuery.prototype.isUpsertQuery = function () { AbstractQuery.prototype.isUpsertQuery = function () {
return this.options.type === QueryTypes.UPSERT; return this.options.type === QueryTypes.UPSERT;
}; };
......
...@@ -17,6 +17,10 @@ module.exports = (function() { ...@@ -17,6 +17,10 @@ module.exports = (function() {
return 'SHOW TABLES'; return 'SHOW TABLES';
}, },
versionQuery: function() {
return 'SELECT VERSION() as `version`';
},
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
options = Utils._.extend({ options = Utils._.extend({
engine: 'InnoDB', engine: 'InnoDB',
......
...@@ -97,6 +97,8 @@ module.exports = (function() { ...@@ -97,6 +97,8 @@ module.exports = (function() {
result = data[0]; result = data[0];
} else if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery() || this.isUpsertQuery()) { } else if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery() || this.isUpsertQuery()) {
result = data.affectedRows; result = data.affectedRows;
} else if (this.isVersionQuery()) {
result = data[0].version;
} }
return result; return result;
......
...@@ -28,6 +28,10 @@ module.exports = (function() { ...@@ -28,6 +28,10 @@ module.exports = (function() {
return "SELECT schema_name FROM information_schema.schemata WHERE schema_name <> 'information_schema' AND schema_name != 'public' AND schema_name !~ E'^pg_';"; return "SELECT schema_name FROM information_schema.schemata WHERE schema_name <> 'information_schema' AND schema_name != 'public' AND schema_name !~ E'^pg_';";
}, },
versionQuery: function() {
return 'SELECT VERSION() as "version"';
},
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
var self = this; var self = this;
......
...@@ -234,6 +234,8 @@ module.exports = (function() { ...@@ -234,6 +234,8 @@ module.exports = (function() {
} }
return self.callee || (rows && rows[0]) || undefined; return self.callee || (rows && rows[0]) || undefined;
} else if (self.isVersionQuery()) {
return results[0].version;
} else { } else {
return results; return results;
} }
......
...@@ -28,6 +28,10 @@ module.exports = (function() { ...@@ -28,6 +28,10 @@ module.exports = (function() {
return "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"; return "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';";
}, },
versionQuery: function() {
return "SELECT sqlite_version() as `version`";
},
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
options = options || {}; options = options || {};
......
...@@ -137,6 +137,8 @@ module.exports = (function() { ...@@ -137,6 +137,8 @@ module.exports = (function() {
result = metaData.changes; result = metaData.changes;
} else if (self.options.type === QueryTypes.UPSERT) { } else if (self.options.type === QueryTypes.UPSERT) {
result = undefined; result = undefined;
} else if (self.options.type === QueryTypes.VERSION) {
result = results[0].version;
} }
resolve(result); resolve(result);
......
...@@ -61,6 +61,13 @@ module.exports = (function() { ...@@ -61,6 +61,13 @@ module.exports = (function() {
}); });
}; };
QueryInterface.prototype.databaseVersion = function() {
return this.sequelize.query(this.QueryGenerator.versionQuery(), null, {
raw: true,
type: QueryTypes.VERSION
});
};
QueryInterface.prototype.createTable = function(tableName, attributes, options) { QueryInterface.prototype.createTable = function(tableName, attributes, options) {
var keys = Object.keys(attributes) var keys = Object.keys(attributes)
, keyLen = keys.length , keyLen = keys.length
......
...@@ -6,4 +6,5 @@ module.exports = { ...@@ -6,4 +6,5 @@ module.exports = {
BULKUPDATE: 'BULKUPDATE', BULKUPDATE: 'BULKUPDATE',
BULKDELETE: 'BULKDELETE', BULKDELETE: 'BULKDELETE',
UPSERT: 'UPSERT', UPSERT: 'UPSERT',
VERSION: 'VERSION'
}; };
...@@ -787,6 +787,10 @@ module.exports = (function() { ...@@ -787,6 +787,10 @@ module.exports = (function() {
}); });
}; };
Sequelize.prototype.databaseVersion = function() {
return this.queryInterface.databaseVersion();
};
Sequelize.prototype.validate = Sequelize.prototype.authenticate; Sequelize.prototype.validate = Sequelize.prototype.authenticate;
/** /**
......
...@@ -1118,4 +1118,13 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -1118,4 +1118,13 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}) })
} }
}) })
})
describe('databaseVersion', function () {
it('should database/dialect version', function () {
return this.sequelize.databaseVersion().then(function (version) {
expect(typeof version).to.equal('string');
expect(version).to.be.ok;
});
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!