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

📄 ogrsqlitedatasource.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                          "Layer %s already exists, CreateLayer failed.\n"                          "Use the layer creation option OVERWRITE=YES to "                          "replace it.",                          pszLayerName );                CPLFree( pszLayerName );                return NULL;            }        }    }/* -------------------------------------------------------------------- *//*      Try to get the SRS Id of this spatial reference system,         *//*      adding tot the srs table if needed.                             *//* -------------------------------------------------------------------- */#ifdef notdef    int nSRSId = -1;    if( poSRS != NULL )        nSRSId = FetchSRSId( poSRS );#endif/* -------------------------------------------------------------------- *//*      Create a basic table with the FID.  Also include the            *//*      geometry if this is not a PostGIS enabled table.                *//* -------------------------------------------------------------------- */    int rc;    char *pszErrMsg;    const char *pszGeomCol = NULL;    if( eType == wkbNone )        sprintf( szCommand,                  "CREATE TABLE '%s' ( OGC_FID INTEGER PRIMARY KEY )",                  pszLayerName );    else    {        sprintf( szCommand,                  "CREATE TABLE '%s' ( "                 "  OGC_FID INTEGER PRIMARY KEY,"                 "  WKT_GEOMETRY VARCHAR )",                  pszLayerName );        pszGeomCol = "WKT_GEOMETRY";    }    CPLDebug( "OGR_SQLITE", "exec(%s)", szCommand );    rc = sqlite3_exec( hDB, szCommand, NULL, NULL, &pszErrMsg );    if( rc != SQLITE_OK )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Unable to create table %s: %s",                  pszLayerName, pszErrMsg );        sqlite3_free( pszErrMsg );        return FALSE;    }/* -------------------------------------------------------------------- *//*      Eventually we should be adding this table to a table of         *//*      "geometric layers", capturing the WKT projection, and           *//*      perhaps some other housekeeping.                                *//* -------------------------------------------------------------------- */#ifdef notdef    if( bHavePostGIS )    {        const char *pszGeometryType;        /* Sometimes there is an old cruft 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 );                         CPLDebug( "OGR_PG", "PQexec(%s)", szCommand );        hResult = PQexec(hPGConn, szCommand);        PQclear( hResult );        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;        }        sprintf( szCommand,                  "select AddGeometryColumn('%s','%s','wkb_geometry',%d,'%s',%d)",                 pszDBName, pszLayerName, nSRSId, pszGeometryType, 3 );        CPLDebug( "OGR_PG", "PQexec(%s)", szCommand );        hResult = PQexec(hPGConn, szCommand);        if( !hResult             || PQresultStatus(hResult) != PGRES_TUPLES_OK )        {            CPLError( CE_Failure, CPLE_AppDefined,                       "AddGeometryColumn failed for layer %s, layer creation has failed.",                      pszLayerName );                        CPLFree( pszLayerName );            PQclear( hResult );            hResult = PQexec(hPGConn, "ROLLBACK");            PQclear( hResult );            return NULL;        }    }#endif/* -------------------------------------------------------------------- *//*      Create the layer object.                                        *//* -------------------------------------------------------------------- */    OGRSQLiteTableLayer     *poLayer;    poLayer = new OGRSQLiteTableLayer( this );    poLayer->Initialize( pszLayerName, pszGeomCol );    poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) );/* -------------------------------------------------------------------- *//*      Add layer to data source layer list.                            *//* -------------------------------------------------------------------- */    papoLayers = (OGRSQLiteLayer **)        CPLRealloc( papoLayers,  sizeof(OGRSQLiteLayer *) * (nLayers+1) );        papoLayers[nLayers++] = poLayer;    CPLFree( pszLayerName );    return poLayer;}/************************************************************************//*                            LaunderName()                             *//************************************************************************/char *OGRSQLiteDataSource::LaunderName( const char *pszSrcName ){    char    *pszSafeName = CPLStrdup( pszSrcName );    int     i;    for( i = 0; pszSafeName[i] != '\0'; i++ )    {        pszSafeName[i] = (char) tolower( pszSafeName[i] );        if( pszSafeName[i] == '-' || pszSafeName[i] == '#' )            pszSafeName[i] = '_';    }    return pszSafeName;}/************************************************************************//*                            DeleteLayer()                             *//************************************************************************/void OGRSQLiteDataSource::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_PG", "DeleteLayer(%s)", pszLayerName );    delete papoLayers[iLayer];    memmove( papoLayers + iLayer, papoLayers + iLayer + 1,              sizeof(void *) * (nLayers - iLayer - 1) );    nLayers--;/* -------------------------------------------------------------------- *//*      Remove from the database.                                       *//* -------------------------------------------------------------------- */    int rc;    char *pszErrMsg;    rc = sqlite3_exec( hDB, CPLSPrintf( "DROP TABLE '%s'", pszLayerName ),                       NULL, NULL, &pszErrMsg );    if( rc != SQLITE_OK )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "Unable to drop table %s: %s",                  pszLayerName, pszErrMsg );        sqlite3_free( pszErrMsg );        return;    }    /* We may need to drop the column from GEOMETRY_COLUMNS in the future. */}/************************************************************************//*                        SoftStartTransaction()                        *//*                                                                      *//*      Create a transaction scope.  If we already have a               *//*      transaction active this isn't a real transaction, but just      *//*      an increment to the scope count.                                *//************************************************************************/OGRErr OGRSQLiteDataSource::SoftStartTransaction(){    nSoftTransactionLevel++;    if( nSoftTransactionLevel == 1 )    {        int rc;        char *pszErrMsg;                CPLDebug( "OGR_SQLITE", "BEGIN Transaction" );        rc = sqlite3_exec( hDB, "BEGIN", NULL, NULL, &pszErrMsg );        if( rc != SQLITE_OK )        {            nSoftTransactionLevel--;            CPLError( CE_Failure, CPLE_AppDefined,                       "BEGIN transaction failed: %s",                      pszErrMsg );            sqlite3_free( pszErrMsg );            return OGRERR_FAILURE;        }    }    return OGRERR_NONE;}/************************************************************************//*                             SoftCommit()                             *//*                                                                      *//*      Commit the current transaction if we are at the outer           *//*      scope.                                                          *//************************************************************************/OGRErr OGRSQLiteDataSource::SoftCommit(){    if( nSoftTransactionLevel <= 0 )    {        CPLDebug( "OGR_SQLITE", "SoftCommit() with no transaction active." );        return OGRERR_FAILURE;    }    nSoftTransactionLevel--;    if( nSoftTransactionLevel == 0 )    {        int rc;        char *pszErrMsg;                CPLDebug( "OGR_SQLITE", "COMMIT Transaction" );        rc = sqlite3_exec( hDB, "COMMIT", NULL, NULL, &pszErrMsg );        if( rc != SQLITE_OK )        {            CPLError( CE_Failure, CPLE_AppDefined,                       "COMMIT transaction failed: %s",                      pszErrMsg );            sqlite3_free( pszErrMsg );            return OGRERR_FAILURE;        }    }    return OGRERR_NONE;}/************************************************************************//*                            SoftRollback()                            *//*                                                                      *//*      Force a rollback of the current transaction if there is one,    *//*      even if we are nested several levels deep.                      *//************************************************************************/OGRErr OGRSQLiteDataSource::SoftRollback(){    if( nSoftTransactionLevel <= 0 )    {        CPLDebug( "OGR_SQLITE", "SoftRollback() with no transaction active." );        return OGRERR_FAILURE;    }    nSoftTransactionLevel = 0;    int rc;    char *pszErrMsg;        CPLDebug( "OGR_SQLITE", "ROLLBACK Transaction" );    rc = sqlite3_exec( hDB, "ROLLBACK", NULL, NULL, &pszErrMsg );    if( rc != SQLITE_OK )    {        CPLError( CE_Failure, CPLE_AppDefined,                   "ROLLBACK transaction failed: %s",                  pszErrMsg );        sqlite3_free( pszErrMsg );        return OGRERR_FAILURE;    }        return OGRERR_NONE;}/************************************************************************//*                        FlushSoftTransaction()                        *//*                                                                      *//*      Force the unwinding of any active transaction, and it's         *//*      commit.                                                         *//************************************************************************/OGRErr OGRSQLiteDataSource::FlushSoftTransaction(){    if( nSoftTransactionLevel <= 0 )        return OGRERR_NONE;    nSoftTransactionLevel = 1;    return SoftCommit();}

⌨️ 快捷键说明

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