-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_db.cpp
72 lines (61 loc) · 2.31 KB
/
sqlite_db.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "logger.h"
#include "sqlite_db.h"
#include "db_factory.h"
int (*sqlite_db::fptr_store_row_data)(void *, int, char **, char **)=NULL;
sqlite_db::sqlite_db(){
sqlite=NULL;
sqlite_db::fptr_store_row_data=&sqlite_db::store_row_data;
}
sqlite_db::~sqlite_db(){
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"destructing sqlite");
sqlite=NULL;
}
void sqlite_db::open(const std::string& filename){
db_uri_=filename;
if(sqlite3_open(filename.c_str(),&sqlite)!=SQLITE_OK) throw failed_to_open_db(db_factory::get_instance()->error_message());
return;
}
void sqlite_db::close(){
if(sqlite3_close(sqlite)!=SQLITE_OK) throw failed_to_close_db(db_factory::get_instance()->error_message());
return;
}
const std::string sqlite_db::error_message(){
return sqlite3_errmsg(sqlite);
}
query_result *sqlite_db::exec_sql(const std::string& sql){
query_result *result_set=NULL;
result_set=new query_result();
if(sqlite3_exec(sqlite, sql.c_str(), sqlite_db::fptr_store_row_data, result_set, NULL)!=SQLITE_OK){
logger::singleton()==NULL?(void)0:logger::singleton()->log(0,"sql: "+sql);
throw sql_execution_error(db_factory::get_instance()->error_message());
}
if(result_set->nr_of_result_rows()==0){
delete result_set;
result_set=NULL;
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"Result set empty.");
}
return result_set;
}
/*PRIVATE*/
int sqlite_db::store_row_data(void *p_result, int nr_of_columns, char **field_values, char **fields){/*CALLBACK function for sqlite3_exec*/
unsigned int row_index=0;
query_result *result=NULL;
field field;
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"nr of columns in row: "+std::to_string(nr_of_columns));
result=(query_result*)(p_result);
if(nr_of_columns!=0&&field_values!=NULL&&fields!=NULL){
result->set_metadata(nr_of_columns,(const char**) &(*fields));
for(unsigned int i=0;i<nr_of_columns;++i){
field.field_name=fields[i];
if(field_values[i]!=NULL) field.field_value=field_values[i];
else field.field_value.clear();
logger::singleton()==NULL?(void)0:logger::singleton()->log(3,"field name: "+field.field_name+", field value: "+field.field_value);
row_index=result->result_set().size()/nr_of_columns;
result->insert(std::make_pair(row_index,field));
}
}
return 0;
}
std::string sqlite_db::db_uri(){
return db_uri_;
}