| 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 | } |
|---|