@@ -1159,8 +1159,9 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
11591159 " nt.nspname AS table_schema, "
11601160 " t.relname AS table_name, "
11611161 " a.attname AS column_name, "
1162- " t.relkind as table_type, "
1163- " typ.typname as column_type "
1162+ " t.relkind AS table_type, "
1163+ " typ.typname AS column_type, "
1164+ " vs.definition AS view_definition "
11641165 " FROM "
11651166 " pg_namespace nv, "
11661167 " pg_class v, "
@@ -1170,7 +1171,8 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
11701171 " pg_namespace nt, "
11711172 " pg_attribute a,"
11721173 " pg_user u, "
1173- " pg_type typ "
1174+ " pg_type typ, "
1175+ " pg_views vs "
11741176 " WHERE "
11751177 " nv.oid = v.relnamespace AND "
11761178 " v.relkind = 'v'::\" char\" AND "
@@ -1188,7 +1190,9 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
11881190 " t.oid = a.attrelid AND "
11891191 " dt.refobjsubid = a.attnum AND "
11901192 " nv.nspname NOT IN ('pg_catalog', 'information_schema' ) AND "
1191- " a.atttypid = typ.oid" ;
1193+ " a.atttypid = typ.oid AND "
1194+ " nv.nspname = vs.schemaname AND "
1195+ " v.relname = vs.viewname" ;
11921196
11931197 // A structure to store the results of the above sql.
11941198 typedef std::map<QString, TT> columnRelationsType;
@@ -1213,41 +1217,24 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
12131217 temp.column_name = PQgetvalue (result, i, 5 );
12141218 temp.table_type = PQgetvalue (result, i, 6 );
12151219 temp.column_type = PQgetvalue (result, i, 7 );
1220+ QString viewDef = PQgetvalue (result, i, 8 );
12161221
12171222 // BUT, the above SQL doesn't always give the correct value for the view
12181223 // column name (that's because that information isn't available directly
12191224 // from the database), mainly when the view column name has been renamed
12201225 // using 'AS'. To fix this we need to look in the view definition and
12211226 // adjust the view column name if necessary.
12221227
1223-
1224- QString viewQuery = " SELECT definition FROM pg_views "
1225- " WHERE schemaname = '" + temp.view_schema + " ' AND "
1226- " viewname = '" + temp.view_name + " '" ;
1227-
1228- // Maintain a cache of the above SQL.
1229- QString viewDef;
1230- if (!viewDefs.contains (viewQuery))
1231- {
1232- PGresult* r = PQexec (connection, (const char *)(viewQuery.utf8 ()));
1233- if (PQntuples (r) > 0 )
1234- viewDef = PQgetvalue (r, 0 , 0 );
1235- else
1236- QgsDebugMsg (" Failed to get view definition for " + temp.view_schema + " ." + temp.view_name );
1237- viewDefs[viewQuery] = viewDef;
1238- }
1239-
1240- viewDef = viewDefs.value (viewQuery);
1241-
12421228 // Now pick the view definiton apart, looking for
12431229 // temp.column_name to the left of an 'AS'.
12441230
1245- // This regular expression needs more testing. Since the view
1246- // definition comes from postgresql and has been 'standardised', we
1247- // don't need to deal with everything that the user could put in a view
1248- // definition. Does the regexp have to deal with the schema??
12491231 if (!viewDef.isEmpty ())
12501232 {
1233+ // This regular expression needs more testing. Since the view
1234+ // definition comes from postgresql and has been 'standardised', we
1235+ // don't need to deal with everything that the user could put in a view
1236+ // definition. Does the regexp have to deal with the schema??
1237+
12511238 QRegExp s (" .* \" ?" + QRegExp::escape (temp.table_name ) +
12521239 " \" ?\\ .\" ?" + QRegExp::escape (temp.column_name ) +
12531240 " \" ? AS \" ?(\\ w+)\" ?,* .*" );
@@ -1257,7 +1244,6 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
12571244 if (s.indexIn (viewDef) != -1 )
12581245 {
12591246 temp.view_column_name = s.cap (1 );
1260- // std::cerr<<__FILE__<<__LINE__<<' '<<temp.view_column_name.toLocal8Bit().data()<<'\n';
12611247 }
12621248 }
12631249
0 commit comments