-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_functions.cpp
More file actions
175 lines (149 loc) · 5.36 KB
/
Copy pathsql_functions.cpp
File metadata and controls
175 lines (149 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "sql_functions.h"
//bool sql_functions::stop_light = false;
sql_functions::sql_functions(MYSQL * conn){
this->connection = conn;
}
bool sql_functions::query_error(std::string cmd_query){
int err = mysql_query(this->connection,cmd_query.c_str());//this query will return a nonZero value in case of error
if (err!=0){
if(mysql_errno(this->connection)!=2014){ //if the error it's not produced by the fact that 2 clients try to execute a query at the same time
return false;
}
}
/* Error number: 2014; Symbol: CR_COMMANDS_OUT_OF_SYNC;
Message: Commands out of sync; you can't run this command now
This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result() .
*/
while (mysql_errno(this->connection)==2014){//we wait, until it's made mysql_free() of the query of the other
//Very unlikely to enter in this while
mysql_query(this->connection,cmd_query.c_str());
}
return true;
}
MYSQL_RES* sql_functions::execute_query (){
//send the querry to the database
return (mysql_use_result(this->connection));
}
std::string sql_functions::get_result_of_the_query(std::string query){
MYSQL_RES *result;
MYSQL_ROW row; // the results row (line by line)
MYSQL_FIELD *field; //contains information about the table's header
int number_fields; //numbr of columns which are 'printed' by the query
std::string res="";
if( this->query_error(query.c_str()) == false ){//if is was some error at the execution of the query
printf("the error code is : %d \n", mysql_errno(this->connection));
char error[1024];
sprintf(error,"ERROR :%s\n",mysql_error(this->connection) );
return error;
}
//if the query executed just fine;
std::string first_word_from_quey = query.substr(0,6);
if(first_word_from_quey!="UPDATE" && first_word_from_quey!="INSERT" && first_word_from_quey!="DELETE"){
result = this->execute_query();
number_fields = mysql_num_fields(result);
while ((row=mysql_fetch_row(result)) != NULL){ //row gets one by one line from the result;
for( int i=0 ; i<number_fields; ++i ) {//
if(i==0){
while((field = mysql_fetch_field(result))!= NULL){//this resets once a query is again executed
res=res + (field->name) + "|";
}
res = res + "\n";
}
res = res + (row[i]? row[i] : "NULL" ) + "|";
}//for
}//while
mysql_free_result(result);//clean up the result of the query
if(res == ""){
return res;
}
else if (res.at(0) == 'c'){
return res;
}
else{
return format_response(res,number_fields);
}
}
else if(first_word_from_quey=="UPDATE"){
return "The update of the delay was successfully made!";
}
else if (first_word_from_quey=="INSERT"){
return "The insert of the train was successfully made!";
}
else{
return "The train deleted successfully!";
}
}
std::string sql_functions::format_response(std::string response, int nr_col){
/* EX:
nume_col1|nume_col2|nume_col3|
abcdsdf|fdsf|a64|
bunaziuaa|numelemeu este|cossssmin
-->
+----------+---------------+-----------+
|nume_col1 |nume_col2 |nume_col3S |
+----------+---------------+-----------+
|abcdsdf |fdsf |a64 |
|bunaziuaa |numelemeu este |cossssmin |
*/
std::string response_formatat = "";
std::string aux_res = response ;
int max_chars_in_each_coloumn[]={1,1,1,1,1,1,1,1,1,1};//we know that our DB doesn t use more that 10 columns
int k = 0 ;
while(aux_res.find("|")!= std::string::npos){
int poz = aux_res.find("|");
int nr_chr = (aux_res.substr(0,poz)).length();
if(aux_res.substr(0,1) != "\n")
nr_chr++;
if ( max_chars_in_each_coloumn[k] < nr_chr ){
max_chars_in_each_coloumn[k] = nr_chr;
}
k++;
k = k % nr_col;
aux_res.erase(0,poz+1);
}
//first line of ---
response_formatat = "+";
for( int i = 0 ; i< nr_col ; ++i){
std::string s(max_chars_in_each_coloumn[i],'-');
response_formatat += s + "+";
}
//line of columns
response_formatat += "\n|";
for( int i = 0 ; i< nr_col ; ++i){
int poz = response.find("|");
std::string word = response.substr(0,poz);
std::string s(max_chars_in_each_coloumn[i] - word.length(),' ');
response_formatat += word + s + "|";
response.erase(0,poz+1);
}
response_formatat += "\n+";
//second line of ----
for( int i = 0 ; i< nr_col ; ++i){
std::string s(max_chars_in_each_coloumn[i],'-');
response_formatat += s + "+";
}
response_formatat += "\n|";
k=0;
// we insert each word + the space
while(response.find("|")!= std::string::npos){
if (response.at(0)=='\n')
response.erase(0,1);
int poz = response.find("|");
std::string word =response.substr(0,poz);
int nr_chr = word.length();
std::string space(max_chars_in_each_coloumn[k]-nr_chr,' ');
k++;
response_formatat += word + space + "|";
response.erase(0,poz+1);
if( k == nr_col && response.find("|")!= std::string::npos)
response_formatat += "\n|";
k = k % nr_col;
}
//last line of ---
response_formatat += "\n+";
for( int i = 0 ; i< nr_col ; ++i){
std::string s(max_chars_in_each_coloumn[i],'-');
response_formatat += s + "+";
}
return response_formatat;
}