LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/pg - ogr_pg.h (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 14 14 100.0 %
Date: 2012-04-28 Functions: 12 12 100.0 %

       1                 : /******************************************************************************
       2                 :  * $Id: ogr_pg.h 24334 2012-04-28 14:32:29Z rouault $
       3                 :  *
       4                 :  * Project:  OpenGIS Simple Features Reference Implementation
       5                 :  * Purpose:  Private definitions for OGR/PostgreSQL driver.
       6                 :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2000, Frank Warmerdam
      10                 :  *
      11                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      12                 :  * copy of this software and associated documentation files (the "Software"),
      13                 :  * to deal in the Software without restriction, including without limitation
      14                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15                 :  * and/or sell copies of the Software, and to permit persons to whom the
      16                 :  * Software is furnished to do so, subject to the following conditions:
      17                 :  *
      18                 :  * The above copyright notice and this permission notice shall be included
      19                 :  * in all copies or substantial portions of the Software.
      20                 :  *
      21                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27                 :  * DEALINGS IN THE SOFTWARE.
      28                 :  ****************************************************************************/
      29                 : 
      30                 : #ifndef _OGR_PG_H_INCLUDED
      31                 : #define _OGR_PG_H_INCLUDED
      32                 : 
      33                 : #include "ogrsf_frmts.h"
      34                 : #include "libpq-fe.h"
      35                 : #include "cpl_string.h"
      36                 : 
      37                 : #include "ogrpgutility.h"
      38                 : 
      39                 : /* These are the OIDs for some builtin types, as returned by PQftype(). */
      40                 : /* They were copied from pg_type.h in src/include/catalog/pg_type.h */
      41                 : 
      42                 : #define BOOLOID                 16
      43                 : #define BYTEAOID                17
      44                 : #define CHAROID                 18
      45                 : #define NAMEOID                 19
      46                 : #define INT8OID                 20
      47                 : #define INT2OID                 21
      48                 : #define INT2VECTOROID           22
      49                 : #define INT4OID                 23
      50                 : #define REGPROCOID              24
      51                 : #define TEXTOID                 25
      52                 : #define OIDOID                  26
      53                 : #define TIDOID                  27
      54                 : #define XIDOID                  28
      55                 : #define CIDOID                  29
      56                 : #define OIDVECTOROID            30
      57                 : #define FLOAT4OID               700
      58                 : #define FLOAT8OID               701
      59                 : #define INT4ARRAYOID            1007
      60                 : #define TEXTARRAYOID            1009
      61                 : #define BPCHARARRAYOID          1014
      62                 : #define VARCHARARRAYOID         1015
      63                 : #define FLOAT4ARRAYOID          1021
      64                 : #define FLOAT8ARRAYOID          1022
      65                 : #define BPCHAROID   1042
      66                 : #define VARCHAROID    1043
      67                 : #define DATEOID     1082
      68                 : #define TIMEOID     1083
      69                 : #define TIMESTAMPOID          1114
      70                 : #define TIMESTAMPTZOID          1184
      71                 : #define NUMERICOID              1700
      72                 : 
      73                 : CPLString OGRPGEscapeString(PGconn *hPGConn,
      74                 :                             const char* pszStrValue, int nMaxLength,
      75                 :                             const char* pszFieldName);
      76                 : CPLString OGRPGEscapeColumnName(const char* pszColumnName);
      77                 : 
      78                 : #define UNDETERMINED_SRID       -2 /* Special value when we haven't yet looked for SRID */
      79                 : 
      80                 : /************************************************************************/
      81                 : /*                            OGRPGLayer                                */
      82                 : /************************************************************************/
      83                 : 
      84                 : 
      85                 : class OGRPGDataSource;
      86                 : 
      87                 : class OGRPGLayer : public OGRLayer
      88                 : {
      89                 :   protected:
      90                 :     OGRFeatureDefn     *poFeatureDefn;
      91                 : 
      92                 :     // Layer spatial reference system, and srid.
      93                 :     OGRSpatialReference *poSRS;
      94                 :     int                 nSRSId;
      95                 :     int                 nCoordDimension;
      96                 : 
      97                 :     int                 iNextShapeId;
      98                 : 
      99                 :     static char        *GByteArrayToBYTEA( const GByte* pabyData, int nLen);
     100                 :     static char        *GeometryToBYTEA( OGRGeometry * );
     101                 :     static GByte       *BYTEAToGByteArray( const char *pszBytea, int* pnLength );
     102                 :     static OGRGeometry *BYTEAToGeometry( const char * );
     103                 :     static OGRGeometry *HEXToGeometry( const char * );
     104                 :     static OGRGeometry *EWKBToGeometry( GByte* pabyWKB, int nLength );
     105                 :     static char        *GeometryToHex( OGRGeometry * poGeometry, int nSRSId );
     106                 :     Oid                 GeometryToOID( OGRGeometry * );
     107                 :     OGRGeometry        *OIDToGeometry( Oid );
     108                 : 
     109                 :     OGRPGDataSource    *poDS;
     110                 : 
     111                 :     char               *pszQueryStatement;
     112                 : 
     113                 :     char               *pszCursorName;
     114                 :     PGresult           *hCursorResult;
     115                 : 
     116                 :     int                 nResultOffset;
     117                 : 
     118                 :     int                 bHasWkb;
     119                 :     int                 bWkbAsOid;
     120                 :     int                 bHasPostGISGeometry;
     121                 :     int                 bHasPostGISGeography;
     122                 :     char                *pszGeomColumn;
     123                 : 
     124                 :     int                 bHasFid;
     125                 :     char                *pszFIDColumn;
     126                 : 
     127                 :     int                 bCanUseBinaryCursor;
     128                 :     int                *panMapFieldNameToIndex;
     129                 : 
     130                 :     int                 ParsePGDate( const char *, OGRField * );
     131                 : 
     132                 :     void                SetInitialQueryCursor();
     133                 :     void                CloseCursor();
     134                 : 
     135                 :     OGRErr              RunGetExtentRequest( OGREnvelope *psExtent, int bForce,
     136                 :                                              CPLString osCommand);
     137                 :     void                CreateMapFromFieldNameToIndex();
     138                 : 
     139                 :     OGRFeatureDefn     *ReadResultDefinition(PGresult *hInitialResultIn);
     140                 : 
     141                 :     OGRFeature         *RecordToFeature( int iRecord );
     142                 :     OGRFeature         *GetNextRawFeature();
     143                 : 
     144                 :   public:
     145                 :                         OGRPGLayer();
     146                 :     virtual             ~OGRPGLayer();
     147                 : 
     148                 :     virtual void        ResetReading();
     149                 : 
     150                 :     virtual OGRFeatureDefn *    GetLayerDefn();
     151                 : 
     152                 :     virtual OGRErr      StartTransaction();
     153                 :     virtual OGRErr      CommitTransaction();
     154                 :     virtual OGRErr      RollbackTransaction();
     155                 : 
     156                 :     virtual OGRSpatialReference *GetSpatialRef();
     157                 : 
     158                 :     virtual const char *GetFIDColumn();
     159                 :     virtual const char *GetGeometryColumn();
     160                 : 
     161                 :     virtual OGRErr      SetNextByIndex( long nIndex );
     162                 : };
     163                 : 
     164                 : /************************************************************************/
     165                 : /*                           OGRPGTableLayer                            */
     166                 : /************************************************************************/
     167                 : 
     168                 : typedef enum
     169                 : {
     170                 :     GEOM_TYPE_UNKNOWN = 0,
     171                 :     GEOM_TYPE_GEOMETRY = 1,
     172                 :     GEOM_TYPE_GEOGRAPHY = 2
     173                 : } PostgisType;
     174                 : 
     175                 : class OGRPGTableLayer : public OGRPGLayer
     176                 : {
     177                 :     int                 bUpdateAccess;
     178                 : 
     179                 :     OGRFeatureDefn     *ReadTableDefinition();
     180                 : 
     181                 :     void                BuildWhere(void);
     182                 :     CPLString           BuildFields(void);
     183                 :     void                BuildFullQueryStatement(void);
     184                 : 
     185                 :     char               *pszTableName;
     186                 :     char               *pszSchemaName;
     187                 :     char               *pszSqlTableName;
     188                 : 
     189                 :     CPLString           osPrimaryKey;
     190                 : 
     191                 :     int                 bGeometryInformationSet;
     192                 :     OGRwkbGeometryType  nGeomType;
     193                 : 
     194                 :     /* Name of the parent table with the geometry definition if it is a derived table or NULL */
     195                 :     char               *pszSqlGeomParentTableName;
     196                 : 
     197                 :     CPLString           osDefnName;
     198                 : 
     199                 :     CPLString           osQuery;
     200                 :     CPLString           osWHERE;
     201                 : 
     202                 :     int                 bLaunderColumnNames;
     203                 :     int                 bPreservePrecision;
     204                 :     int                 bUseCopy;
     205                 :     int                 bCopyActive;
     206                 :     int                 bFIDColumnInCopyFields;
     207                 : 
     208                 :     OGRErr    CreateFeatureViaCopy( OGRFeature *poFeature );
     209                 :     OGRErr    CreateFeatureViaInsert( OGRFeature *poFeature );
     210                 :     CPLString           BuildCopyFields(int bSetFID);
     211                 : 
     212                 :     void                AppendFieldValue(PGconn *hPGConn, CPLString& osCommand,
     213                 :                                          OGRFeature* poFeature, int i);
     214                 :                   
     215                 :     int                 bHasWarnedIncompatibleGeom;
     216                 :     void                CheckGeomTypeCompatibility(OGRGeometry* poGeom);
     217                 : 
     218                 :     int                 bRetrieveFID;
     219                 :     int                 bHasWarnedAlreadySetFID;
     220                 : 
     221                 : public:
     222                 :                         OGRPGTableLayer( OGRPGDataSource *,
     223                 :                                          CPLString& osCurrentSchema,
     224                 :                                          const char * pszTableName,
     225                 :                                          const char * pszSchemaName,
     226                 :                                          const char * pszGeomColumnIn,
     227                 :                                          int bUpdate,
     228                 :                                          int bAdvertizeGeomColumn,
     229                 :                                          int nSRSId = UNDETERMINED_SRID );
     230                 :                         ~OGRPGTableLayer();
     231                 : 
     232                 :     void                SetGeometryInformation(const char* pszGeomType,
     233                 :                                                int nCoordDimension,
     234                 :                                                int nSRID,
     235                 :                                                PostgisType ePostgisType);
     236                 : 
     237           64604 :     virtual const char  *GetName() { return osDefnName.c_str(); }
     238                 :     virtual OGRwkbGeometryType GetGeomType();
     239                 : 
     240                 :     virtual OGRFeatureDefn *    GetLayerDefn();
     241                 : 
     242                 :     virtual OGRFeature *GetFeature( long nFeatureId );
     243                 :     virtual void        ResetReading();
     244                 :     virtual OGRFeature *GetNextFeature();
     245                 :     virtual int         GetFeatureCount( int );
     246                 : 
     247                 :     virtual void        SetSpatialFilter( OGRGeometry * );
     248                 : 
     249                 :     virtual OGRErr      SetAttributeFilter( const char * );
     250                 : 
     251                 :     virtual OGRErr      SetFeature( OGRFeature *poFeature );
     252                 :     virtual OGRErr      DeleteFeature( long nFID );
     253                 :     virtual OGRErr      CreateFeature( OGRFeature *poFeature );
     254                 : 
     255                 :     virtual OGRErr      CreateField( OGRFieldDefn *poField,
     256                 :                                      int bApproxOK = TRUE );
     257                 :     virtual OGRErr      DeleteField( int iField );
     258                 :     virtual OGRErr      AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags );
     259                 : 
     260                 :     virtual OGRSpatialReference *GetSpatialRef();
     261                 : 
     262                 :     virtual int         TestCapability( const char * );
     263                 : 
     264                 :     virtual OGRErr      GetExtent( OGREnvelope *psExtent, int bForce );
     265                 : 
     266             140 :     const char*         GetTableName() { return pszTableName; }
     267             140 :     const char*         GetSchemaName() { return pszSchemaName; }
     268                 : 
     269                 :     // follow methods are not base class overrides
     270             108 :     void                SetLaunderFlag( int bFlag )
     271             108 :                                 { bLaunderColumnNames = bFlag; }
     272             108 :     void                SetPrecisionFlag( int bFlag )
     273             108 :                                 { bPreservePrecision = bFlag; }
     274                 : 
     275                 :     virtual OGRErr      StartCopy(int bSetFID);
     276                 :     virtual OGRErr      EndCopy();
     277                 : 
     278                 :     OGRFeatureDefn     *GetLayerDefnCanReturnNULL();
     279                 : };
     280                 : 
     281                 : /************************************************************************/
     282                 : /*                           OGRPGResultLayer                           */
     283                 : /************************************************************************/
     284                 : 
     285                 : class OGRPGResultLayer : public OGRPGLayer
     286                 : {
     287                 :     void                BuildFullQueryStatement(void);
     288                 : 
     289                 :     char                *pszRawStatement;
     290                 : 
     291                 :     CPLString           osWHERE;
     292                 : 
     293                 :   public:
     294                 :                         OGRPGResultLayer( OGRPGDataSource *,
     295                 :                                           const char * pszRawStatement,
     296                 :                                           PGresult *hInitialResult );
     297                 :     virtual             ~OGRPGResultLayer();
     298                 : 
     299                 :     virtual void        ResetReading();
     300                 :     virtual int         GetFeatureCount( int );
     301                 : 
     302                 :     virtual void        SetSpatialFilter( OGRGeometry * );
     303                 : 
     304                 :     virtual OGRErr      GetExtent( OGREnvelope *psExtent, int bForce );
     305                 : 
     306                 :     virtual int         TestCapability( const char * );
     307                 : 
     308                 :     virtual OGRFeature *GetNextFeature();
     309                 : 
     310                 :     virtual OGRSpatialReference *GetSpatialRef();
     311                 : };
     312                 : 
     313                 : /************************************************************************/
     314                 : /*                           OGRPGDataSource                            */
     315                 : /************************************************************************/
     316                 : class OGRPGDataSource : public OGRDataSource
     317                 : {
     318                 :     typedef struct
     319                 :     {
     320                 :         int nMajor;
     321                 :         int nMinor;
     322                 :         int nRelease;
     323                 :     } PGver;
     324                 : 
     325                 :     OGRPGTableLayer   **papoLayers;
     326                 :     int                 nLayers;
     327                 : 
     328                 :     char               *pszName;
     329                 :     char               *pszDBName;
     330                 : 
     331                 :     int                 bDSUpdate;
     332                 :     int                 bHavePostGIS;
     333                 :     int                 bHaveGeography;
     334                 : 
     335                 :     int                 nSoftTransactionLevel;
     336                 : 
     337                 :     PGconn              *hPGConn;
     338                 : 
     339                 :     int                 DeleteLayer( int iLayer );
     340                 : 
     341                 :     Oid                 nGeometryOID;
     342                 :     Oid                 nGeographyOID;
     343                 : 
     344                 :     // We maintain a list of known SRID to reduce the number of trips to
     345                 :     // the database to get SRSes.
     346                 :     int                 nKnownSRID;
     347                 :     int                 *panSRID;
     348                 :     OGRSpatialReference **papoSRS;
     349                 : 
     350                 :     OGRPGTableLayer     *poLayerInCopyMode;
     351                 : 
     352                 :     void                OGRPGDecodeVersionString(PGver* psVersion, const char* pszVer);
     353                 : 
     354                 :     CPLString           GetCurrentSchema();
     355                 : 
     356                 :     int                 nUndefinedSRID;
     357                 : 
     358                 :   public:
     359                 :     PGver               sPostgreSQLVersion;
     360                 :     PGver               sPostGISVersion;
     361                 : 
     362                 :     int                 bUseBinaryCursor;
     363                 :     int                 bBinaryTimeFormatIsInt8;
     364                 :     int                 bUseEscapeStringSyntax;
     365                 : 
     366               8 :     int                GetUndefinedSRID() const { return nUndefinedSRID; }
     367                 : 
     368                 :   public:
     369                 :                         OGRPGDataSource();
     370                 :                         ~OGRPGDataSource();
     371                 : 
     372           38854 :     PGconn              *GetPGConn() { return hPGConn; }
     373                 : 
     374                 :     int                 FetchSRSId( OGRSpatialReference * poSRS );
     375                 :     OGRSpatialReference *FetchSRS( int nSRSId );
     376                 :     OGRErr              InitializeMetadataTables();
     377                 : 
     378                 :     int                 Open( const char *, int bUpdate, int bTestOpen );
     379                 :     OGRPGTableLayer*    OpenTable( CPLString& osCurrentSchema,
     380                 :                                    const char * pszTableName,
     381                 :                                    const char * pszSchemaName,
     382                 :                                    const char * pszGeomColumnIn,
     383                 :                                    int bUpdate, int bTestOpen,
     384                 :                                    int bAdvertizeGeomColumn );
     385                 : 
     386             206 :     const char          *GetName() { return pszName; }
     387             504 :     int                 GetLayerCount() { return nLayers; }
     388                 :     OGRLayer            *GetLayer( int );
     389                 :     OGRLayer            *GetLayerByName(const char * pszName);
     390                 : 
     391                 :     virtual OGRLayer    *CreateLayer( const char *,
     392                 :                                       OGRSpatialReference * = NULL,
     393                 :                                       OGRwkbGeometryType = wkbUnknown,
     394                 :                                       char ** = NULL );
     395                 : 
     396                 :     int                 TestCapability( const char * );
     397                 : 
     398                 :     OGRErr              SoftStartTransaction();
     399                 :     OGRErr              SoftCommit();
     400                 :     OGRErr              SoftRollback();
     401                 : 
     402                 :     OGRErr              FlushSoftTransaction();
     403                 : 
     404             424 :     Oid                 GetGeometryOID() { return nGeometryOID; }
     405             432 :     Oid                 GetGeographyOID() { return nGeographyOID; }
     406                 : 
     407                 :     virtual OGRLayer *  ExecuteSQL( const char *pszSQLCommand,
     408                 :                                     OGRGeometry *poSpatialFilter,
     409                 :                                     const char *pszDialect );
     410                 :     virtual void        ReleaseResultSet( OGRLayer * poLayer );
     411                 : 
     412                 :     char               *LaunderName( const char * );
     413                 : 
     414                 :     int                 UseCopy();
     415                 :     void                StartCopy( OGRPGTableLayer *poPGLayer );
     416                 :     OGRErr              EndCopy( );
     417                 :     int                 CopyInProgress( );
     418                 : };
     419                 : 
     420                 : /************************************************************************/
     421                 : /*                             OGRPGDriver                              */
     422                 : /************************************************************************/
     423                 : 
     424                 : class OGRPGDriver : public OGRSFDriver
     425             389 : {
     426                 :   public:
     427                 :                 ~OGRPGDriver();
     428                 : 
     429                 :     const char *GetName();
     430                 :     OGRDataSource *Open( const char *, int );
     431                 : 
     432                 :     virtual OGRDataSource *CreateDataSource( const char *pszName,
     433                 :                                              char ** = NULL );
     434                 : 
     435                 :     int                 TestCapability( const char * );
     436                 : };
     437                 : 
     438                 : #endif /* ndef _OGR_PG_H_INCLUDED */
     439                 : 

Generated by: LCOV version 1.7