-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlinterpreter.cpp
216 lines (165 loc) · 4.99 KB
/
sqlinterpreter.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "sqlinterpreter.h"
#include <list>
void SQLInterpreter::__compile(const char separator, std::list<QString>& paramList, QString& query, const std::string & buff) const noexcept
{
//Starting new line so make it to defaults
QString temp = "";
paramList.clear();
//Detecting all params
for(quint32 i = 0; i < buff.length(); i++)
{
if(buff[i] == separator)
{
//cout << this << "compile() -> Detected: " << temp;
temp.replace(" ", "");
paramList.push_back(temp);
temp = "";
}else if(buff[i] == '\n' || buff[i] == '\r') continue; else temp+=buff[i];
}
//Replacing single params and creating allParams
QString allParams = "";
quint8 i = 1;
for(auto var = paramList.begin();var != paramList.end(); var++)
{
//Replacing params
temp = "::" + QString((__mSymbols.size()+1 > 9) ? "x" : "") + QString::number(i);
query.replace(temp, *var);
//Generating allParams
allParams += "'" + (*var) +"'";
if(i != __mSymbols.size()) allParams+= ", ";
i++;
}
//Replacing 'paste all paramas'
query.replace("::0", allParams);
//cout <<this << "compile() query: "<<query;
}
SQLInterpreter::~SQLInterpreter()
{
__mSymbols.clear();
__mSrc = nullptr;
}
void SQLInterpreter::operator delete(void * src)
{
//Have to be sure
auto* var = static_cast<SQLInterpreter*>(src);
var->~SQLInterpreter();
free(src);
}
SQLInterpreter::SQLInterpreter(const QString& iInput, const QString& iOutput, QTextEdit * iSrc, QObject * parent) noexcept
:__mInputPath{iInput}, __mOutputPath{iOutput}, __mSrc{iSrc}, QObject{parent}
{
//cout << this << " Created!";
std::ifstream * input = new std::ifstream{iInput.toStdString()};
if(!input->good())
{
//cout <<this << " Cannot open: " << iInput;
input->close();
delete input;
return;
}
//Gathering info from first row
std::string linia;
std::getline(*input, linia);
input->close();
QString temp = "";
//Choose separator recomended semicolon, because yes
const char separator = linia[0];
//Creating index od shorts and column names
for(quint32 i = 1; i < linia.length(); i++)
{
if(linia[i] != separator)
temp += linia[i];
else
{
//cout << this << "Adding following column: " << temp;
__mSymbols["::"+ QString((__mSymbols.size()+1 > 9) ? "x" : "") + QString::number(__mSymbols.size()+1)] = temp;
temp = "";
}
}
//cout << this << "Finished adding. Emitting...";
//Write on screen when ready avaiable shorts
isOk = true;
}
bool SQLInterpreter::save()
{
//Open locations
std::list<QString> paramList;
//Trying to opopen source file
std::ifstream input(__mInputPath.toStdString());
if(!input.good()) { input.close(); return false; }
//Trying to create (or recreate) destination file
std::ofstream output(__mOutputPath.toStdString());
if(!output.good()) { output.close(); return false; }
//std::getline result
std::string buff;
//getting defaults
QString patternSQL = __mSrc->toPlainText();
QString query;
//cout << this << " PatterSQL: "<<patternSQL;
//First line is config so let's get a separator
std::getline(input, buff);
const char separator = buff[0];
//Reading line by line
while (std::getline(input, buff))
{
//Skip bad lines
if(buff == "" || buff == "\n" || buff == " " || buff == "\r") continue;
//it would be very *plum* NOIZZ if somebody write it at the bottom of txt :)
if(buff[0] == '!' && buff[1] == 'E' && buff[2] == 'N' && buff[3] == 'D')
{
break;
};
//Forgotten separator correction
if(buff[buff.size()-1] != separator) buff+=separator;
query = patternSQL;
//Processing input
__compile(separator, paramList, query, buff);
//Save it
output << query.toStdString() << "\n";
}
//Closing stuff
input.close();
output.close();
return true;
}
QString SQLInterpreter::example()
{
//cout << this << "Starting example";
//Open locations
std::list<QString> paramList;
//Trying to opopen source file
std::ifstream input(__mInputPath.toStdString());
if(!input.good()) { input.close(); return QString(); }
std::string buff;
//getting defaults
QString query = __mSrc->toPlainText();
//cout << this << " PatterSQL: "<<query;
//First line is config so let's get a separator
std::getline(input, buff);
const char separator = buff[0];
//Searching for nice, one, little line
while (std::getline(input, buff))
{
//cout << this << "std::getline(input, buff) -> result: "<<QString(buff.c_str());
if(buff == "" || buff == "\n" || buff == " " || buff == "\r") continue; //Skip bad lines
else
if(buff[0] == '!' && buff[1] == 'E' && buff[2] == 'N' && buff[3] == 'D') return QString(); //it would be very *plum* NOIZZ if somebody writ it at the bottom of txt :)
else break; //If line is not empty break;
}
//Forgotten separator correction
if(buff[buff.size()-1] != separator) buff+=separator;
//Processing input
__compile(separator, paramList, query, buff);
//Closing stuff
input.close();
//Save it
return query;
}
size_t SQLInterpreter::howManyColumns() const
{
return __mSymbols.size();
}
QString SQLInterpreter::operator[](const QString src) const
{
return __mSymbols[src];
}