#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 () |
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) |
Protected Attributes | |
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 |
Private Attributes | |
sqlite3 * | m_conn |
Definition at line 8 of file SqliteInterface.h.
SqliteInterface::SqliteInterface | ( | ) |
SqliteInterface::~SqliteInterface | ( | ) |
int SqliteInterface::connect | ( | std::string | fname | ) | [protected] |
Definition at line 35 of file SqliteInterface.cxx.
References m_conn, and DbInterface::m_isConnected.
00036 { 00037 int status = sqlite3_open_v2(fname.c_str(), &m_conn, SQLITE_OPEN_READONLY,0); 00038 if( status ){ 00039 cerr << "Can't open database " << fname << ": " 00040 << sqlite3_errmsg(m_conn) << endl; 00041 cerr.flush(); 00042 sqlite3_close(m_conn); 00043 return -1; 00044 } 00045 00046 //std::cout << "Open database " << fname << std::endl; 00047 00048 m_isConnected = true; 00049 00050 return 0; 00051 }
int SqliteInterface::connect | ( | ) | [virtual] |
Implements DbInterface.
Definition at line 30 of file SqliteInterface.cxx.
Referenced by select_db().
int SqliteInterface::disconnect | ( | ) | [virtual] |
Implements DbInterface.
Definition at line 158 of file SqliteInterface.cxx.
References m_conn, DbInterface::m_dbName, and DbInterface::m_isConnected.
Referenced by query(), and select_db().
00159 { 00160 int res = sqlite3_close(m_conn); 00161 if (res) 00162 cerr << "ERROR: Cannot close connection to " << m_dbName << endl; 00163 m_isConnected = false; 00164 return 0; 00165 }
bool DbInterface::is_connected | ( | ) | [inline, inherited] |
Definition at line 27 of file DbInterface.h.
References DbInterface::m_isConnected.
Referenced by DatabaseSvc::finalize().
00027 { return m_isConnected; }
int SqliteInterface::query | ( | std::string | dbname, | |
std::string | query, | |||
DatabaseRecordVector & | records | |||
) | [virtual] |
Implements DbInterface.
Definition at line 81 of file SqliteInterface.cxx.
References DatabaseRecordVector::clear(), disconnect(), genRecEmupikp::i, m_conn, select_db(), and type.
00082 { 00083 char *zErrMsg = 0; 00084 // char ***pazResult; 00085 // int pnRow; 00086 // int pnColumn; 00087 00088 int status = select_db(dbname); 00089 if(status<0) 00090 return -1; 00091 00092 records.clear(); 00093 // status = sqlite3_exec(m_conn, sql.c_str(), callback, &records, &zErrMsg); 00094 sqlite3_stmt *ppStmt; 00095 sqlite3_prepare_v2(m_conn, sql.c_str(), -1, &ppStmt, 0); 00096 00097 while ( true ) 00098 { 00099 status = sqlite3_step(ppStmt); 00100 if(status == SQLITE_DONE) 00101 { 00102 break; 00103 } 00104 00105 if(status == SQLITE_ROW) 00106 { 00107 DatabaseRecord* dbrec = new DatabaseRecord; 00108 00109 //loop over columns 00110 int ncolumns = sqlite3_column_count(ppStmt); 00111 int i; 00112 for(i=0; i<ncolumns; i++) 00113 { 00114 // create new record 00115 unsigned long field_len = sqlite3_column_bytes(ppStmt,i); 00116 char* new_record; 00117 const char* col_name = sqlite3_column_name(ppStmt,i); 00118 char column_name[255]; 00119 strcpy(column_name,col_name); 00120 int type = sqlite3_column_type(ppStmt,i); 00121 if(type == SQLITE_BLOB) 00122 { 00123 new_record = new char[field_len]; 00124 char* col_result = (char*)sqlite3_column_blob(ppStmt,i); 00125 memcpy(new_record,col_result,field_len); 00126 } 00127 else if (type != SQLITE_NULL) 00128 { 00129 new_record = new char[field_len+1]; 00130 char* col_result = (char*)sqlite3_column_text(ppStmt,i); 00131 strcpy(new_record,col_result); 00132 } 00133 else 00134 { 00135 new_record = NULL; 00136 } 00137 (*dbrec)[column_name] = new_record; 00138 } 00139 00140 records.push_back(dbrec); 00141 00142 continue; 00143 } 00144 00145 // anything else means that error happened 00146 cerr << "SQLITE query error: " << zErrMsg << endl; 00147 return -1; 00148 } 00149 00150 sqlite3_free(zErrMsg); 00151 sqlite3_finalize(ppStmt); 00152 // if(! m_reuseConnection) 00153 disconnect(); 00154 00155 return records.size(); 00156 }
int SqliteInterface::query | ( | std::string | dbname, | |
std::string | query | |||
) | [virtual] |
Implements DbInterface.
Definition at line 74 of file SqliteInterface.cxx.
00075 { 00076 DatabaseRecordVector dummy; 00077 int status = query(dbname, sql, dummy); 00078 return status; 00079 }
int SqliteInterface::select_db | ( | std::string | dbname | ) | [virtual] |
Implements DbInterface.
Definition at line 53 of file SqliteInterface.cxx.
References connect(), disconnect(), fname, DbInterface::m_dbName, DbInterface::m_dbPath, DbInterface::m_isConnected, and deljobs::string.
Referenced by query().
00054 { 00055 if(m_isConnected) 00056 { 00057 if(m_dbName != dbname) // need to change db 00058 disconnect(); 00059 } 00060 00061 m_dbName = dbname; 00062 string fname = m_dbPath+"/"+m_dbName+".db"; 00063 00064 if(! m_isConnected) 00065 { 00066 int status = connect(fname); 00067 if(status<0) 00068 return -1; 00069 } 00070 00071 return 0; 00072 }
void DbInterface::set_dbpath | ( | std::string | path | ) | [inline, inherited] |
Definition at line 32 of file DbInterface.h.
References DbInterface::m_dbPath.
Referenced by DatabaseSvc::initialize().
00032 {m_dbPath=path;};
void DbInterface::set_host | ( | std::string | host | ) | [inline, inherited] |
Definition at line 29 of file DbInterface.h.
References DbInterface::m_dbHost.
Referenced by DatabaseSvc::initialize().
00029 {m_dbHost=host;};
void DbInterface::set_passwd | ( | std::string | passwd | ) | [inline, inherited] |
Definition at line 31 of file DbInterface.h.
References DbInterface::m_dbPasswd.
Referenced by DatabaseSvc::initialize().
00031 {m_dbPasswd=passwd;};
void DbInterface::set_reuse_connection | ( | bool | flag | ) | [inline, inherited] |
Definition at line 33 of file DbInterface.h.
References DbInterface::m_reuseConnection.
Referenced by DatabaseSvc::initialize().
00033 { m_reuseConnection=flag;};
void DbInterface::set_user | ( | std::string | user | ) | [inline, inherited] |
Definition at line 30 of file DbInterface.h.
References DbInterface::m_dbUser.
Referenced by DatabaseSvc::initialize().
00030 {m_dbUser=user;};
sqlite3* SqliteInterface::m_conn [private] |
Definition at line 24 of file SqliteInterface.h.
Referenced by connect(), disconnect(), and query().
std::string DbInterface::m_dbHost [protected, inherited] |
Definition at line 41 of file DbInterface.h.
Referenced by MysqlInterface::connect(), MysqlInterface::query(), MysqlInterface::select_db(), and DbInterface::set_host().
std::string DbInterface::m_dbName [protected, inherited] |
Definition at line 39 of file DbInterface.h.
Referenced by MysqlInterface::connect(), DbInterface::DbInterface(), disconnect(), MysqlInterface::query(), and select_db().
std::string DbInterface::m_dbPasswd [protected, inherited] |
Definition at line 43 of file DbInterface.h.
Referenced by MysqlInterface::connect(), MysqlInterface::query(), MysqlInterface::select_db(), and DbInterface::set_passwd().
std::string DbInterface::m_dbPath [protected, inherited] |
Definition at line 44 of file DbInterface.h.
Referenced by select_db(), and DbInterface::set_dbpath().
std::string DbInterface::m_dbUser [protected, inherited] |
Definition at line 42 of file DbInterface.h.
Referenced by MysqlInterface::connect(), MysqlInterface::query(), MysqlInterface::select_db(), and DbInterface::set_user().
bool DbInterface::m_isConnected [protected, inherited] |
Definition at line 33 of file DbInterface.h.
Referenced by connect(), MysqlInterface::connect(), DbInterface::DbInterface(), disconnect(), MysqlInterface::disconnect(), DbInterface::is_connected(), and select_db().
bool DbInterface::m_reuseConnection [protected, inherited] |
Definition at line 37 of file DbInterface.h.
Referenced by MysqlInterface::connect(), DbInterface::DbInterface(), MysqlInterface::query(), and DbInterface::set_reuse_connection().