BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
SqliteInterface.cxx
Go to the documentation of this file.
2
3#include <iostream>
4#include <cstring>
5
6#include <sqlite3.h>
7
8using namespace std;
9/*
10static int callback(void *result, int argc, char **argv, char **azColName){
11 int i;
12 DatabaseRecordVector* recv = static_cast<DatabaseRecordVector*>(result);
13 DatabaseRecord dbrec;
14 for(i=0; i<argc; i++){
15 // printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
16 unsigned long field_len = 1;
17 char* new_record = new char[];
18 memcpy(new_record,argv[i],field_len);
19 dbrec[azColName[i]]=new_record;
20 }
21 recv->push_back(dbrec);
22
23 return 0;
24}
25*/
27
29
31{
32 return 0;
33}
34
35int SqliteInterface::connect(std::string fname)
36{
37 int status = sqlite3_open_v2(fname.c_str(), &m_conn, SQLITE_OPEN_READONLY,0);
38 if( status ){
39 cerr << "Can't open database " << fname << ": "
40 << sqlite3_errmsg(m_conn) << endl;
41 cerr.flush();
42 sqlite3_close(m_conn);
43 return -1;
44 }
45
46 //std::cout << "Open database " << fname << std::endl;
47
48 m_isConnected = true;
49
50 return 0;
51}
52
53int SqliteInterface::select_db(std::string dbname)
54{
56 {
57 if(m_dbName != dbname) // need to change db
58 disconnect();
59 }
60
61 m_dbName = dbname;
62 string fname = m_dbPath+"/"+m_dbName+".db";
63
64 if(! m_isConnected)
65 {
66 int status = connect(fname);
67 if(status<0)
68 return -1;
69 }
70
71 return 0;
72}
73
74int SqliteInterface::query(std::string dbname, std::string sql)
75{
77 int status = query(dbname, sql, dummy);
78 return status;
79}
80
81int SqliteInterface::query(std::string dbname, std::string sql, DatabaseRecordVector& records)
82{
83 char *zErrMsg = 0;
84 // char ***pazResult;
85 // int pnRow;
86 // int pnColumn;
87
88 int status = select_db(dbname);
89 if(status<0)
90 return -1;
91
92 records.clear();
93 // status = sqlite3_exec(m_conn, sql.c_str(), callback, &records, &zErrMsg);
94 sqlite3_stmt *ppStmt;
95 sqlite3_prepare_v2(m_conn, sql.c_str(), -1, &ppStmt, 0);
96
97 while ( true )
98 {
99 status = sqlite3_step(ppStmt);
100 if(status == SQLITE_DONE)
101 {
102 break;
103 }
104
105 if(status == SQLITE_ROW)
106 {
107 DatabaseRecord* dbrec = new DatabaseRecord;
108
109 //loop over columns
110 int ncolumns = sqlite3_column_count(ppStmt);
111 int i;
112 for(i=0; i<ncolumns; i++)
113 {
114 // create new record
115 unsigned long field_len = sqlite3_column_bytes(ppStmt,i);
116 char* new_record;
117 const char* col_name = sqlite3_column_name(ppStmt,i);
118 char column_name[255];
119 strcpy(column_name,col_name);
120 int type = sqlite3_column_type(ppStmt,i);
121 if(type == SQLITE_BLOB)
122 {
123 new_record = new char[field_len];
124 char* col_result = (char*)sqlite3_column_blob(ppStmt,i);
125 memcpy(new_record,col_result,field_len);
126 }
127 else if (type != SQLITE_NULL)
128 {
129 new_record = new char[field_len+1];
130 char* col_result = (char*)sqlite3_column_text(ppStmt,i);
131 strcpy(new_record,col_result);
132 }
133 else
134 {
135 new_record = NULL;
136 }
137 (*dbrec)[column_name] = new_record;
138 }
139
140 records.push_back(dbrec);
141
142 continue;
143 }
144
145 // anything else means that error happened
146 cerr << "SQLITE query error: " << zErrMsg << endl;
147 return -1;
148 }
149
150 sqlite3_free(zErrMsg);
151 sqlite3_finalize(ppStmt);
152// if(! m_reuseConnection)
153 disconnect();
154
155 return records.size();
156}
157
159{
160 int res = sqlite3_close(m_conn);
161 if (res)
162 cerr << "ERROR: Cannot close connection to " << m_dbName << endl;
163 m_isConnected = false;
164 return 0;
165}
std::string m_dbName
Definition: DbInterface.h:39
std::string m_dbPath
Definition: DbInterface.h:44
bool m_isConnected
Definition: DbInterface.h:36
int query(std::string dbname, std::string query)
int select_db(std::string dbname)