ogrmysqldatasource.cpp
来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,104 行 · 第 1/3 页
CPP
1,104 行
char szCommand[1024]; MYSQL_RES *hResult; OGRErr eErr = OGRERR_NONE; sprintf( szCommand, "DESCRIBE geometry_columns" ); if( mysql_query(GetConn(), szCommand ) ) { sprintf(szCommand, "CREATE TABLE geometry_columns " "( F_TABLE_CATALOG VARCHAR(256), " "F_TABLE_SCHEMA VARCHAR(256), " "F_TABLE_NAME VARCHAR(256) NOT NULL," "F_GEOMETRY_COLUMN VARCHAR(256) NOT NULL, " "COORD_DIMENSION INT, " "SRID INT," "TYPE VARCHAR(256) NOT NULL)"); if( mysql_query(GetConn(), szCommand ) ) { ReportError( szCommand ); eErr = OGRERR_FAILURE; } else CPLDebug("MYSQL","Creating geometry_columns metadata table"); } // make sure to attempt to free results of successful queries hResult = mysql_store_result( GetConn() ); if( hResult != NULL ) { mysql_free_result( hResult ); hResult = NULL; } sprintf( szCommand, "DESCRIBE spatial_ref_sys" ); if( mysql_query(GetConn(), szCommand ) ) { sprintf(szCommand, "CREATE TABLE spatial_ref_sys " "(SRID INT NOT NULL, " "AUTH_NAME VARCHAR(256), " "AUTH_SRID INT, " "SRTEXT VARCHAR(2048))"); if( mysql_query(GetConn(), szCommand ) ) { ReportError( szCommand ); eErr = OGRERR_FAILURE; } else CPLDebug("MYSQL","Creating spatial_ref_sys metadata table"); } // make sure to attempt to free results of successful queries hResult = mysql_store_result( GetConn() ); if( hResult != NULL ) { mysql_free_result( hResult ); hResult = NULL; } return eErr;}/************************************************************************//* FetchSRS() *//* *//* Return a SRS corresponding to a particular id. Note that *//* reference counting should be honoured on the returned *//* OGRSpatialReference, as handles may be cached. *//************************************************************************/OGRSpatialReference *OGRMySQLDataSource::FetchSRS( int nId ){ char szCommand[1024]; char **papszRow; MYSQL_RES *hResult; if( nId < 0 ) return NULL;/* -------------------------------------------------------------------- *//* First, we look through our SRID cache, is it there? *//* -------------------------------------------------------------------- */ int i; for( i = 0; i < nKnownSRID; i++ ) { if( panSRID[i] == nId ) return papoSRS[i]; } OGRSpatialReference *poSRS = NULL; // make sure to attempt to free any old results hResult = mysql_store_result( GetConn() ); if( hResult != NULL ) mysql_free_result( hResult ); hResult = NULL; sprintf( szCommand, "SELECT srtext FROM spatial_ref_sys WHERE srid = %d", nId ); if( !mysql_query( GetConn(), szCommand ) ) hResult = mysql_store_result( GetConn() ); char *pszWKT = NULL; papszRow = NULL; if( hResult != NULL ) papszRow = mysql_fetch_row( hResult ); if( papszRow != NULL && papszRow[0] != NULL ) { pszWKT =papszRow[0]; } // make sure to attempt to free results of successful queries hResult = mysql_store_result( GetConn() ); if( hResult != NULL ) mysql_free_result( hResult ); hResult = NULL; poSRS = new OGRSpatialReference(); if( pszWKT == NULL || poSRS->importFromWkt( &pszWKT ) != OGRERR_NONE ) { delete poSRS; poSRS = NULL; }/* -------------------------------------------------------------------- *//* 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 OGRMySQLDataSource::FetchSRSId( OGRSpatialReference * poSRS ){ char **papszRow; MYSQL_RES *hResult=NULL; 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. *//* -------------------------------------------------------------------- */ sprintf( szCommand, "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'", pszWKT ); if( !mysql_query( GetConn(), szCommand ) ) hResult = mysql_store_result( GetConn() ); if (!mysql_num_rows(hResult)) { CPLDebug("MYSQL", "No rows exist currently exist in spatial_ref_sys"); mysql_free_result( hResult ); hResult = NULL; } papszRow = NULL; if( hResult != NULL ) papszRow = mysql_fetch_row( hResult ); if( papszRow != NULL && papszRow[0] != NULL ) { nSRSId = atoi(papszRow[0]); if( hResult != NULL ) mysql_free_result( hResult ); hResult = NULL; return nSRSId; } // make sure to attempt to free results of successful queries hResult = mysql_store_result( GetConn() ); if( hResult != NULL ) mysql_free_result( hResult ); hResult = NULL;/* -------------------------------------------------------------------- *//* Get the current maximum srid in the srs table. *//* -------------------------------------------------------------------- */ sprintf( szCommand, "SELECT MAX(srid) FROM spatial_ref_sys"); if( !mysql_query( GetConn(), szCommand ) ) { hResult = mysql_store_result( GetConn() ); papszRow = mysql_fetch_row( hResult ); } if( papszRow != NULL && papszRow[0] != NULL ) { nSRSId = atoi(papszRow[0]) + 1; if( hResult != NULL ) mysql_free_result( hResult ); hResult = NULL; } 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 ); if( !mysql_query( GetConn(), szCommand ) ) hResult = mysql_store_result( GetConn() ); // make sure to attempt to free results of successful queries hResult = mysql_store_result( GetConn() ); if( hResult != NULL ) mysql_free_result( hResult ); hResult = NULL; return nSRSId;}/************************************************************************//* 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 */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?