Skip to content

Commit

Permalink
better fix for slowdown of r12418
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@12450 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 14, 2009
1 parent 41dd6b4 commit 39defcc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 43 deletions.
105 changes: 66 additions & 39 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -55,7 +55,8 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
: QgsVectorDataProvider( uri ),
mFetching( false ),
geomType( QGis::WKBUnknown ),
mFeatureQueueSize( 200 )
mFeatureQueueSize( 200 ),
mPrimaryKeyDefault( QString::null )
{
// assume this is a valid layer until we determine otherwise
valid = true;
Expand Down Expand Up @@ -1064,13 +1065,13 @@ QString QgsPostgresProvider::getPrimaryKey()
}
else
{
primaryKeyDefault = defaultValue( primaryKey ).toString();
if ( primaryKeyDefault.isNull() )
mPrimaryKeyDefault = defaultValue( primaryKey ).toString();
if ( mPrimaryKeyDefault.isNull() )
{
primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
.arg( quotedIdentifier( primaryKey ) )
.arg( quotedIdentifier( mSchemaName ) )
.arg( quotedIdentifier( mTableName ) );
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
.arg( quotedIdentifier( primaryKey ) )
.arg( quotedIdentifier( mSchemaName ) )
.arg( quotedIdentifier( mTableName ) );
}
}
}
Expand All @@ -1093,39 +1094,18 @@ QString QgsPostgresProvider::getPrimaryKey()
type = PQgetvalue( result, 0, 0 );
}

// mPrimaryKeyDefault stays null and is retrieved later on demand

if (( type != "int4" && type != "oid" ) ||
!uniqueData( mSchemaName, mTableName, primaryKey ) )
{
primaryKey = "";
}
}

// Have a poke around the view to see if any of the columns
// could be used as the primary key.
tableCols cols;

// Given a schema.view, populate the cols variable with the
// schema.table.column's that underly the view columns.
findColumns( cols );

if ( primaryKey.isEmpty() )
{
// From the view columns, choose one for which the underlying
// column is suitable for use as a key into the view.
primaryKey = chooseViewColumn( cols );
}

tableCols::const_iterator it = cols.find( primaryKey );
if ( it != cols.end() )
{
primaryKeyDefault = defaultValue( it->second.column, it->second.relation, it->second.schema ).toString();
if ( primaryKeyDefault.isNull() )
{
primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
.arg( quotedIdentifier( it->second.column ) )
.arg( quotedIdentifier( it->second.schema ) )
.arg( quotedIdentifier( it->second.relation ) );
}
parseView();
}
}
else
Expand Down Expand Up @@ -1227,13 +1207,13 @@ QString QgsPostgresProvider::getPrimaryKey()
}
else
{
primaryKeyDefault = defaultValue( primaryKey ).toString();
if ( primaryKeyDefault.isNull() )
mPrimaryKeyDefault = defaultValue( primaryKey ).toString();
if ( mPrimaryKeyDefault.isNull() )
{
primaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
.arg( quotedIdentifier( primaryKey ) )
.arg( quotedIdentifier( mSchemaName ) )
.arg( quotedIdentifier( mTableName ) );
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
.arg( quotedIdentifier( primaryKey ) )
.arg( quotedIdentifier( mSchemaName ) )
.arg( quotedIdentifier( mTableName ) );
}
}
}
Expand All @@ -1250,11 +1230,58 @@ QString QgsPostgresProvider::getPrimaryKey()
return primaryKey;
}

void QgsPostgresProvider::parseView()
{
// Have a poke around the view to see if any of the columns
// could be used as the primary key.
tableCols cols;

// Given a schema.view, populate the cols variable with the
// schema.table.column's that underly the view columns.
findColumns( cols );

// pick the primary key, if we don't have one yet
if ( primaryKey.isEmpty() )
{
// From the view columns, choose one for which the underlying
// column is suitable for use as a key into the view.
primaryKey = chooseViewColumn( cols );
}

tableCols::const_iterator it = cols.find( primaryKey );
if ( it != cols.end() )
{
mPrimaryKeyDefault = defaultValue( it->second.column, it->second.relation, it->second.schema ).toString();
if ( mPrimaryKeyDefault.isNull() )
{
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
.arg( quotedIdentifier( it->second.column ) )
.arg( quotedIdentifier( it->second.schema ) )
.arg( quotedIdentifier( it->second.relation ) );
}
}
else
{
mPrimaryKeyDefault = QString( "max(%1)+1 from %2.%3" )
.arg( quotedIdentifier( primaryKey ) )
.arg( quotedIdentifier( mSchemaName ) )
.arg( quotedIdentifier( mTableName ) );
}
}

QString QgsPostgresProvider::primaryKeyDefault()
{
if ( mPrimaryKeyDefault.isNull() )
parseView();

return mPrimaryKeyDefault;
}

// Given the table and column that each column in the view refers to,
// choose one. Prefers column with an index on them, but will
// otherwise choose something suitable.

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

if ( primaryKeyType != "tid" )
{
int id = paramValue( primaryKeyDefault, primaryKeyDefault ).toInt();
int id = paramValue( primaryKeyDefault(), primaryKeyDefault() ).toInt();
params << QString::number( id );
newIds << id;
}
Expand Down
12 changes: 8 additions & 4 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -408,10 +408,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
* the oid is used to fetch features.
*/
QString primaryKey;
/**
* Default value for primary key
*/
QString primaryKeyDefault;
/**
* Data type for the primary key
*/
Expand Down Expand Up @@ -699,6 +695,14 @@ class QgsPostgresProvider : public QgsVectorDataProvider
void disconnectDb();

static int providerIds;

QString primaryKeyDefault();
void parseView();

/**
* Default value for primary key
*/
QString mPrimaryKeyDefault;
};

#endif

0 comments on commit 39defcc

Please sign in to comment.