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
This commit is contained in:
jef 2007-12-04 09:00:22 +00:00
parent 7af34268ae
commit 5b4e6997dd
5 changed files with 26 additions and 15 deletions

View File

@ -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

View File

@ -68,6 +68,7 @@ public:
QString sql() const;
QString geometryColumn() const;
void clearSchema();
void setSql(QString sql);
private:

View File

@ -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() + ".";
}

View File

@ -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

View File

@ -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
*/