@@ -764,6 +764,7 @@ bool QgsOracleProvider::hasSufficientPermsAndCapabilities()
764
764
| QgsVectorDataProvider::AddAttributes
765
765
| QgsVectorDataProvider::DeleteAttributes
766
766
| QgsVectorDataProvider::ChangeGeometries
767
+ | QgsVectorDataProvider::RenameAttributes
767
768
;
768
769
}
769
770
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()
788
789
}
789
790
else if ( priv == " ALTER TABLE" )
790
791
{
791
- mEnabledCapabilities |= QgsVectorDataProvider::AddAttributes | QgsVectorDataProvider::DeleteAttributes;
792
+ mEnabledCapabilities |= QgsVectorDataProvider::AddAttributes | QgsVectorDataProvider::DeleteAttributes | QgsVectorDataProvider::RenameAttributes ;
792
793
}
793
794
}
794
795
@@ -1006,6 +1007,9 @@ bool QgsOracleProvider::uniqueData( QString query, QString colName )
1006
1007
// Returns the minimum value of an attribute
1007
1008
QVariant QgsOracleProvider::minimumValue ( int index )
1008
1009
{
1010
+ if ( !mConnection )
1011
+ return QVariant ( QString::null );
1012
+
1009
1013
try
1010
1014
{
1011
1015
// get the field name
@@ -1044,6 +1048,9 @@ QVariant QgsOracleProvider::minimumValue( int index )
1044
1048
// Returns the list of unique values of an attribute
1045
1049
void QgsOracleProvider::uniqueValues ( int index, QList<QVariant> &uniqueValues, int limit )
1046
1050
{
1051
+ if ( !mConnection )
1052
+ return ;
1053
+
1047
1054
uniqueValues.clear ();
1048
1055
1049
1056
try
@@ -1090,6 +1097,9 @@ void QgsOracleProvider::uniqueValues( int index, QList<QVariant> &uniqueValues,
1090
1097
// Returns the maximum value of an attribute
1091
1098
QVariant QgsOracleProvider::maximumValue ( int index )
1092
1099
{
1100
+ if ( !mConnection )
1101
+ return QVariant ();
1102
+
1093
1103
try
1094
1104
{
1095
1105
// get the field name
@@ -1165,7 +1175,7 @@ bool QgsOracleProvider::addFeatures( QgsFeatureList &flist )
1165
1175
if ( flist.size () == 0 )
1166
1176
return true ;
1167
1177
1168
- if ( mIsQuery )
1178
+ if ( mIsQuery || ! mConnection )
1169
1179
return false ;
1170
1180
1171
1181
bool returnvalue = true ;
@@ -1414,7 +1424,7 @@ bool QgsOracleProvider::deleteFeatures( const QgsFeatureIds & id )
1414
1424
{
1415
1425
bool returnvalue = true ;
1416
1426
1417
- if ( mIsQuery )
1427
+ if ( mIsQuery || ! mConnection )
1418
1428
return false ;
1419
1429
1420
1430
QSqlDatabase db ( *mConnection );
@@ -1464,7 +1474,7 @@ bool QgsOracleProvider::addAttributes( const QList<QgsField> &attributes )
1464
1474
{
1465
1475
bool returnvalue = true ;
1466
1476
1467
- if ( mIsQuery )
1477
+ if ( mIsQuery || ! mConnection )
1468
1478
return false ;
1469
1479
1470
1480
QSqlDatabase db ( *mConnection );
@@ -1545,7 +1555,7 @@ bool QgsOracleProvider::deleteAttributes( const QgsAttributeIds& ids )
1545
1555
{
1546
1556
bool returnvalue = true ;
1547
1557
1548
- if ( mIsQuery )
1558
+ if ( mIsQuery || ! mConnection )
1549
1559
return false ;
1550
1560
1551
1561
QSqlDatabase db ( *mConnection );
@@ -1604,11 +1614,89 @@ bool QgsOracleProvider::deleteAttributes( const QgsAttributeIds& ids )
1604
1614
return returnvalue;
1605
1615
}
1606
1616
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
+
1607
1695
bool QgsOracleProvider::changeAttributeValues ( const QgsChangedAttributesMap &attr_map )
1608
1696
{
1609
1697
bool returnvalue = true ;
1610
1698
1611
- if ( mIsQuery )
1699
+ if ( mIsQuery || ! mConnection )
1612
1700
return false ;
1613
1701
1614
1702
if ( attr_map.isEmpty () )
@@ -1897,7 +1985,7 @@ bool QgsOracleProvider::changeGeometryValues( const QgsGeometryMap &geometry_map
1897
1985
{
1898
1986
QgsDebugMsg ( " entering." );
1899
1987
1900
- if ( mIsQuery || mGeometryColumn .isNull () )
1988
+ if ( mIsQuery || mGeometryColumn .isNull () || ! mConnection )
1901
1989
return false ;
1902
1990
1903
1991
QSqlDatabase db ( *mConnection );
@@ -1963,6 +2051,9 @@ int QgsOracleProvider::capabilities() const
1963
2051
1964
2052
bool QgsOracleProvider::setSubsetString ( const QString& theSQL, bool updateFeatureCount )
1965
2053
{
2054
+ if ( !mConnection )
2055
+ return false ;
2056
+
1966
2057
QString prevWhere = mSqlWhereClause ;
1967
2058
1968
2059
mSqlWhereClause = theSQL.trimmed ();
@@ -2014,7 +2105,7 @@ bool QgsOracleProvider::setSubsetString( const QString& theSQL, bool updateFeatu
2014
2105
*/
2015
2106
long QgsOracleProvider::featureCount () const
2016
2107
{
2017
- if ( mFeaturesCounted >= 0 )
2108
+ if ( mFeaturesCounted >= 0 || ! mConnection )
2018
2109
return mFeaturesCounted ;
2019
2110
2020
2111
// get total number of features
@@ -2053,7 +2144,7 @@ long QgsOracleProvider::featureCount() const
2053
2144
2054
2145
QgsRectangle QgsOracleProvider::extent ()
2055
2146
{
2056
- if ( mGeometryColumn .isNull () )
2147
+ if ( mGeometryColumn .isNull () || ! mConnection )
2057
2148
return QgsRectangle ();
2058
2149
2059
2150
if ( mLayerExtent .isEmpty () )
@@ -2321,6 +2412,9 @@ bool QgsOracleProvider::getGeometryDetails()
2321
2412
2322
2413
bool QgsOracleProvider::createSpatialIndex ()
2323
2414
{
2415
+ if ( !mConnection )
2416
+ return false ;
2417
+
2324
2418
QSqlQuery qry ( *mConnection );
2325
2419
2326
2420
if ( !crs ().geographicFlag () )
@@ -2844,6 +2938,9 @@ QgsCoordinateReferenceSystem QgsOracleProvider::crs()
2844
2938
{
2845
2939
QgsCoordinateReferenceSystem srs;
2846
2940
2941
+ if ( !mConnection )
2942
+ return srs;
2943
+
2847
2944
QSqlQuery qry ( *mConnection );
2848
2945
2849
2946
// 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 )
3299
3396
if ( !conn )
3300
3397
{
3301
3398
errCause = QObject::tr ( " Could not connect to database" );
3302
- return false ;
3399
+ return QString::null ;
3303
3400
}
3304
3401
3305
3402
QSqlQuery qry ( *conn );
0 commit comments