BOSS 6.6.4.p01
BESIII Offline Software System
Loading...
Searching...
No Matches
ReadRuntripFromDb.cxx
Go to the documentation of this file.
2#include <string.h>
3
4using namespace std;
5
7 m_isTrip(false){
8 }
9
11{
12 timest.clear();
13 timend.clear();
14}
15
16MYSQL* ReadRuntripFromDb::OpenDb() const {
17
18 const char host[] = "bes3db2.ihep.ac.cn";
19 const char user[] = "guest";
20 const char passwd[] = "guestpass";
21 const char db[] = "offlinedb";
22 unsigned int port_num = 3306;
23
24 MYSQL* mysql = mysql_init(NULL);
25 mysql = mysql_real_connect(mysql, host, user, passwd, db, port_num,
26 NULL, // socket
27 0); // client_flag
28
29 if (mysql == NULL) {
30 fprintf(stderr, "can not open database: offlinedb\n");
31 }
32
33 return mysql;
34}
35
36void ReadRuntripFromDb::CloseDb(MYSQL* mysql) const {
37 mysql_close(mysql);
38}
39
40
42{
43 MYSQL* mysql=OpenDb();
44 char stmt[1024];
45
46 snprintf(stmt, 1024,
47 "select trip_start, trip_stop "
48 "from RunParams where run_number = %d", run);
49 //printf("--------- %s -------\n",stmt);
50 if (mysql_real_query(mysql, stmt, strlen(stmt)))
51 {
52 fprintf(stderr, "query error\n");
53 m_isTrip = false;
54 CloseDb(mysql);
55 return;
56 }
57 MYSQL_RES* result_set = mysql_store_result(mysql);
58 MYSQL_ROW row = mysql_fetch_row(result_set);
59 if (row[0]==NULL || row[1]==NULL)
60 {
61 fprintf(stderr, "can't find trip information for tripped run: %d\n", run);
62 mysql_free_result(result_set);
63 CloseDb(mysql);
64 m_isTrip = false;
65 return;
66 }
67
68 timest.clear();
69 timend.clear();
70 char *token;
71 char *doken;
72 token = strtok(row[0],",");
73 string tripst(token);
74 timest.push_back(tripst);
75 while((token =strtok(NULL,","))!= NULL){
76 string tripst(token);
77 timest.push_back(tripst);
78 }
79 doken = strtok(row[1],",");
80 string tripnd(doken);
81 timend.push_back(tripnd);
82 while((doken = strtok(NULL,","))!= NULL){
83 string tripnd(doken);
84 timend.push_back(tripnd);
85 }
86
87 // Free the memory to prevent memory leak!
88 mysql_free_result(result_set);
89 CloseDb(mysql);
90 m_isTrip = true;
91}
92
94{
95 return m_isTrip;
96}
97
99{
100 return timest[i];
101}
103{
104 return timend[i];
105}
106
108{
109 return timest.size();
110}
111
113{
114 return timend.size();
115}
struct st_mysql_res MYSQL_RES
struct st_mysql MYSQL
string gettimest(int i)