BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
SqliteInterface.cxx
Go to the documentation of this file.
1#include "SqliteInterface.h"
2
3#include <cstring>
4#include <iostream>
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
30int SqliteInterface::connect() { return 0; }
31
32int SqliteInterface::connect( std::string fname ) {
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}
48
49int SqliteInterface::select_db( std::string dbname ) {
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}
67
68int SqliteInterface::query( std::string dbname, std::string sql ) {
70 int status = query( dbname, sql, dummy );
71 return status;
72}
73
74int SqliteInterface::query( std::string dbname, std::string sql,
75 DatabaseRecordVector& records ) {
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}
143
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
std::string m_dbPath
Definition DbInterface.h:46
bool m_isConnected
Definition DbInterface.h:37
int query(std::string dbname, std::string query)
int select_db(std::string dbname)