Changeset 12510

Show
Ignore:
Timestamp:
10/23/07 08:41:24 (1 year ago)
Author:
mloskot
Message:

Ported fix for detection of single-/multi-column Primary Key in PostgreSQL driver to branches/1.4 (Ticket #1889).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.4/gdal/ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp

    r11351 r12510  
    135135    } 
    136136 
    137     /* TODO make changes corresponded to Frank issues 
    138         
    139     sprintf ( szCommand, 
    140               "SELECT a.attname " 
    141               "FROM pg_attribute a, pg_constraint c, pg_class cl " 
    142               "WHERE c.contype='p' AND c.conrelid=cl.oid " 
    143               "AND a.attnum = c.conkey[1] AND a.attrelid=cl.oid " 
    144               "AND cl.relname = '%s'", 
    145               pszTableIn ); 
    146  
    147     hResult = PQexec(hPGConn, szCommand ); 
    148  
    149     if ( hResult && PQntuples( hResult ) == 1 && PQgetisnull( hResult,0,0 ) == false ) 
    150     { 
    151         sprintf( szPrimaryKey, "%s", PQgetvalue(hResult,0,0) ); 
    152         CPLDebug( "OGR_PG", "Primary key name (FID): %s", szPrimaryKey ); 
     137    CPLString osSchemaClause; 
     138    if( pszSchemaNameIn ) 
     139        osSchemaClause.Printf("AND n.nspname='%s'", pszSchemaNameIn); 
     140 
     141    osCommand.Printf("SELECT a.attname, a.attnum, t.typname, " 
     142              "t.typname = ANY(ARRAY['int2','int4','serial']) AS isfid " 
     143              "FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n, pg_index i " 
     144              "WHERE a.attnum > 0 AND a.attrelid = c.oid " 
     145              "AND a.atttypid = t.oid AND c.relnamespace = n.oid " 
     146              "AND c.oid = i.indrelid AND i.indisprimary = 't' " 
     147              "AND t.typname !~ '^geom' AND c.relname = '%s' " 
     148              "AND a.attnum = ANY (i.indkey) " 
     149              "%s" 
     150              "ORDER BY a.attnum", 
     151              pszTableIn, osSchemaClause.c_str() ); 
     152 
     153    hResult = PQexec(hPGConn, osCommand.c_str() ); 
     154 
     155    if ( hResult && PGRES_TUPLES_OK == PQresultStatus( hResult) ) 
     156    { 
     157        if ( PQntuples( hResult ) == 1 && PQgetisnull( hResult,0,0 ) == false ) 
     158        { 
     159            /* Check if single-field PK can be represented as 32-bit integer. */ 
     160            CPLString osValue(PQgetvalue(hResult, 0, 3)); 
     161            if( osValue == "t" ) 
     162            { 
     163                osPrimaryKey.Printf( "%s", PQgetvalue(hResult,0,0) ); 
     164                CPLDebug( "OGR_PG", "Primary key name (FID): %s", osPrimaryKey.c_str() ); 
     165            } 
     166        } 
     167        else 
     168        { 
     169            CPLError( CE_Warning, CPLE_AppDefined, 
     170                      "Multi-column primary key detected but not supported." ); 
     171        } 
    153172    } 
    154173    else 
     
    156175        CPLError( CE_Failure, CPLE_AppDefined, 
    157176                  "%s", PQerrorMessage(hPGConn) ); 
     177    } 
     178 
     179    /* TODO - mloskot: Remove this warning after multi-column PK is supported. */ 
     180    if( osPrimaryKey.empty() || osPrimaryKey == "ogc_fid" ) 
     181    { 
    158182        CPLError( CE_Warning, CPLE_AppDefined, 
    159                   "Unable to detect table primary key. Use default 'ogc_fid'"); 
    160     }*/ 
     183                  "Unable to detect single-column primary key for '%s'. Use default 'ogc_fid'", 
     184                  pszTableIn); 
     185    } 
    161186 
    162187/* -------------------------------------------------------------------- */ 
     
    170195 
    171196        CPLString osSchemaClause; 
    172         if (pszSchemaNameIn
    173           osSchemaClause.Printf("AND n.nspname='%s'", pszSchemaNameIn); 
     197        if( pszSchemaNameIn
     198            osSchemaClause.Printf("AND n.nspname='%s'", pszSchemaNameIn); 
    174199 
    175200        osCommand.Printf(