Skip to content

Commit

Permalink
real fix for #843
Browse files Browse the repository at this point in the history
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/qgis@7711 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 4, 2007
1 parent 7980fed commit 254712a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
14 changes: 9 additions & 5 deletions src/core/qgsdatasourceuri.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsdatasourceuri.h
Expand Up @@ -68,6 +68,7 @@ class CORE_EXPORT QgsDataSourceURI
QString sql() const;
QString geometryColumn() const;

void clearSchema();
void setSql(QString sql);

private:
Expand Down
11 changes: 1 addition & 10 deletions src/plugins/grass/qgsgrassmodule.cpp
Expand Up @@ -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() + ".";
}

Expand Down
11 changes: 11 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -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
*/
Expand Down

0 comments on commit 254712a

Please sign in to comment.