ogrmysqltablelayer.cpp

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

CPP
1,065
字号
        {            osCommand +=                 CPLString().Printf(                    "GeometryFromText('%s',%d) ", pszWKT, nSRSId );            OGRFree( pszWKT );        }        else            osCommand += "''";    }    // Set the FID     if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL )    {        if( bNeedComma )            osCommand += ", ";        osCommand += CPLString().Printf( "%ld ", poFeature->GetFID() );        bNeedComma = TRUE;    }    for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )    {        const char *pszStrValue = poFeature->GetFieldAsString(i);        char *pszNeedToFree = NULL;        if( !poFeature->IsFieldSet( i ) )            continue;        if( bNeedComma )            osCommand += ", ";        else            bNeedComma = TRUE;        if( poFeatureDefn->GetFieldDefn(i)->GetType() != OFTInteger                 && poFeatureDefn->GetFieldDefn(i)->GetType() != OFTReal )        {            int         iChar;            //We need to quote and escape string fields.             osCommand += "'";            for( iChar = 0; pszStrValue[iChar] != '\0'; iChar++ )            {                if( poFeatureDefn->GetFieldDefn(i)->GetType() != OFTIntegerList                    && poFeatureDefn->GetFieldDefn(i)->GetType() != OFTRealList                    && poFeatureDefn->GetFieldDefn(i)->GetWidth() > 0                    && iChar == poFeatureDefn->GetFieldDefn(i)->GetWidth() )                {                    CPLDebug( "MYSQL",                              "Truncated %s field value, it was too long.",                              poFeatureDefn->GetFieldDefn(i)->GetNameRef() );                    break;                }                if( pszStrValue[iChar] == '\\'                    || pszStrValue[iChar] == '\'' )                {                    osCommand += '\\';                    osCommand += pszStrValue[iChar];                }                else                    osCommand += pszStrValue[iChar];            }            osCommand += "'";        }        else        {            osCommand += pszStrValue;        }        if( pszNeedToFree )            CPLFree( pszNeedToFree );    }    osCommand += ")";        int nQueryResult = mysql_query(poDS->GetConn(), osCommand.c_str() );        if( nQueryResult ){           int eErrorCode = mysql_errno(poDS->GetConn());        if (eErrorCode == 1153) {//ER_NET_PACKET_TOO_LARGE)            poDS->ReportError("CreateFeature failed because the MySQL server " \                              "cannot read the entire query statement.  Increase " \                              "the size of statements your server will allow by " \                              "altering the 'max_allowed_packet' parameter in "\                              "your MySQL server configuration.");        }        else        {        CPLDebug("MYSQL","Error number %d", eErrorCode);            poDS->ReportError(  osCommand.c_str() );        }        // make sure to attempt to free results        hResult = mysql_store_result( poDS->GetConn() );        if( hResult != NULL )            mysql_free_result( hResult );        hResult = NULL;                    return OGRERR_FAILURE;       }    // make sure to attempt to free results of successful queries    hResult = mysql_store_result( poDS->GetConn() );    if( hResult != NULL )        mysql_free_result( hResult );    hResult = NULL;        return OGRERR_NONE;}/************************************************************************//*                            CreateField()                             *//************************************************************************/OGRErr OGRMySQLTableLayer::CreateField( OGRFieldDefn *poFieldIn, int bApproxOK ){    MYSQL_RES           *hResult=NULL;    char        		szCommand[1024];        char                szFieldType[256];    OGRFieldDefn        oField( poFieldIn );/* -------------------------------------------------------------------- *//*      Do we want to "launder" the column names into Postgres          *//*      friendly format?                                                *//* -------------------------------------------------------------------- */    if( bLaunderColumnNames )    {        char    *pszSafeName = poDS->LaunderName( oField.GetNameRef() );        oField.SetName( pszSafeName );        CPLFree( pszSafeName );    }/* -------------------------------------------------------------------- *//*      Work out the MySQL type.                                        *//* -------------------------------------------------------------------- */    if( oField.GetType() == OFTInteger )    {        if( oField.GetWidth() > 0 && bPreservePrecision )            sprintf( szFieldType, "DECIMAL(%d,0)", oField.GetWidth() );        else            strcpy( szFieldType, "INTEGER" );    }    else if( oField.GetType() == OFTReal )    {        if( oField.GetWidth() > 0 && oField.GetPrecision() > 0            && bPreservePrecision )            sprintf( szFieldType, "DOUBLE(%d,%d)",                     oField.GetWidth(), oField.GetPrecision() );        else            strcpy( szFieldType, "DOUBLE" );    }    else if( oField.GetType() == OFTDate )    {        sprintf( szFieldType, "DATE" );    }    else if( oField.GetType() == OFTDateTime )    {        sprintf( szFieldType, "DATETIME" );    }    else if( oField.GetType() == OFTTime )    {        sprintf( szFieldType, "TIME" );    }    else if( oField.GetType() == OFTString )    {        if( oField.GetWidth() == 0 || !bPreservePrecision )            strcpy( szFieldType, "TEXT" );        else            sprintf( szFieldType, "VARCHAR(%d)", oField.GetWidth() );    }    else if( bApproxOK )    {        CPLError( CE_Warning, CPLE_NotSupported,                  "Can't create field %s with type %s on MySQL layers.  Creating as TEXT.",                  oField.GetNameRef(),                  OGRFieldDefn::GetFieldTypeName(oField.GetType()) );        strcpy( szFieldType, "TEXT" );    }    else    {        CPLError( CE_Failure, CPLE_NotSupported,                  "Can't create field %s with type %s on MySQL layers.",                  oField.GetNameRef(),                  OGRFieldDefn::GetFieldTypeName(oField.GetType()) );        return OGRERR_FAILURE;    }    sprintf( szCommand,             "ALTER TABLE %s ADD COLUMN %s %s",             poFeatureDefn->GetName(), oField.GetNameRef(), szFieldType );    if( mysql_query(poDS->GetConn(), szCommand ) )    {        poDS->ReportError( szCommand );        return OGRERR_FAILURE;    }    // make sure to attempt to free results of successful queries    hResult = mysql_store_result( poDS->GetConn() );    if( hResult != NULL )        mysql_free_result( hResult );    hResult = NULL;       poFeatureDefn->AddFieldDefn( &oField );            return OGRERR_NONE;}/************************************************************************//*                             GetFeature()                             *//************************************************************************/OGRFeature *OGRMySQLTableLayer::GetFeature( long nFeatureId ){    if( pszFIDColumn == NULL )        return OGRMySQLLayer::GetFeature( nFeatureId );/* -------------------------------------------------------------------- *//*      Discard any existing resultset.                                 *//* -------------------------------------------------------------------- */    ResetReading();/* -------------------------------------------------------------------- *//*      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;    unsigned long *panLengths;    papszRow = mysql_fetch_row( hResultSet );    if( papszRow == NULL )        return NULL;    panLengths = mysql_fetch_lengths( hResultSet );/* -------------------------------------------------------------------- *//*      Transform into a feature.                                       *//* -------------------------------------------------------------------- */    iNextShapeId = nFeatureId;    OGRFeature *poFeature = RecordToFeature( papszRow, panLengths );    iNextShapeId = 0;/* -------------------------------------------------------------------- *//*      Cleanup                                                         *//* -------------------------------------------------------------------- */    if( hResultSet != NULL )        mysql_free_result( hResultSet ); 		hResultSet = NULL;    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]);    if( hResultSet != NULL )        mysql_free_result( hResultSet ); 		hResultSet = NULL;        return nCount;}

⌨️ 快捷键说明

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