CGEM BOSS 6.6.5.h
BESIII Offline Software System
Loading...
Searching...
No Matches
SqliteInterface Class Reference

#include <SqliteInterface.h>

+ Inheritance diagram for SqliteInterface:

Public Member Functions

 SqliteInterface ()
 
 ~SqliteInterface ()
 
int connect ()
 
int select_db (std::string dbname)
 
int query (std::string dbname, std::string query)
 
int query (std::string dbname, std::string query, DatabaseRecordVector &records)
 
int disconnect ()
 
- Public Member Functions inherited from DbInterface
 DbInterface ()
 
virtual ~DbInterface ()
 
bool is_connected ()
 
void set_host (std::string host)
 
void set_user (std::string user)
 
void set_passwd (std::string passwd)
 
void set_dbpath (std::string path)
 
void set_reuse_connection (bool flag)
 

Protected Member Functions

int connect (std::string fname)
 

Additional Inherited Members

- Protected Attributes inherited from DbInterface
bool m_isConnected
 
bool m_reuseConnection
 
std::string m_dbName
 
std::string m_dbHost
 
std::string m_dbUser
 
std::string m_dbPasswd
 
std::string m_dbPath
 

Detailed Description

Definition at line 8 of file SqliteInterface.h.

Constructor & Destructor Documentation

◆ SqliteInterface()

SqliteInterface::SqliteInterface ( )

Definition at line 26 of file SqliteInterface.cxx.

26{}

◆ ~SqliteInterface()

SqliteInterface::~SqliteInterface ( )

Definition at line 28 of file SqliteInterface.cxx.

28{}

Member Function Documentation

◆ connect() [1/2]

int SqliteInterface::connect ( )
virtual

Implements DbInterface.

Definition at line 30 of file SqliteInterface.cxx.

31{
32 return 0;
33}

Referenced by select_db().

◆ connect() [2/2]

int SqliteInterface::connect ( std::string fname)
protected

Definition at line 35 of file SqliteInterface.cxx.

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}
bool m_isConnected
Definition DbInterface.h:36

◆ disconnect()

int SqliteInterface::disconnect ( )
virtual

Implements DbInterface.

Definition at line 158 of file SqliteInterface.cxx.

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

Referenced by query(), and select_db().

◆ query() [1/2]

int SqliteInterface::query ( std::string dbname,
std::string query )
virtual

Implements DbInterface.

Definition at line 74 of file SqliteInterface.cxx.

75{
77 int status = query(dbname, sql, dummy);
78 return status;
79}
int query(std::string dbname, std::string query)

Referenced by query().

◆ query() [2/2]

int SqliteInterface::query ( std::string dbname,
std::string query,
DatabaseRecordVector & records )
virtual

Implements DbInterface.

Definition at line 81 of file SqliteInterface.cxx.

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}
int select_db(std::string dbname)

◆ select_db()

int SqliteInterface::select_db ( std::string dbname)
virtual

Implements DbInterface.

Definition at line 53 of file SqliteInterface.cxx.

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}
std::string m_dbPath
Definition DbInterface.h:44

Referenced by query().


The documentation for this class was generated from the following files: