updated retrodb classes and resolved some data access issues with

ContentValue class. 
Changed parameters to some RetroDb methods
updated RetroCursor with functionality to traverse result set correctly

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-new_cache_system@4999 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2012-02-26 23:30:26 +00:00
parent ccda22fe5e
commit 64de3d3643

View File

@ -5,12 +5,17 @@
#include <string> #include <string>
#include <set> #include <set>
#include <map>
class ContentValue;
class RetroCursor;
/*! /*!
* The idea of RsDb is to provide a means for Retroshare core and \n * The idea of RsDb is to provide a means for Retroshare core and \n
* its services to maintain an easy to use random access files via a database \n * its services to maintain an easy to use random access file via a database \n
* Especially for messages, rather than all data to memory \n
* It models itself after android's sqlite functionality \n * It models itself after android's sqlite functionality \n
* This is essentially close of Androids SQLiteDatabase
*/ */
class RetroDb class RetroDb
{ {
@ -18,8 +23,9 @@ public:
/*! /*!
* @param dbPath path to data base file * @param dbPath path to data base file
* @param flags determine where to open read only or read/write
*/ */
RetroDb(const std::string dbPath); RetroDb(const std::string& dbPath, int flags);
/*! /*!
* closes db if it is not already closed * closes db if it is not already closed
@ -27,122 +33,323 @@ public:
~RetroDb(); ~RetroDb();
/*! /*!
* opens an sqlite data base * opens sqlite data base
* @return false if we failed to open
*/
bool openDb(const std::string& dbPath, int flags = OPEN_READONLY);
/*!
* close the database
*/ */
bool openDb(const std::string dbPath);
void closeDb(); void closeDb();
/* modifying db */ /* modifying db */
public: public:
void execSQL(const std::string&); /*!
* To make queries which does not return a result
* @param query SQL query
*/
void execSQL(const std::string& query);
/*! /*!
* insert a row in a database table * inserts a row in a database table
* @param table table you want to insert content values into
* @param cv hold entries to insert
* @return true if insertion successful, false otherwise * @return true if insertion successful, false otherwise
*/ */
bool sqlInsert(std::string& table, const ContentValue&); bool sqlInsert(std::string& table, std::string nullColumnHack, const ContentValue& cv);
/*! /*!
* update row in a database table * update row in a database table
* @param tableName the table on which to apply the UPDATE
* @param whereClause formatted as where statement without 'WHERE' itself
* @param cv Values used to replace current values in accessed record
* @return true if update was successful, false otherwise * @return true if update was successful, false otherwise
*/ */
bool sqlUpdate(const std::string& tableName, const std::set<std::string>& columns, const std::string& query); bool sqlUpdate(const std::string& tableName, const std::string whereClause, const ContentValue& cv);
/*! /*!
* Query the given table, returning a Cursor over the result set * Query the given table, returning a Cursor over the result set
* @param tableName * @param tableName the table name
* @param columns * @param columns columns that should be returned
* @param query * @param selection A filter declaring which rows to return, formatted as \n
* an SQL WHERE clause (excluding the WHERE itself). Passing null will \n
* return all rows for the given table.
* @param order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself)
* @return cursor over result set * @return cursor over result set
*/ */
RetroCursor* sqlQuery(const std::string& tableName, const std::set<std::string>& columns, const std::string& query); RetroCursor* sqlQuery(const std::string& tableName, const std::set<std::string>& columns,
const std::string& selection, const std::string& orderBy);
/*! /*!
* delete row in an sql table * delete row in an sql table
* * @param tableName the table on which to apply the DELETE
* @param whereClause formatted as where statement without 'WHERE' itself
* @return false
*/ */
bool sqlDelete(const std::string& tableName, const std::string& query); bool sqlDelete(const std::string& tableName, const std::string& whereClause);
/*! /*!
* defragment database, should be done on databases if many modifications have occured * defragment database, should be done on databases if many modifications have occured
*/ */
void vacuum(); void vacuum();
/*!
* TODO: remove, do not use; for testing
*/
sqlite3_stmt* getCurrStmt();
public:
static const int OPEN_READONLY;
static const int OPEN_READWRITE;
private: private:
sqlite3* mDb; sqlite3* mDb;
sqlite3_stmt* mCurrStmt; // TODO: remove
}; };
/*! /*!
* Exposes results from retrodb query * Exposes result set from retrodb query
*/ */
class RetroCursor { class RetroCursor {
public: public:
RetroCursor(); /*!
* Initialises a null cursor
*/
RetroCursor(sqlite3_stmt*);
/*! /*!
* move to first row of result, * move to first row of results
* @return false if no result * @return false if no result
*/ */
bool moveToFirst(); bool moveToFirst();
/*! /*!
* move to * move to next row of results
* @return false if no row to move next to * @return false if no row to move next to
*/ */
bool moveToNext(); bool moveToNext();
/*! /*!
* move to last row of result * move to last row of results
* @return false if no result * @return false if no result
*/ */
bool moveToLast(); bool moveToLast();
/*!
* gets current position of cursor
* @return current position of cursor
*/
uint32_t getPosition();
/* data retrieval */ /* data retrieval */
public: public:
int32_t getInt32(const std::string& );
uint32_t getUint32(const std::string& ); /*!
int64_t getInt64(const std::string& ); * Returns the value of the requested column as a String.
uint64_t getUint64(const std::string& ); * @param columnIndex the zero-based index of the target column.
bool getBool(const std::string& ); * @return the value of the column as 32 bit integer
std::string getString(const std::string& ); */
void* getData(const std::string&, uint32_t& ); int32_t getInt(int columnIndex);
/*!
* Returns the value of the requested column as a String.
* @param columnIndex the zero-based index of the target column.
* @return the value of the column as 64 bit integer
*/
int64_t getInt64(int columnIndex);
/*!
* Returns the value of the requested column as a String.
* @param columnIndex the zero-based index of the target column.
* @return the value of the column as 64 bit float
*/
double getDouble(int columnIndex);
/*!
* Returns the value of the requested column as a String.
* @param columnIndex the zero-based index of the target column.
* @return the value of the column as bool
*/
bool getBool(int columnIndex);
/*!
* Returns the value of the requested column as a String.
* @param columnIndex the zero-based index of the target column.
* @return the value of the column as a string
*/
std::string getString(int columnIndex);
/*!
* Returns the value of the requested column as a String.
* @param columnIndex the zero-based index of the target column.
* @return the value of the column as pointer to raw data
*/
void* getData(int columnIndex, uint32_t& datSize);
private:
sqlite3_stmt* mStmt;
int mCount;
}; };
/*! /*!
* @brief Convenience class for making additions to databases * @brief Convenience container for making additions to databases
* * This class provides a means of holding column values to insert into a database
*
*
*/ */
class ContentValue { class ContentValue {
public: public:
static const uint8_t INT32_TYPE;
static const uint8_t INT64_TYPE;
static const uint8_t DOUBLE_TYPE;
static const uint8_t STRING_TYPE;
static const uint8_t DATA_TYPE;
static const uint8_t BOOL_TYPE;
ContentValue(); ContentValue();
void put(const std::string&, uint32_t ); /*!
void put(const std::string &, uint64_t); * copy constructor that copys the key value set from another \n
void put(const std::string& , const std::string& ); * ContentValue object to this one
void put(const std::string& , bool); * makes a deep copy of raw data
void put(const std::string& , int64_t); * @param from ContentValue instance to copy key value set from
void put(const std::string &, int32_t); */
void put(const std::string&, uint32_t len, void* data); ContentValue(ContentValue& from);
void put(const std::string&, int64_t );
/*!
*
*
*
*/
~ContentValue();
/*!
* Adds a value to the set
* @param key the name of the value to put
* @param value the data for the value to put
*/
void put(const std::string& key, const std::string& value);
/*!
* Adds a value to the set
* @param key the name of the value to put
* @param value the data for the value to put
*/
void put(const std::string& key, bool value);
/*!
* Adds a value to the set
* @param key the name of the value to put
* @param value the data for the value to put
*/
void put(const std::string& key, int64_t value);
/*!
* Adds a value to the set
* @param key the name of the value to put
* @param value the data for the value to put
*/
void put(const std::string& key, int32_t value);
/*!
* Adds a value to the set
* @param key the name of the value to put
* @param value the data for the value to put
*/
void put(const std::string& key, double value);
/*!
* Adds a value to the set
* @param key the name of the value to put
* @param value the data for the value to put
*/
void put(const std::string& key, uint32_t len, char* value);
int32_t getAsInt32(const std::string& ); /*!
uint32_t getAsUint32(const std::string& ); * get as value as
int64_t getAsInt64(const std::string& ); * @param key the value to get
uint64_t getAsUint64(const std::string& ); */
bool getAsBool(const std::string& ); bool getAsInt32(const std::string& key, int32_t& value) const;
std::string getAsString(const std::string& );
void* getAsData(const std::string&, uint32_t& ); /*!
* get as value as 64 bit integer
* @param key the value to get
*/
bool getAsInt64(const std::string& key, int64_t& value) const;
/*!
* get as value as bool
* @param key the value to get
*/
bool getAsBool(const std::string& key, bool& value) const;
/*!
* get as value as double
* @param key the value to get
*/
bool getAsDouble(const std::string& key, double& value) const;
/*!
* get as value as string
* @param key the value to get
* @param value the data retrieved
*/
bool getAsString(const std::string& key, std::string& value) const;
/*!
* get as value as raw data
* @warning Deep copy of data reference should be made, if this instance ContentValue \n
* is destroyed pointer returned (value) is pointing to invalid memory
* @param key the value to get
*/
bool getAsData(const std::string&, uint32_t& len, char*& value) const;
/*!
* @param keySet the is set with key to type pairs contained in the ContentValue instance
*/
void getKeyTypeMap(std::map<std::string, uint8_t>& keySet) const;
/*!
* @param key the key of the key value pair to remove
* @return true if key was found and removed, false otherwise
*/
bool removeKeyValue(const std::string& key);
/*!
* clears this data structure of all its key value pairs held
*/
void clear();
private:
/*!
* release memory resource associated with mKvData
*/
void clearData();
private:
std::map<std::string, int32_t> mKvInt32;
std::map<std::string, int64_t> mKvInt64;
std::map<std::string, double> mKvDouble;
std::map<std::string, std::string> mKvString;
std::map<std::string, std::pair<uint32_t, char*> > mKvData;
std::map<std::string, bool> mKvBool;
std::map<std::string, uint8_t> mKvSet;
}; };