From 5b4e6997ddcb36aa4facce9ee17c86e9ad14cde0 Mon Sep 17 00:00:00 2001 From: jef Date: Tue, 4 Dec 2007 09:00:22 +0000 Subject: [PATCH] real fix for #843 determine the current schema in the postgresprovider and clear the schema name of the table, if it's identical. That way we only qualify tables, if we need to (like GDAL does). Please test! git-svn-id: http://svn.osgeo.org/qgis/trunk@7711 c8812cc2-4d05-0410-92ff-de0c093fc19c --- src/core/qgsdatasourceuri.cpp | 14 +++++++++----- src/core/qgsdatasourceuri.h | 1 + src/plugins/grass/qgsgrassmodule.cpp | 11 +---------- src/providers/postgres/qgspostgresprovider.cpp | 11 +++++++++++ src/providers/postgres/qgspostgresprovider.h | 4 ++++ 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/core/qgsdatasourceuri.cpp b/src/core/qgsdatasourceuri.cpp index bc1b940a0d6..7bfe20fe400 100644 --- a/src/core/qgsdatasourceuri.cpp +++ b/src/core/qgsdatasourceuri.cpp @@ -156,6 +156,11 @@ void QgsDataSourceURI::setSql(QString sql) mSql = sql; } +void QgsDataSourceURI::clearSchema() +{ + mSchema = ""; +} + void QgsDataSourceURI::skipBlanks(const QString &uri, int &i) { // skip space before value @@ -246,11 +251,10 @@ QString QgsDataSourceURI::connInfo() const QString QgsDataSourceURI::uri() const { return connInfo() - + QString(" table=\"%1\".\"%2\" (%3) sql=%4") - .arg(mSchema) - .arg(mTable) - .arg(mGeometryColumn) - .arg(mSql); + + QString(" table=%1 (%2) sql=%3") + .arg( quotedTablename() ) + .arg( mGeometryColumn ) + .arg( mSql ); } QString QgsDataSourceURI::quotedTablename() const diff --git a/src/core/qgsdatasourceuri.h b/src/core/qgsdatasourceuri.h index 0770bfe11b5..b662f15eab3 100644 --- a/src/core/qgsdatasourceuri.h +++ b/src/core/qgsdatasourceuri.h @@ -68,6 +68,7 @@ public: QString sql() const; QString geometryColumn() const; + void clearSchema(); void setSql(QString sql); private: diff --git a/src/plugins/grass/qgsgrassmodule.cpp b/src/plugins/grass/qgsgrassmodule.cpp index e363d90ac72..bbfa214fa22 100644 --- a/src/plugins/grass/qgsgrassmodule.cpp +++ b/src/plugins/grass/qgsgrassmodule.cpp @@ -2581,16 +2581,7 @@ void QgsGrassModuleGdalInput::updateQgisLayers() QgsDataSourceURI dsUri(provider->dataSourceUri()); uri = "PG:" + dsUri.connInfo(); - // FIXME: - // GDAL prepends the schema only to tables that are not in the - // current schema. The default schema is the user schema, if it - // exists or public otherwise. - // So we need to query current_schema() here like GDAL does (see - // OGRPGTableLayer::ReadTableDefinition). But do we want a static - // PostgreSQL depencency here? - // This workaround makes public tables inaccessible, if a - // user schema exists. - if( dsUri.schema()!="public" && dsUri.schema()!=dsUri.username() ) { + if( dsUri.schema()!="" ) { ogrLayer = dsUri.schema() + "."; } diff --git a/src/providers/postgres/qgspostgresprovider.cpp b/src/providers/postgres/qgspostgresprovider.cpp index 3fc9cb9ae6b..7c8631033ff 100644 --- a/src/providers/postgres/qgspostgresprovider.cpp +++ b/src/providers/postgres/qgspostgresprovider.cpp @@ -126,6 +126,17 @@ const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider"; } PQclear(testAccess); + PGresult *schema = PQexec(connection, "SELECT current_schema()"); + if (PQresultStatus(schema) == PGRES_TUPLES_OK) + { + mCurrentSchema = PQgetvalue(schema, 0, 0); + if(mCurrentSchema==mSchemaName) { + mUri.clearSchema(); + setDataSourceUri( mUri.uri() ); + } + } + PQclear(schema); + if (!getGeometryDetails()) // gets srid and geometry type { // the table is not a geometry table diff --git a/src/providers/postgres/qgspostgresprovider.h b/src/providers/postgres/qgspostgresprovider.h index d1b4c98f230..bec111514bc 100644 --- a/src/providers/postgres/qgspostgresprovider.h +++ b/src/providers/postgres/qgspostgresprovider.h @@ -369,6 +369,10 @@ class QgsPostgresProvider:public QgsVectorDataProvider * Name of the schema */ QString mSchemaName; + /** + * Name of the current schema + */ + QString mCurrentSchema; /** * SQL statement used to limit the features retreived */