@@ -55,7 +55,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
55
55
{
56
56
mProviderId = sProviderIds ++;
57
57
58
- QgsDebugMsg ( " URI: " + uri );
58
+ QgsDebugMsg ( QString ( " URI: %1 " ). arg ( uri ) );
59
59
60
60
mUri = QgsDataSourceURI ( uri );
61
61
@@ -94,12 +94,12 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
94
94
mUseEstimatedMetadata = mUri .useEstimatedMetadata ();
95
95
mSelectAtIdDisabled = mUri .selectAtIdDisabled ();
96
96
97
- QgsDebugMsg ( " Connection info is " + mUri .connectionInfo () );
98
- QgsDebugMsg ( " Geometry column is: " + mGeometryColumn );
99
- QgsDebugMsg ( " Schema is: " + mSchemaName );
100
- QgsDebugMsg ( " Table name is: " + mTableName );
101
- QgsDebugMsg ( " Query is: " + mQuery );
102
- QgsDebugMsg ( " Where clause is: " + mSqlWhereClause );
97
+ QgsDebugMsg ( QString ( " Connection info is %1 " ). arg ( mUri .connectionInfo () ) );
98
+ QgsDebugMsg ( QString ( " Geometry column is: %1 " ). arg ( mGeometryColumn ) );
99
+ QgsDebugMsg ( QString ( " Schema is: %1 " ). arg ( mSchemaName ) );
100
+ QgsDebugMsg ( QString ( " Table name is: %1 " ). arg ( mTableName ) );
101
+ QgsDebugMsg ( QString ( " Query is: %1 " ). arg ( mQuery ) );
102
+ QgsDebugMsg ( QString ( " Where clause is: %1 " ). arg ( mSqlWhereClause ) );
103
103
104
104
// no table/query passed, the provider could be used to get tables
105
105
if ( mQuery .isEmpty () )
@@ -763,7 +763,7 @@ void QgsPostgresProvider::appendPkParams( QgsFeatureId featureId, QStringList &p
763
763
}
764
764
}
765
765
766
- QgsDebugMsg ( " keys params: " + params.join ( " ; " ) );
766
+ QgsDebugMsg ( QString ( " keys params: %1 " ). arg ( params.join ( " ; " ) ) );
767
767
}
768
768
break ;
769
769
@@ -975,7 +975,7 @@ bool QgsPostgresProvider::loadFields()
975
975
{
976
976
if ( !mIsQuery )
977
977
{
978
- QgsDebugMsg ( " Loading fields for table " + mTableName );
978
+ QgsDebugMsg ( QString ( " Loading fields for table %1 " ). arg ( mTableName ) );
979
979
980
980
// Get the relation oid for use in later queries
981
981
QString sql = QString ( " SELECT regclass(%1)::oid" ).arg ( quotedValue ( mQuery ) );
@@ -1133,7 +1133,7 @@ bool QgsPostgresProvider::loadFields()
1133
1133
}
1134
1134
else
1135
1135
{
1136
- QgsLogger::warning ( " Field " + fieldName + " ignored, because of unsupported type " + fieldTypeName );
1136
+ QgsMessageLog::logMessage ( tr ( " Field %1 ignored, because of unsupported type %2 " ). arg ( fieldName ). arg ( fieldTypeName ), tr ( " PostGIS " ) );
1137
1137
continue ;
1138
1138
}
1139
1139
@@ -1152,7 +1152,7 @@ bool QgsPostgresProvider::loadFields()
1152
1152
}
1153
1153
else
1154
1154
{
1155
- QgsLogger::warning ( " Field " + fieldName + " ignored, because of unsupported type type " + fieldTType );
1155
+ QgsMessageLog::logMessage ( tr ( " Field %1 ignored, because of unsupported type type %2 " ). arg ( fieldName ). arg ( fieldTType ), tr ( " PostGIS " ) );
1156
1156
continue ;
1157
1157
}
1158
1158
@@ -1319,7 +1319,7 @@ bool QgsPostgresProvider::hasSufficientPermsAndCapabilities()
1319
1319
while ( mQuery .contains ( regex ) );
1320
1320
1321
1321
// convert the custom query into a subquery
1322
- mQuery = QString ( " %1 as %2" )
1322
+ mQuery = QString ( " %1 AS %2" )
1323
1323
.arg ( mQuery )
1324
1324
.arg ( quotedIdentifier ( alias ) );
1325
1325
@@ -1417,34 +1417,17 @@ bool QgsPostgresProvider::determinePrimaryKey()
1417
1417
QString primaryKey = mUri .keyColumn ();
1418
1418
int idx = fieldNameIndex ( mUri .keyColumn () );
1419
1419
1420
- if ( idx >= 0 )
1420
+ if ( idx >= 0 && ( mAttributeFields [idx]. type () == QVariant::Int || mAttributeFields [idx]. type () == QVariant::LongLong ) )
1421
1421
{
1422
- // validate last used candidate
1423
- sql = QString ( " SELECT pg_type.typname FROM pg_attribute,pg_type WHERE atttypid=pg_type.oid AND attname=%1 AND attrelid=%2::regclass" )
1424
- .arg ( quotedValue ( primaryKey ) )
1425
- .arg ( quotedValue ( mQuery ) );
1426
- QgsDebugMsg ( " Retrieving primary key type for view: " + sql );
1427
- res = mConnectionRO ->PQexec ( sql );
1428
- QgsDebugMsg ( QString ( " Got %1 rows." ).arg ( res.PQntuples () ) );
1429
-
1430
- QString type;
1431
- if ( res.PQresultStatus () == PGRES_TUPLES_OK && res.PQntuples () == 1 )
1432
- {
1433
- type = res.PQgetvalue ( 0 , 0 );
1434
- }
1435
-
1436
- // if mUseEstimatedMetadata is on assume that the already keyfield is still unique
1437
-
1438
- if (( type.startsWith ( " int" ) || type.startsWith ( " serial" ) ) &&
1439
- ( mUseEstimatedMetadata || uniqueData ( mQuery , primaryKey ) ) )
1422
+ if ( mUseEstimatedMetadata || uniqueData ( mQuery , primaryKey ) )
1440
1423
{
1441
1424
mPrimaryKeyType = pktInt;
1442
1425
mPrimaryKeyAttrs << idx;
1443
1426
}
1444
1427
}
1445
-
1446
- if ( mPrimaryKeyType != pktInt )
1428
+ else
1447
1429
{
1430
+ QgsMessageLog::logMessage ( tr ( " No key field for view given." ), tr ( " PostGIS" ) );
1448
1431
mPrimaryKeyType = pktUnknown;
1449
1432
}
1450
1433
}
@@ -1488,6 +1471,25 @@ bool QgsPostgresProvider::determinePrimaryKey()
1488
1471
mPrimaryKeyType = ( mPrimaryKeyAttrs .size () == 1 && isInt ) ? pktInt : pktFidMap;
1489
1472
}
1490
1473
}
1474
+ else
1475
+ {
1476
+ QString primaryKey = mUri .keyColumn ();
1477
+ int idx = fieldNameIndex ( mUri .keyColumn () );
1478
+
1479
+ if ( idx >= 0 && ( mAttributeFields [idx].type () == QVariant::Int || mAttributeFields [idx].type () == QVariant::LongLong ) )
1480
+ {
1481
+ if ( mUseEstimatedMetadata || uniqueData ( mQuery , primaryKey ) )
1482
+ {
1483
+ mPrimaryKeyType = pktInt;
1484
+ mPrimaryKeyAttrs << idx;
1485
+ }
1486
+ }
1487
+ else
1488
+ {
1489
+ QgsMessageLog::logMessage ( tr ( " No key field for query given." ), tr ( " PostGIS" ) );
1490
+ mPrimaryKeyType = pktUnknown;
1491
+ }
1492
+ }
1491
1493
1492
1494
mValid = mPrimaryKeyType != pktUnknown;
1493
1495
@@ -2603,7 +2605,7 @@ bool QgsPostgresProvider::getGeometryDetails()
2603
2605
{
2604
2606
sql = QString ( " SELECT %1 FROM %2 LIMIT 0" ).arg ( quotedIdentifier ( mGeometryColumn ) ).arg ( mQuery );
2605
2607
2606
- QgsDebugMsg ( " Getting geometry column: " + sql );
2608
+ QgsDebugMsg ( QString ( " Getting geometry column: %1 " ). arg ( sql ) );
2607
2609
2608
2610
QgsPostgresResult result = mConnectionRO ->PQexec ( sql );
2609
2611
if ( PGRES_TUPLES_OK == result.PQresultStatus () )
@@ -2635,60 +2637,73 @@ bool QgsPostgresProvider::getGeometryDetails()
2635
2637
}
2636
2638
}
2637
2639
}
2640
+ else
2641
+ {
2642
+ schemaName = " " ;
2643
+ tableName = mQuery ;
2644
+ }
2645
+ }
2646
+ else
2647
+ {
2648
+ mValid = false ;
2649
+ return false ;
2638
2650
}
2639
2651
}
2640
2652
2641
2653
QString detectedType;
2642
2654
QString detectedSrid;
2643
2655
2644
- // check geometry columns
2645
- sql = QString ( " SELECT upper(type),srid FROM geometry_columns WHERE f_table_name=%1 AND f_geometry_column=%2 AND f_table_schema=%3" )
2646
- .arg ( quotedValue ( tableName ) )
2647
- .arg ( quotedValue ( geomCol ) )
2648
- .arg ( quotedValue ( schemaName ) );
2649
-
2650
- QgsDebugMsg ( " Getting geometry column: " + sql );
2651
- result = mConnectionRO ->PQexec ( sql );
2652
- QgsDebugMsg ( QString ( " Geometry column query returned %1 rows" ).arg ( result.PQntuples () ) );
2653
-
2654
- if ( result.PQntuples () == 1 )
2655
- {
2656
- detectedType = result.PQgetvalue ( 0 , 0 );
2657
- detectedSrid = result.PQgetvalue ( 0 , 1 );
2658
- }
2659
-
2660
- if ( !detectedType.isEmpty () )
2656
+ if ( !schemaName.isEmpty () )
2661
2657
{
2662
2658
// check geometry columns
2663
- sql = QString ( " SELECT upper(type),srid FROM geography_columns WHERE f_table_name=%1 AND f_geography_column =%2 AND f_table_schema=%3" )
2659
+ sql = QString ( " SELECT upper(type),srid FROM geometry_columns WHERE f_table_name=%1 AND f_geometry_column =%2 AND f_table_schema=%3" )
2664
2660
.arg ( quotedValue ( tableName ) )
2665
2661
.arg ( quotedValue ( geomCol ) )
2666
2662
.arg ( quotedValue ( schemaName ) );
2667
2663
2668
- QgsDebugMsg ( " Getting geography column: " + sql );
2669
- result = mConnectionRO ->PQexec ( sql, false );
2670
- QgsDebugMsg ( " Geography column query returned " + QString::number ( result.PQntuples () ) );
2664
+ QgsDebugMsg ( QString ( " Getting geometry column: %1 " ). arg ( sql ) );
2665
+ result = mConnectionRO ->PQexec ( sql );
2666
+ QgsDebugMsg ( QString ( " Geometry column query returned %1 rows " ). arg ( result.PQntuples () ) );
2671
2667
2672
- if ( result.PQresultStatus () == PGRES_TUPLES_OK )
2668
+ if ( result.PQntuples () == 1 )
2673
2669
{
2674
- if ( result.PQntuples () == 1 )
2675
- {
2676
- detectedType = result.PQgetvalue ( 0 , 0 );
2677
- detectedSrid = result.PQgetvalue ( 0 , 1 );
2678
- mIsGeography = true ;
2679
- }
2670
+ detectedType = result.PQgetvalue ( 0 , 0 );
2671
+ detectedSrid = result.PQgetvalue ( 0 , 1 );
2680
2672
}
2681
- else
2673
+
2674
+ if ( !detectedType.isEmpty () )
2682
2675
{
2683
- mConnectionRO ->PQexecNR ( " ROLLBACK" );
2676
+ // check geometry columns
2677
+ sql = QString ( " SELECT upper(type),srid FROM geography_columns WHERE f_table_name=%1 AND f_geography_column=%2 AND f_table_schema=%3" )
2678
+ .arg ( quotedValue ( tableName ) )
2679
+ .arg ( quotedValue ( geomCol ) )
2680
+ .arg ( quotedValue ( schemaName ) );
2681
+
2682
+ QgsDebugMsg ( QString ( " Getting geography column: %1" ).arg ( sql ) );
2683
+ result = mConnectionRO ->PQexec ( sql, false );
2684
+ QgsDebugMsg ( QString ( " Geography column query returned %1" ).arg ( result.PQntuples () ) );
2685
+
2686
+ if ( result.PQresultStatus () == PGRES_TUPLES_OK )
2687
+ {
2688
+ if ( result.PQntuples () == 1 )
2689
+ {
2690
+ detectedType = result.PQgetvalue ( 0 , 0 );
2691
+ detectedSrid = result.PQgetvalue ( 0 , 1 );
2692
+ mIsGeography = true ;
2693
+ }
2694
+ }
2695
+ else
2696
+ {
2697
+ mConnectionRO ->PQexecNR ( " ROLLBACK" );
2698
+ }
2684
2699
}
2685
2700
}
2686
2701
2687
2702
if ( QgsPostgresConn::geomTypeFromPostgis ( detectedType ) == QGis::UnknownGeometry )
2688
2703
{
2689
2704
QgsPostgresLayerProperty layerProperty;
2690
- layerProperty.schemaName = mSchemaName ;
2691
- layerProperty.tableName = mTableName ;
2705
+ layerProperty.schemaName = schemaName ;
2706
+ layerProperty.tableName = tableName ;
2692
2707
layerProperty.geometryColName = mGeometryColumn ;
2693
2708
layerProperty.isGeography = mIsGeography ;
2694
2709
@@ -2758,10 +2773,10 @@ bool QgsPostgresProvider::getGeometryDetails()
2758
2773
mDetectedGeomType = QgsPostgresConn::geomTypeFromPostgis ( detectedType );
2759
2774
mDetectedSrid = detectedSrid;
2760
2775
2761
- QgsDebugMsg ( " Detected SRID is " + mDetectedSrid );
2762
- QgsDebugMsg ( " Requested SRID is " + mRequestedSrid );
2763
- QgsDebugMsg ( " Detected type is " + QString::number ( mDetectedGeomType ) );
2764
- QgsDebugMsg ( " Requested type is " + QString::number ( mRequestedGeomType ) );
2776
+ QgsDebugMsg ( QString ( " Detected SRID is %1 " ). arg ( mDetectedSrid ) );
2777
+ QgsDebugMsg ( QString ( " Requested SRID is %1 " ). arg ( mRequestedSrid ) );
2778
+ QgsDebugMsg ( QString ( " Detected type is %1 " ). arg ( mDetectedGeomType ) );
2779
+ QgsDebugMsg ( QString ( " Requested type is %1 " ). arg ( mRequestedGeomType ) );
2765
2780
2766
2781
mValid = ( mDetectedGeomType != QGis::UnknownGeometry || mRequestedGeomType != QGis::UnknownGeometry )
2767
2782
&& ( !mDetectedSrid .isEmpty () || !mRequestedSrid .isEmpty () );
@@ -2781,8 +2796,8 @@ bool QgsPostgresProvider::getGeometryDetails()
2781
2796
mEnabledCapabilities &= ~( QgsVectorDataProvider::ChangeGeometries | QgsVectorDataProvider::AddFeatures );
2782
2797
}
2783
2798
2784
- QgsDebugMsg ( " Feature type name is " + QString ( QGis::qgisFeatureTypes[ geometryType ()] ) );
2785
- QgsDebugMsg ( " Geometry is geography " + mIsGeography );
2799
+ QgsDebugMsg ( QString ( " Feature type name is %1 " ). arg ( QGis::qgisFeatureTypes[ geometryType ()] ) );
2800
+ QgsDebugMsg ( QString ( " Geometry is geography %1 " ). arg ( mIsGeography ) );
2786
2801
2787
2802
return mValid ;
2788
2803
}
@@ -2864,10 +2879,10 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
2864
2879
}
2865
2880
schemaTableName += quotedIdentifier ( tableName );
2866
2881
2867
- QgsDebugMsg ( " Connection info is " + dsUri.connectionInfo () );
2868
- QgsDebugMsg ( " Geometry column is: " + geometryColumn );
2869
- QgsDebugMsg ( " Schema is: " + schemaName );
2870
- QgsDebugMsg ( " Table name is: " + tableName );
2882
+ QgsDebugMsg ( QString ( " Connection info is " ). arg ( dsUri.connectionInfo () ) );
2883
+ QgsDebugMsg ( QString ( " Geometry column is: " ). arg ( geometryColumn ) );
2884
+ QgsDebugMsg ( QString ( " Schema is: " ). arg ( schemaName ) );
2885
+ QgsDebugMsg ( QString ( " Table name is: %1 " ). arg ( tableName ) );
2871
2886
2872
2887
// create the table
2873
2888
QgsPostgresConn *conn = QgsPostgresConn::connectDb ( dsUri.connectionInfo (), false );
@@ -2918,7 +2933,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
2918
2933
primaryKeyType = " serial" ;
2919
2934
#if 0
2920
2935
// TODO: check the feature count to choose if create a serial8 pk field
2921
- if ( layer->featureCount() > 0xFFFFFF )
2936
+ if ( layer->featureCount() > 0xffffffff )
2922
2937
{
2923
2938
primaryKeyType = "serial8";
2924
2939
}
@@ -3007,7 +3022,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
3007
3022
}
3008
3023
conn->disconnect ();
3009
3024
3010
- QgsDebugMsg ( " layer " + schemaTableName + " created. " );
3025
+ QgsDebugMsg ( QString ( " layer %1 created " ). arg ( schemaTableName ) );
3011
3026
3012
3027
// use the provider to edit the table
3013
3028
dsUri.setDataSource ( schemaName, tableName, geometryColumn, QString (), primaryKey );
@@ -3057,13 +3072,11 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
3057
3072
return QgsVectorLayerImport::ErrAttributeTypeUnsupported;
3058
3073
}
3059
3074
3060
- QgsDebugMsg ( " creating field #" + QString::number ( fldIt.key () ) +
3061
- " -> #" + QString::number ( offset ) +
3062
- " name " + fld.name () +
3063
- " type " + QString ( QVariant::typeToName ( fld.type () ) ) +
3064
- " typename " + fld.typeName () +
3065
- " width " + QString::number ( fld.length () ) +
3066
- " precision " + QString::number ( fld.precision () ) );
3075
+ QgsDebugMsg ( QString ( " creating field #%1 -> #%2 name %3 type %4 typename %5 width %6 precision %7" )
3076
+ .arg ( fldIt.key () ).arg ( offset )
3077
+ .arg ( fld.name () ).arg ( QVariant::typeToName ( fld.type () ) ).arg ( fld.typeName () )
3078
+ .arg ( fld.length () ).arg ( fld.precision () )
3079
+ );
3067
3080
3068
3081
flist.append ( fld );
3069
3082
if ( oldToNewAttrIdxMap )
0 commit comments