Skip to content

Commit ca47ccc

Browse files
author
jef
committedDec 14, 2009
better fix for slowdown of r12418
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12450 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

2 files changed

+74
-43
lines changed

2 files changed

+74
-43
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
5555
: QgsVectorDataProvider( uri ),
5656
mFetching( false ),
5757
geomType( QGis::WKBUnknown ),
58-
mFeatureQueueSize( 200 )
58+
mFeatureQueueSize( 200 ),
59+
mPrimaryKeyDefault( QString::null )
5960
{
6061
// assume this is a valid layer until we determine otherwise
6162
valid = true;
@@ -1064,13 +1065,13 @@ QString QgsPostgresProvider::getPrimaryKey()
10641065
}
10651066
else
10661067
{
1067-
primaryKeyDefault = defaultValue( primaryKey ).toString();
1068-
if ( primaryKeyDefault.isNull() )
1068+
mPrimaryKeyDefault = defaultValue( primaryKey ).toString();
1069+
if ( mPrimaryKeyDefault.isNull() )
10691070
{
1070-
primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
1071-
.arg( quotedIdentifier( primaryKey ) )
1072-
.arg( quotedIdentifier( mSchemaName ) )
1073-
.arg( quotedIdentifier( mTableName ) );
1071+
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
1072+
.arg( quotedIdentifier( primaryKey ) )
1073+
.arg( quotedIdentifier( mSchemaName ) )
1074+
.arg( quotedIdentifier( mTableName ) );
10741075
}
10751076
}
10761077
}
@@ -1093,39 +1094,18 @@ QString QgsPostgresProvider::getPrimaryKey()
10931094
type = PQgetvalue( result, 0, 0 );
10941095
}
10951096

1097+
// mPrimaryKeyDefault stays null and is retrieved later on demand
1098+
10961099
if (( type != "int4" && type != "oid" ) ||
10971100
!uniqueData( mSchemaName, mTableName, primaryKey ) )
10981101
{
10991102
primaryKey = "";
11001103
}
11011104
}
11021105

1103-
// Have a poke around the view to see if any of the columns
1104-
// could be used as the primary key.
1105-
tableCols cols;
1106-
1107-
// Given a schema.view, populate the cols variable with the
1108-
// schema.table.column's that underly the view columns.
1109-
findColumns( cols );
1110-
11111106
if ( primaryKey.isEmpty() )
11121107
{
1113-
// From the view columns, choose one for which the underlying
1114-
// column is suitable for use as a key into the view.
1115-
primaryKey = chooseViewColumn( cols );
1116-
}
1117-
1118-
tableCols::const_iterator it = cols.find( primaryKey );
1119-
if ( it != cols.end() )
1120-
{
1121-
primaryKeyDefault = defaultValue( it->second.column, it->second.relation, it->second.schema ).toString();
1122-
if ( primaryKeyDefault.isNull() )
1123-
{
1124-
primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
1125-
.arg( quotedIdentifier( it->second.column ) )
1126-
.arg( quotedIdentifier( it->second.schema ) )
1127-
.arg( quotedIdentifier( it->second.relation ) );
1128-
}
1108+
parseView();
11291109
}
11301110
}
11311111
else
@@ -1227,13 +1207,13 @@ QString QgsPostgresProvider::getPrimaryKey()
12271207
}
12281208
else
12291209
{
1230-
primaryKeyDefault = defaultValue( primaryKey ).toString();
1231-
if ( primaryKeyDefault.isNull() )
1210+
mPrimaryKeyDefault = defaultValue( primaryKey ).toString();
1211+
if ( mPrimaryKeyDefault.isNull() )
12321212
{
1233-
primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
1234-
.arg( quotedIdentifier( primaryKey ) )
1235-
.arg( quotedIdentifier( mSchemaName ) )
1236-
.arg( quotedIdentifier( mTableName ) );
1213+
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
1214+
.arg( quotedIdentifier( primaryKey ) )
1215+
.arg( quotedIdentifier( mSchemaName ) )
1216+
.arg( quotedIdentifier( mTableName ) );
12371217
}
12381218
}
12391219
}
@@ -1250,11 +1230,58 @@ QString QgsPostgresProvider::getPrimaryKey()
12501230
return primaryKey;
12511231
}
12521232

1233+
void QgsPostgresProvider::parseView()
1234+
{
1235+
// Have a poke around the view to see if any of the columns
1236+
// could be used as the primary key.
1237+
tableCols cols;
1238+
1239+
// Given a schema.view, populate the cols variable with the
1240+
// schema.table.column's that underly the view columns.
1241+
findColumns( cols );
1242+
1243+
// pick the primary key, if we don't have one yet
1244+
if ( primaryKey.isEmpty() )
1245+
{
1246+
// From the view columns, choose one for which the underlying
1247+
// column is suitable for use as a key into the view.
1248+
primaryKey = chooseViewColumn( cols );
1249+
}
1250+
1251+
tableCols::const_iterator it = cols.find( primaryKey );
1252+
if ( it != cols.end() )
1253+
{
1254+
mPrimaryKeyDefault = defaultValue( it->second.column, it->second.relation, it->second.schema ).toString();
1255+
if ( mPrimaryKeyDefault.isNull() )
1256+
{
1257+
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
1258+
.arg( quotedIdentifier( it->second.column ) )
1259+
.arg( quotedIdentifier( it->second.schema ) )
1260+
.arg( quotedIdentifier( it->second.relation ) );
1261+
}
1262+
}
1263+
else
1264+
{
1265+
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
1266+
.arg( quotedIdentifier( primaryKey ) )
1267+
.arg( quotedIdentifier( mSchemaName ) )
1268+
.arg( quotedIdentifier( mTableName ) );
1269+
}
1270+
}
1271+
1272+
QString QgsPostgresProvider::primaryKeyDefault()
1273+
{
1274+
if ( mPrimaryKeyDefault.isNull() )
1275+
parseView();
1276+
1277+
return mPrimaryKeyDefault;
1278+
}
1279+
12531280
// Given the table and column that each column in the view refers to,
12541281
// choose one. Prefers column with an index on them, but will
12551282
// otherwise choose something suitable.
12561283

1257-
QString QgsPostgresProvider::chooseViewColumn( const tableCols& cols )
1284+
QString QgsPostgresProvider::chooseViewColumn( const tableCols &cols )
12581285
{
12591286
// For each relation name and column name need to see if it
12601287
// has unique constraints on it, or is a primary key (if not,
@@ -2206,7 +2233,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
22062233

22072234
if ( primaryKeyType != "tid" )
22082235
{
2209-
int id = paramValue( primaryKeyDefault, primaryKeyDefault ).toInt();
2236+
int id = paramValue( primaryKeyDefault(), primaryKeyDefault() ).toInt();
22102237
params << QString::number( id );
22112238
newIds << id;
22122239
}

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
408408
* the oid is used to fetch features.
409409
*/
410410
QString primaryKey;
411-
/**
412-
* Default value for primary key
413-
*/
414-
QString primaryKeyDefault;
415411
/**
416412
* Data type for the primary key
417413
*/
@@ -699,6 +695,14 @@ class QgsPostgresProvider : public QgsVectorDataProvider
699695
void disconnectDb();
700696

701697
static int providerIds;
698+
699+
QString primaryKeyDefault();
700+
void parseView();
701+
702+
/**
703+
* Default value for primary key
704+
*/
705+
QString mPrimaryKeyDefault;
702706
};
703707

704708
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.