ogrmysqltablelayer.cpp

来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,065 行 · 第 1/3 页

CPP
1,065
字号
/****************************************************************************** * $Id: ogrmysqltablelayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project:  OpenGIS Simple Features Reference Implementation * Purpose:  Implements OGRMySQLTableLayer class. * Author:   Frank Warmerdam, warmerdam@pobox.com * Author:   Howard Butler, hobu@hobu.net * ****************************************************************************** * 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. ****************************************************************************/#include "cpl_conv.h"#include "cpl_string.h"#include "ogr_mysql.h"CPL_CVSID("$Id: ogrmysqltablelayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $");/************************************************************************//*                         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 = NULL;    bLaunderColumnNames = TRUE;}/************************************************************************//*                        ~OGRMySQLTableLayer()                         *//************************************************************************/OGRMySQLTableLayer::~OGRMySQLTableLayer(){    CPLFree( pszQuery );    CPLFree( pszWHERE );}/************************************************************************//*                        Initialize()                                  *//*                                                                      *//*      Make sure we only do a ResetReading once we really have a       *//*      FieldDefn.  Otherwise, we'll segfault.  After you construct     *//*      the MySQLTableLayer, make sure to do pLayer->Initialize()       *//************************************************************************/OGRErr  OGRMySQLTableLayer::Initialize(const char * pszTableName){    poFeatureDefn = ReadTableDefinition( pszTableName );       if (poFeatureDefn)    {        ResetReading();        return OGRERR_NONE;    }    else    {        return OGRERR_FAILURE;    }}/************************************************************************//*                        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 );    pszGeomColumnTable = CPLStrdup(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;    poDefn->Reference();    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,"varbinary")            || (strlen(pszType)>3 && EQUAL(pszType+strlen(pszType)-4,"blob")))        {            oField.SetType( OFTBinary );        }        else if( EQUAL(pszType,"varchar")                  || EQUAL(pszType+strlen(pszType)-4,"enum")                  || EQUAL(pszType+strlen(pszType)-4,"set") )        {            oField.SetType( OFTString );        }        else if( EQUALN(pszType,"char",4)  )        {            oField.SetType( OFTString );            char ** papszTokens;            papszTokens = CSLTokenizeString2(pszType,"(),",0);                        /* width is the second */            oField.SetWidth(atoi(papszTokens[1]));            CSLDestroy( papszTokens );            oField.SetType( OFTString );        }                if(strlen(pszType)>3 && EQUAL(pszType+strlen(pszType)-4,"text"))        {            oField.SetType( OFTString );                    }        else if( EQUALN(pszType,"varchar",6)  )        {            /*                pszType is usually in the form "varchar(15)"                so we'll split it up and get the width and precision            */                        oField.SetType( OFTString );            char ** papszTokens;            papszTokens = CSLTokenizeString2(pszType,"(),",0);                        /* width is the second */            oField.SetWidth(atoi(papszTokens[1]));            CSLDestroy( papszTokens );            oField.SetType( OFTString );        }        else if( EQUALN(pszType,"int", 3) )        {            oField.SetType( OFTInteger );        }        else if( EQUALN(pszType,"tinyint", 7) )        {            oField.SetType( OFTInteger );        }        else if( EQUALN(pszType,"smallint", 8) )        {            oField.SetType( OFTInteger );        }        else if( EQUALN(pszType,"mediumint",9) )        {            oField.SetType( OFTInteger );        }        else if( EQUALN(pszType,"bigint",6) )        {            oField.SetType( OFTInteger );        }        else if( EQUALN(pszType,"decimal",7) )        {            /*                pszType is usually in the form "decimal(15,2)"                so we'll split it up and get the width and precision            */            oField.SetType( OFTReal );            char ** papszTokens;            papszTokens = CSLTokenizeString2(pszType,"(),",0);                        /* width is the second and precision is the third */            oField.SetWidth(atoi(papszTokens[1]));            oField.SetPrecision(atoi(papszTokens[2]));            CSLDestroy( papszTokens );        }        else if( EQUALN(pszType,"float", 5) )        {            oField.SetType( OFTReal );        }        else if( EQUAL(pszType,"double") )        {            oField.SetType( OFTReal );        }        else if( EQUALN(pszType,"double",6) )        {            // double can also be double(15,2)            // so we'll handle this case here after             // we check for just a regular double             // without a width and precision specified                        char ** papszTokens=NULL;            papszTokens = CSLTokenizeString2(pszType,"(),",0);            /* width is the second and precision is the third */            oField.SetWidth(atoi(papszTokens[1]));            oField.SetPrecision(atoi(papszTokens[2]));            CSLDestroy( papszTokens );              oField.SetType( OFTReal );        }        else if( EQUAL(pszType,"decimal") )        {            oField.SetType( OFTReal );        }        else if( EQUAL(pszType, "date") )        {            oField.SetType( OFTDate );        }        else if( EQUAL(pszType, "time") )        {            oField.SetType( OFTTime );        }        else if( EQUAL(pszType, "datetime")                  || EQUAL(pszType, "timestamp") )        {            oField.SetType( OFTDateTime );        }        else if( EQUAL(pszType, "year") )          {            oField.SetType( OFTString );            oField.SetWidth( 10 );        }        else if( EQUAL(pszType, "geometry") )         {            pszGeomColumn = CPLStrdup(papszRow[0]);            continue;        }        // 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 );    }    // set to none for now... if we have a geometry column it will be set layer.    poDefn->SetGeomType( wkbNone );    if( hResult != NULL )    {        mysql_free_result( hResult );        hResultSet = NULL;    }    if( bHasFid )        CPLDebug( "MySQL", "table %s has FID column %s.",                  pszTable, pszFIDColumn );    else        CPLDebug( "MySQL",                   "table %s has no FID column, FIDs will not be reliable!",                  pszTable );    if (pszGeomColumn)     {        char*        pszType=NULL;                // set to unknown first        poDefn->SetGeomType( wkbUnknown );                sprintf(szCommand, "SELECT type, coord_dimension FROM geometry_columns WHERE f_table_name='%s'",                pszTable );                hResult = NULL;        if( !mysql_query( poDS->GetConn(), szCommand ) )            hResult = mysql_store_result( poDS->GetConn() );        papszRow = NULL;        if( hResult != NULL )            papszRow = mysql_fetch_row( hResult );        if( papszRow != NULL && papszRow[0] != NULL )        {            pszType = papszRow[0];            OGRwkbGeometryType nGeomType = wkbUnknown;            // check only standard OGC geometry types            if ( EQUAL(pszType, "POINT") )                nGeomType = wkbPoint;            else if ( EQUAL(pszType,"LINESTRING"))                nGeomType = wkbLineString;            else if ( EQUAL(pszType,"POLYGON"))                nGeomType = wkbPolygon;            else if ( EQUAL(pszType,"MULTIPOINT"))                nGeomType = wkbMultiPoint;            else if ( EQUAL(pszType,"MULTILINESTRING"))                nGeomType = wkbMultiLineString;            else if ( EQUAL(pszType,"MULTIPOLYGON"))                nGeomType = wkbMultiPolygon;            else if ( EQUAL(pszType,"GEOMETRYCOLLECTION"))                nGeomType = wkbGeometryCollection;            if( papszRow[1] != NULL && atoi(papszRow[1]) == 3 )                nGeomType = (OGRwkbGeometryType) (nGeomType | wkb25DBit);

⌨️ 快捷键说明

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