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

Commit e306d32c by Jack

Added convenience method and result fields when querying foreign keys in a Postg…

…res db. These fields provide the names of the referencing column and referenced table and column in the result similar to SQLite syntax.
1 parent cd853d11
Showing with 16 additions and 0 deletions
...@@ -119,6 +119,18 @@ module.exports = (function() { ...@@ -119,6 +119,18 @@ 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 \((.+){1}\) REFERENCES (.+){1}\((.+){1}\)/))) {
row.table = defParts[2];
row.from = defParts[1];
row.to = defParts[3];
}
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 +263,10 @@ module.exports = (function() { ...@@ -251,6 +263,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 this.sql.match(/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;/g);
}
Query.prototype.getInsertIdField = function() { Query.prototype.getInsertIdField = function() {
return 'id'; return 'id';
}; };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!