ogrmysqldatasource.cpp

来自「GIS系统支持库Geospatial Data Abstraction Libr」· C++ 代码 · 共 1,114 行 · 第 1/3 页

CPP
1,114
字号
/*      Fetch the id corresponding to an SRS, and if not found, add     *//*      it to the table.                                                *//************************************************************************/int OGRMySQLDataSource::FetchSRSId( OGRSpatialReference * poSRS ){    PGresult            *hResult;    char                szCommand[10000];    char                *pszWKT = NULL;    int                 nSRSId;    if( poSRS == NULL )        return -1;/* -------------------------------------------------------------------- *//*      Translate SRS to WKT.                                           *//* -------------------------------------------------------------------- */    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )        return -1;        CPLAssert( strlen(pszWKT) < sizeof(szCommand) - 500 );/* -------------------------------------------------------------------- *//*      Try to find in the existing table.                              *//* -------------------------------------------------------------------- */    hResult = PQexec(hPGConn, "BEGIN");    sprintf( szCommand,              "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",             pszWKT );    hResult = PQexec(hPGConn, szCommand );                     /* -------------------------------------------------------------------- *//*      We got it!  Return it.                                          *//* -------------------------------------------------------------------- */    if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK         && PQntuples(hResult) > 0 )    {        nSRSId = atoi(PQgetvalue( hResult, 0, 0 ));                PQclear( hResult );        hResult = PQexec(hPGConn, "COMMIT");        PQclear( hResult );        return nSRSId;    }    /* -------------------------------------------------------------------- *//*      If the command actually failed, then the metadata table is      *//*      likely missing. Try defining it.                                *//* -------------------------------------------------------------------- */    int         bTableMissing;    bTableMissing =         hResult == NULL || PQresultStatus(hResult) == PGRES_NONFATAL_ERROR;    hResult = PQexec(hPGConn, "COMMIT");    PQclear( hResult );    if( bTableMissing )    {        if( InitializeMetadataTables() != OGRERR_NONE )            return -1;    }/* -------------------------------------------------------------------- *//*      Get the current maximum srid in the srs table.                  *//* -------------------------------------------------------------------- */    hResult = PQexec(hPGConn, "BEGIN");    PQclear( hResult );    hResult = PQexec(hPGConn, "SELECT MAX(srid) FROM spatial_ref_sys" );    if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK )    {        nSRSId = atoi(PQgetvalue(hResult,0,0)) + 1;        PQclear( hResult );    }    else        nSRSId = 1;    /* -------------------------------------------------------------------- *//*      Try adding the SRS to the SRS table.                            *//* -------------------------------------------------------------------- */    sprintf( szCommand,              "INSERT INTO spatial_ref_sys (srid,srtext) VALUES (%d,'%s')",             nSRSId, pszWKT );                 hResult = PQexec(hPGConn, szCommand );    PQclear( hResult );    hResult = PQexec(hPGConn, "COMMIT");    PQclear( 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 OGRMySQLDataSource::SoftStartTransaction(){    nSoftTransactionLevel++;    if( nSoftTransactionLevel == 1 )    {        PGresult            *hResult;        PGconn          *hPGConn = GetPGConn();        //CPLDebug( "OGR_MYSQL", "BEGIN Transaction" );        hResult = PQexec(hPGConn, "BEGIN");        if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK )        {            CPLDebug( "OGR_MYSQL", "BEGIN Transaction failed:\n%s",                      PQerrorMessage( hPGConn ) );            return OGRERR_FAILURE;        }        PQclear( hResult );    }    return OGRERR_NONE;}/************************************************************************//*                             SoftCommit()                             *//*                                                                      *//*      Commit the current transaction if we are at the outer           *//*      scope.                                                          *//************************************************************************/OGRErr OGRMySQLDataSource::SoftCommit(){    if( nSoftTransactionLevel <= 0 )    {        CPLDebug( "OGR_MYSQL", "SoftCommit() with no transaction active." );        return OGRERR_FAILURE;    }    nSoftTransactionLevel--;    if( nSoftTransactionLevel == 0 )    {        PGresult            *hResult;        PGconn          *hPGConn = GetPGConn();        //CPLDebug( "OGR_MYSQL", "COMMIT Transaction" );        hResult = PQexec(hPGConn, "COMMIT");        if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK )        {            CPLDebug( "OGR_MYSQL", "COMMIT Transaction failed:\n%s",                      PQerrorMessage( hPGConn ) );            return OGRERR_FAILURE;        }        PQclear( 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 OGRMySQLDataSource::SoftRollback(){    if( nSoftTransactionLevel <= 0 )    {        CPLDebug( "OGR_MYSQL", "SoftRollback() with no transaction active." );        return OGRERR_FAILURE;    }    nSoftTransactionLevel = 0;    PGresult            *hResult;    PGconn              *hPGConn = GetPGConn();        hResult = PQexec(hPGConn, "ROLLBACK");        if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK )        return OGRERR_FAILURE;        PQclear( hResult );    return OGRERR_NONE;}/************************************************************************//*                        FlushSoftTransaction()                        *//*                                                                      *//*      Force the unwinding of any active transaction, and it's         *//*      commit.                                                         *//************************************************************************/OGRErr OGRMySQLDataSource::FlushSoftTransaction(){    if( nSoftTransactionLevel <= 0 )        return OGRERR_NONE;    nSoftTransactionLevel = 1;    return SoftCommit();}#endif/************************************************************************//*                             ExecuteSQL()                             *//************************************************************************/OGRLayer * OGRMySQLDataSource::ExecuteSQL( const char *pszSQLCommand,                                        OGRGeometry *poSpatialFilter,                                        const char *pszDialect ){    if( poSpatialFilter != NULL )    {        CPLDebug( "OGR_MYSQL",           "Spatial filter ignored for now in OGRMySQLDataSource::ExecuteSQL()" );    }/* -------------------------------------------------------------------- *//*      Use generic implementation for OGRSQL dialect.                  *//* -------------------------------------------------------------------- */    if( pszDialect != NULL && EQUAL(pszDialect,"OGRSQL") )        return OGRDataSource::ExecuteSQL( pszSQLCommand,                                           poSpatialFilter,                                           pszDialect );/* -------------------------------------------------------------------- *//*      Special case DELLAYER: command.                                 *//* -------------------------------------------------------------------- */#ifdef notdef    if( EQUALN(pszSQLCommand,"DELLAYER:",9) )    {        const char *pszLayerName = pszSQLCommand + 9;        while( *pszLayerName == ' ' )            pszLayerName++;        DeleteLayer( pszLayerName );        return NULL;    }#endif/* -------------------------------------------------------------------- *//*      Make sure there isn't an active transaction already.            *//* -------------------------------------------------------------------- */    InterruptLongResult();/* -------------------------------------------------------------------- *//*      Execute the statement.                                          *//* -------------------------------------------------------------------- */    MYSQL_RES *hResultSet;    if( mysql_query( hConn, pszSQLCommand ) )    {        ReportError( pszSQLCommand );        return NULL;    }    hResultSet = mysql_use_result( hConn );    if( hResultSet == NULL )    {        if( mysql_field_count( hConn ) == 0 )        {            CPLDebug( "MYSQL", "Command '%s' succeeded, %d rows affected.",                       pszSQLCommand,                       (int) mysql_affected_rows(hConn) );            return NULL;        }        else        {            ReportError( pszSQLCommand );            return NULL;        }    }/* -------------------------------------------------------------------- *//*      Do we have a tuple result? If so, instantiate a results         *//*      layer for it.                                                   *//* -------------------------------------------------------------------- */    OGRMySQLResultLayer *poLayer = NULL;    poLayer = new OGRMySQLResultLayer( this, pszSQLCommand, hResultSet );            return poLayer;}/************************************************************************//*                          ReleaseResultSet()                          *//************************************************************************/void OGRMySQLDataSource::ReleaseResultSet( OGRLayer * poLayer ){    delete poLayer;}/************************************************************************//*                            LaunderName()                             *//************************************************************************/char *OGRMySQLDataSource::LaunderName( const char *pszSrcName ){    char    *pszSafeName = CPLStrdup( pszSrcName );    int     i;    for( i = 0; pszSafeName[i] != '\0'; i++ )    {        pszSafeName[i] = (char) tolower( pszSafeName[i] );        if( pszSafeName[i] == '-' || pszSafeName[i] == '#' )            pszSafeName[i] = '_';    }    return pszSafeName;}/************************************************************************//*                         RequestLongResult()                          *//*                                                                      *//*      Layers need to use mysql_use_result() instead of                *//*      mysql_store_result() so that we won't have to load entire       *//*      result sets into RAM.  But only one "streamed" resultset can    *//*      be active on a database connection at a time.  So we need to    *//*      maintain a way of closing off an active streaming resultset     *//*      before any other sort of query with a resultset is              *//*      executable.  This method (and InterruptLongResult())            *//*      implement that exclusion.                                       *//************************************************************************/void OGRMySQLDataSource::RequestLongResult( OGRMySQLLayer * poNewLayer ){    InterruptLongResult();    poLongResultLayer = poNewLayer;}/************************************************************************//*                        InterruptLongResult()                         *//************************************************************************/void OGRMySQLDataSource::InterruptLongResult(){    if( poLongResultLayer != NULL )    {        poLongResultLayer->ResetReading();        poLongResultLayer = NULL;    }}

⌨️ 快捷键说明

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