⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogrmysqldatasource.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************** * $Id: ogrmysqldatasource.cpp,v 1.3 2004/10/12 16:59:31 fwarmerdam Exp $ * * Project:  OpenGIS Simple Features Reference Implementation * Purpose:  Implements OGRMySQLDataSource 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: ogrmysqldatasource.cpp,v $ * Revision 1.3  2004/10/12 16:59:31  fwarmerdam * rearrange include files for win32 * * Revision 1.2  2004/10/08 20:50:05  fwarmerdam * Implemented ExecuteSQL(). * Added support for getting host, password, port, and user from the * datasource name or from the mysql init files (ie. ~/.my.cnf). * * Revision 1.1  2004/10/07 20:56:15  fwarmerdam * New * */#include <string>#include "ogr_mysql.h"#include <my_sys.h>#include "cpl_conv.h"#include "cpl_string.h"CPL_CVSID("$Id: ogrmysqldatasource.cpp,v 1.3 2004/10/12 16:59:31 fwarmerdam Exp $");/************************************************************************//*                         OGRMySQLDataSource()                         *//************************************************************************/OGRMySQLDataSource::OGRMySQLDataSource(){    pszName = NULL;    papoLayers = NULL;    nLayers = 0;    hConn = 0;    nSoftTransactionLevel = 0;    nKnownSRID = 0;    panSRID = NULL;    papoSRS = NULL;    poLongResultLayer = NULL;}/************************************************************************//*                        ~OGRMySQLDataSource()                         *//************************************************************************/OGRMySQLDataSource::~OGRMySQLDataSource(){    int         i;//    FlushSoftTransaction();    InterruptLongResult();    CPLFree( pszName );    for( i = 0; i < nLayers; i++ )        delete papoLayers[i];        CPLFree( papoLayers );    if( hConn != NULL )        mysql_close( hConn );    for( i = 0; i < nKnownSRID; i++ )    {        if( papoSRS[i] != NULL && papoSRS[i]->Dereference() == 0 )            delete papoSRS[i];    }    CPLFree( panSRID );    CPLFree( papoSRS );}/************************************************************************//*                            ReportError()                             *//************************************************************************/void OGRMySQLDataSource::ReportError( const char *pszDescription ){    if( pszDescription )        CPLError( CE_Failure, CPLE_AppDefined,                   "%s\n%s", pszDescription, mysql_error( hConn ) );    else        CPLError( CE_Failure, CPLE_AppDefined,                   "%s", mysql_error( hConn ) );}/************************************************************************//*                                Open()                                *//************************************************************************/int OGRMySQLDataSource::Open( const char * pszNewName, int bUpdate,                              int bTestOpen ){    CPLAssert( nLayers == 0 );/* -------------------------------------------------------------------- *//*      Verify postgresql prefix.                                       *//* -------------------------------------------------------------------- */    if( !EQUALN(pszNewName,"MYSQL:",6) )    {        if( !bTestOpen )            CPLError( CE_Failure, CPLE_AppDefined,                       "%s does not conform to MySQL naming convention,"                      " MYSQL:dbname[, user=..][,password=..][,host=..][,port=..][tables=table;table;...]",                      pszNewName );        return FALSE;    }    /* -------------------------------------------------------------------- *//*      Use options process to get .my.cnf file contents.               *//* -------------------------------------------------------------------- */    int nPort = 0, i;    char **papszTableNames=NULL;    std::string oHost, oPassword, oUser, oDB;    char *apszArgv[2] = { "org", NULL };    char **papszArgv = apszArgv;    int  nArgc = 1;    const char *client_groups[] = {"client", "ogr", NULL };    my_init(); // I hope there is no problem with calling this multiple times!    load_defaults( "my", client_groups, &nArgc, &papszArgv );    for( i = 0; i < nArgc; i++ )    {        if( EQUALN(papszArgv[i],"--user=",7) )            oUser = papszArgv[i] + 7;        else if( EQUALN(papszArgv[i],"--host=",7) )            oHost = papszArgv[i] + 7;        else if( EQUALN(papszArgv[i],"--password=",11) )            oPassword = papszArgv[i] + 11;        else if( EQUALN(papszArgv[i],"--port=",7) )            nPort = atoi(papszArgv[i] + 7);    }    // cleanup    free_defaults( papszArgv );/* -------------------------------------------------------------------- *//*      Parse out connection information.                               *//* -------------------------------------------------------------------- */    char **papszItems = CSLTokenizeString2( pszNewName+6, ",",                                             CSLT_HONOURSTRINGS );    if( CSLCount(papszItems) < 1 )    {        CSLDestroy( papszItems );        CPLError( CE_Failure, CPLE_AppDefined,                   "MYSQL: request missing databasename." );        return FALSE;    }    oDB = papszItems[0];    for( i = 1; papszItems[i] != NULL; i++ )    {        if( EQUALN(papszItems[i],"user=",5) )            oUser = papszItems[i] + 5;        else if( EQUALN(papszItems[i],"password=",9) )            oPassword = papszItems[i] + 9;        else if( EQUALN(papszItems[i],"host=",5) )            oHost = papszItems[i] + 5;        else if( EQUALN(papszItems[i],"port=",5) )            nPort = atoi(papszItems[i] + 5);        else if( EQUALN(papszItems[i],"tables=",7) )        {            papszTableNames = CSLTokenizeStringComplex(                 papszItems[i] + 7, ";", FALSE, FALSE );        }        else            CPLError( CE_Warning, CPLE_AppDefined,                       "'%s' in MYSQL datasource definition not recognised and ignored.", papszItems[i] );    }    CSLDestroy( papszItems );/* -------------------------------------------------------------------- *//*      Try to establish connection.                                    *//* -------------------------------------------------------------------- */    hConn = mysql_init( NULL );    if( hConn == NULL )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "mysql_init() failed." );    }    if( hConn        && mysql_real_connect( hConn,                                oHost.length() ? oHost.c_str() : NULL,                               oUser.length() ? oUser.c_str() : NULL,                               oPassword.length() ? oPassword.c_str() : NULL,                               oDB.length() ? oDB.c_str() : NULL,                               nPort, NULL, 0 ) == NULL )    {        CPLError( CE_Failure, CPLE_AppDefined,                  "MySQL connect failed for: %s\n%s",                   pszNewName + 6, mysql_error( hConn ) );        mysql_close( hConn );        hConn = NULL;    }    if( hConn == NULL )    {        CSLDestroy( papszTableNames );        return FALSE;    }        pszName = CPLStrdup( pszNewName );        bDSUpdate = bUpdate;/* -------------------------------------------------------------------- *//*      Get a list of available tables.                                 *//* -------------------------------------------------------------------- */    if( papszTableNames == NULL )    {        MYSQL_RES *hResultSet;        MYSQL_ROW papszRow;        if( mysql_query( hConn, "SHOW TABLES" ) )        {            ReportError( "SHOW TABLES Failed" );            return FALSE;        }        hResultSet = mysql_store_result( hConn );        if( hResultSet == NULL )        {            ReportError( "mysql_store_result() failed on SHOW TABLES result.");            return FALSE;        }            while( (papszRow = mysql_fetch_row( hResultSet )) != NULL )        {            if( papszRow[0] == NULL )                continue;            if( EQUAL(papszRow[0],"spatial_ref_sys")                || EQUAL(papszRow[0],"geometry_columns") )                continue;            papszTableNames = CSLAddString(papszTableNames, papszRow[0] );        }        mysql_free_result( hResultSet );    }/* -------------------------------------------------------------------- *//*      Get the schema of the available tables.                         *//* -------------------------------------------------------------------- */    int iRecord;    for( iRecord = 0;          papszTableNames != NULL && papszTableNames[iRecord] != NULL;         iRecord++ )    {        OpenTable( papszTableNames[iRecord], bUpdate, FALSE );    }    CSLDestroy( papszTableNames );        return nLayers > 0 || bUpdate;}/************************************************************************//*                             OpenTable()                              *//************************************************************************/int OGRMySQLDataSource::OpenTable( const char *pszNewName, int bUpdate,                                int bTestOpen ){/* -------------------------------------------------------------------- *//*      Create the layer object.                                        *//* -------------------------------------------------------------------- */    OGRMySQLLayer  *poLayer;    poLayer = new OGRMySQLTableLayer( this, pszNewName, bUpdate );/* -------------------------------------------------------------------- *//*      Add layer to data source layer list.                            *//* -------------------------------------------------------------------- */    papoLayers = (OGRMySQLLayer **)        CPLRealloc( papoLayers,  sizeof(OGRMySQLLayer *) * (nLayers+1) );    papoLayers[nLayers++] = poLayer;        return TRUE;}#ifdef notdef/************************************************************************//*                            DeleteLayer()                             *//************************************************************************/void OGRMySQLDataSource::DeleteLayer( const char *pszLayerName ){    int iLayer;/* -------------------------------------------------------------------- *//*      Try to find layer.                                              *//* -------------------------------------------------------------------- */    for( iLayer = 0; iLayer < nLayers; iLayer++ )    {        if( EQUAL(pszLayerName,papoLayers[iLayer]->GetLayerDefn()->GetName()) )            break;    }    if( iLayer == nLayers )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Attempt to delete layer '%s', but this layer is not known to OGR.",                   pszLayerName );        return;    }/* -------------------------------------------------------------------- *//*      Blow away our OGR structures related to the layer.  This is     *//*      pretty dangerous if anything has a reference to this layer!     *//* -------------------------------------------------------------------- */    CPLDebug( "OGR_MYSQL", "DeleteLayer(%s)", pszLayerName );    delete papoLayers[iLayer];    memmove( papoLayers + iLayer, papoLayers + iLayer + 1,              sizeof(void *) * (nLayers - iLayer - 1) );    nLayers--;/* -------------------------------------------------------------------- *//*      Remove from the database.                                       *//* -------------------------------------------------------------------- */    PGresult            *hResult;    char                szCommand[1024];    hResult = PQexec(hPGConn, "BEGIN");    PQclear( hResult );    if( bHavePostGIS )    {        sprintf( szCommand, 

⌨️ 快捷键说明

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