Skip to content

Commit

Permalink
postgres provider:
Browse files Browse the repository at this point in the history
- disable "Add" in selection dialog while column type thread is running
- fix detection of integer keys
- when adding feature update attributes and feature id in passed features
  • Loading branch information
jef-n committed Jan 18, 2012
1 parent 7b0db11 commit 6a8a9d4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/providers/postgres/qgspgsourceselect.cpp 100644 → 100755
Expand Up @@ -535,7 +535,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
}
}

if ( cmbConnections->count() > 0 )
if ( cmbConnections->count() > 0 && !mColumnTypeThread )
mAddButton->setEnabled( true );

mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmTable, Qt::AscendingOrder );
Expand Down Expand Up @@ -572,6 +572,9 @@ void QgsPgSourceSelect::columnThreadFinished()
mColumnTypeThread = 0;
btnConnect->setText( tr( "Connect" ) );
QApplication::restoreOverrideCursor();

if ( cmbConnections->count() > 0 )
mAddButton->setEnabled( true );
}

QStringList QgsPgSourceSelect::selectedTables()
Expand Down
6 changes: 6 additions & 0 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -117,6 +117,12 @@ int QgsPostgresResult::PQftype( int col )
return ::PQftype( mRes, col );
}

Oid QgsPostgresResult::PQoidValue()
{
Q_ASSERT( mRes );
return ::PQoidValue( mRes );
}

QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRO;
QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRW;
const int QgsPostgresConn::sGeomTypeSelectLimit = 100;
Expand Down
1 change: 1 addition & 0 deletions src/providers/postgres/qgspostgresconn.h
Expand Up @@ -69,6 +69,7 @@ class QgsPostgresResult
int PQftable( int col );
int PQftype( int col );
int PQftablecol( int col );
Oid PQoidValue();

PGresult *result() const { return mRes; }

Expand Down
64 changes: 54 additions & 10 deletions src/providers/postgres/qgspostgresprovider.cpp 100644 → 100755
Expand Up @@ -1478,7 +1478,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
continue;
}

if ( isInt && ( mAttributeFields[j].type() == QVariant::Int || mAttributeFields[j].type() == QVariant::LongLong ) )
if ( isInt && mAttributeFields[j].type() != QVariant::Int && mAttributeFields[j].type() != QVariant::LongLong )
isInt = false;

mPrimaryKeyAttrs << j;
Expand Down Expand Up @@ -1774,7 +1774,7 @@ QString QgsPostgresProvider::paramValue( QString fieldValue, const QString &defa
if ( fieldValue == defaultValue && !defaultValue.isNull() )
{
QgsPostgresResult result = mConnectionRW->PQexec( QString( "SELECT %1" ).arg( defaultValue ) );
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
throw PGException( result );

return result.PQgetvalue( 0, 0 );
Expand Down Expand Up @@ -1936,8 +1936,6 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
if ( stmt.PQresultStatus() == PGRES_FATAL_ERROR )
throw PGException( stmt );

QList<int> newIds;

for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
{
const QgsAttributeMap &attributevec = features->attributeMap();
Expand All @@ -1949,16 +1947,62 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
}

for ( int i = 0; i < fieldId.size(); i++ )
params << paramValue( attributevec.value( fieldId[i], defaultValues[i] ).toString(), defaultValues[i] );
{
QgsAttributeMap::const_iterator attr = attributevec.find( fieldId[i] );

QString v;
if ( attr == attributevec.end() )
{
v = paramValue( defaultValues[i], defaultValues[i] );
features->addAttribute( fieldId[i], v );
}
else
{
v = paramValue( attr.value().toString(), defaultValues[i] );

if ( v != attr.value().toString() )
features->changeAttribute( fieldId[i], v );
}

params << v;
}

QgsPostgresResult result = mConnectionRW->PQexecPrepared( "addfeatures", params );
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
throw PGException( result );

if ( mPrimaryKeyType == pktOid )
{
features->setFeatureId( result.PQoidValue() );
QgsDebugMsgLevel( QString( "new fid=%1" ).arg( features->id() ), 4 );
}
}

if ( flist.size() == newIds.size() )
for ( int i = 0; i < flist.size(); i++ )
flist[i].setFeatureId( newIds[i] );
// update feature ids
if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap )
{
for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
{
const QgsAttributeMap &attributevec = features->attributeMap();

if ( mPrimaryKeyType == pktInt )
{
features->setFeatureId( STRING_TO_FID( attributevec[ mPrimaryKeyAttrs[0] ] ) );
}
else
{
QList<QVariant> primaryKeyVals;

foreach( int idx, mPrimaryKeyAttrs )
{
primaryKeyVals << attributevec[ mPrimaryKeyAttrs[idx] ];
}

features->setFeatureId( lookupFid( QVariant( primaryKeyVals ) ) );
}
QgsDebugMsgLevel( QString( "new fid=%1" ).arg( features->id() ), 4 );
}
}

mConnectionRW->PQexecNR( "DEALLOCATE addfeatures" );
mConnectionRW->PQexecNR( "COMMIT" );
Expand Down Expand Up @@ -2704,7 +2748,7 @@ bool QgsPostgresProvider::getGeometryDetails()
QgsDebugMsg( "Requested SRID is " + mRequestedSrid );
QgsDebugMsg( "Detected type is " + QString::number( mDetectedGeomType ) );
QgsDebugMsg( "Requested type is " + QString::number( mRequestedGeomType ) );
QgsDebugMsg( "Feature type name is " + QString( QGis::qgisFeatureTypes[ geometryType() ] ) );
QgsDebugMsg( "Feature type name is " + QString( QGis::qgisFeatureTypes[ geometryType()] ) );
QgsDebugMsg( "Geometry is geography " + mIsGeography );
}
else
Expand Down

0 comments on commit 6a8a9d4

Please sign in to comment.