📄 ogrpgdatasource.cpp
字号:
/* -------------------------------------------------------------------- *//* Try looking up in spatial_ref_sys table. *//* -------------------------------------------------------------------- */ PGresult *hResult; char szCommand[1024]; OGRSpatialReference *poSRS = NULL; SoftStartTransaction(); sprintf( szCommand, "SELECT srtext FROM spatial_ref_sys " "WHERE srid = %d", nId ); hResult = PQexec(hPGConn, szCommand ); if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK && PQntuples(hResult) == 1 ) { char *pszWKT; pszWKT = PQgetvalue(hResult,0,0); poSRS = new OGRSpatialReference(); if( poSRS->importFromWkt( &pszWKT ) != OGRERR_NONE ) { delete poSRS; poSRS = NULL; } } PQclear( hResult ); SoftCommit();/* -------------------------------------------------------------------- *//* Add to the cache. *//* -------------------------------------------------------------------- */ panSRID = (int *) CPLRealloc(panSRID,sizeof(int) * (nKnownSRID+1) ); papoSRS = (OGRSpatialReference **) CPLRealloc(papoSRS, sizeof(void*) * (nKnownSRID + 1) ); panSRID[nKnownSRID] = nId; papoSRS[nKnownSRID] = poSRS; return poSRS;}/************************************************************************//* FetchSRSId() *//* *//* Fetch the id corresponding to an SRS, and if not found, add *//* it to the table. *//************************************************************************/int OGRPGDataSource::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 OGRPGDataSource::SoftStartTransaction(){ nSoftTransactionLevel++; if( nSoftTransactionLevel == 1 ) { PGresult *hResult; PGconn *hPGConn = GetPGConn(); //CPLDebug( "OGR_PG", "BEGIN Transaction" ); hResult = PQexec(hPGConn, "BEGIN"); if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK ) { CPLDebug( "OGR_PG", "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 OGRPGDataSource::SoftCommit(){ if( nSoftTransactionLevel <= 0 ) { CPLDebug( "OGR_PG", "SoftCommit() with no transaction active." ); return OGRERR_FAILURE; } nSoftTransactionLevel--; if( nSoftTransactionLevel == 0 ) { PGresult *hResult; PGconn *hPGConn = GetPGConn(); //CPLDebug( "OGR_PG", "COMMIT Transaction" ); hResult = PQexec(hPGConn, "COMMIT"); if( !hResult || PQresultStatus(hResult) != PGRES_COMMAND_OK ) { CPLDebug( "OGR_PG", "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 OGRPGDataSource::SoftRollback(){ if( nSoftTransactionLevel <= 0 ) { CPLDebug( "OGR_PG", "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 OGRPGDataSource::FlushSoftTransaction(){ 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++; DeleteLayer( pszLayerName ); 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 ) ); } if( hResult ) PQclear( hResult ); FlushSoftTransaction(); return NULL;}/************************************************************************//* ReleaseResultSet() *//************************************************************************/void OGRPGDataSource::ReleaseResultSet( OGRLayer * poLayer ){ delete poLayer;}/************************************************************************//* LaunderName() *//************************************************************************/char *OGRPGDataSource::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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -