Skip to content

Commit 2e0d652

Browse files
author
jef
committed
speed up loading of projects with (complex) views:
save key column of postgresql layers to projects file and try it first on reload. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10657 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 8cddba4 commit 2e0d652

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

python/core/qgsdatasourceuri.sip

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public:
4343
void setDataSource(const QString& aSchema,
4444
const QString& aTable,
4545
const QString& aGeometryColumn,
46-
const QString& aSql = QString());
46+
const QString& aSql = QString(),
47+
const QString& aKeyColumn = QString());
4748

4849
/** Removes password from uris
4950
* @note this method was added in QGIS 1.1
@@ -64,4 +65,8 @@ public:
6465
SSLmode sslMode() const;
6566

6667
void setSql(QString sql);
68+
69+
// added in 1.2
70+
QString keyColumn() const;
71+
void setKeyColumn(QString column);
6772
};

src/core/qgsdatasourceuri.cpp

+20-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
#include <QStringList>
2424
#include <QRegExp>
2525

26-
QgsDataSourceURI::QgsDataSourceURI() : mSSLmode( SSLprefer )
26+
QgsDataSourceURI::QgsDataSourceURI() : mSSLmode( SSLprefer ), mKeyColumn( "" )
2727
{
2828
// do nothing
2929
}
3030

31-
QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer )
31+
QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer ), mKeyColumn( "" )
3232
{
3333
int i = 0;
3434
while ( i < uri.length() )
@@ -104,6 +104,10 @@ QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer )
104104
i++;
105105
}
106106
}
107+
else if ( pname == "key" )
108+
{
109+
mKeyColumn = pval;
110+
}
107111
else if ( pname == "service" )
108112
{
109113
QgsDebugMsg( "service keyword ignored" );
@@ -257,6 +261,16 @@ QString QgsDataSourceURI::geometryColumn() const
257261
return mGeometryColumn;
258262
}
259263

264+
QString QgsDataSourceURI::keyColumn() const
265+
{
266+
return mKeyColumn;
267+
}
268+
269+
void QgsDataSourceURI::setKeyColumn( QString column )
270+
{
271+
mKeyColumn = column;
272+
}
273+
260274
void QgsDataSourceURI::setSql( QString sql )
261275
{
262276
mSql = sql;
@@ -376,7 +390,8 @@ QString QgsDataSourceURI::connectionInfo() const
376390
QString QgsDataSourceURI::uri() const
377391
{
378392
return connectionInfo()
379-
+ QString( " table=%1 (%2) sql=%3" )
393+
+ QString( " key='%1' table=%2 (%3) sql=%4" )
394+
.arg( keyColumn() )
380395
.arg( quotedTablename() )
381396
.arg( mGeometryColumn )
382397
.arg( mSql );
@@ -408,10 +423,12 @@ void QgsDataSourceURI::setConnection( const QString &host,
408423
void QgsDataSourceURI::setDataSource( const QString &schema,
409424
const QString &table,
410425
const QString &geometryColumn,
426+
const QString &keyColumn,
411427
const QString &sql )
412428
{
413429
mSchema = schema;
414430
mTable = table;
415431
mGeometryColumn = geometryColumn;
432+
mKeyColumn = keyColumn;
416433
mSql = sql;
417434
}

src/core/qgsdatasourceuri.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class CORE_EXPORT QgsDataSourceURI
6161
void setDataSource( const QString& aSchema,
6262
const QString& aTable,
6363
const QString& aGeometryColumn,
64-
const QString& aSql = QString() );
64+
const QString& aSql = QString(),
65+
const QString& aKeyColumn = QString() );
6566

6667
//! Removes password element from uris
6768
static QString removePassword( const QString& aUri );
@@ -82,6 +83,10 @@ class CORE_EXPORT QgsDataSourceURI
8283
QString password() const;
8384
enum SSLmode sslMode() const;
8485

86+
// added in version 1.2
87+
QString keyColumn() const;
88+
void setKeyColumn( QString column );
89+
8590
private:
8691
void skipBlanks( const QString &uri, int &i );
8792
QString getValue( const QString &uri, int &i );
@@ -108,6 +113,8 @@ class CORE_EXPORT QgsDataSourceURI
108113
QString mPassword;
109114
//! ssl mode
110115
enum SSLmode mSSLmode;
116+
//! key column
117+
QString mKeyColumn;
111118
};
112119

113120
#endif //QGSDATASOURCEURI_H

src/providers/postgres/qgspostgresprovider.cpp

+40-14
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
7474
mTableName = mUri.table();
7575
geometryColumn = mUri.geometryColumn();
7676
sqlWhereClause = mUri.sql();
77+
primaryKey = mUri.keyColumn();
7778

7879
// Keep a schema qualified table name for convenience later on.
7980
mSchemaTableName = mUri.quotedTablename();
@@ -483,15 +484,15 @@ bool QgsPostgresProvider::getFeature( PGresult *queryResult, int row, bool fetch
483484

484485
if ( block > 0xffff )
485486
{
486-
qWarning( "block number %x exceed 16 bit", block );
487+
QgsDebugMsg( QString( "block number %1 exceeds 16 bit" ).arg( block ) );
487488
return false;
488489
}
489490

490491
oid = ( block << 16 ) + offset;
491492
}
492493
else
493494
{
494-
qWarning( "expecting 6 bytes for tid (found %d bytes)", PQgetlength( queryResult, row, 0 ) );
495+
QgsDebugMsg( QString( "expecting 6 bytes for tid (found %1 bytes)" ).arg( PQgetlength( queryResult, row, 0 ) ) );
495496
return false;
496497
}
497498

@@ -623,7 +624,7 @@ bool QgsPostgresProvider::nextFeature( QgsFeature& feature )
623624
QString fetch = QString( "fetch forward %1 from %2" ).arg( mFeatureQueueSize ).arg( cursorName );
624625
if ( connectionRO->PQsendQuery( fetch ) == 0 ) // fetch features asynchronously
625626
{
626-
qWarning( "PQsendQuery failed (1)" );
627+
QgsDebugMsg( "PQsendQuery failed (1)" );
627628
}
628629

629630
Result queryResult;
@@ -948,12 +949,12 @@ QString QgsPostgresProvider::getPrimaryKey()
948949
Result tableType = connectionRO->PQexec( sql );
949950
QString type = QString::fromUtf8( PQgetvalue( tableType, 0, 0 ) );
950951

951-
primaryKey = "";
952-
953952
if ( type == "r" ) // the relation is a table
954953
{
955954
QgsDebugMsg( "Relation is a table. Checking to see if it has an oid column." );
956955

956+
primaryKey = "";
957+
957958
// If there is an oid on the table, use that instead,
958959
// otherwise give up
959960
sql = QString( "SELECT attname FROM pg_attribute WHERE attname='oid' AND attrelid=regclass(%1)" )
@@ -1008,15 +1009,40 @@ QString QgsPostgresProvider::getPrimaryKey()
10081009
}
10091010
else if ( type == "v" ) // the relation is a view
10101011
{
1011-
// Have a poke around the view to see if any of the columns
1012-
// could be used as the primary key.
1013-
tableCols cols;
1014-
// Given a schema.view, populate the cols variable with the
1015-
// schema.table.column's that underly the view columns.
1016-
findColumns( cols );
1017-
// From the view columns, choose one for which the underlying
1018-
// column is suitable for use as a key into the view.
1019-
primaryKey = chooseViewColumn( cols );
1012+
if ( !primaryKey.isEmpty() )
1013+
{
1014+
// check last used candidate
1015+
sql = QString( "select pg_type.typname from pg_attribute,pg_type where atttypid=pg_type.oid and attname=%1 and attrelid=regclass(%2)" )
1016+
.arg( quotedValue( primaryKey ) ).arg( quotedValue( mSchemaTableName ) );
1017+
1018+
QgsDebugMsg( "checking candidate: " + sql );
1019+
1020+
Result result = connectionRO->PQexec( sql );
1021+
1022+
if ( PQresultStatus( result ) != PGRES_TUPLES_OK ||
1023+
PQntuples( result ) != 1 ||
1024+
QString( PQgetvalue( result, 0, 0 ) ) != "int4" ||
1025+
!uniqueData( mSchemaName, mTableName, primaryKey ) )
1026+
{
1027+
primaryKey = "";
1028+
}
1029+
}
1030+
1031+
if ( primaryKey.isEmpty() )
1032+
{
1033+
// Have a poke around the view to see if any of the columns
1034+
// could be used as the primary key.
1035+
tableCols cols;
1036+
// Given a schema.view, populate the cols variable with the
1037+
// schema.table.column's that underly the view columns.
1038+
findColumns( cols );
1039+
// From the view columns, choose one for which the underlying
1040+
// column is suitable for use as a key into the view.
1041+
primaryKey = chooseViewColumn( cols );
1042+
1043+
mUri.setKeyColumn( primaryKey );
1044+
setDataSourceUri( mUri.uri() );
1045+
}
10201046
}
10211047
else
10221048
QgsDebugMsg( "Unexpected relation type of '" + type + "'." );

0 commit comments

Comments
 (0)