ogrpgdatasource.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,355 行 · 第 1/4 页

CPP
1,355
字号
    {        nSRSId = atoi(PQgetvalue(hResult,0,0)) + 1;        OGRPGClearResult( hResult );    }    else    {        nSRSId = 1;    }/* -------------------------------------------------------------------- *//*      Try adding the SRS to the SRS table.                            *//* -------------------------------------------------------------------- */    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )    {        return -1;    }    CPLAssert( strlen(pszWKT) < sizeof(szCommand) - 500 );    char    *pszProj4 = NULL;    if( poSRS->exportToProj4( &pszProj4 ) != OGRERR_NONE )    {        CPLFree( pszWKT );  // Prevent mem leaks        pszWKT = NULL;		        return -1;    }    sprintf( szCommand,             "INSERT INTO spatial_ref_sys (srid,srtext,proj4text) VALUES (%d,'%s','%s')",             nSRSId, pszWKT, pszProj4 );	    // Free everything that was allocated.    CPLFree( pszProj4 );    CPLFree( pszWKT);    hResult = PQexec(hPGConn, szCommand );    OGRPGClearResult( hResult );    hResult = PQexec(hPGConn, "COMMIT");    OGRPGClearResult( hResult );    return nSRSId;}/************************************************************************//*                        SoftStartTransaction()                        *//*                                                                      *//*      Create a transaction scope.  If we already have a               *//*      transaction active this isn't a real transaction, but just      *//*      an increment to the scope count.                                *//************************************************************************/OGRErr OGRPGDataSource::SoftStartTransaction(){    nSoftTransactionLevel++;    if( nSoftTransactionLevel == 1 )    {        PGresult    *hResult = NULL;        PGconn      *hPGConn = GetPGConn();        //CPLDebug( "OGR_PG", "BEGIN Transaction" );        hResult = PQexec(hPGConn, "BEGIN");        if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK )        {            OGRPGClearResult( hResult );            CPLDebug( "OGR_PG", "BEGIN Transaction failed:\n%s",                      PQerrorMessage( hPGConn ) );            return OGRERR_FAILURE;        }        OGRPGClearResult( hResult );    }    return OGRERR_NONE;}/************************************************************************//*                             SoftCommit()                             *//*                                                                      *//*      Commit the current transaction if we are at the outer           *//*      scope.                                                          *//************************************************************************/OGRErr OGRPGDataSource::SoftCommit(){    EndCopy();    if( nSoftTransactionLevel <= 0 )    {        CPLDebug( "OGR_PG", "SoftCommit() with no transaction active." );        return OGRERR_FAILURE;    }    nSoftTransactionLevel--;    if( nSoftTransactionLevel == 0 )    {        PGresult    *hResult = NULL;        PGconn      *hPGConn = GetPGConn();        //CPLDebug( "OGR_PG", "COMMIT Transaction" );        hResult = PQexec(hPGConn, "COMMIT");        if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK )        {            OGRPGClearResult( hResult );            CPLDebug( "OGR_PG", "COMMIT Transaction failed:\n%s",                      PQerrorMessage( hPGConn ) );            return OGRERR_FAILURE;        }                OGRPGClearResult( hResult );    }    return OGRERR_NONE;}/************************************************************************//*                            SoftRollback()                            *//*                                                                      *//*      Force a rollback of the current transaction if there is one,    *//*      even if we are nested several levels deep.                      *//************************************************************************/OGRErr OGRPGDataSource::SoftRollback(){    if( nSoftTransactionLevel <= 0 )    {        CPLDebug( "OGR_PG", "SoftRollback() with no transaction active." );        return OGRERR_FAILURE;    }    nSoftTransactionLevel = 0;    PGresult    *hResult = NULL;    PGconn      *hPGConn = GetPGConn();    hResult = PQexec(hPGConn, "ROLLBACK");    if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK )    {        OGRPGClearResult( hResult );        return OGRERR_FAILURE;    }    OGRPGClearResult( hResult );    return OGRERR_NONE;}/************************************************************************//*                        FlushSoftTransaction()                        *//*                                                                      *//*      Force the unwinding of any active transaction, and it's         *//*      commit.                                                         *//************************************************************************/OGRErr OGRPGDataSource::FlushSoftTransaction(){    /* This must come first because of ogr2ogr.  If you want       to use ogr2ogr with COPY support, then you must specify       that ogr2ogr does not use transactions.  Thus,        nSoftTransactionLevel will always be zero, so this has       to come first. */    EndCopy();     if( nSoftTransactionLevel <= 0 )        return OGRERR_NONE;    nSoftTransactionLevel = 1;    return SoftCommit();}/************************************************************************//*                             ExecuteSQL()                             *//************************************************************************/OGRLayer * OGRPGDataSource::ExecuteSQL( const char *pszSQLCommand,                                        OGRGeometry *poSpatialFilter,                                        const char *pszDialect ){    if( poSpatialFilter != NULL )    {        CPLDebug( "OGR_PG",          "Spatial filter ignored for now in OGRPGDataSource::ExecuteSQL()" );    }/* -------------------------------------------------------------------- *//*      Use generic implementation for OGRSQL dialect.                  *//* -------------------------------------------------------------------- */    if( pszDialect != NULL && EQUAL(pszDialect,"OGRSQL") )        return OGRDataSource::ExecuteSQL( pszSQLCommand,                                          poSpatialFilter,                                          pszDialect );/* -------------------------------------------------------------------- *//*      Special case DELLAYER: command.                                 *//* -------------------------------------------------------------------- */    if( EQUALN(pszSQLCommand,"DELLAYER:",9) )    {        const char *pszLayerName = pszSQLCommand + 9;        while( *pszLayerName == ' ' )            pszLayerName++;                for( int iLayer = 0; iLayer < nLayers; iLayer++ )        {            if( EQUAL(papoLayers[iLayer]->GetLayerDefn()->GetName(),                       pszLayerName ))            {                DeleteLayer( iLayer );                break;            }        }        return NULL;    }/* -------------------------------------------------------------------- *//*      Execute the statement.                                          *//* -------------------------------------------------------------------- */    PGresult    *hResult = NULL;    FlushSoftTransaction();    if( SoftStartTransaction() == OGRERR_NONE  )    {        CPLDebug( "OGR_PG", "PQexec(%s)", pszSQLCommand );        hResult = PQexec(hPGConn, pszSQLCommand );        CPLDebug( "OGR_PG", "Command Results Tuples = %d", PQntuples(hResult) );    }/* -------------------------------------------------------------------- *//*      Do we have a tuple result? If so, instantiate a results         *//*      layer for it.                                                   *//* -------------------------------------------------------------------- */    if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK        && PQntuples(hResult) > 0 )    {        OGRPGResultLayer *poLayer = NULL;        poLayer = new OGRPGResultLayer( this, pszSQLCommand, hResult );        return poLayer;    }/* -------------------------------------------------------------------- *//*      Generate an error report if an error occured.                   *//* -------------------------------------------------------------------- */    if( hResult &&        (PQresultStatus(hResult) == PGRES_NONFATAL_ERROR         || PQresultStatus(hResult) == PGRES_FATAL_ERROR ) )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "%s", PQresultErrorMessage( hResult ) );    }    OGRPGClearResult( hResult );    FlushSoftTransaction();    return NULL;}/************************************************************************//*                          ReleaseResultSet()                          *//************************************************************************/void OGRPGDataSource::ReleaseResultSet( OGRLayer * poLayer ){    delete poLayer;}/************************************************************************//*                            LaunderName()                             *//************************************************************************/char *OGRPGDataSource::LaunderName( const char *pszSrcName ){    char    *pszSafeName = CPLStrdup( pszSrcName );    for( int i = 0; pszSafeName[i] != '\0'; i++ )    {        pszSafeName[i] = (char) tolower( pszSafeName[i] );        if( pszSafeName[i] == '-' || pszSafeName[i] == '#' )            pszSafeName[i] = '_';    }    return pszSafeName;}/************************************************************************//*                             StartCopy()                              *//************************************************************************/void OGRPGDataSource::StartCopy( OGRPGTableLayer *poPGLayer ){    EndCopy();    poLayerInCopyMode = poPGLayer;}/************************************************************************//*                              EndCopy()                               *//************************************************************************/OGRErr OGRPGDataSource::EndCopy( ){    if( poLayerInCopyMode != NULL )    {        OGRErr result = poLayerInCopyMode->EndCopy();        poLayerInCopyMode = NULL;        return result;    }    else        return OGRERR_NONE;}/************************************************************************//*                           CopyInProgress()                           *//************************************************************************/int OGRPGDataSource::CopyInProgress( ){    return ( poLayerInCopyMode != NULL );}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?