📄 ogrmysqltablelayer.cpp
字号:
/****************************************************************************** * $Id: ogrmysqltablelayer.cpp,v 1.4 2005/02/24 21:54:36 fwarmerdam Exp $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements OGRMySQLTableLayer class. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2004, Frank Warmerdam <warmerdam@pobox.com> * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ****************************************************************************** * * $Log: ogrmysqltablelayer.cpp,v $ * Revision 1.4 2005/02/24 21:54:36 fwarmerdam * added support for decimal fields. * * Revision 1.3 2005/02/22 12:54:27 fwarmerdam * use OGRLayer base spatial filter support * * Revision 1.2 2004/10/08 20:50:48 fwarmerdam * avoid leak in GetFeatureCount() * * Revision 1.1 2004/10/07 20:56:15 fwarmerdam * New * */#include "cpl_conv.h"#include "cpl_string.h"#include "ogr_mysql.h"CPL_CVSID("$Id: ogrmysqltablelayer.cpp,v 1.4 2005/02/24 21:54:36 fwarmerdam Exp $");/************************************************************************//* OGRMySQLTableLayer() *//************************************************************************/OGRMySQLTableLayer::OGRMySQLTableLayer( OGRMySQLDataSource *poDSIn, const char * pszTableName, int bUpdate, int nSRSIdIn ){ poDS = poDSIn; pszQuery = NULL; pszWHERE = CPLStrdup( "" ); pszQueryStatement = NULL; bUpdateAccess = bUpdate; iNextShapeId = 0; nSRSId = nSRSIdIn; poFeatureDefn = ReadTableDefinition( pszTableName ); ResetReading(); bLaunderColumnNames = TRUE;}/************************************************************************//* ~OGRMySQLTableLayer() *//************************************************************************/OGRMySQLTableLayer::~OGRMySQLTableLayer(){ CPLFree( pszQuery ); CPLFree( pszWHERE );}/************************************************************************//* ReadTableDefinition() *//* *//* Build a schema from the named table. Done by querying the *//* catalog. *//************************************************************************/OGRFeatureDefn *OGRMySQLTableLayer::ReadTableDefinition( const char *pszTable ){ MYSQL_RES *hResult; char szCommand[1024]; /* -------------------------------------------------------------------- *//* Fire off commands to get back the schema of the table. *//* -------------------------------------------------------------------- */ sprintf( szCommand, "DESCRIBE %s", pszTable ); if( mysql_query( poDS->GetConn(), szCommand ) ) { poDS->ReportError( "DESCRIBE Failed" ); return FALSE; } hResult = mysql_store_result( poDS->GetConn() ); if( hResult == NULL ) { poDS->ReportError( "mysql_store_result() failed on DESCRIBE result." ); return FALSE; }/* -------------------------------------------------------------------- *//* Parse the returned table information. *//* -------------------------------------------------------------------- */ OGRFeatureDefn *poDefn = new OGRFeatureDefn( pszTable ); char **papszRow; while( (papszRow = mysql_fetch_row( hResult )) != NULL ) { const char *pszType; OGRFieldDefn oField( papszRow[0], OFTString); pszType = papszRow[1]; if( pszType == NULL ) continue; if( EQUAL(pszType,"varchar") || (strlen(pszType)>3 && EQUAL(pszType+strlen(pszType)-4,"text")) || (strlen(pszType)>3 && EQUAL(pszType+strlen(pszType)-4,"blob")) || EQUAL(pszType+strlen(pszType)-4,"enum") || EQUAL(pszType+strlen(pszType)-4,"set") ) { oField.SetType( OFTString ); } else if( EQUAL(pszType,"char") ) { oField.SetType( OFTString ); /* TODO: need to work out width */ } else if( EQUALN(pszType,"int",3) ) { oField.SetType( OFTInteger ); } else if( EQUAL(pszType,"tinyint") ) { oField.SetType( OFTInteger ); } else if( EQUAL(pszType,"smallint") ) { oField.SetType( OFTInteger ); } else if( EQUAL(pszType,"mediumint") ) { oField.SetType( OFTInteger ); } else if( EQUAL(pszType,"bigint") ) { oField.SetType( OFTInteger ); } else if( EQUALN(pszType,"decimal",7) ) { oField.SetType( OFTReal ); } else if( EQUAL(pszType,"float") ) { oField.SetType( OFTReal ); } else if( EQUAL(pszType,"double") ) { oField.SetType( OFTReal ); } else if( EQUAL(pszType,"decimal") ) { oField.SetType( OFTReal ); } else if( EQUAL(pszType, "date") ) { oField.SetType( OFTString ); oField.SetWidth( 10 ); } else if( EQUAL(pszType, "time") ) { oField.SetType( OFTString ); oField.SetWidth( 10 ); } else if( EQUAL(pszType, "year") ) { oField.SetType( OFTString ); oField.SetWidth( 10 ); } else if( EQUAL(pszType, "datetime") ) { oField.SetType( OFTString ); oField.SetWidth( 20 ); } // Is this an integer primary key field? if( !bHasFid && papszRow[3] != NULL && EQUAL(papszRow[3],"PRI") && oField.GetType() == OFTInteger ) { bHasFid = TRUE; pszFIDColumn = CPLStrdup(oField.GetNameRef()); continue; } poDefn->AddFieldDefn( &oField ); } mysql_free_result( hResult );/* -------------------------------------------------------------------- *//* Till we support geometry, we can safely mark ourselves as *//* wkbNone. This lets the built GetExtents() produce an *//* optimized response. *//* -------------------------------------------------------------------- */ poDefn->SetGeomType( wkbNone ); return poDefn;}/************************************************************************//* SetSpatialFilter() *//************************************************************************/void OGRMySQLTableLayer::SetSpatialFilter( OGRGeometry * poGeomIn ){ if( !InstallFilter( poGeomIn ) ) return; BuildWhere(); ResetReading();}/************************************************************************//* BuildWhere() *//* *//* Build the WHERE statement appropriate to the current set of *//* criteria (spatial and attribute queries). *//************************************************************************/void OGRMySQLTableLayer::BuildWhere(){ CPLFree( pszWHERE ); pszWHERE = NULL; if( pszQuery != NULL ) { pszWHERE = (char *) CPLMalloc(strlen(pszQuery)+20); sprintf( pszWHERE, "WHERE %s ", pszQuery ); } else { pszWHERE = CPLStrdup(""); }}/************************************************************************//* BuildFullQueryStatement() *//************************************************************************/void OGRMySQLTableLayer::BuildFullQueryStatement(){ if( pszQueryStatement != NULL ) { CPLFree( pszQueryStatement ); pszQueryStatement = NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -