@@ -55,7 +55,7 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
5555{
5656 mProviderId = sProviderIds ++;
5757
58- QgsDebugMsg ( " URI: " + uri );
58+ QgsDebugMsg ( QString ( " URI: %1 " ). arg ( uri ) );
5959
6060 mUri = QgsDataSourceURI ( uri );
6161
@@ -94,12 +94,12 @@ QgsPostgresProvider::QgsPostgresProvider( QString const & uri )
9494 mUseEstimatedMetadata = mUri .useEstimatedMetadata ();
9595 mSelectAtIdDisabled = mUri .selectAtIdDisabled ();
9696
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 ) );
103103
104104 // no table/query passed, the provider could be used to get tables
105105 if ( mQuery .isEmpty () )
@@ -763,7 +763,7 @@ void QgsPostgresProvider::appendPkParams( QgsFeatureId featureId, QStringList &p
763763 }
764764 }
765765
766- QgsDebugMsg ( " keys params: " + params.join ( " ; " ) );
766+ QgsDebugMsg ( QString ( " keys params: %1 " ). arg ( params.join ( " ; " ) ) );
767767 }
768768 break ;
769769
@@ -975,7 +975,7 @@ bool QgsPostgresProvider::loadFields()
975975{
976976 if ( !mIsQuery )
977977 {
978- QgsDebugMsg ( " Loading fields for table " + mTableName );
978+ QgsDebugMsg ( QString ( " Loading fields for table %1 " ). arg ( mTableName ) );
979979
980980 // Get the relation oid for use in later queries
981981 QString sql = QString ( " SELECT regclass(%1)::oid" ).arg ( quotedValue ( mQuery ) );
@@ -1133,7 +1133,7 @@ bool QgsPostgresProvider::loadFields()
11331133 }
11341134 else
11351135 {
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 " ) );
11371137 continue ;
11381138 }
11391139
@@ -1152,7 +1152,7 @@ bool QgsPostgresProvider::loadFields()
11521152 }
11531153 else
11541154 {
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 " ) );
11561156 continue ;
11571157 }
11581158
@@ -1319,7 +1319,7 @@ bool QgsPostgresProvider::hasSufficientPermsAndCapabilities()
13191319 while ( mQuery .contains ( regex ) );
13201320
13211321 // convert the custom query into a subquery
1322- mQuery = QString ( " %1 as %2" )
1322+ mQuery = QString ( " %1 AS %2" )
13231323 .arg ( mQuery )
13241324 .arg ( quotedIdentifier ( alias ) );
13251325
@@ -1417,34 +1417,17 @@ bool QgsPostgresProvider::determinePrimaryKey()
14171417 QString primaryKey = mUri .keyColumn ();
14181418 int idx = fieldNameIndex ( mUri .keyColumn () );
14191419
1420- if ( idx >= 0 )
1420+ if ( idx >= 0 && ( mAttributeFields [idx]. type () == QVariant::Int || mAttributeFields [idx]. type () == QVariant::LongLong ) )
14211421 {
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 ) )
14401423 {
14411424 mPrimaryKeyType = pktInt;
14421425 mPrimaryKeyAttrs << idx;
14431426 }
14441427 }
1445-
1446- if ( mPrimaryKeyType != pktInt )
1428+ else
14471429 {
1430+ QgsMessageLog::logMessage ( tr ( " No key field for view given." ), tr ( " PostGIS" ) );
14481431 mPrimaryKeyType = pktUnknown;
14491432 }
14501433 }
@@ -1488,6 +1471,25 @@ bool QgsPostgresProvider::determinePrimaryKey()
14881471 mPrimaryKeyType = ( mPrimaryKeyAttrs .size () == 1 && isInt ) ? pktInt : pktFidMap;
14891472 }
14901473 }
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+ }
14911493
14921494 mValid = mPrimaryKeyType != pktUnknown;
14931495
@@ -2603,7 +2605,7 @@ bool QgsPostgresProvider::getGeometryDetails()
26032605 {
26042606 sql = QString ( " SELECT %1 FROM %2 LIMIT 0" ).arg ( quotedIdentifier ( mGeometryColumn ) ).arg ( mQuery );
26052607
2606- QgsDebugMsg ( " Getting geometry column: " + sql );
2608+ QgsDebugMsg ( QString ( " Getting geometry column: %1 " ). arg ( sql ) );
26072609
26082610 QgsPostgresResult result = mConnectionRO ->PQexec ( sql );
26092611 if ( PGRES_TUPLES_OK == result.PQresultStatus () )
@@ -2635,60 +2637,73 @@ bool QgsPostgresProvider::getGeometryDetails()
26352637 }
26362638 }
26372639 }
2640+ else
2641+ {
2642+ schemaName = " " ;
2643+ tableName = mQuery ;
2644+ }
2645+ }
2646+ else
2647+ {
2648+ mValid = false ;
2649+ return false ;
26382650 }
26392651 }
26402652
26412653 QString detectedType;
26422654 QString detectedSrid;
26432655
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 () )
26612657 {
26622658 // 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" )
26642660 .arg ( quotedValue ( tableName ) )
26652661 .arg ( quotedValue ( geomCol ) )
26662662 .arg ( quotedValue ( schemaName ) );
26672663
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 () ) );
26712667
2672- if ( result.PQresultStatus () == PGRES_TUPLES_OK )
2668+ if ( result.PQntuples () == 1 )
26732669 {
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 );
26802672 }
2681- else
2673+
2674+ if ( !detectedType.isEmpty () )
26822675 {
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+ }
26842699 }
26852700 }
26862701
26872702 if ( QgsPostgresConn::geomTypeFromPostgis ( detectedType ) == QGis::UnknownGeometry )
26882703 {
26892704 QgsPostgresLayerProperty layerProperty;
2690- layerProperty.schemaName = mSchemaName ;
2691- layerProperty.tableName = mTableName ;
2705+ layerProperty.schemaName = schemaName ;
2706+ layerProperty.tableName = tableName ;
26922707 layerProperty.geometryColName = mGeometryColumn ;
26932708 layerProperty.isGeography = mIsGeography ;
26942709
@@ -2758,10 +2773,10 @@ bool QgsPostgresProvider::getGeometryDetails()
27582773 mDetectedGeomType = QgsPostgresConn::geomTypeFromPostgis ( detectedType );
27592774 mDetectedSrid = detectedSrid;
27602775
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 ) );
27652780
27662781 mValid = ( mDetectedGeomType != QGis::UnknownGeometry || mRequestedGeomType != QGis::UnknownGeometry )
27672782 && ( !mDetectedSrid .isEmpty () || !mRequestedSrid .isEmpty () );
@@ -2781,8 +2796,8 @@ bool QgsPostgresProvider::getGeometryDetails()
27812796 mEnabledCapabilities &= ~( QgsVectorDataProvider::ChangeGeometries | QgsVectorDataProvider::AddFeatures );
27822797 }
27832798
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 ) );
27862801
27872802 return mValid ;
27882803}
@@ -2864,10 +2879,10 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
28642879 }
28652880 schemaTableName += quotedIdentifier ( tableName );
28662881
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 ) );
28712886
28722887 // create the table
28732888 QgsPostgresConn *conn = QgsPostgresConn::connectDb ( dsUri.connectionInfo (), false );
@@ -2918,7 +2933,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29182933 primaryKeyType = " serial" ;
29192934#if 0
29202935 // TODO: check the feature count to choose if create a serial8 pk field
2921- if ( layer->featureCount() > 0xFFFFFF )
2936+ if ( layer->featureCount() > 0xffffffff )
29222937 {
29232938 primaryKeyType = "serial8";
29242939 }
@@ -3007,7 +3022,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
30073022 }
30083023 conn->disconnect ();
30093024
3010- QgsDebugMsg ( " layer " + schemaTableName + " created. " );
3025+ QgsDebugMsg ( QString ( " layer %1 created " ). arg ( schemaTableName ) );
30113026
30123027 // use the provider to edit the table
30133028 dsUri.setDataSource ( schemaName, tableName, geometryColumn, QString (), primaryKey );
@@ -3057,13 +3072,11 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
30573072 return QgsVectorLayerImport::ErrAttributeTypeUnsupported;
30583073 }
30593074
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+ );
30673080
30683081 flist.append ( fld );
30693082 if ( oldToNewAttrIdxMap )
0 commit comments