Skip to content

Commit 6a8a9d4

Browse files
committed
postgres provider:
- 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
1 parent 7b0db11 commit 6a8a9d4

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

src/providers/postgres/qgspgsourceselect.cpp

100644100755
+4-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ void QgsPgSourceSelect::on_btnConnect_clicked()
535535
}
536536
}
537537

538-
if ( cmbConnections->count() > 0 )
538+
if ( cmbConnections->count() > 0 && !mColumnTypeThread )
539539
mAddButton->setEnabled( true );
540540

541541
mTablesTreeView->sortByColumn( QgsPgTableModel::dbtmTable, Qt::AscendingOrder );
@@ -572,6 +572,9 @@ void QgsPgSourceSelect::columnThreadFinished()
572572
mColumnTypeThread = 0;
573573
btnConnect->setText( tr( "Connect" ) );
574574
QApplication::restoreOverrideCursor();
575+
576+
if ( cmbConnections->count() > 0 )
577+
mAddButton->setEnabled( true );
575578
}
576579

577580
QStringList QgsPgSourceSelect::selectedTables()

src/providers/postgres/qgspostgresconn.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ int QgsPostgresResult::PQftype( int col )
117117
return ::PQftype( mRes, col );
118118
}
119119

120+
Oid QgsPostgresResult::PQoidValue()
121+
{
122+
Q_ASSERT( mRes );
123+
return ::PQoidValue( mRes );
124+
}
125+
120126
QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRO;
121127
QMap<QString, QgsPostgresConn *> QgsPostgresConn::sConnectionsRW;
122128
const int QgsPostgresConn::sGeomTypeSelectLimit = 100;

src/providers/postgres/qgspostgresconn.h

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class QgsPostgresResult
6969
int PQftable( int col );
7070
int PQftype( int col );
7171
int PQftablecol( int col );
72+
Oid PQoidValue();
7273

7374
PGresult *result() const { return mRes; }
7475

src/providers/postgres/qgspostgresprovider.cpp

100644100755
+54-10
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
14781478
continue;
14791479
}
14801480

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

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

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

1939-
QList<int> newIds;
1940-
19411939
for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
19421940
{
19431941
const QgsAttributeMap &attributevec = features->attributeMap();
@@ -1949,16 +1947,62 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist )
19491947
}
19501948

19511949
for ( int i = 0; i < fieldId.size(); i++ )
1952-
params << paramValue( attributevec.value( fieldId[i], defaultValues[i] ).toString(), defaultValues[i] );
1950+
{
1951+
QgsAttributeMap::const_iterator attr = attributevec.find( fieldId[i] );
1952+
1953+
QString v;
1954+
if ( attr == attributevec.end() )
1955+
{
1956+
v = paramValue( defaultValues[i], defaultValues[i] );
1957+
features->addAttribute( fieldId[i], v );
1958+
}
1959+
else
1960+
{
1961+
v = paramValue( attr.value().toString(), defaultValues[i] );
1962+
1963+
if ( v != attr.value().toString() )
1964+
features->changeAttribute( fieldId[i], v );
1965+
}
1966+
1967+
params << v;
1968+
}
19531969

19541970
QgsPostgresResult result = mConnectionRW->PQexecPrepared( "addfeatures", params );
1955-
if ( result.PQresultStatus() == PGRES_FATAL_ERROR )
1971+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
19561972
throw PGException( result );
1973+
1974+
if ( mPrimaryKeyType == pktOid )
1975+
{
1976+
features->setFeatureId( result.PQoidValue() );
1977+
QgsDebugMsgLevel( QString( "new fid=%1" ).arg( features->id() ), 4 );
1978+
}
19571979
}
19581980

1959-
if ( flist.size() == newIds.size() )
1960-
for ( int i = 0; i < flist.size(); i++ )
1961-
flist[i].setFeatureId( newIds[i] );
1981+
// update feature ids
1982+
if ( mPrimaryKeyType == pktInt || mPrimaryKeyType == pktFidMap )
1983+
{
1984+
for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
1985+
{
1986+
const QgsAttributeMap &attributevec = features->attributeMap();
1987+
1988+
if ( mPrimaryKeyType == pktInt )
1989+
{
1990+
features->setFeatureId( STRING_TO_FID( attributevec[ mPrimaryKeyAttrs[0] ] ) );
1991+
}
1992+
else
1993+
{
1994+
QList<QVariant> primaryKeyVals;
1995+
1996+
foreach( int idx, mPrimaryKeyAttrs )
1997+
{
1998+
primaryKeyVals << attributevec[ mPrimaryKeyAttrs[idx] ];
1999+
}
2000+
2001+
features->setFeatureId( lookupFid( QVariant( primaryKeyVals ) ) );
2002+
}
2003+
QgsDebugMsgLevel( QString( "new fid=%1" ).arg( features->id() ), 4 );
2004+
}
2005+
}
19622006

19632007
mConnectionRW->PQexecNR( "DEALLOCATE addfeatures" );
19642008
mConnectionRW->PQexecNR( "COMMIT" );
@@ -2704,7 +2748,7 @@ bool QgsPostgresProvider::getGeometryDetails()
27042748
QgsDebugMsg( "Requested SRID is " + mRequestedSrid );
27052749
QgsDebugMsg( "Detected type is " + QString::number( mDetectedGeomType ) );
27062750
QgsDebugMsg( "Requested type is " + QString::number( mRequestedGeomType ) );
2707-
QgsDebugMsg( "Feature type name is " + QString( QGis::qgisFeatureTypes[ geometryType() ] ) );
2751+
QgsDebugMsg( "Feature type name is " + QString( QGis::qgisFeatureTypes[ geometryType()] ) );
27082752
QgsDebugMsg( "Geometry is geography " + mIsGeography );
27092753
}
27102754
else

0 commit comments

Comments
 (0)