LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/pgdump - ogrpgdumpdatasource.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 219 162 74.0 %
Date: 2013-03-30 Functions: 14 9 64.3 %

       1                 : /******************************************************************************
       2                 :  * $Id: ogrpgdumpdatasource.cpp 25366 2012-12-27 18:38:53Z rouault $
       3                 :  *
       4                 :  * Project:  OpenGIS Simple Features Reference Implementation
       5                 :  * Purpose:  Implements OGRPGDumpDataSource class.
       6                 :  * Author:   Even Rouault, <even dot rouault at mines dash paris dot org>
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2010, Even Rouault
      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                 : #include <string.h>
      31                 : #include "ogr_pgdump.h"
      32                 : #include "cpl_conv.h"
      33                 : #include "cpl_string.h"
      34                 : 
      35                 : CPL_CVSID("$Id: ogrpgdumpdatasource.cpp 25366 2012-12-27 18:38:53Z rouault $");
      36                 : 
      37                 : /************************************************************************/
      38                 : /*                      OGRPGDumpDataSource()                           */
      39                 : /************************************************************************/
      40                 : 
      41               3 : OGRPGDumpDataSource::OGRPGDumpDataSource(const char* pszName,
      42               3 :                                          char** papszOptions)
      43                 : 
      44                 : {
      45               3 :     nLayers = 0;
      46               3 :     papoLayers = NULL;
      47               3 :     this->pszName = CPLStrdup(pszName);
      48               3 :     bTriedOpen = FALSE;
      49               3 :     fp = NULL;
      50               3 :     bInTransaction = FALSE;
      51               3 :     poLayerInCopyMode = NULL;
      52                 : 
      53               3 :     const char *pszCRLFFormat = CSLFetchNameValue( papszOptions, "LINEFORMAT");
      54                 : 
      55                 :     int bUseCRLF;
      56               3 :     if( pszCRLFFormat == NULL )
      57                 :     {
      58                 : #ifdef WIN32
      59                 :         bUseCRLF = TRUE;
      60                 : #else
      61               1 :         bUseCRLF = FALSE;
      62                 : #endif
      63                 :     }
      64               2 :     else if( EQUAL(pszCRLFFormat,"CRLF") )
      65               1 :         bUseCRLF = TRUE;
      66               1 :     else if( EQUAL(pszCRLFFormat,"LF") )
      67               1 :         bUseCRLF = FALSE;
      68                 :     else
      69                 :     {
      70                 :         CPLError( CE_Warning, CPLE_AppDefined, 
      71                 :                   "LINEFORMAT=%s not understood, use one of CRLF or LF.",
      72               0 :                   pszCRLFFormat );
      73                 : #ifdef WIN32
      74                 :         bUseCRLF = TRUE;
      75                 : #else
      76               0 :         bUseCRLF = FALSE;
      77                 : #endif
      78                 :     }
      79               3 :     pszEOL = (bUseCRLF) ? "\r\n" : "\n";
      80               3 : }
      81                 : 
      82                 : /************************************************************************/
      83                 : /*                          ~OGRPGDumpDataSource()                          */
      84                 : /************************************************************************/
      85                 : 
      86               3 : OGRPGDumpDataSource::~OGRPGDumpDataSource()
      87                 : 
      88                 : {
      89                 :     int i;
      90                 : 
      91               3 :     if (fp)
      92                 :     {
      93               3 :         Commit();
      94               3 :         VSIFCloseL(fp);
      95               3 :         fp = NULL;
      96                 :     }
      97                 : 
      98               6 :     for(i=0;i<nLayers;i++)
      99               3 :         delete papoLayers[i];
     100               3 :     CPLFree(papoLayers);
     101               3 :     CPLFree(pszName);
     102               3 : }
     103                 : 
     104                 : /************************************************************************/
     105                 : /*                         StartTransaction()                           */
     106                 : /************************************************************************/
     107                 : 
     108               3 : void OGRPGDumpDataSource::StartTransaction()
     109                 : {
     110               3 :     if (bInTransaction)
     111               0 :         return;
     112               3 :     bInTransaction = TRUE;
     113               3 :     Log("BEGIN");
     114                 : }
     115                 : 
     116                 : /************************************************************************/
     117                 : /*                              Commit()                                */
     118                 : /************************************************************************/
     119                 : 
     120               6 : void OGRPGDumpDataSource::Commit()
     121                 : {
     122               6 :     EndCopy();
     123                 : 
     124               6 :     if (!bInTransaction)
     125               3 :         return;
     126               3 :     bInTransaction = FALSE;
     127               3 :     Log("COMMIT");
     128                 : }
     129                 : 
     130                 : /************************************************************************/
     131                 : /*                            LaunderName()                             */
     132                 : /************************************************************************/
     133                 : 
     134              16 : char *OGRPGDumpDataSource::LaunderName( const char *pszSrcName )
     135                 : 
     136                 : {
     137              16 :     char    *pszSafeName = CPLStrdup( pszSrcName );
     138                 : 
     139             118 :     for( int i = 0; pszSafeName[i] != '\0'; i++ )
     140                 :     {
     141             102 :         pszSafeName[i] = (char) tolower( pszSafeName[i] );
     142             102 :         if( pszSafeName[i] == '\'' || pszSafeName[i] == '-' || pszSafeName[i] == '#' )
     143               0 :             pszSafeName[i] = '_';
     144                 :     }
     145                 : 
     146              16 :     if( strcmp(pszSrcName,pszSafeName) != 0 )
     147                 :         CPLDebug("PG","LaunderName('%s') -> '%s'", 
     148              13 :                  pszSrcName, pszSafeName);
     149                 : 
     150              16 :     return pszSafeName;
     151                 : }
     152                 : 
     153                 : /************************************************************************/
     154                 : /*                            CreateLayer()                             */
     155                 : /************************************************************************/
     156                 : 
     157                 : OGRLayer *
     158               3 : OGRPGDumpDataSource::CreateLayer( const char * pszLayerName,
     159                 :                                   OGRSpatialReference *poSRS,
     160                 :                                   OGRwkbGeometryType eType,
     161                 :                                   char ** papszOptions )
     162                 : 
     163                 : {
     164               3 :     CPLString            osCommand;
     165               3 :     const char          *pszGeomType = NULL;
     166               3 :     char                *pszTableName = NULL;
     167               3 :     char                *pszSchemaName = NULL;
     168               3 :     int                  nDimension = 3;
     169               3 :     int                  bHavePostGIS = TRUE;
     170                 : 
     171               3 :     const char* pszFIDColumnName = CSLFetchNameValue(papszOptions, "FID");
     172               3 :     CPLString osFIDColumnName;
     173               3 :     if (pszFIDColumnName == NULL)
     174               3 :         osFIDColumnName = "OGC_FID";
     175                 :     else
     176                 :     {
     177               0 :         if( CSLFetchBoolean(papszOptions,"LAUNDER", TRUE) )
     178                 :         {
     179               0 :             char* pszLaunderedFid = LaunderName(pszFIDColumnName);
     180               0 :             osFIDColumnName += OGRPGDumpEscapeColumnName(pszLaunderedFid);
     181               0 :             CPLFree(pszLaunderedFid);
     182                 :         }
     183                 :         else
     184               0 :             osFIDColumnName += OGRPGDumpEscapeColumnName(pszFIDColumnName);
     185                 :     }
     186               3 :     pszFIDColumnName = osFIDColumnName.c_str();
     187                 : 
     188               3 :     if (strncmp(pszLayerName, "pg", 2) == 0)
     189                 :     {
     190                 :         CPLError(CE_Warning, CPLE_AppDefined,
     191               0 :                  "The layer name should not begin by 'pg' as it is a reserved prefix");
     192                 :     }
     193                 :     
     194                 :     //bHavePostGIS = CSLFetchBoolean(papszOptions,"POSTGIS", TRUE);
     195                 : 
     196               3 :     int bCreateTable = CSLFetchBoolean(papszOptions,"CREATE_TABLE", TRUE);
     197               3 :     int bCreateSchema = CSLFetchBoolean(papszOptions,"CREATE_SCHEMA", TRUE);
     198               3 :     const char* pszDropTable = CSLFetchNameValueDef(papszOptions,"DROP_TABLE", "YES");
     199                 : 
     200               3 :     if( wkbFlatten(eType) == eType )
     201               3 :         nDimension = 2;
     202                 : 
     203               3 :     if( CSLFetchNameValue( papszOptions, "DIM") != NULL )
     204               1 :         nDimension = atoi(CSLFetchNameValue( papszOptions, "DIM"));
     205                 : 
     206                 :     /* Should we turn layers with None geometry type as Unknown/GEOMETRY */
     207                 :     /* so they are still recorded in geometry_columns table ? (#4012) */
     208                 :     int bNoneAsUnknown = CSLTestBoolean(CSLFetchNameValueDef(
     209               3 :                                     papszOptions, "NONE_AS_UNKNOWN", "NO"));
     210               3 :     if (bNoneAsUnknown && eType == wkbNone)
     211               0 :         eType = wkbUnknown;
     212               3 :     else if (eType == wkbNone)
     213               1 :         bHavePostGIS = FALSE;
     214                 : 
     215                 :     int bExtractSchemaFromLayerName = CSLTestBoolean(CSLFetchNameValueDef(
     216               3 :                                     papszOptions, "EXTRACT_SCHEMA_FROM_LAYER_NAME", "YES"));
     217                 : 
     218                 :     /* Postgres Schema handling:
     219                 :        Extract schema name from input layer name or passed with -lco SCHEMA.
     220                 :        Set layer name to "schema.table" or to "table" if schema == current_schema()
     221                 :        Usage without schema name is backwards compatible
     222                 :     */
     223               3 :     const char* pszDotPos = strstr(pszLayerName,".");
     224               3 :     if ( pszDotPos != NULL && bExtractSchemaFromLayerName )
     225                 :     {
     226               0 :       int length = pszDotPos - pszLayerName;
     227               0 :       pszSchemaName = (char*)CPLMalloc(length+1);
     228               0 :       strncpy(pszSchemaName, pszLayerName, length);
     229               0 :       pszSchemaName[length] = '\0';
     230                 : 
     231               0 :       if( CSLFetchBoolean(papszOptions,"LAUNDER", TRUE) )
     232               0 :           pszTableName = LaunderName( pszDotPos + 1 ); //skip "."
     233                 :       else
     234               0 :           pszTableName = CPLStrdup( pszDotPos + 1 ); //skip "."
     235                 :     }
     236                 :     else
     237                 :     {
     238               3 :       pszSchemaName = NULL;
     239               3 :       if( CSLFetchBoolean(papszOptions,"LAUNDER", TRUE) )
     240               3 :           pszTableName = LaunderName( pszLayerName ); //skip "."
     241                 :       else
     242               0 :           pszTableName = CPLStrdup( pszLayerName ); //skip "."
     243                 :     }
     244                 : 
     245               3 :     Commit();
     246                 : 
     247                 : /* -------------------------------------------------------------------- */
     248                 : /*      Set the default schema for the layers.                          */
     249                 : /* -------------------------------------------------------------------- */
     250               3 :     if( CSLFetchNameValue( papszOptions, "SCHEMA" ) != NULL )
     251                 :     {
     252               2 :         CPLFree(pszSchemaName);
     253               2 :         pszSchemaName = CPLStrdup(CSLFetchNameValue( papszOptions, "SCHEMA" ));
     254               2 :         if (bCreateSchema)
     255                 :         {
     256               2 :             osCommand.Printf("CREATE SCHEMA \"%s\"", pszSchemaName);
     257               2 :             Log(osCommand);
     258                 :         }
     259                 :     }
     260                 : 
     261               3 :     if ( pszSchemaName == NULL)
     262                 :     {
     263               1 :         pszSchemaName = CPLStrdup("public");
     264                 :     }
     265                 : 
     266                 : /* -------------------------------------------------------------------- */
     267                 : /*      Do we already have this layer?                                  */
     268                 : /* -------------------------------------------------------------------- */
     269                 :     int iLayer;
     270                 : 
     271               3 :     for( iLayer = 0; iLayer < nLayers; iLayer++ )
     272                 :     {
     273               0 :         if( EQUAL(pszLayerName,papoLayers[iLayer]->GetLayerDefn()->GetName()) )
     274                 :         {
     275                 :             CPLError( CE_Failure, CPLE_AppDefined,
     276                 :                       "Layer %s already exists, CreateLayer failed.\n", 
     277               0 :                       pszLayerName );
     278               0 :             CPLFree( pszTableName );
     279               0 :             CPLFree( pszSchemaName );
     280               0 :             return NULL;
     281                 :         }
     282                 :     }
     283                 : 
     284                 : 
     285               3 :     if (bCreateTable && (EQUAL(pszDropTable, "YES") ||
     286                 :                          EQUAL(pszDropTable, "ON") ||
     287                 :                          EQUAL(pszDropTable, "TRUE") ||
     288                 :                          EQUAL(pszDropTable, "IF_EXISTS")))
     289                 :     {
     290               3 :         if (EQUAL(pszDropTable, "IF_EXISTS"))
     291               0 :             osCommand.Printf("DROP TABLE IF EXISTS \"%s\".\"%s\" CASCADE", pszSchemaName, pszTableName );
     292                 :         else
     293               3 :             osCommand.Printf("DROP TABLE \"%s\".\"%s\" CASCADE", pszSchemaName, pszTableName );
     294               3 :         Log(osCommand);
     295                 :     }
     296                 :     
     297                 : /* -------------------------------------------------------------------- */
     298                 : /*      Handle the GEOM_TYPE option.                                    */
     299                 : /* -------------------------------------------------------------------- */
     300               3 :     pszGeomType = CSLFetchNameValue( papszOptions, "GEOM_TYPE" );
     301               3 :     if( pszGeomType == NULL )
     302                 :     {
     303               3 :         pszGeomType = "geometry";
     304                 :     }
     305                 : 
     306               3 :     if( !EQUAL(pszGeomType,"geometry") && !EQUAL(pszGeomType, "geography"))
     307                 :     {
     308                 :         CPLError( CE_Failure, CPLE_AppDefined,
     309                 :                     "GEOM_TYPE in PostGIS enabled databases must be 'geometry' or 'geography'.\n"
     310                 :                     "Creation of layer %s with GEOM_TYPE %s has failed.",
     311               0 :                     pszLayerName, pszGeomType );
     312               0 :         CPLFree( pszTableName );
     313               0 :         CPLFree( pszSchemaName );
     314               0 :         return NULL;
     315                 :     }
     316                 : 
     317                 : /* -------------------------------------------------------------------- */
     318                 : /*      Try to get the SRS Id of this spatial reference system,         */
     319                 : /*      adding tot the srs table if needed.                             */
     320                 : /* -------------------------------------------------------------------- */
     321               3 :     int nSRSId = -1;
     322               3 :     const char* pszPostgisVersion = CSLFetchNameValue( papszOptions, "POSTGIS_VERSION" );
     323               3 :     if( pszPostgisVersion != NULL && atoi(pszPostgisVersion) >= 2 )
     324               0 :         nSRSId = 0;
     325                 : 
     326               3 :     if( CSLFetchNameValue( papszOptions, "SRID") != NULL )
     327               1 :         nSRSId = atoi(CSLFetchNameValue( papszOptions, "SRID"));
     328                 :     else
     329                 :     {
     330               2 :         if (poSRS)
     331                 :         {
     332               0 :             const char* pszAuthorityName = poSRS->GetAuthorityName(NULL);
     333               0 :             if( pszAuthorityName != NULL && EQUAL( pszAuthorityName, "EPSG" ) )
     334                 :             {
     335                 :                 /* Assume the EPSG Id is the SRS ID. Might be a wrong guess ! */
     336               0 :                 nSRSId = atoi( poSRS->GetAuthorityCode(NULL) );
     337                 :             }
     338                 :             else
     339                 :             {
     340               0 :                 const char* pszGeogCSName = poSRS->GetAttrValue("GEOGCS");
     341               0 :                 if (pszGeogCSName != NULL && EQUAL(pszGeogCSName, "GCS_WGS_1984"))
     342               0 :                     nSRSId = 4326;
     343                 :             }
     344                 :         }
     345                 :     }
     346                 : 
     347               3 :     CPLString osEscapedTableNameSingleQuote = OGRPGDumpEscapeString(pszTableName, -1, "");
     348               3 :     const char* pszEscapedTableNameSingleQuote = osEscapedTableNameSingleQuote.c_str();
     349                 : 
     350               3 :     const char *pszGeometryType = OGRToOGCGeomType(eType);
     351                 : 
     352               3 :     const char *pszGFldName = NULL;
     353               3 :     if( bHavePostGIS && !EQUAL(pszGeomType, "geography"))
     354                 :     {
     355               2 :         if( CSLFetchNameValue( papszOptions, "GEOMETRY_NAME") != NULL )
     356               1 :             pszGFldName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME");
     357                 :         else
     358               1 :             pszGFldName = "wkb_geometry";
     359                 : 
     360               2 :         if( pszPostgisVersion == NULL || atoi(pszPostgisVersion) < 2 )
     361                 :         {
     362                 :             /* Sometimes there is an old cruft entry in the geometry_columns
     363                 :             * table if things were not properly cleaned up before.  We make
     364                 :             * an effort to clean out such cruft.
     365                 :             * Note: PostGIS 2.0 defines geometry_columns as a view (no clean up is needed)
     366                 :             */
     367                 :             osCommand.Printf(
     368                 :                     "DELETE FROM geometry_columns WHERE f_table_name = %s AND f_table_schema = '%s'",
     369               2 :                     pszEscapedTableNameSingleQuote, pszSchemaName );
     370               2 :             if (bCreateTable)
     371               2 :                 Log(osCommand);
     372                 :         }
     373                 :     }
     374                 : 
     375                 : 
     376               3 :     StartTransaction();
     377                 : 
     378                 : /* -------------------------------------------------------------------- */
     379                 : /*      Create a basic table with the FID.  Also include the            */
     380                 : /*      geometry if this is not a PostGIS enabled table.                */
     381                 : /* -------------------------------------------------------------------- */
     382                 :     
     383               3 :     CPLString osCreateTable;
     384                 :     int bTemporary = CSLFetchNameValue( papszOptions, "TEMPORARY" ) != NULL &&
     385               3 :                      CSLTestBoolean(CSLFetchNameValue( papszOptions, "TEMPORARY" ));
     386               3 :     if (bTemporary)
     387                 :     {
     388               0 :         CPLFree(pszSchemaName);
     389               0 :         pszSchemaName = CPLStrdup("pg_temp_1");
     390               0 :         osCreateTable.Printf("CREATE TEMPORARY TABLE \"%s\"", pszTableName);
     391                 :     }
     392                 :     else
     393               3 :         osCreateTable.Printf("CREATE TABLE \"%s\".\"%s\"", pszSchemaName, pszTableName);
     394                 : 
     395               3 :     if( !bHavePostGIS )
     396                 :     {
     397               1 :         if (eType == wkbNone)
     398                 :             osCommand.Printf(
     399                 :                     "%s ( "
     400                 :                     "   %s SERIAL, "
     401                 :                     "   CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
     402               1 :                     osCreateTable.c_str(), pszFIDColumnName, pszTableName, pszFIDColumnName );
     403                 :         else
     404                 :             osCommand.Printf(
     405                 :                     "%s ( "
     406                 :                     "   %s SERIAL, "
     407                 :                     "   WKB_GEOMETRY %s, "
     408                 :                     "   CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
     409               0 :                     osCreateTable.c_str(), pszFIDColumnName, pszGeomType, pszTableName, pszFIDColumnName );
     410                 :     }
     411               2 :     else if ( EQUAL(pszGeomType, "geography") )
     412                 :     {
     413               0 :         if( CSLFetchNameValue( papszOptions, "GEOMETRY_NAME") != NULL )
     414               0 :             pszGFldName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME");
     415                 :         else
     416               0 :             pszGFldName = "the_geog";
     417                 : 
     418               0 :         if (nSRSId)
     419                 :             osCommand.Printf(
     420                 :                      "%s ( %s SERIAL, \"%s\" geography(%s%s,%d), CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
     421               0 :                      osCreateTable.c_str(), pszFIDColumnName, pszGFldName, pszGeometryType, nDimension == 2 ? "" : "Z", nSRSId, pszTableName, pszFIDColumnName );
     422                 :         else
     423                 :             osCommand.Printf(
     424                 :                      "%s ( %s SERIAL, \"%s\" geography(%s%s), CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
     425               0 :                      osCreateTable.c_str(), pszFIDColumnName, pszGFldName, pszGeometryType, nDimension == 2 ? "" : "Z", pszTableName, pszFIDColumnName );
     426                 :     }
     427                 :     else
     428                 :     {
     429                 :         osCommand.Printf(
     430                 :                  "%s ( %s SERIAL, CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
     431               2 :                  osCreateTable.c_str(), pszFIDColumnName, pszTableName, pszFIDColumnName );
     432                 :     }
     433                 : 
     434               3 :     if (bCreateTable)
     435               3 :         Log(osCommand);
     436                 : 
     437                 : /* -------------------------------------------------------------------- */
     438                 : /*      Eventually we should be adding this table to a table of         */
     439                 : /*      "geometric layers", capturing the WKT projection, and           */
     440                 : /*      perhaps some other housekeeping.                                */
     441                 : /* -------------------------------------------------------------------- */
     442               3 :     if( bCreateTable && bHavePostGIS && !EQUAL(pszGeomType, "geography"))
     443                 :     {
     444                 :         osCommand.Printf(
     445                 :                 "SELECT AddGeometryColumn('%s',%s,'%s',%d,'%s',%d)",
     446                 :                 pszSchemaName, pszEscapedTableNameSingleQuote, pszGFldName,
     447               2 :                 nSRSId, pszGeometryType, nDimension );
     448               2 :         Log(osCommand);
     449                 :     }
     450                 : 
     451               3 :     if( bCreateTable && bHavePostGIS )
     452                 :     {
     453                 : /* -------------------------------------------------------------------- */
     454                 : /*      Create the spatial index.                                       */
     455                 : /*                                                                      */
     456                 : /*      We're doing this before we add geometry and record to the table */
     457                 : /*      so this may not be exactly the best way to do it.               */
     458                 : /* -------------------------------------------------------------------- */
     459               2 :         const char *pszSI = CSLFetchNameValue( papszOptions, "SPATIAL_INDEX" );
     460               2 :         if( pszSI == NULL || CSLTestBoolean(pszSI) )
     461                 :         {
     462                 :             osCommand.Printf("CREATE INDEX \"%s_geom_idx\" "
     463                 :                             "ON \"%s\".\"%s\" "
     464                 :                             "USING GIST (\"%s\")",
     465               2 :                     pszTableName, pszSchemaName, pszTableName, pszGFldName);
     466                 : 
     467               2 :             Log(osCommand);
     468                 :         }
     469                 :     }
     470                 : 
     471                 : /* -------------------------------------------------------------------- */
     472                 : /*      Create the layer object.                                        */
     473                 : /* -------------------------------------------------------------------- */
     474                 :     OGRPGDumpLayer     *poLayer;
     475                 : 
     476               3 :     int bWriteAsHex = !CSLFetchBoolean(papszOptions,"WRITE_EWKT_GEOM",FALSE);
     477                 : 
     478                 :     poLayer = new OGRPGDumpLayer( this, pszSchemaName, pszTableName, pszGFldName,
     479               3 :                                   pszFIDColumnName, nDimension, nSRSId, bWriteAsHex, bCreateTable );
     480               6 :     poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) );
     481               3 :     poLayer->SetPrecisionFlag( CSLFetchBoolean(papszOptions,"PRECISION",TRUE));
     482                 : 
     483               3 :     const char* pszOverrideColumnTypes = CSLFetchNameValue( papszOptions, "COLUMN_TYPES" );
     484               3 :     poLayer->SetOverrideColumnTypes(pszOverrideColumnTypes);
     485                 : 
     486                 : /* -------------------------------------------------------------------- */
     487                 : /*      Add layer to data source layer list.                            */
     488                 : /* -------------------------------------------------------------------- */
     489                 :     papoLayers = (OGRPGDumpLayer **)
     490               3 :         CPLRealloc( papoLayers,  sizeof(OGRPGDumpLayer *) * (nLayers+1) );
     491                 : 
     492               3 :     papoLayers[nLayers++] = poLayer;
     493                 : 
     494               3 :     CPLFree( pszTableName );
     495               3 :     CPLFree( pszSchemaName );
     496                 : 
     497               3 :     return poLayer;
     498                 : }
     499                 : 
     500                 : /************************************************************************/
     501                 : /*                           TestCapability()                           */
     502                 : /************************************************************************/
     503                 : 
     504               0 : int OGRPGDumpDataSource::TestCapability( const char * pszCap )
     505                 : 
     506                 : {
     507               0 :     if( EQUAL(pszCap,ODsCCreateLayer) )
     508               0 :         return TRUE;
     509                 :     else
     510               0 :         return FALSE;
     511                 : }
     512                 : 
     513                 : /************************************************************************/
     514                 : /*                              GetLayer()                              */
     515                 : /************************************************************************/
     516                 : 
     517               0 : OGRLayer *OGRPGDumpDataSource::GetLayer( int iLayer )
     518                 : 
     519                 : {
     520               0 :     if( iLayer < 0 || iLayer >= nLayers )
     521               0 :         return NULL;
     522                 :     else
     523               0 :         return papoLayers[iLayer];
     524                 : }
     525                 : 
     526                 : /************************************************************************/
     527                 : /*                                  Log()                               */
     528                 : /************************************************************************/
     529                 : 
     530              69 : void  OGRPGDumpDataSource::Log(const char* pszStr, int bAddSemiColumn)
     531                 : {
     532              69 :     if (fp == NULL)
     533                 :     {
     534               3 :         if (bTriedOpen)
     535               0 :             return;
     536               3 :         bTriedOpen = TRUE;
     537               3 :         fp = VSIFOpenL(pszName, "wb");
     538               3 :         if (fp == NULL)
     539                 :         {
     540               0 :             CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s", pszName);
     541               0 :             return;
     542                 :         }
     543                 :     }
     544                 : 
     545              69 :     if (bAddSemiColumn)
     546              47 :         VSIFPrintfL(fp, "%s;%s", pszStr, pszEOL);
     547                 :     else
     548              22 :         VSIFPrintfL(fp, "%s%s", pszStr, pszEOL);
     549                 : }
     550                 : 
     551                 : /************************************************************************/
     552                 : /*                             StartCopy()                              */
     553                 : /************************************************************************/
     554               2 : void OGRPGDumpDataSource::StartCopy( OGRPGDumpLayer *poPGLayer )
     555                 : {
     556               2 :     EndCopy();
     557               2 :     poLayerInCopyMode = poPGLayer;
     558               2 : }
     559                 : 
     560                 : /************************************************************************/
     561                 : /*                              EndCopy()                               */
     562                 : /************************************************************************/
     563               8 : OGRErr OGRPGDumpDataSource::EndCopy( )
     564                 : {
     565               8 :     if( poLayerInCopyMode != NULL )
     566                 :     {
     567               2 :         OGRErr result = poLayerInCopyMode->EndCopy();
     568               2 :         poLayerInCopyMode = NULL;
     569                 : 
     570               2 :         return result;
     571                 :     }
     572                 :     else
     573               6 :         return OGRERR_NONE;
     574                 : }

Generated by: LCOV version 1.7