Skip to content
Permalink
Browse files

Partial fix for ticket #277

git-svn-id: http://svn.osgeo.org/qgis/trunk@6266 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
g_j_m
g_j_m committed Dec 16, 2006
1 parent d96ece9 commit e0249e3c19a0d3512293adae30cdb1b8deea4a77
Showing with 43 additions and 0 deletions.
  1. +43 −0 src/providers/postgres/qgspostgresprovider.cpp
@@ -1401,6 +1401,49 @@ void QgsPostgresProvider::findColumns(tableCols& cols)
temp.table_type = PQgetvalue(result, i, 6);
temp.column_type = PQgetvalue(result, i, 7);

// BUT, the above SQL doesn't always give the correct value for the view
// column name (that's because that information isn't available directly
// from the database), mainly when the view column name has been renamed
// using 'AS'. To fix this we need to look in the view definition and
// adjust the view column name if necessary.

QString view = "SELECT definition FROM pg_views WHERE schemaname = '" + temp.view_schema + "' AND "
" viewname = '" + temp.view_name + "'";

PGresult* r = PQexec(connection, (const char*)(view.utf8()));
if (PQntuples(r) > 0)
{
QString viewDef = PQgetvalue(r, 0, 0);
// Now pick the view definiton apart, looking for
// temp.column_name to the left of an 'AS'.

// This regular expression needs more work and testing. Since the view
// definition comes from postgresql and has been 'standardised', we
// don't need to deal with everything that the user could put in a view
// definition. Possible variations could include:
// - lowercase AS
// - quotes around table and column names
// - inclusion of the scheam
// - meaningful characters in the schema/table/column names (such as . ' " )
// - anything else???
QRegExp s(".* " + temp.table_name + "\\." + temp.column_name + " AS (\\w+),* .*");

#ifdef QGSIDEBUG
std::cerr <<__FILE__<<__LINE__ << ' ' << view.toLocal8Bit().data() << '\n'
<< viewDef.toLocal8Bit().data() << '\n'
<< s.pattern().toLocal8Bit().data() << '\n';
#endif

if (s.indexIn(viewDef) != -1)
{
temp.view_column_name = s.cap(1);
//std::cerr<<__FILE__<<__LINE__<<' '<<temp.view_column_name.toLocal8Bit().data()<<'\n';
}
}
else
QgsDebugMsg("Failed to get view definition for " + temp.view_schema + "." + temp.view_name);


QgsDebugMsg(temp.view_schema + "."
+ temp.view_name + "."
+ temp.view_column_name + " <- "

0 comments on commit e0249e3

Please sign in to comment.
You can’t perform that action at this time.