forked from urkle/libuc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathucsqlite.cpp
96 lines (82 loc) · 2.32 KB
/
ucsqlite.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* UniversalContainer library.
* Copyright Jason Denton, 2008,2010.
* Made available under the new BSD license, as described in LICENSE
*
* Send comments and bug reports to [email protected]
* http://www.greatpanic.com/code.html
*/
#include <iostream>
#include "ucsqlite.h"
using namespace std;
/*
Interface to sqlite3 databases for libuc.
*/
namespace JAD {
void SQLiteDatabase::real_setup(const char* filename)
{
db_info["database_type"] = "SQLite";
db_info["filename"] = filename;
own_db = true;
if (sqlite3_open(filename,&database))
throw ucexception(uce_DB_Connection);
}
SQLiteDatabase::SQLiteDatabase(string& filename)
{
real_setup(filename.c_str());
}
SQLiteDatabase::SQLiteDatabase(const char* filename)
{
real_setup(filename);
}
//This constructor allows an externally created DB to be used.
SQLiteDatabase::SQLiteDatabase(sqlite3* db)
{
db_info["database_type"] = "SQLite";
database = db;
own_db = false;
}
SQLiteDatabase::~SQLiteDatabase(void)
{
if (own_db) sqlite3_close(database);
}
//return the db handle to enable more complete db operations
//using sqlite3 libraries.
sqlite3* SQLiteDatabase::get_db_handle(void)
{
return database;
}
//for queries that returns a result set, this is called once
//for each row. It gets a pointer to a uc, and parses the
//incoming data into that uc.
int SQLiteDatabase::sqlite_callback(void* context, int argc,
char** argv, char** colname)
{
UniversalContainer* uc = static_cast<UniversalContainer*>(context);
int pos = uc->size();
for (int i = 0; i < argc; i++)
(*uc)[pos][colname[i]].string_interpret(argv[i]);
return 0;
}
//execute a query and setup the uc to hold the proper return values.
UniversalContainer SQLiteDatabase::sql_exec(string query)
{
UniversalContainer uc;
char* err = NULL;
int status;
status = sqlite3_exec(database,query.c_str(),sqlite_callback, &uc,
&err);
if (status) {
uc["status_code"] = status;
uc["message"] = err;
uc["#boolean_value"] = false;
sqlite3_free(err);
}
else if (!uc) { // if operation did nothing with the uc
uc["#boolean_value"] = true;
uc["message"] = "success";
uc["status_code"] = 0;
}
return uc;
}
} //end namespace