osgEarth 2.1.1
Public Member Functions | Public Attributes

MetadataTable Struct Reference

List of all members.

Public Member Functions

 MetadataTable ()
bool initialize (sqlite3 *db)
bool store (const MetadataRecord &rec, sqlite3 *db)
bool load (const std::string &key, sqlite3 *db, MetadataRecord &output)
bool loadAllLayers (sqlite3 *db, std::vector< std::string > &output)

Public Attributes

std::string _insertSQL
std::string _selectSQL

Detailed Description

This database table holds one record for each cached layer in the database.

Definition at line 125 of file Sqlite3Cache.cpp.


Constructor & Destructor Documentation

MetadataTable::MetadataTable ( ) [inline]

Definition at line 127 of file Sqlite3Cache.cpp.

{ }

Member Function Documentation

bool MetadataTable::initialize ( sqlite3 *  db) [inline]

Definition at line 129 of file Sqlite3Cache.cpp.

    {
        std::string sql =
            "CREATE TABLE IF NOT EXISTS metadata ("
            "layer varchar(255) PRIMARY KEY UNIQUE, "
            "format varchar(255), "
            "compressor varchar(64), "
            "tilesize int, "
            "srs varchar(1024), "
            "xmin double, "
            "ymin double, "
            "xmax double, "
            "ymax double, "
            "tw int, "
            "th int )";

        OE_DEBUG << LC << "SQL = " << sql << std::endl;

        char* errMsg = 0L;
        int err = sqlite3_exec( db, sql.c_str(), 0L, 0L, &errMsg );
        if ( err != SQLITE_OK )
        {
            OE_WARN << LC << "[Sqlite3Cache] Creating metadata: " << errMsg << std::endl;
            sqlite3_free( errMsg );
            return false;
        }

        // prep the insert/select statement SQL strings:
        _insertSQL = 
            "INSERT OR REPLACE INTO metadata "
            "(layer,format,compressor,tilesize,srs,xmin,ymin,xmax,ymax,tw,th) "
            "VALUES (?,?,?,?,?,?,?,?,?,?,?)";

        _selectSQL =
            "SELECT layer,format,compressor,tilesize,srs,xmin,ymin,xmax,ymax,tw,th "
            "FROM metadata WHERE layer = ?";

        return true;
    }
bool MetadataTable::load ( const std::string &  key,
sqlite3 *  db,
MetadataRecord output 
) [inline]

Definition at line 217 of file Sqlite3Cache.cpp.

    {
        bool success = true;

        sqlite3_stmt* select = 0L;
        int rc = sqlite3_prepare_v2( db, _selectSQL.c_str(), _selectSQL.length(), &select, 0L );
        if ( rc != SQLITE_OK )
        {
            OE_WARN 
                << LC << "Error preparing SQL: " 
                << sqlite3_errmsg( db )
                << "(SQL: " << _insertSQL << ")"
                << std::endl;
            return false;
        }

        sqlite3_bind_text( select, 1, key.c_str(), -1, 0L );

        rc = sqlite3_step( select );
        if ( rc == SQLITE_ROW ) 
        {
            // got a result            
            output._layerName = (char*)sqlite3_column_text( select, 0 );
            output._format = (char*)sqlite3_column_text( select, 1 );
            output._compressor = (char*)sqlite3_column_text( select, 2 );
            output._tileSize = sqlite3_column_int( select, 3 );
            ProfileOptions pconf;
            pconf.srsString() = (char*)sqlite3_column_text( select, 4 );
            pconf.bounds() = Bounds(
                sqlite3_column_double( select, 5 ),
                sqlite3_column_double( select, 6 ),
                sqlite3_column_double( select, 7 ),
                sqlite3_column_double( select, 8 ) );
            pconf.numTilesWideAtLod0() = sqlite3_column_int( select, 9 );
            pconf.numTilesHighAtLod0() = sqlite3_column_int( select, 10 );
            output._profile = Profile::create( pconf );
            success = true;
        }
        else
        {
            // no result
            OE_DEBUG << "NO metadata record found for \"" << key << "\"" << std::endl;
            success = false;
        }

        sqlite3_finalize( select );
        return success;
    }

Here is the call graph for this function:

bool MetadataTable::loadAllLayers ( sqlite3 *  db,
std::vector< std::string > &  output 
) [inline]

Definition at line 267 of file Sqlite3Cache.cpp.

    {
        bool success = true;

        sqlite3_stmt* select = 0L;
        std::string selectLayersSQL = "select layer from \"metadata\"";
        int rc = sqlite3_prepare_v2( db, selectLayersSQL.c_str(), selectLayersSQL.length(), &select, 0L );
        if ( rc != SQLITE_OK )
        {
            OE_WARN 
                << LC << "Error preparing SQL: " 
                << sqlite3_errmsg( db )
                << "(SQL: " << _insertSQL << ")"
                << std::endl;
            return false;
        }

        success = true;
        rc = sqlite3_step( select );
        while (rc == SQLITE_ROW) {
            output.push_back((char*)sqlite3_column_text( select, 0 ));
            rc = sqlite3_step( select );
        }

        if (rc != SQLITE_DONE)
        {
            // no result
            OE_WARN << "NO layers found in metadata" << std::endl;
            success = false;
        }

        sqlite3_finalize( select );
        return success;
    }
bool MetadataTable::store ( const MetadataRecord rec,
sqlite3 *  db 
) [inline]

Definition at line 169 of file Sqlite3Cache.cpp.

    {
        sqlite3_stmt* insert = 0;
        int rc = sqlite3_prepare_v2( db, _insertSQL.c_str(), _insertSQL.length(), &insert, 0L );
        if ( rc != SQLITE_OK )
        {
            OE_WARN 
                << LC << "Error preparing SQL: " 
                << sqlite3_errmsg( db )
                << "(SQL: " << _insertSQL << ")"
                << std::endl;
            return false;
        }

        sqlite3_bind_text( insert, 1, rec._layerName.c_str(), -1, 0L );
        sqlite3_bind_text( insert, 2, rec._format.c_str(), -1, 0L );
        sqlite3_bind_text( insert, 3, rec._compressor.c_str(), -1, 0L );
        sqlite3_bind_int ( insert, 4, rec._tileSize );
        sqlite3_bind_text( insert, 5, rec._profile->getSRS()->getInitString().c_str(), -1, 0L );
        sqlite3_bind_double( insert, 6, rec._profile->getExtent().xMin() );
        sqlite3_bind_double( insert, 7, rec._profile->getExtent().yMin() );
        sqlite3_bind_double( insert, 8, rec._profile->getExtent().xMax() );
        sqlite3_bind_double( insert, 9, rec._profile->getExtent().yMax() );
        unsigned int tw, th;
        rec._profile->getNumTiles( 0, tw, th );
        sqlite3_bind_int( insert, 10, tw );
        sqlite3_bind_int( insert, 11, th );

        bool success;

        rc = sqlite3_step(insert);
        if ( rc != SQLITE_DONE )
        {
            OE_WARN << LC << "SQL INSERT failed: " << sqlite3_errmsg( db )
                << "; SQL = " << _insertSQL
                << std::endl;
            success = false;
        }
        else
        {
            OE_DEBUG << LC << "Stored metadata record for \"" << rec._layerName << "\"" << std::endl;
            success = true;
        }

        sqlite3_finalize( insert );
        return success;
    }

Member Data Documentation

Definition at line 302 of file Sqlite3Cache.cpp.

Definition at line 303 of file Sqlite3Cache.cpp.


The documentation for this struct was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines