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

Commit 7733bd7d by Mick Hansen

Merge pull request #2931 from mbroadst/refactor-select-indexes

refactor show indexes query type
2 parents 96d54225 f8103bfd
...@@ -243,7 +243,7 @@ module.exports = (function() { ...@@ -243,7 +243,7 @@ module.exports = (function() {
// $ inside value strings are illegal when using $$ as literal strings/delimiters for function bodys // $ inside value strings are illegal when using $$ as literal strings/delimiters for function bodys
value = value.replace(/\$/g, '\\$'); value = value.replace(/\$/g, '\\$');
} }
values.push(value); values.push(value);
} }
} }
} }
...@@ -524,15 +524,15 @@ module.exports = (function() { ...@@ -524,15 +524,15 @@ module.exports = (function() {
}, },
/* /*
Returns an show index query. Returns a query listing indexes for a given table.
Parameters: Parameters:
- tableName: Name of an existing table. - tableName: Name of an existing table.
- options: - options:
- database: Name of the database. - database: Name of the database.
*/ */
/* istanbul ignore next */ /* istanbul ignore next */
showIndexQuery: function(tableName, options) { showIndexesQuery: function(tableName, options) {
throwMethodUndefined('showIndexQuery'); throwMethodUndefined('showIndexesQuery');
}, },
/* /*
......
...@@ -133,6 +133,10 @@ module.exports = (function() { ...@@ -133,6 +133,10 @@ module.exports = (function() {
})); }));
}; };
AbstractQuery.prototype.isShowIndexesQuery = function () {
return this.options.type === QueryTypes.SHOWINDEXES;
};
AbstractQuery.prototype.isSelectQuery = function() { AbstractQuery.prototype.isSelectQuery = function() {
return this.options.type === QueryTypes.SELECT; return this.options.type === QueryTypes.SELECT;
}; };
......
...@@ -293,7 +293,7 @@ module.exports = (function() { ...@@ -293,7 +293,7 @@ module.exports = (function() {
}); });
}, },
showIndexQuery: function(tableName, options) { showIndexesQuery: function(tableName, options) {
// FIXME: temporary until I implement proper schema support // FIXME: temporary until I implement proper schema support
var dequotedTableName = tableName.toString().replace(/['"]+/g, ''); var dequotedTableName = tableName.toString().replace(/['"]+/g, '');
var sql = "EXEC sys.sp_helpindex @objname = N'[<%= tableName %>]';"; var sql = "EXEC sys.sp_helpindex @objname = N'[<%= tableName %>]';";
......
...@@ -226,7 +226,7 @@ module.exports = (function() { ...@@ -226,7 +226,7 @@ module.exports = (function() {
return 'DELETE FROM ' + table + ' WHERE ' + where + limit; return 'DELETE FROM ' + table + ' WHERE ' + where + limit;
}, },
showIndexQuery: function(tableName, options) { showIndexesQuery: function(tableName, options) {
var sql = 'SHOW INDEX FROM <%= tableName %><%= options %>'; var sql = 'SHOW INDEX FROM <%= tableName %><%= options %>';
return Utils._.template(sql)({ return Utils._.template(sql)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
......
...@@ -160,10 +160,6 @@ module.exports = (function() { ...@@ -160,10 +160,6 @@ module.exports = (function() {
} }
}; };
Query.prototype.isShowIndexesQuery = function () {
return this.sql.toLowerCase().indexOf('show index from') === 0;
};
Query.prototype.handleShowIndexesQuery = function (data) { Query.prototype.handleShowIndexesQuery = function (data) {
// Group by index name, and collect all fields // Group by index name, and collect all fields
data = Utils._.foldl(data, function (acc, item) { data = Utils._.foldl(data, function (acc, item) {
......
...@@ -447,7 +447,7 @@ module.exports = (function() { ...@@ -447,7 +447,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements); return Utils._.template(query)(replacements);
}, },
showIndexQuery: function(tableName, options) { showIndexesQuery: function(tableName, options) {
// This is ARCANE! // This is ARCANE!
var query = "SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) AND t.relkind = 'r' and t.relname = '<%= tableName %>' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;"; var query = "SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND a.attnum = ANY(ix.indkey) AND t.relkind = 'r' and t.relname = '<%= tableName %>' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;";
......
...@@ -310,10 +310,6 @@ module.exports = (function() { ...@@ -310,10 +310,6 @@ module.exports = (function() {
} }
}; };
Query.prototype.isShowIndexesQuery = function () {
return this.sql.indexOf('pg_get_indexdef') !== -1;
};
Query.prototype.isForeignKeysQuery = function() { Query.prototype.isForeignKeysQuery = function() {
return /SELECT conname as constraint_name, pg_catalog\.pg_get_constraintdef\(r\.oid, true\) as condef FROM pg_catalog\.pg_constraint r WHERE r\.conrelid = \(SELECT oid FROM pg_class WHERE relname = '.*' LIMIT 1\) AND r\.contype = 'f' ORDER BY 1;/.test(this.sql); return /SELECT conname as constraint_name, pg_catalog\.pg_get_constraintdef\(r\.oid, true\) as condef FROM pg_catalog\.pg_constraint r WHERE r\.conrelid = \(SELECT oid FROM pg_class WHERE relname = '.*' LIMIT 1\) AND r\.contype = 'f' ORDER BY 1;/.test(this.sql);
}; };
......
...@@ -323,7 +323,7 @@ module.exports = (function() { ...@@ -323,7 +323,7 @@ module.exports = (function() {
return fields; return fields;
}, },
showIndexQuery: function(tableName) { showIndexesQuery: function(tableName) {
var sql = "PRAGMA INDEX_LIST(<%= tableName %>)"; var sql = "PRAGMA INDEX_LIST(<%= tableName %>)";
return Utils._.template(sql, { tableName: this.quoteTable(tableName) }); return Utils._.template(sql, { tableName: this.quoteTable(tableName) });
}, },
......
...@@ -420,9 +420,12 @@ module.exports = (function() { ...@@ -420,9 +420,12 @@ module.exports = (function() {
}; };
QueryInterface.prototype.showIndex = function(tableName, options) { QueryInterface.prototype.showIndex = function(tableName, options) {
var sql = this.QueryGenerator.showIndexQuery(tableName, options); var sql = this.QueryGenerator.showIndexesQuery(tableName, options);
options = options || {}; options = options || {};
return this.sequelize.query(sql, null, {logging: options.hasOwnProperty('logging') ? options.logging : this.sequelize.options.logging}); return this.sequelize.query(sql, null, {
logging: options.hasOwnProperty('logging') ? options.logging : this.sequelize.options.logging,
type: QueryTypes.SHOWINDEXES
});
}; };
QueryInterface.prototype.nameIndexes = function(indexes, rawTablename) { QueryInterface.prototype.nameIndexes = function(indexes, rawTablename) {
......
...@@ -7,5 +7,6 @@ module.exports = { ...@@ -7,5 +7,6 @@ module.exports = {
BULKDELETE: 'BULKDELETE', BULKDELETE: 'BULKDELETE',
UPSERT: 'UPSERT', UPSERT: 'UPSERT',
VERSION: 'VERSION', VERSION: 'VERSION',
SHOWTABLES: 'SHOWTABLES' SHOWTABLES: 'SHOWTABLES',
SHOWINDEXES: 'SHOWINDEXES'
}; };
...@@ -543,7 +543,7 @@ if (Support.dialectIsMySQL()) { ...@@ -543,7 +543,7 @@ if (Support.dialectIsMySQL()) {
} }
], ],
showIndexQuery: [ showIndexesQuery: [
{ {
arguments: ['User'], arguments: ['User'],
expectation: 'SHOW INDEX FROM `User`' expectation: 'SHOW INDEX FROM `User`'
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!