Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 68 lines (59 sloc) 2.454 kb
bada5e2 Ulisses Costa Added Data Layer: mysql++
authored
1 #include "Data.hxx"
2
3 Data::Data(string server, string db, string user, string passwd) {
4 _server = server.c_str();
5 _dbname = db.c_str();
6 _username = user.c_str();
7 _password = passwd.c_str();
8
db0630a Ulisses Costa completed the fill method, now we are able to insert new stuff or update...
authored
9 /* Connect to the the database. */
10 try {
11 _conn = mysqlpp::Connection(_dbname,_server, _username, _password);
12 } catch (const mysqlpp::Exception& er) {
13 cerr << "Error making connection: " << er.what() << endl;
bada5e2 Ulisses Costa Added Data Layer: mysql++
authored
14 }
15 }
16
17 void Data::fill(vector<Record*> *r) {
db0630a Ulisses Costa completed the fill method, now we are able to insert new stuff or update...
authored
18 try {
19 for(vector<Record*>::const_iterator it=r->begin(); it != r->end(); it++) {
20 Record *elem = *it;
21 mysqlpp::Query query = _conn.query("select * from dnsperf where webserver = %0q");
22 query.parse();
23
24 mysqlpp::SQLQueryParms sqp;
25 sqp << elem->getWebserver();
26 mysqlpp::StoreQueryResult res = query.store(sqp);
27
28 if (res.empty()) {
29 /* We don't have this row in database, insert this new one*/
30 mysqlpp::Query newquery = _conn.query("insert INTO dnsperf (webserver,avgqtime,nrq,timefst,timelst) VALUES(%0q , %1 , 1 , %2q , %3q)");
31 newquery.parse();
32
33 mysqlpp::SQLQueryParms sqpNew;
34 sqpNew << mysqlpp::String(elem->getWebserver())
35 << mysqlpp::String(to_string<int>(elem->getTime(),std::dec))
36 << mysqlpp::String(to_string<long>(elem->getTimestamp(),std::dec))
37 << mysqlpp::String(to_string<long>(elem->getTimestamp(),std::dec));
38 newquery.store(sqpNew);
39 } else {
40 /* Update a row */
41 for (size_t i = 0; i < res.num_rows(); ++i) {
42 mysqlpp::Query newquery = _conn.query("update dnsperf SET webserver=%0q,avgqtime=%1,nrq=%2,timefst=%3q,timelst=%4q where webserver=%0q");
43 newquery.parse();
bada5e2 Ulisses Costa Added Data Layer: mysql++
authored
44
db0630a Ulisses Costa completed the fill method, now we are able to insert new stuff or update...
authored
45 int newTime = (int) (atoi(res[i]["avgqtime"])+elem->getTime())/2;
46 int newNrq = (int) (atoi(res[i]["nrq"])+1);
47
48 mysqlpp::SQLQueryParms sqpNew;
49 sqpNew << mysqlpp::String(elem->getWebserver())
50 << mysqlpp::String(to_string<int>(newTime,std::dec))
51 << mysqlpp::String(to_string<int>(newNrq,std::dec))
52 << mysqlpp::String(res[i]["timefst"])
53 << mysqlpp::String(to_string<long>(elem->getTimestamp(),std::dec));
54 newquery.store(sqpNew);
55 }
56 }
57 }
58 } catch (const mysqlpp::BadQuery& er) {
59 /* Handles any query errors */
60 cerr << "Query error(fill): " << er.what() << endl;
61 exit(1);
62 } catch (const mysqlpp::Exception& er) {
63 // Catch-all for any other MySQL++ exceptions
64 cerr << "Error(fill): " << er.what() << endl;
65 exit(1);
bada5e2 Ulisses Costa Added Data Layer: mysql++
authored
66 }
67 }
Something went wrong with that request. Please try again.