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

Commit a2d31971 by Jan Aagaard Meier

Merge pull request #2349 from 3ch01c/pg

Added convenience fields to Postgres foreign key query results
2 parents 1bb59106 2fcfc5bb
...@@ -119,6 +119,25 @@ module.exports = (function() { ...@@ -119,6 +119,25 @@ module.exports = (function() {
delete result.columns; delete result.columns;
}); });
return results; return results;
} else if (self.isForeignKeysQuery()) {
result = [];
rows.forEach(function(row) {
var defParts;
if (row.condef !== undefined && (defParts = row.condef.match(/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)( ON (UPDATE|DELETE) (CASCADE|RESTRICT))?( ON (UPDATE|DELETE) (CASCADE|RESTRICT))?/))) {
row.id = row.constraint_name;
row.table = defParts[2];
row.from = defParts[1];
row.to = defParts[3];
var i;
for (i=5;i<=8;i+=3) {
if (/(UPDATE|DELETE)/.test(defParts[i])) {
row['on_'+defParts[i].toLowerCase()] = defParts[i+1];
}
}
}
result.push(row);
});
return result;
} else if (self.isSelectQuery()) { } else if (self.isSelectQuery()) {
if (self.sql.toLowerCase().indexOf('select c.column_name') === 0) { if (self.sql.toLowerCase().indexOf('select c.column_name') === 0) {
result = {}; result = {};
...@@ -251,6 +270,10 @@ module.exports = (function() { ...@@ -251,6 +270,10 @@ module.exports = (function() {
return this.sql.indexOf('pg_get_indexdef') !== -1; return this.sql.indexOf('pg_get_indexdef') !== -1;
}; };
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);
};
Query.prototype.getInsertIdField = function() { Query.prototype.getInsertIdField = function() {
return 'id'; return 'id';
}; };
......
...@@ -249,4 +249,66 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -249,4 +249,66 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
}); });
}); });
}); });
describe('describeForeignKeys', function() {
beforeEach(function () {
return this.queryInterface.createTable('users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoincrement: true
},
}).bind(this).then(function () {
return this.queryInterface.createTable('hosts', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoincrement: true
},
admin: {
type: DataTypes.INTEGER,
references: 'users',
referenceKey: 'id',
},
operator: {
type: DataTypes.INTEGER,
references: 'users',
referenceKey: 'id',
onUpdate: 'cascade',
},
owner: {
type: DataTypes.INTEGER,
references: 'users',
referenceKey: 'id',
onUpdate: 'cascade',
onDelete: 'restrict'
},
})
})
})
it('should get a list of foreign keys for the table', function (done) {
this.sequelize.query(this.queryInterface.QueryGenerator.getForeignKeysQuery('hosts', this.sequelize.config.database)).complete(function(err, fks) {
expect(err).to.be.null
expect(fks).to.have.length(3)
var keys = Object.keys(fks[0]),
keys2 = Object.keys(fks[1]),
keys3 = Object.keys(fks[2])
if (dialect === "postgres" || dialect === "postgres-native") {
expect(keys).to.have.length(6)
expect(keys2).to.have.length(7)
expect(keys3).to.have.length(8)
} else if (dialect === "sqlite") {
expect(keys).to.have.length(8)
} else if (dialect === "mysql") {
expect(keys).to.have.length(1)
} else {
console.log("This test doesn't support " + dialect)
}
done()
})
})
})
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!