BOSS 8.0.0
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_port (int port)
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
int m_dbPort
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.

30{ return 0; }

Referenced by select_db().

◆ connect() [2/2]

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

Definition at line 32 of file SqliteInterface.cxx.

32 {
33 int status = sqlite3_open_v2( fname.c_str(), &m_conn, SQLITE_OPEN_READONLY, 0 );
34 if ( status )
35 {
36 cerr << "Can't open database " << fname << ": " << sqlite3_errmsg( m_conn ) << endl;
37 cerr.flush();
38 sqlite3_close( m_conn );
39 return -1;
40 }
41
42 // std::cout << "Open database " << fname << std::endl;
43
44 m_isConnected = true;
45
46 return 0;
47}
bool m_isConnected
Definition DbInterface.h:37

◆ disconnect()

int SqliteInterface::disconnect ( )
virtual

Implements DbInterface.

Definition at line 144 of file SqliteInterface.cxx.

144 {
145 int res = sqlite3_close( m_conn );
146 if ( res ) cerr << "ERROR: Cannot close connection to " << m_dbName << endl;
147 m_isConnected = false;
148 return 0;
149}
std::string m_dbName
Definition DbInterface.h:40

Referenced by query(), and select_db().

◆ query() [1/2]

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

Implements DbInterface.

Definition at line 68 of file SqliteInterface.cxx.

68 {
69 DatabaseRecordVector dummy;
70 int status = query( dbname, sql, dummy );
71 return status;
72}
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 74 of file SqliteInterface.cxx.

75 {
76 char* zErrMsg = 0;
77 // char ***pazResult;
78 // int pnRow;
79 // int pnColumn;
80
81 int status = select_db( dbname );
82 if ( status < 0 ) return -1;
83
84 records.clear();
85 // status = sqlite3_exec(m_conn, sql.c_str(), callback, &records, &zErrMsg);
86 sqlite3_stmt* ppStmt;
87 sqlite3_prepare_v2( m_conn, sql.c_str(), -1, &ppStmt, 0 );
88
89 while ( true )
90 {
91 status = sqlite3_step( ppStmt );
92 if ( status == SQLITE_DONE ) { break; }
93
94 if ( status == SQLITE_ROW )
95 {
96 DatabaseRecord* dbrec = new DatabaseRecord;
97
98 // loop over columns
99 int ncolumns = sqlite3_column_count( ppStmt );
100 int i;
101 for ( i = 0; i < ncolumns; i++ )
102 {
103 // create new record
104 unsigned long field_len = sqlite3_column_bytes( ppStmt, i );
105 char* new_record;
106 const char* col_name = sqlite3_column_name( ppStmt, i );
107 char column_name[255];
108 strcpy( column_name, col_name );
109 int type = sqlite3_column_type( ppStmt, i );
110 if ( type == SQLITE_BLOB )
111 {
112 new_record = new char[field_len];
113 char* col_result = (char*)sqlite3_column_blob( ppStmt, i );
114 memcpy( new_record, col_result, field_len );
115 }
116 else if ( type != SQLITE_NULL )
117 {
118 new_record = new char[field_len + 1];
119 char* col_result = (char*)sqlite3_column_text( ppStmt, i );
120 strcpy( new_record, col_result );
121 }
122 else { new_record = NULL; }
123 ( *dbrec )[column_name] = new_record;
124 }
125
126 records.push_back( dbrec );
127
128 continue;
129 }
130
131 // anything else means that error happened
132 cerr << "SQLITE query error: " << zErrMsg << endl;
133 return -1;
134 }
135
136 sqlite3_free( zErrMsg );
137 sqlite3_finalize( ppStmt );
138 // if(! m_reuseConnection)
139 disconnect();
140
141 return records.size();
142}
int select_db(std::string dbname)

◆ select_db()

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

Implements DbInterface.

Definition at line 49 of file SqliteInterface.cxx.

49 {
50 if ( m_isConnected )
51 {
52 if ( m_dbName != dbname ) // need to change db
53 disconnect();
54 }
55
56 m_dbName = dbname;
57 string fname = m_dbPath + "/" + m_dbName + ".db";
58
59 if ( !m_isConnected )
60 {
61 int status = connect( fname );
62 if ( status < 0 ) return -1;
63 }
64
65 return 0;
66}
std::string m_dbPath
Definition DbInterface.h:46

Referenced by query().


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