Skip to content

Commit 6c878f9

Browse files
committed
[FEATURE] oracle provider: add support for renaming attributes
1 parent 006b581 commit 6c878f9

File tree

3 files changed

+157
-43
lines changed

3 files changed

+157
-43
lines changed

src/app/qgsfieldsproperties.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,9 +750,21 @@ void QgsFieldsProperties::attributesListCellChanged( int row, int column )
750750
else if ( column == attrNameCol && mLayer && mLayer->isEditable() )
751751
{
752752
QTableWidgetItem *nameItem = mFieldsList->item( row, column );
753-
if ( nameItem && !nameItem->text().isEmpty() )
753+
if ( !nameItem ||
754+
nameItem->text().isEmpty() ||
755+
!mLayer->fields().exists( row ) ||
756+
mLayer->fields().at( row ).name() == nameItem->text() )
757+
return;
758+
759+
mLayer->beginEditCommand( tr( "Rename attribute" ) );
760+
if ( mLayer->renameAttribute( row, nameItem->text() ) )
754761
{
755-
mLayer->renameAttribute( row, nameItem->text() );
762+
mLayer->endEditCommand();
763+
}
764+
else
765+
{
766+
mLayer->destroyEditCommand();
767+
QMessageBox::critical( this, tr( "Failed to rename field" ), tr( "Failed to rename field to '%1'. Is the field name unique?" ).arg( nameItem->text() ) );
756768
}
757769
}
758770
}

src/providers/oracle/qgsoracleprovider.cpp

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ bool QgsOracleProvider::hasSufficientPermsAndCapabilities()
764764
| QgsVectorDataProvider::AddAttributes
765765
| QgsVectorDataProvider::DeleteAttributes
766766
| QgsVectorDataProvider::ChangeGeometries
767+
| QgsVectorDataProvider::RenameAttributes
767768
;
768769
}
769770
else if ( exec( qry, QString( "SELECT privilege FROM all_tab_privs WHERE table_schema=%1 AND table_name=%2 AND privilege IN ('DELETE','UPDATE','INSERT','ALTER TABLE')" )
@@ -788,7 +789,7 @@ bool QgsOracleProvider::hasSufficientPermsAndCapabilities()
788789
}
789790
else if ( priv == "ALTER TABLE" )
790791
{
791-
mEnabledCapabilities |= QgsVectorDataProvider::AddAttributes | QgsVectorDataProvider::DeleteAttributes;
792+
mEnabledCapabilities |= QgsVectorDataProvider::AddAttributes | QgsVectorDataProvider::DeleteAttributes | QgsVectorDataProvider::RenameAttributes;
792793
}
793794
}
794795

@@ -1006,6 +1007,9 @@ bool QgsOracleProvider::uniqueData( QString query, QString colName )
10061007
// Returns the minimum value of an attribute
10071008
QVariant QgsOracleProvider::minimumValue( int index )
10081009
{
1010+
if ( !mConnection )
1011+
return QVariant( QString::null );
1012+
10091013
try
10101014
{
10111015
// get the field name
@@ -1044,6 +1048,9 @@ QVariant QgsOracleProvider::minimumValue( int index )
10441048
// Returns the list of unique values of an attribute
10451049
void QgsOracleProvider::uniqueValues( int index, QList<QVariant> &uniqueValues, int limit )
10461050
{
1051+
if ( !mConnection )
1052+
return;
1053+
10471054
uniqueValues.clear();
10481055

10491056
try
@@ -1090,6 +1097,9 @@ void QgsOracleProvider::uniqueValues( int index, QList<QVariant> &uniqueValues,
10901097
// Returns the maximum value of an attribute
10911098
QVariant QgsOracleProvider::maximumValue( int index )
10921099
{
1100+
if ( !mConnection )
1101+
return QVariant();
1102+
10931103
try
10941104
{
10951105
// get the field name
@@ -1165,7 +1175,7 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )
11651175
if ( flist.size() == 0 )
11661176
return true;
11671177

1168-
if ( mIsQuery )
1178+
if ( mIsQuery || !mConnection )
11691179
return false;
11701180

11711181
bool returnvalue = true;
@@ -1414,7 +1424,7 @@ bool QgsOracleProvider::deleteFeatures( const QgsFeatureIds & id )
14141424
{
14151425
bool returnvalue = true;
14161426

1417-
if ( mIsQuery )
1427+
if ( mIsQuery || !mConnection )
14181428
return false;
14191429

14201430
QSqlDatabase db( *mConnection );
@@ -1464,7 +1474,7 @@ bool QgsOracleProvider::addAttributes( const QList<QgsField> &attributes )
14641474
{
14651475
bool returnvalue = true;
14661476

1467-
if ( mIsQuery )
1477+
if ( mIsQuery || !mConnection )
14681478
return false;
14691479

14701480
QSqlDatabase db( *mConnection );
@@ -1545,7 +1555,7 @@ bool QgsOracleProvider::deleteAttributes( const QgsAttributeIds& ids )
15451555
{
15461556
bool returnvalue = true;
15471557

1548-
if ( mIsQuery )
1558+
if ( mIsQuery || !mConnection )
15491559
return false;
15501560

15511561
QSqlDatabase db( *mConnection );
@@ -1604,11 +1614,89 @@ bool QgsOracleProvider::deleteAttributes( const QgsAttributeIds& ids )
16041614
return returnvalue;
16051615
}
16061616

1617+
bool QgsOracleProvider::renameAttributes( const QgsFieldNameMap &renamedAttributes )
1618+
{
1619+
if ( mIsQuery || !mConnection )
1620+
return false;
1621+
1622+
QgsFieldNameMap::const_iterator renameIt = renamedAttributes.constBegin();
1623+
for ( ; renameIt != renamedAttributes.constEnd(); ++renameIt )
1624+
{
1625+
int fieldIndex = renameIt.key();
1626+
if ( fieldIndex < 0 || fieldIndex >= mAttributeFields.count() )
1627+
{
1628+
pushError( tr( "Invalid attribute index: %1" ).arg( fieldIndex ) );
1629+
return false;
1630+
}
1631+
if ( mAttributeFields.indexFromName( renameIt.value() ) >= 0 )
1632+
{
1633+
//field name already in use
1634+
pushError( tr( "Error renaming field %1: name '%2' already exists" ).arg( fieldIndex ).arg( renameIt.value() ) );
1635+
return false;
1636+
}
1637+
}
1638+
1639+
QSqlDatabase db( *mConnection );
1640+
1641+
bool returnvalue = true;
1642+
1643+
try
1644+
{
1645+
QSqlQuery qry( db );
1646+
1647+
if ( !db.transaction() )
1648+
{
1649+
throw OracleException( tr( "Could not start transaction" ), db );
1650+
}
1651+
1652+
qry.finish();
1653+
1654+
for ( renameIt = renamedAttributes.constBegin(); renameIt != renamedAttributes.constEnd(); ++renameIt )
1655+
{
1656+
QString src( mAttributeFields.at( renameIt.key() ).name() );
1657+
1658+
if ( !exec( qry, QString( "ALTER TABLE %1 RENAME COLUMN %2 TO %3" )
1659+
.arg( mQuery,
1660+
quotedIdentifier( src ),
1661+
quotedIdentifier( renameIt.value() ) ) ) )
1662+
{
1663+
throw OracleException( tr( "Renaming column %1 to %2 failed" )
1664+
.arg( quotedIdentifier( src ),
1665+
quotedIdentifier( renameIt.value() ) ),
1666+
qry );
1667+
}
1668+
}
1669+
1670+
if ( !db.commit() )
1671+
{
1672+
throw OracleException( tr( "Could not commit transaction" ), db );
1673+
}
1674+
}
1675+
catch ( OracleException &e )
1676+
{
1677+
pushError( tr( "Oracle error while renaming attributes: %1" ).arg( e.errorMessage() ) );
1678+
if ( !db.rollback() )
1679+
{
1680+
QgsMessageLog::logMessage( tr( "Could not rollback transaction" ), tr( "Oracle" ) );
1681+
}
1682+
returnvalue = false;
1683+
}
1684+
1685+
if ( !loadFields() )
1686+
{
1687+
QgsMessageLog::logMessage( tr( "Could not reload fields." ), tr( "Oracle" ) );
1688+
returnvalue = false;
1689+
}
1690+
1691+
return returnvalue;
1692+
}
1693+
1694+
16071695
bool QgsOracleProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_map )
16081696
{
16091697
bool returnvalue = true;
16101698

1611-
if ( mIsQuery )
1699+
if ( mIsQuery || !mConnection )
16121700
return false;
16131701

16141702
if ( attr_map.isEmpty() )
@@ -1897,7 +1985,7 @@ bool QgsOracleProvider::changeGeometryValues( const QgsGeometryMap &geometry_map
18971985
{
18981986
QgsDebugMsg( "entering." );
18991987

1900-
if ( mIsQuery || mGeometryColumn.isNull() )
1988+
if ( mIsQuery || mGeometryColumn.isNull() || !mConnection )
19011989
return false;
19021990

19031991
QSqlDatabase db( *mConnection );
@@ -1963,6 +2051,9 @@ int QgsOracleProvider::capabilities() const
19632051

19642052
bool QgsOracleProvider::setSubsetString( const QString& theSQL, bool updateFeatureCount )
19652053
{
2054+
if ( !mConnection )
2055+
return false;
2056+
19662057
QString prevWhere = mSqlWhereClause;
19672058

19682059
mSqlWhereClause = theSQL.trimmed();
@@ -2014,7 +2105,7 @@ bool QgsOracleProvider::setSubsetString( const QString& theSQL, bool updateFeatu
20142105
*/
20152106
long QgsOracleProvider::featureCount() const
20162107
{
2017-
if ( mFeaturesCounted >= 0 )
2108+
if ( mFeaturesCounted >= 0 || !mConnection )
20182109
return mFeaturesCounted;
20192110

20202111
// get total number of features
@@ -2053,7 +2144,7 @@ long QgsOracleProvider::featureCount() const
20532144

20542145
QgsRectangle QgsOracleProvider::extent()
20552146
{
2056-
if ( mGeometryColumn.isNull() )
2147+
if ( mGeometryColumn.isNull() || !mConnection )
20572148
return QgsRectangle();
20582149

20592150
if ( mLayerExtent.isEmpty() )
@@ -2321,6 +2412,9 @@ bool QgsOracleProvider::getGeometryDetails()
23212412

23222413
bool QgsOracleProvider::createSpatialIndex()
23232414
{
2415+
if ( !mConnection )
2416+
return false;
2417+
23242418
QSqlQuery qry( *mConnection );
23252419

23262420
if ( !crs().geographicFlag() )
@@ -2844,6 +2938,9 @@ QgsCoordinateReferenceSystem QgsOracleProvider::crs()
28442938
{
28452939
QgsCoordinateReferenceSystem srs;
28462940

2941+
if ( !mConnection )
2942+
return srs;
2943+
28472944
QSqlQuery qry( *mConnection );
28482945

28492946
// apparently some EPSG codes don't have the auth_name setup in cs_srs
@@ -3299,7 +3396,7 @@ QGISEXTERN QString loadStyle( const QString &uri, QString &errCause )
32993396
if ( !conn )
33003397
{
33013398
errCause = QObject::tr( "Could not connect to database" );
3302-
return false;
3399+
return QString::null;
33033400
}
33043401

33053402
QSqlQuery qry( *conn );

0 commit comments

Comments
 (0)