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

📄 ogrsqlitetablelayer.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            nSRSId = atoi(PQgetvalue(hResult,0,0));        }        poDS->SoftCommit();    }#endif    return OGRSQLiteLayer::GetSpatialRef();}/************************************************************************//*                            CreateField()                             *//************************************************************************/OGRErr OGRSQLiteTableLayer::CreateField( OGRFieldDefn *poFieldIn,                                          int bApproxOK ){    char                szFieldType[256];    OGRFieldDefn        oField( poFieldIn );    ResetReading();/* -------------------------------------------------------------------- *//*      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 PostgreSQL type.                                   *//* -------------------------------------------------------------------- */    if( oField.GetType() == OFTInteger )    {        strcpy( szFieldType, "INTEGER" );    }    else if( oField.GetType() == OFTReal )    {        strcpy( szFieldType, "FLOAT" );    }    else    {        strcpy( szFieldType, "VARCHAR" );    }/* -------------------------------------------------------------------- *//*      How much space do we need for the list of fields.               *//* -------------------------------------------------------------------- */    int iField, nFieldListLen = 100;    char *pszOldFieldList, *pszNewFieldList;    for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ )    {        nFieldListLen +=             strlen(poFeatureDefn->GetFieldDefn(iField)->GetNameRef()) + 50;    }    nFieldListLen += strlen( oField.GetNameRef() );    pszOldFieldList = (char *) CPLCalloc(1,nFieldListLen);    pszNewFieldList = (char *) CPLCalloc(1,nFieldListLen);/* -------------------------------------------------------------------- *//*      Build list of old fields, and the list of new fields.           *//* -------------------------------------------------------------------- */    const char *pszType;    sprintf( pszOldFieldList, "%s", "OGC_FID" );    sprintf( pszNewFieldList, "%s", "OGC_FID INTEGER PRIMARY KEY" );        int iNextOrdinal = 3; /* _rowid_ is 1, OGC_FID is 2 */    if( poFeatureDefn->GetGeomType() != wkbNone )    {        strcat( pszOldFieldList, ", WKT_GEOMETRY" );        strcat( pszNewFieldList, ", WKT_GEOMETRY VARCHAR" );        iNextOrdinal++;    }    for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ )    {        OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(iField);        // we already added OGC_FID so don't do it again        if( EQUAL(poFldDefn->GetNameRef(),"OGC_FID") )            continue;        if( poFldDefn->GetType() == OFTInteger )            pszType = "INTEGER";        else if( poFldDefn->GetType() == OFTReal )            pszType = "FLOAT";        else            pszType = "VARCHAR";                sprintf( pszOldFieldList+strlen(pszOldFieldList),                  ", '%s'", poFldDefn->GetNameRef() );        sprintf( pszNewFieldList+strlen(pszNewFieldList),                  ", '%s' %s", poFldDefn->GetNameRef(), pszType );        iNextOrdinal++;    }/* -------------------------------------------------------------------- *//*      Add the new field.                                              *//* -------------------------------------------------------------------- */    if( oField.GetType() == OFTInteger )        pszType = "INTEGER";    else if( oField.GetType() == OFTReal )        pszType = "FLOAT";    else        pszType = "VARCHAR";        sprintf( pszNewFieldList+strlen(pszNewFieldList),              ", '%s' %s", oField.GetNameRef(), pszType );/* ==================================================================== *//*      Backup, destroy, recreate and repopulate the table.  SQLite     *//*      has no ALTER TABLE so we have to do all this to add a           *//*      column.                                                         *//* ==================================================================== *//* -------------------------------------------------------------------- *//*      Do this all in a transaction.                                   *//* -------------------------------------------------------------------- */    poDS->SoftStartTransaction();/* -------------------------------------------------------------------- *//*      Make a backup of the table.                                     *//* -------------------------------------------------------------------- */    int rc;    char *pszErrMsg = NULL;    sqlite3 *hDB = poDS->GetDB();        rc = sqlite3_exec( hDB,                        CPLSPrintf( "CREATE TEMPORARY TABLE t1_back(%s)",                                   pszOldFieldList ),                       NULL, NULL, &pszErrMsg );    if( rc == SQLITE_OK )        rc = sqlite3_exec( hDB,                            CPLSPrintf( "INSERT INTO t1_back SELECT %s FROM '%s'",                                       pszOldFieldList,                                        poFeatureDefn->GetName() ),                           NULL, NULL, &pszErrMsg );/* -------------------------------------------------------------------- *//*      Drop the original table, and recreate with new field.           *//* -------------------------------------------------------------------- */    if( rc == SQLITE_OK )        rc = sqlite3_exec( hDB,                            CPLSPrintf( "DROP TABLE '%s'",                                        poFeatureDefn->GetName() ),                           NULL, NULL, &pszErrMsg );    if( rc == SQLITE_OK )    {        const char *pszCmd =             CPLSPrintf( "CREATE TABLE '%s' (%s)",                         poFeatureDefn->GetName(),                        pszNewFieldList );        rc = sqlite3_exec( hDB, pszCmd,                            NULL, NULL, &pszErrMsg );        CPLDebug( "OGR_SQLITE", "exec(%s)", pszCmd );    }/* -------------------------------------------------------------------- *//*      Copy backup field values into new table.                        *//* -------------------------------------------------------------------- */        if( rc == SQLITE_OK )        rc = sqlite3_exec( hDB,                            CPLSPrintf( "INSERT INTO '%s' SELECT %s, NULL FROM t1_back",                                       poFeatureDefn->GetName(),                                       pszOldFieldList ),                           NULL, NULL, &pszErrMsg );/* -------------------------------------------------------------------- *//*      Cleanup backup table.                                           *//* -------------------------------------------------------------------- */        if( rc == SQLITE_OK )        rc = sqlite3_exec( hDB,                            CPLSPrintf( "DROP TABLE t1_back" ),                           NULL, NULL, &pszErrMsg );/* -------------------------------------------------------------------- *//*      COMMIT on success or ROLLBACK on failuire.                      *//* -------------------------------------------------------------------- */    if( rc == SQLITE_OK )    {        poDS->SoftCommit();    }    else    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Failed to add field %s to table %s:\n %s",                  oField.GetNameRef(), poFeatureDefn->GetName(),                   pszErrMsg );        sqlite3_free( pszErrMsg );        poDS->SoftRollback();        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Add the field to the OGRFeatureDefn.                            *//* -------------------------------------------------------------------- */    int iNewField;    poFeatureDefn->AddFieldDefn( &oField );    iNewField = poFeatureDefn->GetFieldCount() - 1;    panFieldOrdinals = (int *)         CPLRealloc(panFieldOrdinals, (iNewField+1) * sizeof(int) );    panFieldOrdinals[iNewField] = iNextOrdinal;    return OGRERR_NONE;}/************************************************************************//*                             SetFeature()                             *//************************************************************************/OGRErr OGRSQLiteTableLayer::SetFeature( OGRFeature *poFeature ){    CPLAssert( pszFIDColumn != NULL );        if( poFeature->GetFID() == OGRNullFID )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "SetFeature() with unset FID fails." );        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      Drop the record with this FID.                                  *//* -------------------------------------------------------------------- */    int rc;    char *pszErrMsg = NULL;    const char *pszSQL;    pszSQL =         CPLSPrintf( "DELETE FROM '%s' WHERE \"%s\" = %d",                     poFeatureDefn->GetName(),                     pszFIDColumn,                    poFeature->GetFID() );    CPLDebug( "OGR_SQLITE", "exec(%s)", pszSQL );        rc = sqlite3_exec( poDS->GetDB(), pszSQL,                       NULL, NULL, &pszErrMsg );        if( rc != SQLITE_OK )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Attempt to delete old feature with FID %d failed.\n%s",                   poFeature->GetFID(), pszErrMsg );        return OGRERR_FAILURE;    }    /* -------------------------------------------------------------------- *//*      Recreate the feature.                                           *//* -------------------------------------------------------------------- */    return CreateFeature( poFeature );}/************************************************************************//*                           CreateFeature()                            *//************************************************************************/OGRErr OGRSQLiteTableLayer::CreateFeature( OGRFeature *poFeature ){    std::string    oCommand;    std::string    oValues;    int            bNeedComma = FALSE;    ResetReading();/* -------------------------------------------------------------------- *//*      Form the INSERT command.                                        *//* -------------------------------------------------------------------- */    oCommand += CPLSPrintf( "INSERT INTO '%s' (", poFeatureDefn->GetName() );/* -------------------------------------------------------------------- *//*      Add FID if we have a cleartext FID column.                      *//* -------------------------------------------------------------------- */    if( pszFIDColumn != NULL && !EQUAL(pszFIDColumn,"OGC_FID")         && poFeature->GetFID() != OGRNullFID )    {        oCommand += pszFIDColumn;        oValues += CPLSPrintf( "%d", poFeature->GetFID() );        bNeedComma = TRUE;    }/* -------------------------------------------------------------------- *//*      Add geometry.                                                   *//* -------------------------------------------------------------------- */    if( pszGeomColumn != NULL && poFeature->GetGeometryRef() != NULL )    {        char *pszWKT = NULL;        if( bNeedComma )        {            oCommand += ",";            oValues += ",";        }        oCommand += pszGeomColumn;        poFeature->GetGeometryRef()->exportToWkt( &pszWKT );        oValues += "'";        oValues += pszWKT;        oValues += "'";        CPLFree( pszWKT );        bNeedComma = TRUE;    }/* -------------------------------------------------------------------- *//*      Add field values.                                               *//* -------------------------------------------------------------------- */    int iField;    for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ )    {        const char *pszRawValue;        if( !poFeature->IsFieldSet( iField ) )            continue;        if( bNeedComma )        {            oCommand += ",";            oValues += ",";        }        oCommand += "'";        oCommand +=poFeatureDefn->GetFieldDefn(iField)->GetNameRef();        oCommand += "'";        pszRawValue = poFeature->GetFieldAsString( iField );        if( strchr( pszRawValue, '\'' ) != NULL )        {            char *pszEscapedValue =                 CPLEscapeString( pszRawValue, -1, CPLES_SQL );            oValues += "'";            oValues += pszEscapedValue;            oValues += "'";            CPLFree( pszEscapedValue );        }        else        {            oValues += "'";            oValues += pszRawValue;            oValues += "'";        }                    bNeedComma = TRUE;    }/* -------------------------------------------------------------------- *//*      Merge final command.                                            *//* -------------------------------------------------------------------- */    oCommand += ") VALUES (";    oCommand += oValues;    oCommand += ")";/* -------------------------------------------------------------------- *//*      Execute the insert.                                             *//* -------------------------------------------------------------------- */    int rc;    char *pszErrMsg = NULL;    CPLDebug( "OGR_SQLITE", "exec(%s)", oCommand.c_str() );    rc = sqlite3_exec( poDS->GetDB(), oCommand.c_str(),                        NULL, NULL, &pszErrMsg );    if( rc != SQLITE_OK )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "CreateFeature() failed: %s",                  pszErrMsg );        sqlite3_free( pszErrMsg );        return OGRERR_FAILURE;    }/* -------------------------------------------------------------------- *//*      We likely ought to consider fetching the rowid/fid and          *//*      putting it back in the feature.                                 *//* -------------------------------------------------------------------- */    // todo     return OGRERR_NONE;}

⌨️ 快捷键说明

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