ogrmysqldatasource.cpp

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

CPP
1,104
字号
/*      result sets into RAM.  But only one "streamed" resultset can    *//*      be active on a database connection at a time.  So we need to    *//*      maintain a way of closing off an active streaming resultset     *//*      before any other sort of query with a resultset is              *//*      executable.  This method (and InterruptLongResult())            *//*      implement that exclusion.                                       *//************************************************************************/void OGRMySQLDataSource::RequestLongResult( OGRMySQLLayer * poNewLayer ){    InterruptLongResult();    poLongResultLayer = poNewLayer;}/************************************************************************//*                        InterruptLongResult()                         *//************************************************************************/void OGRMySQLDataSource::InterruptLongResult(){    if( poLongResultLayer != NULL )    {        poLongResultLayer->ResetReading();        poLongResultLayer = NULL;    }}/************************************************************************//*                            DeleteLayer()                             *//************************************************************************/int OGRMySQLDataSource::DeleteLayer( int iLayer){    if( iLayer < 0 || iLayer >= nLayers )        return OGRERR_FAILURE;        /* -------------------------------------------------------------------- *//*      Blow away our OGR structures related to the layer.  This is     *//*      pretty dangerous if anything has a reference to this layer!     *//* -------------------------------------------------------------------- */    CPLString osLayerName = papoLayers[iLayer]->GetLayerDefn()->GetName();        CPLDebug( "MYSQL", "DeleteLayer(%s)", osLayerName.c_str() );    delete papoLayers[iLayer];    memmove( papoLayers + iLayer, papoLayers + iLayer + 1,             sizeof(void *) * (nLayers - iLayer - 1) );    nLayers--;/* -------------------------------------------------------------------- *//*      Remove from the database.                                       *//* -------------------------------------------------------------------- */    char        		szCommand[1024];    sprintf( szCommand,             "DROP TABLE %s ",             osLayerName.c_str() );    if( !mysql_query(GetConn(), szCommand ) )    {        CPLDebug("MYSQL","Dropped table %s.", osLayerName.c_str());        return OGRERR_NONE;    }    else    {        ReportError( szCommand );        return OGRERR_FAILURE;    }}/************************************************************************//*                            CreateLayer()                             *//************************************************************************/OGRLayer *OGRMySQLDataSource::CreateLayer( const char * pszLayerNameIn,                              OGRSpatialReference *poSRS,                              OGRwkbGeometryType eType,                              char ** papszOptions ){    MYSQL_RES           *hResult=NULL;    char        		szCommand[1024];    const char          *pszGeometryType;    const char			*pszGeomColumnName;    const char 			*pszExpectedFIDName; 	    char                *pszLayerName;    int                 nDimension = 3; // MySQL only supports 2d currently    if( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) )        pszLayerName = LaunderName( pszLayerNameIn );    else        pszLayerName = CPLStrdup( pszLayerNameIn );    if( wkbFlatten(eType) == eType )        nDimension = 2;    CPLDebug("MYSQL","Creating layer %s.", pszLayerName);/* -------------------------------------------------------------------- *//*      Do we already have this layer?  If so, should we blow it        *//*      away?                                                           *//* -------------------------------------------------------------------- */    int iLayer;    for( iLayer = 0; iLayer < nLayers; iLayer++ )    {        if( EQUAL(pszLayerName,papoLayers[iLayer]->GetLayerDefn()->GetName()) )        {			            if( CSLFetchNameValue( papszOptions, "OVERWRITE" ) != NULL                && !EQUAL(CSLFetchNameValue(papszOptions,"OVERWRITE"),"NO") )            {                DeleteLayer( iLayer );            }            else            {                CPLFree( pszLayerName );                CPLError( CE_Failure, CPLE_AppDefined,                          "Layer %s already exists, CreateLayer failed.\n"                          "Use the layer creation option OVERWRITE=YES to "                          "replace it.",                          pszLayerName );                return NULL;            }        }    }    pszGeomColumnName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME" );    if (!pszGeomColumnName)        pszGeomColumnName="SHAPE";    pszExpectedFIDName = CSLFetchNameValue( papszOptions, "MYSQL_FID" );    if (!pszExpectedFIDName)        pszExpectedFIDName="OGR_FID";    CPLDebug("MYSQL","Geometry Column Name %s.", pszGeomColumnName);    CPLDebug("MYSQL","FID Column Name %s.", pszExpectedFIDName);    if( wkbFlatten(eType) == wkbNone )    {        sprintf( szCommand,                 "CREATE TABLE %s ( "                 "   %s INT UNIQUE NOT NULL AUTO_INCREMENT )",                 pszLayerName, pszExpectedFIDName );    }    else    {        sprintf( szCommand,                 "CREATE TABLE %s ( "                 "   %s INT UNIQUE NOT NULL AUTO_INCREMENT, "                 "   %s GEOMETRY NOT NULL )",                 pszLayerName, pszExpectedFIDName, pszGeomColumnName );    }    if( CSLFetchNameValue( papszOptions, "ENGINE" ) != NULL )    {        strcat( szCommand, " ENGINE = " );        strcat( szCommand, CSLFetchNameValue( papszOptions, "ENGINE" ) );    }	    if( !mysql_query(GetConn(), szCommand ) )    {        if( mysql_field_count( GetConn() ) == 0 )            CPLDebug("MYSQL","Created table %s.", pszLayerName);        else        {            ReportError( szCommand );            return NULL;        }    }    else    {        ReportError( szCommand );        return NULL;    }    // make sure to attempt to free results of successful queries    hResult = mysql_store_result( GetConn() );    if( hResult != NULL )        mysql_free_result( hResult );    hResult = NULL;        // Calling this does no harm    InitializeMetadataTables();    /* -------------------------------------------------------------------- *//*      Try to get the SRS Id of this spatial reference system,         *//*      adding tot the srs table if needed.                             *//* -------------------------------------------------------------------- */    int nSRSId = -1;    if( poSRS != NULL )        nSRSId = FetchSRSId( poSRS );/* -------------------------------------------------------------------- *//*      Sometimes there is an old crufty entry in the geometry_columns  *//*      table if things were not properly cleaned up before.  We make   *//*      an effort to clean out such cruft.                              *//*                                                                      *//* -------------------------------------------------------------------- */    sprintf( szCommand,             "DELETE FROM geometry_columns WHERE f_table_name = '%s'",             pszLayerName );    if( mysql_query(GetConn(), szCommand ) )    {        ReportError( szCommand );        return NULL;    }    // make sure to attempt to free results of successful queries    hResult = mysql_store_result( GetConn() );    if( hResult != NULL )        mysql_free_result( hResult );    hResult = NULL;           /* -------------------------------------------------------------------- *//*      Attempt to add this table to the geometry_columns table, if     *//*      it is a spatial layer.                                          *//* -------------------------------------------------------------------- */    if( eType != wkbNone )    {        int nCoordDimension;        if( eType == wkbFlatten(eType) )            nCoordDimension = 2;        else            nCoordDimension = 3;        switch( wkbFlatten(eType) )        {            case wkbPoint:                pszGeometryType = "POINT";                break;            case wkbLineString:                pszGeometryType = "LINESTRING";                break;            case wkbPolygon:                pszGeometryType = "POLYGON";                break;            case wkbMultiPoint:                pszGeometryType = "MULTIPOINT";                break;            case wkbMultiLineString:                pszGeometryType = "MULTILINESTRING";                break;            case wkbMultiPolygon:                pszGeometryType = "MULTIPOLYGON";                break;            case wkbGeometryCollection:                pszGeometryType = "GEOMETRYCOLLECTION";                break;            default:                pszGeometryType = "GEOMETRY";                break;        }        if( nSRSId == -1 )            sprintf( szCommand,                     "INSERT INTO geometry_columns "                     " (F_TABLE_NAME, "                     "  F_GEOMETRY_COLUMN, "                     "  COORD_DIMENSION, "                     "  TYPE) values "                     "  ('%s', '%s', %d, '%s')",                     pszLayerName,                     pszGeomColumnName,                     nCoordDimension,                     pszGeometryType );        else            sprintf( szCommand,                     "INSERT INTO geometry_columns "                     " (F_TABLE_NAME, "                     "  F_GEOMETRY_COLUMN, "                     "  COORD_DIMENSION, "                     "  SRID, "                     "  TYPE) values "                     "  ('%s', '%s', %d, %d, '%s')",                     pszLayerName,                     pszGeomColumnName,                     nCoordDimension,                     nSRSId,                     pszGeometryType );        if( mysql_query(GetConn(), szCommand ) )        {            ReportError( szCommand );            return NULL;        }        // make sure to attempt to free results of successful queries        hResult = mysql_store_result( GetConn() );        if( hResult != NULL )            mysql_free_result( hResult );        hResult = NULL;       }/* -------------------------------------------------------------------- *//*      Create the spatial index.                                       *//*                                                                      *//*      We're doing this before we add geometry and record to the table *//*      so this may not be exactly the best way to do it.               *//* -------------------------------------------------------------------- */    const char *pszSI = CSLFetchNameValue( papszOptions, "SPATIAL_INDEX" );    if( eType != wkbNone && (pszSI == NULL || CSLTestBoolean(pszSI)) )    {        sprintf( szCommand,                 "ALTER TABLE %s ADD SPATIAL INDEX(%s) ",                 pszLayerName,                 pszGeomColumnName);        if( mysql_query(GetConn(), szCommand ) )        {            ReportError( szCommand );            return NULL;        }        // make sure to attempt to free results of successful queries        hResult = mysql_store_result( GetConn() );        if( hResult != NULL )            mysql_free_result( hResult );        hResult = NULL;       }        /* -------------------------------------------------------------------- *//*      Create the layer object.                                        *//* -------------------------------------------------------------------- */    OGRMySQLTableLayer     *poLayer;    OGRErr                  eErr;    poLayer = new OGRMySQLTableLayer( this, pszLayerName, TRUE, nSRSId );    eErr = poLayer->Initialize(pszLayerName);    if (eErr == OGRERR_FAILURE)        return NULL;    poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) );    poLayer->SetPrecisionFlag( CSLFetchBoolean(papszOptions,"PRECISION",TRUE));/* -------------------------------------------------------------------- *//*      Add layer to data source layer list.                            *//* -------------------------------------------------------------------- */    papoLayers = (OGRMySQLLayer **)        CPLRealloc( papoLayers,  sizeof(OGRMySQLLayer *) * (nLayers+1) );    papoLayers[nLayers++] = poLayer;    CPLFree( pszLayerName );    return poLayer;}

⌨️ 快捷键说明

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