Skip to content

Annotate initial queries #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ module.exports = (function() {
// Build query to get a bunch of info from the information_schema
// It's not super important to understand it only that it returns the following fields:
// [Table, #, Column, Type, Null, Constraint, C, consrc, F Key, Default]
var query = "SELECT x.nspname || '.' || x.relname as \"Table\", x.attnum as \"#\", x.attname as \"Column\", x.\"Type\"," +
var query = "-- sails-postgresql.describe table information\n" +
"SELECT x.nspname || '.' || x.relname as \"Table\", x.attnum as \"#\", x.attname as \"Column\", x.\"Type\"," +
" case x.attnotnull when true then 'NOT NULL' else '' end as \"NULL\", r.conname as \"Constraint\", r.contype as \"C\", " +
"r.consrc, fn.nspname || '.' || f.relname as \"F Key\", d.adsrc as \"Default\" FROM (" +
"SELECT c.oid, a.attrelid, a.attnum, n.nspname, c.relname, a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod) as \"Type\", " +
Expand All @@ -164,13 +165,15 @@ module.exports = (function() {
"where x.relname = '" + table + "' order by 1,2;";

// Get Sequences to test if column auto-increments
var autoIncrementQuery = "SELECT t.relname as related_table, a.attname as related_column, s.relname as sequence_name " +
var autoIncrementQuery = "-- sails-postgresql.describe auto increment\n" +
"SELECT t.relname as related_table, a.attname as related_column, s.relname as sequence_name " +
"FROM pg_class s JOIN pg_depend d ON d.objid = s.oid JOIN pg_class t ON d.objid = s.oid AND d.refobjid = t.oid " +
"JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum) JOIN pg_namespace n ON n.oid = s.relnamespace " +
"WHERE s.relkind = 'S' AND n.nspname = 'public';";

// Get Indexes
var indiciesQuery = "SELECT n.nspname as \"Schema\", c.relname as \"Name\", CASE c.relkind WHEN 'r' THEN 'table' " +
var indicesQuery = "-- sails-postgresql.describe indices\n" +
"SELECT n.nspname as \"Schema\", c.relname as \"Name\", CASE c.relkind WHEN 'r' THEN 'table' " +
"WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN " +
"'foreign table' END as \"Type\", pg_catalog.pg_get_userbyid(c.relowner) as \"Owner\", c2.relname as \"Table\" " +
"FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " +
Expand Down Expand Up @@ -199,10 +202,10 @@ module.exports = (function() {
});

// Run Query to get Indexed values
client.query(indiciesQuery, function(err, iResult) {
client.query(indicesQuery, function(err, iResult) {
if(err) return cb(handleQueryError(err));

// Loop through indicies and see if any match
// Loop through indices and see if any match
iResult.rows.forEach(function(column) {
var key = column.Name.split('_index_')[1];

Expand Down