DB_Track/source/SQLHandler.cpp

128 lines
4.5 KiB
C++

//
// SQLHandler.cpp
// DB_Track
//
// Created by Baranyai David on 2018. 10. 13..
//
#include "SQLHandler.hpp"
SQLHandler::SQLHandler()
{
latest_value = 0;
range = 9000000000;
min_hits = 4; //need at least 4 hits
server = mysql_init(server);
try
{
if (!mysql_real_connect(server, "localhost", "root", "raspberry", "adatok", 0, NULL, 0))
{
throw "Can't connect to the server";
}
else
{
std::cout << "-----------------------------" << std::endl;
std::cout << "Successfully connected" << std::endl;
std::cout << "Host info: " << mysql_get_host_info(server) << std::endl;
std::cout << "Client info: " << mysql_get_client_info() << std::endl;
std::cout << "-----------------------------" << std::endl << std::endl;
std::cout << "Getting the first value..." << std::endl;
std::string query = "SELECT min(time_sum) FROM (SELECT *, (time_sec * (power(10, 7)) + time_ns) time_sum FROM fb55) sum";
if (!mysql_query(server, query.c_str()))
{
MYSQL_RES *result = mysql_store_result(server);
if (mysql_num_fields(result) == 1) //1 oszlop
{
MYSQL_ROW row;
if(mysql_num_rows(result) == 1) //1 érték
{
row = mysql_fetch_row(result);
latest_value = std::stoll(row[0]);
std::cout << "Min value = " << latest_value <<std::endl;
}
else throw "More than one row error";
}
else throw "More than one column error";
}
}
} catch (char const* s)
{
std::cout << s <<std::endl;
}
}
SQLHandler::~SQLHandler()
{
mysql_close(server);
delete server;
}
//MAIN QUERY:
//SELECT * FROM (SELECT ch_id, time_sec, time_ns, (time_sec * (power(10, 7)) + time_ns) time_sum FROM fb55) s WHERE time_sum BETWEEN 2521025900 AND 2570247800 ORDER BY time_sum ASC
std::vector<Hit> SQLHandler::QueryNext()
{
try
{
std::cout << "Getting next between " << latest_value << " and " << latest_value + range << std::endl;
std::stringstream query;
unsigned int nRow = 0;
std::vector<Hit> hit_vector;
while(nRow < min_hits)
{
hit_vector.clear();
double max = latest_value + range;
query << "SELECT * FROM (SELECT ch_id, peak_amp, (time_sec * (power(10, 7)) + time_ns) time_sum FROM fb55) s WHERE time_sum BETWEEN " << latest_value << " AND " << max << " ORDER BY time_sum ASC";
if (!mysql_query(server, query.str().c_str())) //Start query
{
MYSQL_RES *result = mysql_store_result(server); //store result
unsigned int nColumn = mysql_num_fields(result); //store column number
if (nColumn == 3) //3 oszlop, se több, se kevesebb
{
MYSQL_ROW row;
nRow = mysql_num_rows(result);
if(nRow > min_hits) //at least
{
Hit hit_helper;
for(int i = 0; i < nRow; i++)
{
row = mysql_fetch_row(result);
hit_helper.channel_id = atoi(row[0]);
hit_helper.amplitude_peak = atoi(row[1]);
hit_helper.elapsed_time = std::stoll(row[2]);
hit_vector.push_back(hit_helper);
}
latest_value = atof(row[2]);
//std::cout << "Min value = " << latest_value <<std::endl;
return hit_vector;
}
else if(nRow != 0)
{
for(int i = 0; i < nRow; i++) //seek to the end
{
row = mysql_fetch_row(result);
}
latest_value = atoi(row[2]); //store the latest value
}
}
else throw "More than three column error";
}
}
return hit_vector;
}
catch (char const* s)
{
std::cout << s << std::endl;
return std::vector<Hit>();
}
}
void SQLHandler::SetRange(unsigned int n_range)
{
range = n_range;
}