ogrmysqltablelayer.cpp
来自「GIS系统支持库Geospatial Data Abstraction Libr」· C++ 代码 · 共 562 行 · 第 1/2 页
CPP
562 行
char *pszFields = BuildFields(); pszQueryStatement = (char *) CPLMalloc(strlen(pszFields)+strlen(pszWHERE) +strlen(poFeatureDefn->GetName()) + 40); sprintf( pszQueryStatement, "SELECT %s FROM %s %s", pszFields, poFeatureDefn->GetName(), pszWHERE ); CPLFree( pszFields );}/************************************************************************//* ResetReading() *//************************************************************************/void OGRMySQLTableLayer::ResetReading(){ BuildFullQueryStatement(); OGRMySQLLayer::ResetReading();}/************************************************************************//* BuildFields() *//* *//* Build list of fields to fetch, performing any required *//* transformations (such as on geometry). *//************************************************************************/char *OGRMySQLTableLayer::BuildFields(){ int i, nSize; char *pszFieldList; nSize = 25; if( pszGeomColumn ) nSize += strlen(pszGeomColumn); if( bHasFid ) nSize += strlen(pszFIDColumn); for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) nSize += strlen(poFeatureDefn->GetFieldDefn(i)->GetNameRef()) + 4; pszFieldList = (char *) CPLMalloc(nSize); pszFieldList[0] = '\0'; if( bHasFid && poFeatureDefn->GetFieldIndex( pszFIDColumn ) == -1 ) sprintf( pszFieldList, "%s", pszFIDColumn ); if( pszGeomColumn ) { if( strlen(pszFieldList) > 0 ) strcat( pszFieldList, ", " ); sprintf( pszFieldList+strlen(pszFieldList), "%s", pszGeomColumn ); } for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { const char *pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); if( strlen(pszFieldList) > 0 ) strcat( pszFieldList, ", " ); strcat( pszFieldList, pszName ); } CPLAssert( (int) strlen(pszFieldList) < nSize ); return pszFieldList;}/************************************************************************//* SetAttributeFilter() *//************************************************************************/OGRErr OGRMySQLTableLayer::SetAttributeFilter( const char *pszQuery ){ CPLFree( this->pszQuery ); if( pszQuery == NULL || strlen(pszQuery) == 0 ) this->pszQuery = NULL; else this->pszQuery = CPLStrdup( pszQuery ); BuildWhere(); ResetReading(); return OGRERR_NONE;}/************************************************************************//* TestCapability() *//************************************************************************/int OGRMySQLTableLayer::TestCapability( const char * pszCap ){ if( EQUAL(pszCap,OLCRandomRead) ) return bHasFid; else if( EQUAL(pszCap,OLCFastFeatureCount) ) return TRUE; else return OGRMySQLLayer::TestCapability( pszCap );}/************************************************************************//* GetFeature() *//************************************************************************/OGRFeature *OGRMySQLTableLayer::GetFeature( long nFeatureId ){ if( pszFIDColumn == NULL ) return OGRMySQLLayer::GetFeature( nFeatureId );/* -------------------------------------------------------------------- *//* Discard any existing resultset. *//* -------------------------------------------------------------------- */ poDS->InterruptLongResult();/* -------------------------------------------------------------------- *//* Prepare query command that will just fetch the one record of *//* interest. *//* -------------------------------------------------------------------- */ char *pszFieldList = BuildFields(); char *pszCommand = (char *) CPLMalloc(strlen(pszFieldList)+2000); sprintf( pszCommand, "SELECT %s FROM %s WHERE %s = %ld", pszFieldList, poFeatureDefn->GetName(), pszFIDColumn, nFeatureId ); CPLFree( pszFieldList );/* -------------------------------------------------------------------- *//* Issue the command. *//* -------------------------------------------------------------------- */ if( mysql_query( poDS->GetConn(), pszCommand ) ) { poDS->ReportError( pszCommand ); return NULL; } CPLFree( pszCommand ); hResultSet = mysql_store_result( poDS->GetConn() ); if( hResultSet == NULL ) { poDS->ReportError( "mysql_store_result() failed on query." ); return NULL; }/* -------------------------------------------------------------------- *//* Fetch the result record. *//* -------------------------------------------------------------------- */ char **papszRow; papszRow = mysql_fetch_row( hResultSet ); if( papszRow == NULL ) return NULL;/* -------------------------------------------------------------------- *//* Transform into a feature. *//* -------------------------------------------------------------------- */ iNextShapeId = nFeatureId; OGRFeature *poFeature = RecordToFeature( papszRow ); iNextShapeId = 0;/* -------------------------------------------------------------------- *//* Cleanup *//* -------------------------------------------------------------------- */ mysql_free_result( hResultSet ); return poFeature;}/************************************************************************//* GetFeatureCount() *//* *//* If a spatial filter is in effect, we turn control over to *//* the generic counter. Otherwise we return the total count. *//* Eventually we should consider implementing a more efficient *//* way of counting features matching a spatial query. *//************************************************************************/int OGRMySQLTableLayer::GetFeatureCount( int bForce ){/* -------------------------------------------------------------------- *//* Ensure any active long result is interrupted. *//* -------------------------------------------------------------------- */ poDS->InterruptLongResult(); /* -------------------------------------------------------------------- *//* Issue the appropriate select command. *//* -------------------------------------------------------------------- */ MYSQL_RES *hResult; const char *pszCommand; pszCommand = CPLSPrintf( "SELECT COUNT(*) FROM %s %s", poFeatureDefn->GetName(), pszWHERE ); if( mysql_query( poDS->GetConn(), pszCommand ) ) { poDS->ReportError( pszCommand ); return FALSE; } hResult = mysql_store_result( poDS->GetConn() ); if( hResult == NULL ) { poDS->ReportError( "mysql_store_result() failed on SELECT COUNT(*)." ); return FALSE; } /* -------------------------------------------------------------------- *//* Capture the result. *//* -------------------------------------------------------------------- */ char **papszRow = mysql_fetch_row( hResult ); int nCount = 0; if( papszRow != NULL && papszRow[0] != NULL ) nCount = atoi(papszRow[0]); mysql_free_result( hResult ); return nCount;}/************************************************************************//* GetSpatialRef() *//* *//* We override this to try and fetch the table SRID from the *//* geometry_columns table if the srsid is -2 (meaning we *//* haven't yet even looked for it). *//************************************************************************/OGRSpatialReference *OGRMySQLTableLayer::GetSpatialRef(){#ifdef notdef if( nSRSId == -2 ) { PGconn *hPGConn = poDS->GetPGConn(); PGresult *hResult; char szCommand[1024]; nSRSId = -1; poDS->SoftStartTransaction(); sprintf( szCommand, "SELECT srid FROM geometry_columns " "WHERE f_table_name = '%s'", poFeatureDefn->GetName() ); hResult = PQexec(hPGConn, szCommand ); if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK && PQntuples(hResult) == 1 ) { nSRSId = atoi(PQgetvalue(hResult,0,0)); } PQclear( hResult ); poDS->SoftCommit(); }#endif return OGRMySQLLayer::GetSpatialRef();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?