mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-07 14:12:43 -04:00
added RetroDb:
- tests added which pass on windows. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-new_cache_system@5036 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
e5ed6bf479
commit
030b009f9a
6 changed files with 1858 additions and 28 deletions
|
@ -471,7 +471,7 @@ HEADERS += util/folderiterator.h \
|
||||||
util/rswin.h \
|
util/rswin.h \
|
||||||
util/rsrandom.h \
|
util/rsrandom.h \
|
||||||
util/pugiconfig.h \
|
util/pugiconfig.h \
|
||||||
util/pugixml.h
|
util/retrodb.h
|
||||||
|
|
||||||
SOURCES += dbase/cachestrapper.cc \
|
SOURCES += dbase/cachestrapper.cc \
|
||||||
dbase/fimonitor.cc \
|
dbase/fimonitor.cc \
|
||||||
|
@ -607,7 +607,7 @@ SOURCES += util/folderiterator.cc \
|
||||||
util/rsversion.cc \
|
util/rsversion.cc \
|
||||||
util/rswin.cc \
|
util/rswin.cc \
|
||||||
util/rsrandom.cc \
|
util/rsrandom.cc \
|
||||||
util/pugixml.cc
|
util/retrodb.cc
|
||||||
|
|
||||||
|
|
||||||
# VOIP TEST STUFF
|
# VOIP TEST STUFF
|
||||||
|
|
374
libretroshare/src/tests/util/testcontentvalue.cpp
Normal file
374
libretroshare/src/tests/util/testcontentvalue.cpp
Normal file
|
@ -0,0 +1,374 @@
|
||||||
|
|
||||||
|
#include <memory.h>
|
||||||
|
|
||||||
|
#include "retrodb.h"
|
||||||
|
#include "utest.h"
|
||||||
|
|
||||||
|
bool operator==(const ContentValue& lCV, ContentValue& rCV){
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t> leftKtMap, rightKtMap;
|
||||||
|
lCV.getKeyTypeMap(leftKtMap);
|
||||||
|
rCV.getKeyTypeMap(rightKtMap);
|
||||||
|
|
||||||
|
return (leftKtMap == rightKtMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Test content value ability to take a set of data and ability to get it out
|
||||||
|
* For all data types
|
||||||
|
*/
|
||||||
|
void testEnterAndRetrieve();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Check to see if copy constructor intialises
|
||||||
|
* 'this' correctly
|
||||||
|
*/
|
||||||
|
void testCopyConstructor();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* check that key type map returned is consistent
|
||||||
|
* with data contained in ContentValue
|
||||||
|
*/
|
||||||
|
void testGetKeyTypeMap();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Test the clearing functionality
|
||||||
|
*/
|
||||||
|
void testClear();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* check errors are meaningful
|
||||||
|
*/
|
||||||
|
void testErrors();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* enter the same key twice and ensure previous data gets overwritten
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void testSameKey();
|
||||||
|
|
||||||
|
|
||||||
|
INITTEST();
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
testEnterAndRetrieve();
|
||||||
|
testCopyConstructor();
|
||||||
|
testClear();
|
||||||
|
testSameKey();
|
||||||
|
testGetKeyTypeMap();
|
||||||
|
testErrors();
|
||||||
|
|
||||||
|
FINALREPORT("TEST_CONTENT_VALUE");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void testSameKey(){
|
||||||
|
|
||||||
|
|
||||||
|
ContentValue cv;
|
||||||
|
|
||||||
|
// test data
|
||||||
|
|
||||||
|
std::string src = "adalkjalfalfalfkal";
|
||||||
|
const char* data = src.data();
|
||||||
|
uint32_t data_len = src.length();
|
||||||
|
std::string data_key = "private_key";
|
||||||
|
cv.put(data_key, data_len, data);
|
||||||
|
|
||||||
|
std::string other_src = "daldko5202402224";
|
||||||
|
data_len = other_src.length();
|
||||||
|
data = other_src.data();
|
||||||
|
|
||||||
|
cv.put(data_key, data_len, data);
|
||||||
|
|
||||||
|
uint32_t val_len;
|
||||||
|
char* val;
|
||||||
|
cv.getAsData(data_key, val_len, val);
|
||||||
|
std::string str_val(val, val_len);
|
||||||
|
|
||||||
|
|
||||||
|
CHECK(str_val == other_src);
|
||||||
|
|
||||||
|
// now check string
|
||||||
|
|
||||||
|
std::string key = "peer";
|
||||||
|
cv.put(key, std::string("sexy_girl"));
|
||||||
|
cv.put(key, std::string("manly man"));
|
||||||
|
|
||||||
|
std::string val_str;
|
||||||
|
cv.getAsString(key, val_str);
|
||||||
|
|
||||||
|
CHECK(val_str == "manly man");
|
||||||
|
|
||||||
|
// and double
|
||||||
|
|
||||||
|
cv.put(key, 4.);
|
||||||
|
cv.put(key, 32.);
|
||||||
|
double val_d;
|
||||||
|
cv.getAsDouble(key, val_d);
|
||||||
|
CHECK(val_d == 32.);
|
||||||
|
|
||||||
|
// and int64
|
||||||
|
int64_t large(20420492040123);
|
||||||
|
cv.put(key, large);
|
||||||
|
cv.put(key, large+34);
|
||||||
|
int64_t val_i64;
|
||||||
|
cv.getAsInt64(key, val_i64);
|
||||||
|
|
||||||
|
CHECK(val_i64 == large+34);
|
||||||
|
|
||||||
|
// and bool
|
||||||
|
|
||||||
|
cv.put(key, false);
|
||||||
|
cv.put(key, true);
|
||||||
|
bool bool_val = false;
|
||||||
|
cv.getAsBool(key, bool_val);
|
||||||
|
|
||||||
|
CHECK(bool_val == true);
|
||||||
|
|
||||||
|
// and int32
|
||||||
|
|
||||||
|
int32_t medium = 20432123;
|
||||||
|
cv.put(key, medium);
|
||||||
|
cv.put(key, medium+34);
|
||||||
|
int32_t val_i32;
|
||||||
|
cv.getAsInt32(key, val_i32);
|
||||||
|
|
||||||
|
CHECK(val_i32 == medium+34);
|
||||||
|
|
||||||
|
REPORT("testSameKey()");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void testErrors(){
|
||||||
|
|
||||||
|
|
||||||
|
ContentValue cv;
|
||||||
|
int32_t val_32;
|
||||||
|
int64_t val_64;
|
||||||
|
char* data;
|
||||||
|
uint32_t data_len;
|
||||||
|
std::string val_str;
|
||||||
|
bool val_bool;
|
||||||
|
double val_d;
|
||||||
|
CHECK(!cv.getAsInt32("dda", val_32));
|
||||||
|
CHECK(!cv.getAsInt64("dda", val_64));
|
||||||
|
CHECK(!cv.getAsData("ds", data_len, data));
|
||||||
|
CHECK(!cv.getAsString("d3536356336356356s", val_str));
|
||||||
|
CHECK(!cv.getAsBool("d424s", val_bool));
|
||||||
|
CHECK(!cv.getAsDouble("daads", val_d));
|
||||||
|
|
||||||
|
REPORT("testErrors()");
|
||||||
|
}
|
||||||
|
|
||||||
|
void testEnterAndRetrieve(){
|
||||||
|
|
||||||
|
// INT 32
|
||||||
|
int32_t value32 = 1, retval32;
|
||||||
|
std::string key = "one";
|
||||||
|
|
||||||
|
ContentValue cv;
|
||||||
|
cv.put(key, value32);
|
||||||
|
|
||||||
|
cv.getAsInt32(key, retval32);
|
||||||
|
CHECK(value32 == retval32);
|
||||||
|
|
||||||
|
// INT 64
|
||||||
|
int64_t value64 = 423425242, retval64;
|
||||||
|
key = "int64";
|
||||||
|
cv.put(key, value64);
|
||||||
|
cv.getAsInt64(key, retval64);
|
||||||
|
CHECK(value64 == retval64);
|
||||||
|
|
||||||
|
|
||||||
|
// Double
|
||||||
|
double dvalue = 3.5, retvaldbl;
|
||||||
|
key = "double";
|
||||||
|
cv.put(key, dvalue);
|
||||||
|
cv.getAsDouble(key, retvaldbl);
|
||||||
|
CHECK(dvalue == retvaldbl );
|
||||||
|
|
||||||
|
// BLOB
|
||||||
|
uint32_t data_len = 45, get_len = 0;
|
||||||
|
|
||||||
|
char* src = new char[data_len];
|
||||||
|
char* get_src = NULL;
|
||||||
|
memset(src, '$', data_len);
|
||||||
|
key = "data";
|
||||||
|
cv.put(key, data_len, src);
|
||||||
|
|
||||||
|
cv.getAsData(key, get_len, get_src);
|
||||||
|
|
||||||
|
bool fine = true;
|
||||||
|
|
||||||
|
CHECK(get_len = data_len);
|
||||||
|
|
||||||
|
if(data_len == get_len){
|
||||||
|
|
||||||
|
for(int i=0; i < data_len; i++){
|
||||||
|
|
||||||
|
if(src[i] != get_src[i])
|
||||||
|
fine &= false;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete[] src;
|
||||||
|
CHECK(fine);
|
||||||
|
|
||||||
|
// STRING
|
||||||
|
std::string strVal = "whoo", getVal("");
|
||||||
|
key = "string";
|
||||||
|
cv.put(key, strVal);
|
||||||
|
cv.getAsString(key, getVal);
|
||||||
|
|
||||||
|
CHECK(getVal == strVal);
|
||||||
|
|
||||||
|
|
||||||
|
// BOOL
|
||||||
|
bool mefalse = false, retvalBool;
|
||||||
|
key = "bool";
|
||||||
|
cv.put(key, mefalse);
|
||||||
|
cv.getAsBool(key, retvalBool);
|
||||||
|
CHECK(mefalse == retvalBool);
|
||||||
|
|
||||||
|
cv.clear();
|
||||||
|
|
||||||
|
REPORT("testEnterAndRetrieve()");
|
||||||
|
}
|
||||||
|
|
||||||
|
void testGetKeyTypeMap(){
|
||||||
|
|
||||||
|
ContentValue cv;
|
||||||
|
std::string key1="key1", key2="key2", key3="key3", key4="key4";
|
||||||
|
std::string key1_val;
|
||||||
|
int32_t key2_val = 42;
|
||||||
|
double key3_val = 23;
|
||||||
|
int64_t key4_val = 42052094224;
|
||||||
|
|
||||||
|
cv.put(key1, key1_val);
|
||||||
|
cv.put(key2, key2_val);
|
||||||
|
cv.put(key3, key3_val);
|
||||||
|
cv.put(key4, key4_val);
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t> kvMap;
|
||||||
|
|
||||||
|
cv.getKeyTypeMap(kvMap);
|
||||||
|
|
||||||
|
CHECK(kvMap.size() == 4);
|
||||||
|
CHECK(kvMap[key1] == ContentValue::STRING_TYPE);
|
||||||
|
CHECK(kvMap[key2] == ContentValue::INT32_TYPE);
|
||||||
|
CHECK(kvMap[key3] == ContentValue::DOUBLE_TYPE);
|
||||||
|
CHECK(kvMap[key4] == ContentValue::INT64_TYPE);
|
||||||
|
|
||||||
|
REPORT("testGetKeyTypeMap()");
|
||||||
|
}
|
||||||
|
|
||||||
|
void testCopyConstructor(){
|
||||||
|
|
||||||
|
ContentValue cv1;
|
||||||
|
|
||||||
|
// INT 32
|
||||||
|
int value32 = 1;
|
||||||
|
std::string key = "one";
|
||||||
|
|
||||||
|
cv1.put(key, value32);
|
||||||
|
|
||||||
|
// INT 64
|
||||||
|
int64_t value64 = 423425242;
|
||||||
|
key = "int64";
|
||||||
|
cv1.put(key, value64);
|
||||||
|
|
||||||
|
// Double
|
||||||
|
double dvalue = 3.5;
|
||||||
|
key = "double";
|
||||||
|
cv1.put(key, dvalue);
|
||||||
|
|
||||||
|
// BLOB
|
||||||
|
uint32_t data_len = 45;
|
||||||
|
char* src = new char[data_len];
|
||||||
|
|
||||||
|
memset(src, '$', data_len);
|
||||||
|
key = "data";
|
||||||
|
cv1.put(key, data_len, src);
|
||||||
|
|
||||||
|
|
||||||
|
delete[] src;
|
||||||
|
|
||||||
|
// STRING
|
||||||
|
std::string strVal = "whoo";
|
||||||
|
key = "string";
|
||||||
|
cv1.put(key, strVal);
|
||||||
|
|
||||||
|
// BOOL
|
||||||
|
bool mefalse = false;
|
||||||
|
key = "bool";
|
||||||
|
cv1.put(key, mefalse);
|
||||||
|
|
||||||
|
ContentValue cv2(cv1);
|
||||||
|
|
||||||
|
CHECK(cv1 == cv2);
|
||||||
|
|
||||||
|
cv1.clear();
|
||||||
|
cv2.clear();
|
||||||
|
|
||||||
|
REPORT("testCopyConstructor()");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void testClear(){
|
||||||
|
|
||||||
|
ContentValue cv1;
|
||||||
|
|
||||||
|
// INT 32
|
||||||
|
int value32 = 1;
|
||||||
|
std::string key = "one";
|
||||||
|
|
||||||
|
cv1.put(key, value32);
|
||||||
|
|
||||||
|
// INT 64
|
||||||
|
int64_t value64 = 423425242;
|
||||||
|
key = "int64";
|
||||||
|
cv1.put(key, value64);
|
||||||
|
|
||||||
|
// Double
|
||||||
|
double dvalue = 3.5;
|
||||||
|
key = "double";
|
||||||
|
cv1.put(key, dvalue);
|
||||||
|
|
||||||
|
// BLOB
|
||||||
|
uint32_t data_len = 45;
|
||||||
|
char* src = new char[data_len];
|
||||||
|
|
||||||
|
memset(src, '$', data_len);
|
||||||
|
key = "data";
|
||||||
|
cv1.put(key, data_len, src);
|
||||||
|
|
||||||
|
|
||||||
|
delete[] src;
|
||||||
|
|
||||||
|
// STRING
|
||||||
|
std::string strVal = "whoo";
|
||||||
|
key = "string";
|
||||||
|
cv1.put(key, strVal);
|
||||||
|
|
||||||
|
// BOOL
|
||||||
|
bool mefalse = false;
|
||||||
|
key = "bool";
|
||||||
|
cv1.put(key, mefalse);
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t> ktMap;
|
||||||
|
|
||||||
|
cv1.getKeyTypeMap(ktMap);
|
||||||
|
CHECK(ktMap.size() > 0);
|
||||||
|
|
||||||
|
cv1.clear();
|
||||||
|
|
||||||
|
cv1.getKeyTypeMap(ktMap);
|
||||||
|
CHECK(ktMap.size() == 0);
|
||||||
|
|
||||||
|
REPORT("testClear()");
|
||||||
|
|
||||||
|
}
|
127
libretroshare/src/tests/util/testretrocursor.cpp
Normal file
127
libretroshare/src/tests/util/testretrocursor.cpp
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "retrodb.h"
|
||||||
|
#include "utest.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define INT32_KEY "day"
|
||||||
|
#define INT64_KEY "pub_key"
|
||||||
|
#define DATA_KEY "data"
|
||||||
|
#define DOUBLE_KEY "a_double"
|
||||||
|
#define STRING_KEY "a_string"
|
||||||
|
#define BOOL_KEY "a_bool"
|
||||||
|
|
||||||
|
#define DB_FILE_NAME "RetroDb"
|
||||||
|
|
||||||
|
static RetroDb* mDb = NULL;
|
||||||
|
|
||||||
|
void createRetroDb();
|
||||||
|
void destroyRetroDb();
|
||||||
|
|
||||||
|
INITTEST();
|
||||||
|
// mainly test ability to move up and down result set
|
||||||
|
|
||||||
|
|
||||||
|
void testTraverseResult();
|
||||||
|
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
testTraverseResult();
|
||||||
|
|
||||||
|
FINALREPORT("RETRO CURSOR TEST");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void createRetroDb(){
|
||||||
|
|
||||||
|
if(mDb)
|
||||||
|
destroyRetroDb();
|
||||||
|
|
||||||
|
|
||||||
|
remove(DB_FILE_NAME); // account for interrupted tests
|
||||||
|
mDb = new RetroDb(DB_FILE_NAME, RetroDb::OPEN_READWRITE_CREATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroyRetroDb(){
|
||||||
|
if(mDb == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mDb->closeDb();
|
||||||
|
delete mDb;
|
||||||
|
mDb = NULL;
|
||||||
|
int rc = remove(DB_FILE_NAME);
|
||||||
|
std::cerr << "remove code: " << rc << std::endl;
|
||||||
|
|
||||||
|
if(rc !=0){
|
||||||
|
perror("Could not delete db: ");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void testTraverseResult(){
|
||||||
|
|
||||||
|
|
||||||
|
createRetroDb();
|
||||||
|
|
||||||
|
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, pub_key INT, data BLOB, a_double REAL, a_string VARCHAR(255), a_bool INT);");
|
||||||
|
CHECK(statementExecuted);
|
||||||
|
|
||||||
|
ContentValue cv;
|
||||||
|
|
||||||
|
cv.put(INT32_KEY, (int32_t)20);
|
||||||
|
int64_t large_num = 32432242344423;
|
||||||
|
cv.put(INT64_KEY, large_num);
|
||||||
|
std::string str = "3dajaojaljfacjlaf£%£^%\"%\"%$";
|
||||||
|
const char* data = str.data();
|
||||||
|
int size = str.size();
|
||||||
|
cv.put(DATA_KEY, size, data);
|
||||||
|
cv.put(DOUBLE_KEY, 3.14);
|
||||||
|
cv.put(STRING_KEY, "hello precious");
|
||||||
|
cv.put(BOOL_KEY, false);
|
||||||
|
|
||||||
|
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
cv.put(INT32_KEY, (int32_t)21);
|
||||||
|
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
cv.put(INT32_KEY, (int32_t)2);
|
||||||
|
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
cv.put(INT32_KEY, (int32_t)204);
|
||||||
|
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
cv.put(INT32_KEY, (int32_t)22);
|
||||||
|
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
|
||||||
|
CHECK(insertExecuted);
|
||||||
|
|
||||||
|
std::list<std::string> columns;
|
||||||
|
columns.push_back(INT32_KEY); columns.push_back(INT64_KEY); columns.push_back(DOUBLE_KEY);
|
||||||
|
columns.push_back(DATA_KEY); columns.push_back(BOOL_KEY); columns.push_back(STRING_KEY);
|
||||||
|
std::string orderBy = std::string(INT32_KEY) + " ASC";
|
||||||
|
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns, "", orderBy);
|
||||||
|
|
||||||
|
CHECK(cursor->getResultCount() == 5);
|
||||||
|
cursor->moveToFirst();
|
||||||
|
CHECK(cursor->getPosition() == 0);
|
||||||
|
|
||||||
|
cursor->moveToLast();
|
||||||
|
CHECK(cursor->getPosition() == cursor->getResultCount());
|
||||||
|
|
||||||
|
cursor->moveToFirst();
|
||||||
|
|
||||||
|
CHECK(cursor->getInt32(0) == 2);
|
||||||
|
cursor->moveToNext();
|
||||||
|
cursor->moveToNext();
|
||||||
|
|
||||||
|
CHECK(cursor->getInt32(0) == 21);
|
||||||
|
|
||||||
|
delete cursor;
|
||||||
|
|
||||||
|
REPORT("testTraverseResult()");
|
||||||
|
destroyRetroDb();
|
||||||
|
}
|
352
libretroshare/src/tests/util/testretrodb.cpp
Normal file
352
libretroshare/src/tests/util/testretrodb.cpp
Normal file
|
@ -0,0 +1,352 @@
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory.h>
|
||||||
|
#include <sstream>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "retrodb.h"
|
||||||
|
#include "utest.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define DAY_KEY "day"
|
||||||
|
#define PUB_KEY "pub_key"
|
||||||
|
#define DATA_KEY "data"
|
||||||
|
#define DOUBLE_KEY "a_double"
|
||||||
|
|
||||||
|
#define DB_FILE_NAME "RetroDb"
|
||||||
|
|
||||||
|
static RetroDb* mDb = NULL;
|
||||||
|
|
||||||
|
void createRetroDb();
|
||||||
|
void destroyRetroDb();
|
||||||
|
|
||||||
|
void testSqlExec();
|
||||||
|
void testSqlUpdate();
|
||||||
|
void testSqlInsert();
|
||||||
|
void testSqlDelete();
|
||||||
|
void testSqlQuery();
|
||||||
|
void testBinaryInsertion();
|
||||||
|
|
||||||
|
INITTEST();
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
testSqlExec();
|
||||||
|
testBinaryInsertion();
|
||||||
|
testSqlInsert();
|
||||||
|
testSqlUpdate();
|
||||||
|
testSqlDelete();
|
||||||
|
testSqlQuery();
|
||||||
|
|
||||||
|
FINALREPORT("RETRO DB TEST");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void createRetroDb(){
|
||||||
|
|
||||||
|
if(mDb)
|
||||||
|
destroyRetroDb();
|
||||||
|
|
||||||
|
|
||||||
|
remove(DB_FILE_NAME); // account for interrupted tests
|
||||||
|
mDb = new RetroDb(DB_FILE_NAME, RetroDb::OPEN_READWRITE_CREATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroyRetroDb(){
|
||||||
|
if(mDb == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mDb->closeDb();
|
||||||
|
delete mDb;
|
||||||
|
mDb = NULL;
|
||||||
|
int rc = remove(DB_FILE_NAME);
|
||||||
|
std::cerr << "remove code: " << rc << std::endl;
|
||||||
|
|
||||||
|
if(rc !=0){
|
||||||
|
perror("Could not delete db: ");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSqlExec(){
|
||||||
|
|
||||||
|
createRetroDb();
|
||||||
|
|
||||||
|
// create simple table
|
||||||
|
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, pub_key, data);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(day, pub_key, data) VALUES(1,2,3);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(day, pub_key, data) VALUES(3,4525624,4524);");
|
||||||
|
|
||||||
|
CHECK(statementExecuted);
|
||||||
|
|
||||||
|
// now check if you can retrieve records
|
||||||
|
std::list<std::string> columns;
|
||||||
|
columns.push_back("day");
|
||||||
|
columns.push_back("pub_key");
|
||||||
|
columns.push_back("data");
|
||||||
|
|
||||||
|
std::string selection, orderBy;
|
||||||
|
RetroCursor* c = mDb->sqlQuery("retroDB", columns, selection, orderBy);
|
||||||
|
|
||||||
|
|
||||||
|
CHECK(c->getResultCount() == 2);
|
||||||
|
|
||||||
|
// got to first record
|
||||||
|
c->moveToFirst();
|
||||||
|
int32_t first =c->getInt32(0), second = c->getInt32(1),
|
||||||
|
third = c->getInt32(2);
|
||||||
|
|
||||||
|
|
||||||
|
CHECK(first == 1);
|
||||||
|
CHECK(second == 2);
|
||||||
|
CHECK(third == 3);
|
||||||
|
|
||||||
|
// get 2nd record
|
||||||
|
c->moveToNext();
|
||||||
|
|
||||||
|
first =c->getInt32(0), second = c->getInt32(1),
|
||||||
|
third = c->getInt32(2);
|
||||||
|
|
||||||
|
CHECK(first == 3);
|
||||||
|
CHECK(second == 4525624);
|
||||||
|
CHECK(third == 4524);
|
||||||
|
|
||||||
|
delete c;
|
||||||
|
|
||||||
|
REPORT("testSqlExec()");
|
||||||
|
destroyRetroDb();
|
||||||
|
}
|
||||||
|
|
||||||
|
void testBinaryInsertion(){
|
||||||
|
|
||||||
|
createRetroDb();
|
||||||
|
// create simple table
|
||||||
|
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, data BLOB);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(day, data) VALUES(1, 'dafadfad%$£%^£%%\"$R\"$\"$\"');");
|
||||||
|
|
||||||
|
// now check if you can retrieve records
|
||||||
|
std::list<std::string> columns;
|
||||||
|
columns.push_back("day");
|
||||||
|
columns.push_back("data");
|
||||||
|
|
||||||
|
std::string selection, orderBy;
|
||||||
|
RetroCursor* c = mDb->sqlQuery("retroDB", columns, selection, orderBy);
|
||||||
|
|
||||||
|
c->moveToFirst();
|
||||||
|
int first = c->getInt32(0);
|
||||||
|
uint32_t size;
|
||||||
|
const char* data = (const char*)c->getData(1, size);
|
||||||
|
std::string str = "dafadfad%$£%^£%%\"$R\"$\"$\"";
|
||||||
|
const char* data_comp = str.data();
|
||||||
|
bool binCompare = ok;
|
||||||
|
|
||||||
|
for(int i=0; i < 24 ; i++)
|
||||||
|
binCompare &= data[i] == data_comp[i];
|
||||||
|
|
||||||
|
CHECK(first == 1);
|
||||||
|
CHECK(binCompare);
|
||||||
|
|
||||||
|
delete c;
|
||||||
|
REPORT("testBinaryInsertion()");
|
||||||
|
destroyRetroDb();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void testSqlUpdate(){
|
||||||
|
|
||||||
|
createRetroDb();
|
||||||
|
|
||||||
|
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, data BLOB, peerId VARCHAR(255), time INT);");
|
||||||
|
CHECK(statementExecuted);
|
||||||
|
|
||||||
|
std::string data = "dadJOOodaodaoro20r2-0r20002ri02fgi3t0***";
|
||||||
|
ContentValue cv;
|
||||||
|
|
||||||
|
cv.put("day", (int32_t)20);
|
||||||
|
std::string peerid = "TheRetroSquad";
|
||||||
|
cv.put("peerId", peerid);
|
||||||
|
cv.put("data", data.size(), data.data());
|
||||||
|
int64_t now = time(NULL);
|
||||||
|
cv.put("time", now);
|
||||||
|
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
|
||||||
|
CHECK(insertExecuted);
|
||||||
|
|
||||||
|
// now check entry is fine
|
||||||
|
|
||||||
|
std::list<std::string> columns;
|
||||||
|
columns.push_back("day"); columns.push_back("peerId"); columns.push_back("data"); columns.push_back("time");
|
||||||
|
|
||||||
|
RetroCursor* c = mDb->sqlQuery("retroDB", columns, "", "");
|
||||||
|
|
||||||
|
CHECK(c->getResultCount() == 1);
|
||||||
|
std::string result;
|
||||||
|
c->moveToFirst();
|
||||||
|
c->getString(1, result);
|
||||||
|
|
||||||
|
CHECK(result == peerid);
|
||||||
|
|
||||||
|
delete c;
|
||||||
|
c = NULL;
|
||||||
|
|
||||||
|
// now make an update and see if this is reflected
|
||||||
|
|
||||||
|
int64_t now_plus = now+203;
|
||||||
|
cv.put("time", now_plus);
|
||||||
|
bool update = mDb->sqlUpdate("retroDB", "", cv);
|
||||||
|
CHECK(update);
|
||||||
|
c = mDb->sqlQuery("retroDB", columns, "", "");
|
||||||
|
c->moveToFirst();
|
||||||
|
int64_t now_plus_compare = c->getDouble(3);
|
||||||
|
|
||||||
|
CHECK(now_plus_compare == now_plus);
|
||||||
|
|
||||||
|
// now attempt an update which should find no valid record
|
||||||
|
|
||||||
|
|
||||||
|
delete c;
|
||||||
|
|
||||||
|
REPORT("testSqlUpdate()");
|
||||||
|
destroyRetroDb();
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSqlInsert(){
|
||||||
|
|
||||||
|
createRetroDb();
|
||||||
|
|
||||||
|
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, a_double DOUBLE, data BLOB);");
|
||||||
|
CHECK(statementExecuted);
|
||||||
|
|
||||||
|
ContentValue cv;
|
||||||
|
|
||||||
|
cv.put("day", (int32_t)20);
|
||||||
|
std::string str = "3dajaojaljfacjlaf£%£^%\"%\"%$";
|
||||||
|
const char* data = str.data();
|
||||||
|
int size = 27;
|
||||||
|
cv.put(DATA_KEY, size, data);
|
||||||
|
cv.put(DOUBLE_KEY, 3.14);
|
||||||
|
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
|
||||||
|
std::list<std::string> columns;
|
||||||
|
columns.push_back("day"); columns.push_back("a_double"); columns.push_back("data");
|
||||||
|
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns, "", "");
|
||||||
|
|
||||||
|
CHECK(cursor != NULL);
|
||||||
|
|
||||||
|
cursor->moveToFirst();
|
||||||
|
|
||||||
|
CHECK(20 == cursor->getInt32(0));
|
||||||
|
CHECK(3.14 == cursor->getDouble(1));
|
||||||
|
|
||||||
|
CHECK(insertExecuted);
|
||||||
|
|
||||||
|
delete cursor;
|
||||||
|
FINALREPORT("testSqlInsert()");
|
||||||
|
destroyRetroDb();
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSqlQuery(){
|
||||||
|
// test ordering of data and selection clause
|
||||||
|
|
||||||
|
|
||||||
|
createRetroDb();
|
||||||
|
|
||||||
|
// create simple table
|
||||||
|
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(priority INTEGER PRIMARY KEY ASC, name VARCHAR(255), friends, games INTEGER);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(4,'sammy',2,30);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(2,'davy',6,9);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(6,'pammy',2,4);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(5,'tommy',3,4534);");
|
||||||
|
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(9,'jonny',3,44);");
|
||||||
|
|
||||||
|
CHECK(statementExecuted);
|
||||||
|
|
||||||
|
std::list<std::string> columns;
|
||||||
|
columns.push_back("name");
|
||||||
|
std::string selection = "games <= 31";
|
||||||
|
std::string orderBy = "priority DESC";
|
||||||
|
|
||||||
|
|
||||||
|
// output should be by name: pammy, sammy and davy
|
||||||
|
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns,selection, orderBy);
|
||||||
|
|
||||||
|
cursor->moveToFirst();
|
||||||
|
std::string name;
|
||||||
|
cursor->getString(0,name);
|
||||||
|
CHECK(name == "pammy");
|
||||||
|
|
||||||
|
cursor->moveToNext();
|
||||||
|
cursor->getString(0,name);
|
||||||
|
CHECK(name == "sammy");
|
||||||
|
|
||||||
|
cursor->moveToNext();
|
||||||
|
cursor->getString(0,name);
|
||||||
|
CHECK(name == "davy");
|
||||||
|
|
||||||
|
|
||||||
|
delete cursor;
|
||||||
|
FINALREPORT("testSqlQuery()");
|
||||||
|
destroyRetroDb();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void testSqlDelete(){
|
||||||
|
|
||||||
|
createRetroDb();
|
||||||
|
|
||||||
|
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, a_double DOUBLE, data BLOB);");
|
||||||
|
CHECK(statementExecuted);
|
||||||
|
|
||||||
|
ContentValue cv;
|
||||||
|
|
||||||
|
cv.put("day", (int32_t)20);
|
||||||
|
std::string str = "3dajaojaljfacjlaf£%£^%\"%\"%$";
|
||||||
|
const char* data = str.data();
|
||||||
|
uint32_t size = str.size();
|
||||||
|
cv.put(DATA_KEY, size, data);
|
||||||
|
cv.put(DOUBLE_KEY, 3.14);
|
||||||
|
|
||||||
|
// insert to records
|
||||||
|
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
cv.put("day", (int32_t(5)));
|
||||||
|
CHECK(insertExecuted);
|
||||||
|
mDb->sqlInsert("retroDB", "", cv);
|
||||||
|
|
||||||
|
|
||||||
|
// now check that their are two records in the db
|
||||||
|
std::list<std::string> columns;
|
||||||
|
columns.push_back("day"); columns.push_back("a_double"); columns.push_back("data");
|
||||||
|
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns, "", "");
|
||||||
|
CHECK(cursor->getResultCount() == 2);
|
||||||
|
delete cursor;
|
||||||
|
// now remove a record and search for the removed record, query should return no records
|
||||||
|
mDb->sqlDelete("retroDB", "day=5", "");
|
||||||
|
|
||||||
|
|
||||||
|
cursor = mDb->sqlQuery("retroDB", columns, "day=5", "");
|
||||||
|
CHECK(cursor->getResultCount() == 0);
|
||||||
|
delete cursor;
|
||||||
|
|
||||||
|
// now check for the remaining record
|
||||||
|
cursor = mDb->sqlQuery("retroDB", columns, "day=20", "");
|
||||||
|
CHECK(cursor->getResultCount() == 1);
|
||||||
|
cursor->moveToFirst();
|
||||||
|
|
||||||
|
// verify there is no data corruption
|
||||||
|
const char* data_comp1 = (const char*)cursor->getData(2, size);
|
||||||
|
const char* data_comp2 = str.data();
|
||||||
|
|
||||||
|
bool binCompare = true;
|
||||||
|
|
||||||
|
for(int i=0; i < str.size() ; i++)
|
||||||
|
binCompare &= data_comp1[i] == data_comp2[i];
|
||||||
|
|
||||||
|
CHECK(binCompare);
|
||||||
|
|
||||||
|
delete cursor;
|
||||||
|
|
||||||
|
FINALREPORT("testSqlDelete()");
|
||||||
|
destroyRetroDb();
|
||||||
|
}
|
898
libretroshare/src/util/retrodb.cpp
Normal file
898
libretroshare/src/util/retrodb.cpp
Normal file
|
@ -0,0 +1,898 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* RetroShare : RetroDb functionality
|
||||||
|
*
|
||||||
|
* Copyright 2012 Christopher Evi-Parker
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Library General Public
|
||||||
|
* License Version 2 as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Library General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Library General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||||
|
* USA.
|
||||||
|
*
|
||||||
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <memory.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "retrodb.h"
|
||||||
|
|
||||||
|
#define RETRODB_DEBUG
|
||||||
|
|
||||||
|
const uint8_t ContentValue::BOOL_TYPE = 1;
|
||||||
|
const uint8_t ContentValue::DATA_TYPE = 2;
|
||||||
|
const uint8_t ContentValue::STRING_TYPE = 3;
|
||||||
|
const uint8_t ContentValue::DOUBLE_TYPE = 4;
|
||||||
|
const uint8_t ContentValue::INT32_TYPE = 5;
|
||||||
|
const uint8_t ContentValue::INT64_TYPE = 6;
|
||||||
|
|
||||||
|
const int RetroDb::OPEN_READONLY = SQLITE_OPEN_READONLY;
|
||||||
|
const int RetroDb::OPEN_READWRITE = SQLITE_OPEN_READWRITE;
|
||||||
|
const int RetroDb::OPEN_READWRITE_CREATE = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
||||||
|
|
||||||
|
RetroDb::RetroDb(const std::string &dbPath, int flags) : mDb(NULL), mCurrStmt(NULL) {
|
||||||
|
|
||||||
|
int rc = sqlite3_open_v2(dbPath.c_str(), &mDb, flags, NULL);
|
||||||
|
|
||||||
|
if(rc){
|
||||||
|
std::cerr << "Can't open database, Error code: " << sqlite3_errmsg(mDb)
|
||||||
|
<< std::endl;
|
||||||
|
sqlite3_close(mDb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
RetroDb::~RetroDb(){
|
||||||
|
|
||||||
|
if(mDb)
|
||||||
|
sqlite3_close(mDb);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RetroDb::closeDb(){
|
||||||
|
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
if(mDb)
|
||||||
|
rc = sqlite3_close(mDb);
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "RetroDb::closeDb(): Error code on close: " << rc << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TIME_LIMIT 3
|
||||||
|
|
||||||
|
bool RetroDb::execSQL(const std::string &query){
|
||||||
|
|
||||||
|
// prepare statement
|
||||||
|
sqlite3_stmt* stm = NULL;
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "Query: " << query << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int rc = sqlite3_prepare_v2(mDb, query.c_str(), query.length(), &stm, NULL);
|
||||||
|
|
||||||
|
// check if there are any errors
|
||||||
|
if(rc != SQLITE_OK){
|
||||||
|
std::cerr << "RetroDb::execSQL(): Error preparing statement\n";
|
||||||
|
std::cerr << "Error code: " << sqlite3_errmsg(mDb)
|
||||||
|
<< std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t delta = 3;
|
||||||
|
time_t stamp = time(NULL), now = 0;
|
||||||
|
bool timeOut = false, ok = false;
|
||||||
|
|
||||||
|
while(!timeOut){
|
||||||
|
|
||||||
|
rc = sqlite3_step(stm);
|
||||||
|
|
||||||
|
if(rc == SQLITE_DONE){
|
||||||
|
ok = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(rc != SQLITE_BUSY){
|
||||||
|
ok = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
now = time(NULL);
|
||||||
|
delta = stamp - now;
|
||||||
|
|
||||||
|
if(delta > TIME_LIMIT){
|
||||||
|
ok = false;
|
||||||
|
timeOut = true;
|
||||||
|
}
|
||||||
|
// TODO add sleep so not to waste
|
||||||
|
// precious cycles
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!ok){
|
||||||
|
|
||||||
|
if(rc == SQLITE_BUSY){
|
||||||
|
std::cerr << "RetroDb::execSQL()\n" ;
|
||||||
|
std::cerr << "SQL timed out!" << std::endl;
|
||||||
|
}else{
|
||||||
|
std::cerr << "RetroDb::execSQL(): Error executing statement (code: " << rc << ")\n";
|
||||||
|
std::cerr << "Sqlite Error msg: " << sqlite3_errmsg(mDb)
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// finalise statement or else db cannot be closed
|
||||||
|
sqlite3_finalize(stm);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
RetroCursor* RetroDb::sqlQuery(const std::string& tableName, const std::list<std::string>& columns,
|
||||||
|
const std::string& selection, const std::string& orderBy){
|
||||||
|
|
||||||
|
if(tableName.empty() || columns.empty()){
|
||||||
|
std::cerr << "RetroDb::sqlQuery(): No table or columns given" << std::endl;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string columnSelection; // the column names to return
|
||||||
|
sqlite3_stmt* stmt = NULL;
|
||||||
|
std::list<std::string>::const_iterator it = columns.begin();
|
||||||
|
|
||||||
|
for(; it != columns.end(); it++){
|
||||||
|
columnSelection += *it;
|
||||||
|
|
||||||
|
it++;
|
||||||
|
if(it != columns.end())
|
||||||
|
columnSelection += ",";
|
||||||
|
it--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// construct query
|
||||||
|
// SELECT columnSelection FROM tableName WHERE selection
|
||||||
|
std::string sqlQuery = "SELECT " + columnSelection + " FROM " +
|
||||||
|
tableName;
|
||||||
|
|
||||||
|
// add selection clause if present
|
||||||
|
if(!selection.empty())
|
||||||
|
sqlQuery += " WHERE " + selection;
|
||||||
|
|
||||||
|
|
||||||
|
// add 'order by' clause if present
|
||||||
|
if(!orderBy.empty())
|
||||||
|
sqlQuery += " ORDER BY " + orderBy + ";";
|
||||||
|
else
|
||||||
|
sqlQuery += ";";
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "RetroDb::sqlQuery(): " << sqlQuery << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(mDb, sqlQuery.c_str(), sqlQuery.length(), &stmt, NULL);
|
||||||
|
return (new RetroCursor(stmt));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroDb::isOpen() const {
|
||||||
|
return (mDb==NULL ? false : true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroDb::sqlInsert(const std::string &table, const std::string& nullColumnHack, const ContentValue &cv){
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t> keyTypeMap;
|
||||||
|
cv.getKeyTypeMap(keyTypeMap);
|
||||||
|
std::map<std::string, uint8_t>::iterator mit = keyTypeMap.begin();
|
||||||
|
|
||||||
|
// build columns part of insertion
|
||||||
|
std::string qColumns = table + "(";
|
||||||
|
|
||||||
|
for(; mit != keyTypeMap.end(); mit++){
|
||||||
|
|
||||||
|
qColumns += mit->first;
|
||||||
|
|
||||||
|
mit++;
|
||||||
|
|
||||||
|
// add comma if more columns left
|
||||||
|
if(mit == keyTypeMap.end())
|
||||||
|
qColumns += ")"; // close bracket if at end
|
||||||
|
else
|
||||||
|
qColumns += ",";
|
||||||
|
|
||||||
|
mit--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// build values part of insertion
|
||||||
|
std::string qValues = "VALUES(";
|
||||||
|
std::ostringstream oStrStream;
|
||||||
|
|
||||||
|
for(mit=keyTypeMap.begin(); mit!=keyTypeMap.end(); mit++){
|
||||||
|
|
||||||
|
uint8_t type = mit->second;
|
||||||
|
std::string key = mit->first;
|
||||||
|
|
||||||
|
switch(type){
|
||||||
|
|
||||||
|
case ContentValue::BOOL_TYPE:
|
||||||
|
{
|
||||||
|
bool value;
|
||||||
|
cv.getAsBool(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += oStrStream.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::DOUBLE_TYPE:
|
||||||
|
{
|
||||||
|
double value;
|
||||||
|
cv.getAsDouble(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += oStrStream.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::DATA_TYPE:
|
||||||
|
{
|
||||||
|
char* value;
|
||||||
|
uint32_t len;
|
||||||
|
cv.getAsData(key, len, value);
|
||||||
|
oStrStream.write(value, len);
|
||||||
|
qValues += "'" + oStrStream.str() + "'";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::STRING_TYPE:
|
||||||
|
{
|
||||||
|
std::string value;
|
||||||
|
cv.getAsString(key, value);
|
||||||
|
qValues += "'" + value +"'";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::INT32_TYPE:
|
||||||
|
{
|
||||||
|
int32_t value;
|
||||||
|
cv.getAsInt32(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += oStrStream.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::INT64_TYPE:
|
||||||
|
{
|
||||||
|
int64_t value;
|
||||||
|
cv.getAsInt64(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += oStrStream.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mit++;
|
||||||
|
if(mit != keyTypeMap.end()){ // add comma if more columns left
|
||||||
|
qValues += ",";
|
||||||
|
}
|
||||||
|
else{ // at end close brackets
|
||||||
|
qValues += ");";
|
||||||
|
}
|
||||||
|
mit--;
|
||||||
|
|
||||||
|
// clear stream strings
|
||||||
|
oStrStream.str("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// complete insertion query
|
||||||
|
std::string sqlQuery = "INSERT INTO " + qColumns + " " + qValues;
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "RetroDb::sqlInsert(): " << sqlQuery << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// execute query
|
||||||
|
execSQL(sqlQuery);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool RetroDb::sqlDelete(const std::string &tableName, const std::string &whereClause, const std::string &whereArgs){
|
||||||
|
|
||||||
|
std::string sqlQuery = "DELETE FROM " + tableName;
|
||||||
|
|
||||||
|
if(!whereClause.empty()){
|
||||||
|
sqlQuery += " WHERE " + whereClause + ";";
|
||||||
|
}else
|
||||||
|
sqlQuery += ";";
|
||||||
|
|
||||||
|
return execSQL(sqlQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool RetroDb::sqlUpdate(const std::string &tableName, std::string whereClause, const ContentValue& cv){
|
||||||
|
|
||||||
|
std::string sqlQuery = "UPDATE " + tableName + " SET ";
|
||||||
|
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t> keyTypeMap;
|
||||||
|
std::map<std::string, uint8_t>::iterator mit;
|
||||||
|
cv.getKeyTypeMap(keyTypeMap);
|
||||||
|
|
||||||
|
// build SET part of update
|
||||||
|
std::string qValues = "";
|
||||||
|
std::ostringstream oStrStream;
|
||||||
|
|
||||||
|
for(mit=keyTypeMap.begin(); mit!=keyTypeMap.end(); mit++){
|
||||||
|
|
||||||
|
uint8_t type = mit->second;
|
||||||
|
std::string key = mit->first;
|
||||||
|
|
||||||
|
switch(type){
|
||||||
|
|
||||||
|
case ContentValue::BOOL_TYPE:
|
||||||
|
{
|
||||||
|
bool value;
|
||||||
|
cv.getAsBool(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += key + "='" + oStrStream.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::DOUBLE_TYPE:
|
||||||
|
{
|
||||||
|
double value;
|
||||||
|
cv.getAsDouble(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += key + "='" + oStrStream.str();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::DATA_TYPE:
|
||||||
|
{
|
||||||
|
char* value;
|
||||||
|
uint32_t len;
|
||||||
|
cv.getAsData(key, len, value);
|
||||||
|
oStrStream.write(value, len);
|
||||||
|
qValues += key + "='" + oStrStream.str() + "' ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::STRING_TYPE:
|
||||||
|
{
|
||||||
|
std::string value;
|
||||||
|
cv.getAsString(key, value);
|
||||||
|
qValues += key + "='" + value + "' ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::INT32_TYPE:
|
||||||
|
{
|
||||||
|
int32_t value;
|
||||||
|
cv.getAsInt32(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += key + "='" + oStrStream.str() + "' ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ContentValue::INT64_TYPE:
|
||||||
|
{
|
||||||
|
int64_t value;
|
||||||
|
cv.getAsInt64(key, value);
|
||||||
|
oStrStream << value;
|
||||||
|
qValues += key + "='" + oStrStream.str() + "' ";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mit++;
|
||||||
|
if(mit != keyTypeMap.end()){ // add comma if more columns left
|
||||||
|
qValues += ",";
|
||||||
|
}
|
||||||
|
mit--;
|
||||||
|
|
||||||
|
// clear stream strings
|
||||||
|
oStrStream.str("");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(qValues.empty())
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
sqlQuery += qValues;
|
||||||
|
|
||||||
|
// complete update
|
||||||
|
if(!whereClause.empty()){
|
||||||
|
sqlQuery += " WHERE " + whereClause + ";";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
sqlQuery += ";";
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute query
|
||||||
|
return execSQL(sqlQuery);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************** RetroCursor ************************/
|
||||||
|
|
||||||
|
RetroCursor::RetroCursor(sqlite3_stmt *stmt)
|
||||||
|
: mStmt(NULL), mCount(0), mPosCounter(0) {
|
||||||
|
|
||||||
|
open(stmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
RetroCursor::~RetroCursor(){
|
||||||
|
|
||||||
|
// finalise statement
|
||||||
|
if(mStmt){
|
||||||
|
sqlite3_finalize(mStmt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroCursor::moveToFirst(){
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "RetroCursor::moveToFirst()\n";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(!isOpen())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// reset statement
|
||||||
|
int rc = sqlite3_reset(mStmt);
|
||||||
|
|
||||||
|
if(rc != SQLITE_OK){
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "Error code: " << rc << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = sqlite3_step(mStmt);
|
||||||
|
|
||||||
|
if(rc == SQLITE_ROW){
|
||||||
|
mPosCounter = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "Error code: " << rc << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroCursor::moveToLast(){
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "RetroCursor::moveToLast()\n";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(!isOpen())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// go to begining
|
||||||
|
int rc = sqlite3_reset(mStmt);
|
||||||
|
|
||||||
|
if(rc != SQLITE_OK)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
rc = sqlite3_step(mStmt);
|
||||||
|
|
||||||
|
while(rc == SQLITE_ROW){
|
||||||
|
rc = sqlite3_step(mStmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(rc != SQLITE_DONE){
|
||||||
|
std::cerr << "Error executing statement (code: " << rc << ")\n"
|
||||||
|
<< std::endl;
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
mPosCounter = mCount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int RetroCursor::getResultCount() const {
|
||||||
|
|
||||||
|
if(isOpen())
|
||||||
|
return mCount;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroCursor::isOpen() const {
|
||||||
|
return !(mStmt == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroCursor::close(){
|
||||||
|
|
||||||
|
if(!isOpen())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
|
||||||
|
int rc = sqlite3_finalize(mStmt);
|
||||||
|
mStmt = NULL;
|
||||||
|
mPosCounter = 0;
|
||||||
|
mCount = 0;
|
||||||
|
|
||||||
|
return (rc == SQLITE_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroCursor::open(sqlite3_stmt *stm){
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "RetroCursor::open() \n";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(isOpen())
|
||||||
|
close();
|
||||||
|
|
||||||
|
mStmt = stm;
|
||||||
|
|
||||||
|
// ensure statement is valid
|
||||||
|
int rc = sqlite3_reset(mStmt);
|
||||||
|
|
||||||
|
if(rc == SQLITE_OK){
|
||||||
|
|
||||||
|
while((rc = sqlite3_step(mStmt)) == SQLITE_ROW)
|
||||||
|
mCount++;
|
||||||
|
|
||||||
|
sqlite3_reset(mStmt);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
std::cerr << "Error Opening cursor (code: " << rc << ")\n";
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroCursor::moveToNext(){
|
||||||
|
|
||||||
|
#ifdef RETRODB_DEBUG
|
||||||
|
std::cerr << "RetroCursor::moveToNext()\n";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(!isOpen())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int rc = sqlite3_step(mStmt);
|
||||||
|
|
||||||
|
if(rc == SQLITE_ROW){
|
||||||
|
mPosCounter++;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}else if(rc == SQLITE_DONE){ // no more results
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if(rc == SQLITE_BUSY){ // should not enter here
|
||||||
|
std::cerr << "RetroDb::moveToNext()\n" ;
|
||||||
|
std::cerr << "Busy!, possible multiple accesses to Db" << std::endl
|
||||||
|
<< "serious error";
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
std::cerr << "Error executing statement (code: " << rc << ")\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int32_t RetroCursor::getPosition() const {
|
||||||
|
|
||||||
|
if(isOpen())
|
||||||
|
return mPosCounter;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int32_t RetroCursor::getInt32(int columnIndex){
|
||||||
|
return sqlite3_column_int(mStmt, columnIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t RetroCursor::getInt64(int columnIndex){
|
||||||
|
return sqlite3_column_int64(mStmt, columnIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RetroCursor::getBool(int columnIndex){
|
||||||
|
return sqlite3_column_int(mStmt, columnIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
double RetroCursor::getDouble(int columnIndex){
|
||||||
|
return sqlite3_column_double(mStmt, columnIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RetroCursor::getString(int columnIndex, std::string &str){
|
||||||
|
char* raw_str = (char*)sqlite3_column_text(mStmt, columnIndex);
|
||||||
|
if(raw_str != NULL)
|
||||||
|
str.assign(raw_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
const void* RetroCursor::getData(int columnIndex, uint32_t &datSize){
|
||||||
|
|
||||||
|
const void* val = sqlite3_column_blob(mStmt, columnIndex);
|
||||||
|
datSize = sqlite3_column_bytes(mStmt, columnIndex);
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**************** content value implementation ******************/
|
||||||
|
|
||||||
|
typedef std::pair<std::string, uint8_t> KeyTypePair;
|
||||||
|
|
||||||
|
ContentValue::ContentValue(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ContentValue::~ContentValue(){
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ContentValue::ContentValue(ContentValue &from){
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t> keyTypeMap;
|
||||||
|
from.getKeyTypeMap(keyTypeMap);
|
||||||
|
std::map<std::string, uint8_t>::const_iterator cit =
|
||||||
|
keyTypeMap.begin();
|
||||||
|
|
||||||
|
uint8_t type = 0;
|
||||||
|
std::string currKey;
|
||||||
|
std::string val = "";
|
||||||
|
char *src = NULL;
|
||||||
|
uint32_t data_len = 0;
|
||||||
|
|
||||||
|
for(; cit != keyTypeMap.end(); cit++){
|
||||||
|
|
||||||
|
type = cit->second;
|
||||||
|
currKey = cit->first;
|
||||||
|
|
||||||
|
switch(type){
|
||||||
|
|
||||||
|
case INT32_TYPE:
|
||||||
|
{
|
||||||
|
int32_t value;
|
||||||
|
from.getAsInt32(currKey, value);
|
||||||
|
put(currKey, value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case INT64_TYPE:
|
||||||
|
{
|
||||||
|
int64_t value;
|
||||||
|
from.getAsInt64(currKey, value);
|
||||||
|
put(currKey, value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case STRING_TYPE:
|
||||||
|
{
|
||||||
|
from.getAsString(currKey, val);
|
||||||
|
put(currKey, val);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BOOL_TYPE:
|
||||||
|
{
|
||||||
|
bool value;
|
||||||
|
from.getAsBool(currKey, value);
|
||||||
|
put(currKey, value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DATA_TYPE:
|
||||||
|
{
|
||||||
|
from.getAsData(currKey, data_len, src);
|
||||||
|
put(currKey, data_len, src);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DOUBLE_TYPE:
|
||||||
|
double value;
|
||||||
|
from.getAsDouble(currKey, value);
|
||||||
|
put(currKey, value);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cerr << "ContentValue::ContentValue(ContentValue &from):"
|
||||||
|
<< "Error! Unrecognised data type!" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::put(const std::string &key, bool value){
|
||||||
|
|
||||||
|
if(mKvSet.find(key) != mKvSet.end())
|
||||||
|
removeKeyValue(key);
|
||||||
|
|
||||||
|
mKvSet.insert(KeyTypePair(key, BOOL_TYPE));
|
||||||
|
mKvBool.insert(std::pair<std::string, bool>(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::put(const std::string &key, const std::string &value){
|
||||||
|
|
||||||
|
if(mKvSet.find(key) != mKvSet.end())
|
||||||
|
removeKeyValue(key);
|
||||||
|
|
||||||
|
mKvSet.insert(KeyTypePair(key, STRING_TYPE));
|
||||||
|
mKvString.insert(std::pair<std::string, std::string>(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::put(const std::string &key, double value){
|
||||||
|
|
||||||
|
if(mKvSet.find(key) != mKvSet.end())
|
||||||
|
removeKeyValue(key);
|
||||||
|
|
||||||
|
mKvSet.insert(KeyTypePair(key,DOUBLE_TYPE));
|
||||||
|
mKvDouble.insert(std::pair<std::string, double>(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::put(const std::string &key, int32_t value){
|
||||||
|
|
||||||
|
if(mKvSet.find(key) != mKvSet.end())
|
||||||
|
removeKeyValue(key);
|
||||||
|
|
||||||
|
mKvSet.insert(KeyTypePair(key, INT32_TYPE));
|
||||||
|
mKvInt32.insert(std::pair<std::string, int32_t>(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::put(const std::string &key, int64_t value){
|
||||||
|
|
||||||
|
if(mKvSet.find(key) != mKvSet.end())
|
||||||
|
removeKeyValue(key);
|
||||||
|
|
||||||
|
mKvSet.insert(KeyTypePair(key, INT64_TYPE));
|
||||||
|
mKvInt64.insert(std::pair<std::string, int64_t>(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::put(const std::string &key, uint32_t len, const char* value){
|
||||||
|
|
||||||
|
|
||||||
|
// release memory from old key value if key
|
||||||
|
// exists
|
||||||
|
if(mKvSet.find(key) != mKvSet.end()) {
|
||||||
|
removeKeyValue(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
mKvSet.insert(KeyTypePair(key, DATA_TYPE));
|
||||||
|
char* dest = NULL;
|
||||||
|
|
||||||
|
// len is zero then just put a NULL entry
|
||||||
|
if(len != 0){
|
||||||
|
dest = new char[len];
|
||||||
|
memcpy(dest, value, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
mKvData.insert(std::pair<std::string, std::pair<uint32_t, char*> >
|
||||||
|
(key, std::pair<uint32_t, char*>(len, dest)));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContentValue::getAsBool(const std::string &key, bool& value) const{
|
||||||
|
|
||||||
|
if(mKvBool.find(key) == mKvBool.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
value = mKvBool.at(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContentValue::getAsInt32(const std::string &key, int32_t& value) const{
|
||||||
|
|
||||||
|
if(mKvInt32.find(key) == mKvInt32.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
value = mKvInt32.at(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContentValue::getAsInt64(const std::string &key, int64_t& value) const{
|
||||||
|
|
||||||
|
if(mKvInt64.find(key) == mKvInt64.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
value = mKvInt64.at(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContentValue::getAsString(const std::string &key, std::string &value) const{
|
||||||
|
|
||||||
|
if(mKvString.find(key) == mKvString.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
value = mKvString.at(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContentValue::getAsData(const std::string& key, uint32_t &len, char*& value) const{
|
||||||
|
|
||||||
|
if(mKvData.find(key) == mKvData.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const std::pair<uint32_t, char*> &kvRef = mKvData.at(key);
|
||||||
|
|
||||||
|
len = kvRef.first;
|
||||||
|
value = kvRef.second;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContentValue::getAsDouble(const std::string &key, double& value) const{
|
||||||
|
|
||||||
|
if(mKvDouble.find(key) == mKvDouble.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
value = mKvDouble.at(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ContentValue::removeKeyValue(const std::string &key){
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t>::iterator mit;
|
||||||
|
|
||||||
|
if((mit = mKvSet.find(key)) == mKvSet.end())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(mit->second == BOOL_TYPE)
|
||||||
|
mKvBool.erase(key);
|
||||||
|
|
||||||
|
if(mit->second == INT64_TYPE)
|
||||||
|
mKvInt64.erase(key);
|
||||||
|
|
||||||
|
if(mit->second == DATA_TYPE){
|
||||||
|
delete[] (mKvData[key].second);
|
||||||
|
mKvData.erase(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mit->second == DOUBLE_TYPE)
|
||||||
|
mKvDouble.erase(key);
|
||||||
|
|
||||||
|
if(mit->second == STRING_TYPE)
|
||||||
|
mKvString.erase(key);
|
||||||
|
|
||||||
|
if(mit->second == INT32_TYPE)
|
||||||
|
mKvInt32.erase(key);
|
||||||
|
|
||||||
|
|
||||||
|
mKvSet.erase(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ContentValue::getKeyTypeMap(std::map<std::string, uint8_t> &keySet) const {
|
||||||
|
keySet = mKvSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::clear(){
|
||||||
|
mKvSet.clear();
|
||||||
|
mKvBool.clear();
|
||||||
|
mKvDouble.clear();
|
||||||
|
mKvString.clear();
|
||||||
|
mKvInt32.clear();
|
||||||
|
mKvInt64.clear();
|
||||||
|
clearData();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentValue::clearData(){
|
||||||
|
|
||||||
|
std::map<std::string, std::pair<uint32_t, char*> >::iterator
|
||||||
|
mit = mKvData.begin();
|
||||||
|
|
||||||
|
for(; mit != mKvData.end(); mit++){
|
||||||
|
|
||||||
|
if(mit->second.first != 0)
|
||||||
|
delete[] (mit->second.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
mKvData.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,34 @@
|
||||||
#ifndef RSSQLITE_H
|
#ifndef RSSQLITE_H
|
||||||
#define RSSQLITE_H
|
#define RSSQLITE_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* RetroShare : RetroDb functionality
|
||||||
|
*
|
||||||
|
* Copyright 2012 Christopher Evi-Parker
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Library General Public
|
||||||
|
* License Version 2 as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Library General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Library General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||||
|
* USA.
|
||||||
|
*
|
||||||
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#include "sqlite3.h"
|
#include "sqlite3.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,10 +36,10 @@ class ContentValue;
|
||||||
class RetroCursor;
|
class RetroCursor;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* The idea of RsDb is to provide a means for Retroshare core and \n
|
* RetroDb provide a means for Retroshare's core and \n
|
||||||
* its services to maintain an easy to use random access file via a database \n
|
* services to maintain an easy to use random access file via a database \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
|
* This is essentially unashamedly a clone of Android's SQLiteDatabase interface
|
||||||
*/
|
*/
|
||||||
class RetroDb
|
class RetroDb
|
||||||
{
|
{
|
||||||
|
@ -34,7 +58,9 @@ public:
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* opens sqlite data base
|
* opens sqlite data base
|
||||||
* @return false if we failed to open
|
* @param dbPath
|
||||||
|
* @param flags
|
||||||
|
* @return false if failed to open, true otherwise
|
||||||
*/
|
*/
|
||||||
bool openDb(const std::string& dbPath, int flags = OPEN_READONLY);
|
bool openDb(const std::string& dbPath, int flags = OPEN_READONLY);
|
||||||
|
|
||||||
|
@ -43,22 +69,40 @@ public:
|
||||||
*/
|
*/
|
||||||
void closeDb();
|
void closeDb();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
*
|
||||||
|
* @return false if database is not open, true otherwise
|
||||||
|
*/
|
||||||
|
bool isOpen() const;
|
||||||
|
|
||||||
/* modifying db */
|
/* modifying db */
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* To make queries which does not return a result
|
* To a make query which do not return a result \n
|
||||||
|
* below are the type of queries this method should be used for \n
|
||||||
|
* ALTER TABLE \n
|
||||||
|
* CREATE or DROP table / trigger / view / index / virtual table \n
|
||||||
|
* REINDEX \n
|
||||||
|
* RELEASE \n
|
||||||
|
* SAVEPOINT \n
|
||||||
|
* PRAGMA that returns no data \n
|
||||||
* @param query SQL query
|
* @param query SQL query
|
||||||
|
* @return false if there was an sqlite error, true otherwise
|
||||||
*/
|
*/
|
||||||
void execSQL(const std::string& query);
|
bool execSQL(const std::string& query);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* inserts a row in a database table
|
* inserts a row in a database table
|
||||||
* @param table table you want to insert content values into
|
* @param table table you want to insert content values into
|
||||||
|
* @param nullColumnHack SQL doesn't allow inserting a completely \n
|
||||||
|
* empty row without naming at least one column name
|
||||||
* @param cv hold entries to insert
|
* @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, std::string nullColumnHack, const ContentValue& cv);
|
bool sqlInsert(const std::string& table,const std::string& nullColumnHack, const ContentValue& cv);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* update row in a database table
|
* update row in a database table
|
||||||
|
@ -72,14 +116,15 @@ public:
|
||||||
/*!
|
/*!
|
||||||
* Query the given table, returning a Cursor over the result set
|
* Query the given table, returning a Cursor over the result set
|
||||||
* @param tableName the table name
|
* @param tableName the table name
|
||||||
* @param columns columns that should be returned
|
* @param columns list columns that should be returned and their order (the list's order)
|
||||||
* @param selection A filter declaring which rows to return, formatted as \n
|
* @param selection A filter declaring which rows to return, formatted as \n
|
||||||
* an SQL WHERE clause (excluding the WHERE itself). Passing null will \n
|
* an SQL WHERE clause (excluding the WHERE itself). Passing null will \n
|
||||||
* return all rows for the given table.
|
* return all rows for the given table.
|
||||||
* @param order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself)
|
* @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, this allocated resource should be free'd after use \n
|
||||||
|
* column order is in list order.
|
||||||
*/
|
*/
|
||||||
RetroCursor* sqlQuery(const std::string& tableName, const std::set<std::string>& columns,
|
RetroCursor* sqlQuery(const std::string& tableName, const std::list<std::string>& columns,
|
||||||
const std::string& selection, const std::string& orderBy);
|
const std::string& selection, const std::string& orderBy);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -88,9 +133,10 @@ public:
|
||||||
* @param whereClause formatted as where statement without 'WHERE' itself
|
* @param whereClause formatted as where statement without 'WHERE' itself
|
||||||
* @return false
|
* @return false
|
||||||
*/
|
*/
|
||||||
bool sqlDelete(const std::string& tableName, const std::string& whereClause);
|
bool sqlDelete(const std::string& tableName, const std::string& whereClause, const std::string& whereArgs);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
* TODO
|
||||||
* 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();
|
||||||
|
@ -104,6 +150,7 @@ public:
|
||||||
|
|
||||||
static const int OPEN_READONLY;
|
static const int OPEN_READONLY;
|
||||||
static const int OPEN_READWRITE;
|
static const int OPEN_READWRITE;
|
||||||
|
static const int OPEN_READWRITE_CREATE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -122,24 +169,27 @@ public:
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Initialises a null cursor
|
* Initialises a null cursor
|
||||||
|
* @warning cursor takes ownership of statement passed to it
|
||||||
*/
|
*/
|
||||||
RetroCursor(sqlite3_stmt*);
|
RetroCursor(sqlite3_stmt*);
|
||||||
|
|
||||||
|
~RetroCursor();
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* move to first row of results
|
* move to first row of results
|
||||||
* @return false if no result
|
* @return false if no results
|
||||||
*/
|
*/
|
||||||
bool moveToFirst();
|
bool moveToFirst();
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* move to next row of results
|
* move to next row of results
|
||||||
* @return false if no row to move next to
|
* @return false if no further results
|
||||||
*/
|
*/
|
||||||
bool moveToNext();
|
bool moveToNext();
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* move to last row of results
|
* move to last row of results
|
||||||
* @return false if no result
|
* @return false if no result, true otherwise
|
||||||
*/
|
*/
|
||||||
bool moveToLast();
|
bool moveToLast();
|
||||||
|
|
||||||
|
@ -147,9 +197,35 @@ public:
|
||||||
* gets current position of cursor
|
* gets current position of cursor
|
||||||
* @return current position of cursor
|
* @return current position of cursor
|
||||||
*/
|
*/
|
||||||
uint32_t getPosition();
|
int32_t getPosition() const;
|
||||||
|
|
||||||
/* data retrieval */
|
/* data retrieval */
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @return true if cursor is open and active, false otherwise
|
||||||
|
*/
|
||||||
|
bool isOpen() const;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* cursor is closed, statement used to open cursor is deleted
|
||||||
|
* @return false if error on close (was already closed, error occured)
|
||||||
|
*/
|
||||||
|
bool close();
|
||||||
|
|
||||||
|
/*!
|
||||||
|
*
|
||||||
|
* @return -1 if cursor is in error, otherwise number of rows in result
|
||||||
|
*/
|
||||||
|
int32_t getResultCount() const;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Current statement is closed and discarded (finalised)
|
||||||
|
* before actual opening occurs
|
||||||
|
* @param stm statement to open cursor on
|
||||||
|
* @return true if cursor is successfully opened
|
||||||
|
*/
|
||||||
|
bool open(sqlite3_stmt* stm);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
||||||
|
@ -158,7 +234,7 @@ public:
|
||||||
* @param columnIndex the zero-based index of the target column.
|
* @param columnIndex the zero-based index of the target column.
|
||||||
* @return the value of the column as 32 bit integer
|
* @return the value of the column as 32 bit integer
|
||||||
*/
|
*/
|
||||||
int32_t getInt(int columnIndex);
|
int32_t getInt32(int columnIndex);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Returns the value of the requested column as a String.
|
* Returns the value of the requested column as a String.
|
||||||
|
@ -186,20 +262,21 @@ public:
|
||||||
* @param columnIndex the zero-based index of the target column.
|
* @param columnIndex the zero-based index of the target column.
|
||||||
* @return the value of the column as a string
|
* @return the value of the column as a string
|
||||||
*/
|
*/
|
||||||
std::string getString(int columnIndex);
|
void getString(int columnIndex, std::string& str);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Returns the value of the requested column as a String.
|
* Returns the value of the requested column as a String.
|
||||||
* @param columnIndex the zero-based index of the target column.
|
* @param columnIndex the zero-based index of the target column.
|
||||||
* @return the value of the column as pointer to raw data
|
* @return the value of the column as pointer to raw data
|
||||||
*/
|
*/
|
||||||
void* getData(int columnIndex, uint32_t& datSize);
|
const void* getData(int columnIndex, uint32_t& datSize);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
sqlite3_stmt* mStmt;
|
sqlite3_stmt* mStmt;
|
||||||
int mCount;
|
int mCount; /// number of results
|
||||||
|
int mPosCounter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -239,6 +316,8 @@ public:
|
||||||
* Adds a value to the set
|
* Adds a value to the set
|
||||||
* @param key the name of the value to put
|
* @param key the name of the value to put
|
||||||
* @param value the data for the value to put
|
* @param value the data for the value to put
|
||||||
|
* @warning cast string literals explicitly as string, observed string literal \n
|
||||||
|
* being casted to bool instead e.g. string("hello") rather than "hello"
|
||||||
*/
|
*/
|
||||||
void put(const std::string& key, const std::string& value);
|
void put(const std::string& key, const std::string& value);
|
||||||
|
|
||||||
|
@ -275,29 +354,29 @@ public:
|
||||||
* @param key the name of the value to put
|
* @param key the name of the value to put
|
||||||
* @param value the data for the value to put
|
* @param value the data for the value to put
|
||||||
*/
|
*/
|
||||||
void put(const std::string& key, uint32_t len, char* value);
|
void put(const std::string& key, uint32_t len, const char* value);
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* get as value as
|
* get value as 32 bit signed integer
|
||||||
* @param key the value to get
|
* @param key the value to get
|
||||||
*/
|
*/
|
||||||
bool getAsInt32(const std::string& key, int32_t& value) const;
|
bool getAsInt32(const std::string& key, int32_t& value) const;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* get as value as 64 bit integer
|
* get value as 64 bit signed integer
|
||||||
* @param key the value to get
|
* @param key the value to get
|
||||||
*/
|
*/
|
||||||
bool getAsInt64(const std::string& key, int64_t& value) const;
|
bool getAsInt64(const std::string& key, int64_t& value) const;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* get as value as bool
|
* get value as bool
|
||||||
* @param key the value to get
|
* @param key the value to get
|
||||||
*/
|
*/
|
||||||
bool getAsBool(const std::string& key, bool& value) const;
|
bool getAsBool(const std::string& key, bool& value) const;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* get as value as double
|
* get as value double
|
||||||
* @param key the value to get
|
* @param key the value to get
|
||||||
*/
|
*/
|
||||||
bool getAsDouble(const std::string& key, double& value) const;
|
bool getAsDouble(const std::string& key, double& value) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue