Skip to content

Commit 5b4e699

Browse files
author
jef
committedDec 4, 2007
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
1 parent 7af3426 commit 5b4e699

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed
 

‎src/core/qgsdatasourceuri.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ void QgsDataSourceURI::setSql(QString sql)
156156
mSql = sql;
157157
}
158158

159+
void QgsDataSourceURI::clearSchema()
160+
{
161+
mSchema = "";
162+
}
163+
159164
void QgsDataSourceURI::skipBlanks(const QString &uri, int &i)
160165
{
161166
// skip space before value
@@ -246,11 +251,10 @@ QString QgsDataSourceURI::connInfo() const
246251
QString QgsDataSourceURI::uri() const
247252
{
248253
return connInfo()
249-
+ QString(" table=\"%1\".\"%2\" (%3) sql=%4")
250-
.arg(mSchema)
251-
.arg(mTable)
252-
.arg(mGeometryColumn)
253-
.arg(mSql);
254+
+ QString(" table=%1 (%2) sql=%3")
255+
.arg( quotedTablename() )
256+
.arg( mGeometryColumn )
257+
.arg( mSql );
254258
}
255259

256260
QString QgsDataSourceURI::quotedTablename() const

‎src/core/qgsdatasourceuri.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class CORE_EXPORT QgsDataSourceURI
6868
QString sql() const;
6969
QString geometryColumn() const;
7070

71+
void clearSchema();
7172
void setSql(QString sql);
7273

7374
private:

‎src/plugins/grass/qgsgrassmodule.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,16 +2581,7 @@ void QgsGrassModuleGdalInput::updateQgisLayers()
25812581
QgsDataSourceURI dsUri(provider->dataSourceUri());
25822582
uri = "PG:" + dsUri.connInfo();
25832583

2584-
// FIXME:
2585-
// GDAL prepends the schema only to tables that are not in the
2586-
// current schema. The default schema is the user schema, if it
2587-
// exists or public otherwise.
2588-
// So we need to query current_schema() here like GDAL does (see
2589-
// OGRPGTableLayer::ReadTableDefinition). But do we want a static
2590-
// PostgreSQL depencency here?
2591-
// This workaround makes public tables inaccessible, if a
2592-
// user schema exists.
2593-
if( dsUri.schema()!="public" && dsUri.schema()!=dsUri.username() ) {
2584+
if( dsUri.schema()!="" ) {
25942585
ogrLayer = dsUri.schema() + ".";
25952586
}
25962587

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ const QString POSTGRES_DESCRIPTION = "PostgreSQL/PostGIS data provider";
126126
}
127127
PQclear(testAccess);
128128

129+
PGresult *schema = PQexec(connection, "SELECT current_schema()");
130+
if (PQresultStatus(schema) == PGRES_TUPLES_OK)
131+
{
132+
mCurrentSchema = PQgetvalue(schema, 0, 0);
133+
if(mCurrentSchema==mSchemaName) {
134+
mUri.clearSchema();
135+
setDataSourceUri( mUri.uri() );
136+
}
137+
}
138+
PQclear(schema);
139+
129140
if (!getGeometryDetails()) // gets srid and geometry type
130141
{
131142
// the table is not a geometry table

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ class QgsPostgresProvider:public QgsVectorDataProvider
369369
* Name of the schema
370370
*/
371371
QString mSchemaName;
372+
/**
373+
* Name of the current schema
374+
*/
375+
QString mCurrentSchema;
372376
/**
373377
* SQL statement used to limit the features retreived
374378
*/

0 commit comments

Comments
 (0)
Please sign in to comment.