ogrocitablelayer.cpp

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

CPP
1,710
字号
/************************************************************************//*                      BuildFullQueryStatement()                       *//************************************************************************/void OGROCITableLayer::BuildFullQueryStatement(){    if( pszQueryStatement != NULL )    {        CPLFree( pszQueryStatement );        pszQueryStatement = NULL;    }    OGROCIStringBuf oCmd;    char *pszFields = BuildFields();    oCmd.Append( "SELECT " );    oCmd.Append( pszFields );    oCmd.Append( " FROM " );    oCmd.Append( poFeatureDefn->GetName() );    oCmd.Append( " " );    oCmd.Append( pszWHERE );    pszQueryStatement = oCmd.StealString();    CPLFree( pszFields );}/************************************************************************//*                             GetFeature()                             *//************************************************************************/OGRFeature *OGROCITableLayer::GetFeature( long nFeatureId ){/* -------------------------------------------------------------------- *//*      If we don't have an FID column scan for the desired feature.    *//* -------------------------------------------------------------------- */    if( pszFIDName == NULL )        return OGROCILayer::GetFeature( nFeatureId );/* -------------------------------------------------------------------- *//*      Clear any existing query.                                       *//* -------------------------------------------------------------------- */    ResetReading();/* -------------------------------------------------------------------- *//*      Build query for this specific feature.                          *//* -------------------------------------------------------------------- */    OGROCIStringBuf oCmd;    char *pszFields = BuildFields();    oCmd.Append( "SELECT " );    oCmd.Append( pszFields );    oCmd.Append( " FROM " );    oCmd.Append( poFeatureDefn->GetName() );    oCmd.Append( " " );    oCmd.Appendf( 50+strlen(pszFIDName),                   " WHERE \"%s\" = %ld ",                   pszFIDName, nFeatureId );/* -------------------------------------------------------------------- *//*      Execute the statement.                                          *//* -------------------------------------------------------------------- */    if( !ExecuteQuery( oCmd.GetString() ) )        return NULL;/* -------------------------------------------------------------------- *//*      Get the feature.                                                *//* -------------------------------------------------------------------- */    OGRFeature *poFeature;    poFeature = GetNextRawFeature();        if( poFeature != NULL && poFeature->GetGeometryRef() != NULL )        poFeature->GetGeometryRef()->assignSpatialReference( poSRS );/* -------------------------------------------------------------------- *//*      Cleanup the statement.                                          *//* -------------------------------------------------------------------- */    ResetReading();/* -------------------------------------------------------------------- *//*      verify the FID.                                                 *//* -------------------------------------------------------------------- */    if( poFeature != NULL && poFeature->GetFID() != nFeatureId )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "OGROCITableLayer::GetFeature(%d) ... query returned feature %d instead!",                  nFeatureId, poFeature->GetFID() );        delete poFeature;        return NULL;    }    else        return poFeature;}/************************************************************************//*                           GetNextFeature()                           *//*                                                                      *//*      We override the next feature method because we know that we     *//*      implement the attribute query within the statement and so we    *//*      don't have to test here.   Eventually the spatial query will    *//*      be fully tested within the statement as well.                   *//************************************************************************/OGRFeature *OGROCITableLayer::GetNextFeature(){    for( ; TRUE; )    {        OGRFeature      *poFeature;        poFeature = GetNextRawFeature();        if( poFeature == NULL )        {            CPLDebug( "OCI", "Query complete, got %d hits, and %d discards.",                      nHits, nDiscarded );            nHits = 0;            nDiscarded = 0;            return NULL;        }        if( m_poFilterGeom == NULL            || FilterGeometry( poFeature->GetGeometryRef() ) )        {            nHits++;            if( poFeature->GetGeometryRef() != NULL )                poFeature->GetGeometryRef()->assignSpatialReference( poSRS );            return poFeature;        }        if( m_poFilterGeom != NULL )            nDiscarded++;        delete poFeature;    }}/************************************************************************//*                            ResetReading()                            *//************************************************************************/void OGROCITableLayer::ResetReading(){    nHits = 0;    nDiscarded = 0;    FlushPendingFeatures();    BuildFullQueryStatement();    OGROCILayer::ResetReading();}/************************************************************************//*                            BuildFields()                             *//*                                                                      *//*      Build list of fields to fetch, performing any required          *//*      transformations (such as on geometry).                          *//************************************************************************/char *OGROCITableLayer::BuildFields(){    int         i;    OGROCIStringBuf oFldList;    if( pszGeomName )                                                       {        oFldList.Append( "\"" );        oFldList.Append( pszGeomName );        oFldList.Append( "\"" );        iGeomColumn = 0;    }    for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )    {        const char *pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef();        if( oFldList.GetLast() != '\0' )            oFldList.Append( "," );        oFldList.Append( "\"" );        oFldList.Append( pszName );        oFldList.Append( "\"" );    }    if( pszFIDName != NULL )    {        iFIDColumn = poFeatureDefn->GetFieldCount();        oFldList.Append( ",\"" );        oFldList.Append( pszFIDName );        oFldList.Append( "\"" );    }    return oFldList.StealString();}/************************************************************************//*                         SetAttributeFilter()                         *//************************************************************************/OGRErr OGROCITableLayer::SetAttributeFilter( const char *pszQuery ){    if( (pszQuery == NULL && this->pszQuery == NULL)        || (pszQuery != NULL && this->pszQuery != NULL            && strcmp(pszQuery,this->pszQuery) == 0) )        return OGRERR_NONE;        CPLFree( this->pszQuery );    if( pszQuery == NULL )        this->pszQuery = NULL;    else        this->pszQuery = CPLStrdup( pszQuery );    BuildWhere();    ResetReading();    return OGRERR_NONE;}/************************************************************************//*                             SetFeature()                             *//*                                                                      *//*      We implement SetFeature() by deleting the existing row (if      *//*      it exists), and then using CreateFeature() to write it out      *//*      tot he table normally.  CreateFeature() will preserve the       *//*      existing FID if possible.                                       *//************************************************************************/OGRErr OGROCITableLayer::SetFeature( OGRFeature *poFeature ){/* -------------------------------------------------------------------- *//*      Do some validation.                                             *//* -------------------------------------------------------------------- */    if( pszFIDName == NULL )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "OGROCITableLayer::SetFeature(%d) failed because there is "                  "no apparent FID column on table %s.",                  poFeature->GetFID(),                   poFeatureDefn->GetName() );        return OGRERR_FAILURE;    }    if( poFeature->GetFID() == OGRNullFID )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "OGROCITableLayer::SetFeature(%d) failed because the feature "                  "has no FID!", poFeature->GetFID() );        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Prepare the delete command, and execute.  We don't check the    *//*      error result of the execute, since attempting to Set a          *//*      non-existing feature may be OK.                                 *//* -------------------------------------------------------------------- */    OGROCIStringBuf     oCmdText;    OGROCIStatement     oCmdStatement( poDS->GetSession() );    oCmdText.Appendf( strlen(poFeatureDefn->GetName())+strlen(pszFIDName)+100,                      "DELETE FROM %s WHERE \"%s\" = %d",                      poFeatureDefn->GetName(),                       pszFIDName,                       poFeature->GetFID() );    oCmdStatement.Execute( oCmdText.GetString() );    return CreateFeature( poFeature );}/************************************************************************//*                           DeleteFeature()                            *//************************************************************************/OGRErr OGROCITableLayer::DeleteFeature( long nFID ){/* -------------------------------------------------------------------- *//*      Do some validation.                                             *//* -------------------------------------------------------------------- */    if( pszFIDName == NULL )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "OGROCITableLayer::DeleteFeature(%d) failed because there is "                  "no apparent FID column on table %s.",                  nFID,                   poFeatureDefn->GetName() );        return OGRERR_FAILURE;    }    if( nFID == OGRNullFID )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "OGROCITableLayer::DeleteFeature(%d) failed for Null FID",                   nFID );        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Prepare the delete command, and execute.  We don't check the    *//*      error result of the execute, since attempting to Set a          *//*      non-existing feature may be OK.                                 *//* -------------------------------------------------------------------- */    OGROCIStringBuf     oCmdText;    OGROCIStatement     oCmdStatement( poDS->GetSession() );    oCmdText.Appendf( strlen(poFeatureDefn->GetName())+strlen(pszFIDName)+100,                      "DELETE FROM %s WHERE \"%s\" = %d",                      poFeatureDefn->GetName(),                       pszFIDName,                       nFID );    if( oCmdStatement.Execute( oCmdText.GetString() ) == CE_None )        return OGRERR_NONE;    else        return OGRERR_FAILURE;}/************************************************************************//*                           CreateFeature()                            *//************************************************************************/OGRErr OGROCITableLayer::CreateFeature( OGRFeature *poFeature ){/* -------------------------------------------------------------------- *//*      Add extents of this geometry to the existing layer extents.     */

⌨️ 快捷键说明

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