diff --git a/pgadmin/include/schema/pgColumn.h b/pgadmin/include/schema/pgColumn.h index fca8456..9630794 100644 --- a/pgadmin/include/schema/pgColumn.h +++ b/pgadmin/include/schema/pgColumn.h @@ -15,6 +15,8 @@ // App headers #include "pgTable.h" +WX_DECLARE_STRING_HASH_MAP(wxString, inheritHashMap); + class pgCollection; class pgColumnFactory : public pgTableObjFactory diff --git a/pgadmin/schema/pgColumn.cpp b/pgadmin/schema/pgColumn.cpp index b3796e2..c13b13e 100644 --- a/pgadmin/schema/pgColumn.cpp +++ b/pgadmin/schema/pgColumn.cpp @@ -546,17 +546,38 @@ pgObject *pgColumnFactory::CreateObjects(pgCollection *coll, ctlTree *browser, c pgColumn *column = 0; pgDatabase *database = collection->GetDatabase(); wxString sql; - int currentcol; - int currentlimit; - - // grab inherited tables - sql = wxT("SELECT inhparent::regclass AS inhrelname,\n") - wxT(" (SELECT count(*) FROM pg_attribute WHERE attrelid=inhparent AND attnum>0) AS colscount\n") - wxT(" FROM pg_inherits\n") - wxT(" WHERE inhrelid = ") + collection->GetOidStr() + wxT("::oid\n") - wxT(" ORDER BY inhseqno"); + inheritHashMap inhMap; + + // grab inherited tables with attibute names + sql = wxT(" SELECT inhparent::regclass AS inhrelname, a.attname AS attrname FROM pg_inherits i\n") + wxT(" LEFT JOIN pg_attribute a ON (attrelid = inhparent AND attnum > 0)\n") + wxT(" WHERE inhrelid = ") + collection->GetOidStr() + wxT("::oid\n") + wxT(" ORDER BY inhseqno"); pgSet *inhtables = database->ExecuteSet(sql); + if (inhtables) + { + while (!inhtables->Eof()) + { + wxString attrName = inhtables->GetVal(wxT("attrname")); + wxString inhrelName = inhtables->GetVal(wxT("inhrelname")); + + // Check attribute name is already exists in hash map + inheritHashMap::iterator it = inhMap.find(attrName); + if ( it != inhMap.end()) + { + wxString &relName = it->second; + relName += wxT(", ") + inhrelName; + } + else + inhMap[attrName] = inhrelName; + + inhtables->MoveNext(); + } + + delete inhtables; + } + wxString systemRestriction; if (!settings->GetShowSystemObjects()) systemRestriction = wxT("\n AND att.attnum > 0"); @@ -604,17 +625,10 @@ pgObject *pgColumnFactory::CreateObjects(pgCollection *coll, ctlTree *browser, c pgSet *columns = database->ExecuteSet(sql); if (columns) { - currentcol = 0; - if (inhtables && !inhtables->Eof()) - { - currentlimit = inhtables->GetLong(wxT("colscount")); - } while (!columns->Eof()) { - if (columns->GetLong(wxT("attnum")) > 0) // ignore system columns before inherited columns - currentcol++; - - column = new pgColumn(collection->GetTable(), columns->GetVal(wxT("attname"))); + wxString attrName = columns->GetVal(wxT("attname")); + column = new pgColumn(collection->GetTable(), attrName); column->iSetAttTypId(columns->GetOid(wxT("atttypid"))); column->iSetColNumber(columns->GetLong(wxT("attnum"))); @@ -665,18 +679,10 @@ pgObject *pgColumnFactory::CreateObjects(pgCollection *coll, ctlTree *browser, c column->iSetTableName(columns->GetVal(wxT("relname"))); column->iSetInheritedCount(columns->GetLong(wxT("attinhcount"))); - if (inhtables) - { - if (currentcol > currentlimit) - { - inhtables->MoveNext(); - if (!inhtables->Eof()) - currentlimit += inhtables->GetLong(wxT("colscount")); - } - - if (!inhtables->Eof()) - column->iSetInheritedTableName(inhtables->GetVal(wxT("inhrelname"))); - } + // Check whether the attribute is inherited + inheritHashMap::iterator it = inhMap.find(attrName); + if (it != inhMap.end()) + column->iSetInheritedTableName(it->second); column->iSetIsLocal(columns->GetBool(wxT("attislocal"))); column->iSetAttstattarget(columns->GetLong(wxT("attstattarget"))); @@ -711,9 +717,10 @@ pgObject *pgColumnFactory::CreateObjects(pgCollection *coll, ctlTree *browser, c break; } - delete inhtables; delete columns; } + + inhMap.clear(); return column; }