Repository navigation
Expand file tree
/
Copy pathSqlFile.mqh
More file actions
256 lines (235 loc) · 6.23 KB
/
Copy pathSqlFile.mqh
File metadata and controls
256 lines (235 loc) · 6.23 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* https://github.com/Roffild/RoffildLibrary
*/
/**
* A base class for generating data as an array.
*/
class CSqlObject
{
public:
/**
* Create a table.
* @param[out] types [x][0] = name, [x][1] = type
* @param[out] primaryKeys https://en.wikipedia.org/wiki/Primary_key
* @return True if the data is formed, and otherwise False.
*/
virtual bool toSqlTable(string &types[][2], string &primaryKeys[])
{
return false;
}
/**
* Forming data for writing to a table.
* @param[out] list [x][0] = name, [x][1] = data
* @return True if the data is formed, and otherwise False.
*/
virtual bool toSqlRecord(string &list[][2])
{
return false;
}
};
/**
* This class stores the result of the class inherited from CSqlObject into the array.
* Since the formation of data is done using functions,
* the moment of data formation can be much later than expected.
*/
class CSqlObjectSnapShot : CSqlObject
{
protected:
string snap_types[][2];
string snap_primaryKeys[];
bool snap_table;
string snap_list[][2];
bool snap_record;
public:
CSqlObjectSnapShot(CSqlObject &sqlobject)
{
snap_table = sqlobject.toSqlTable(snap_types, snap_primaryKeys);
snap_record = sqlobject.toSqlRecord(snap_list);
}
virtual bool toSqlTable(string &types[][2], string &primaryKeys[])
{
ArrayResize(types, ArrayRange(snap_types, 0));
ArrayResize(primaryKeys, ArrayRange(snap_primaryKeys, 0));
ArrayCopy(types, snap_types);
ArrayCopy(primaryKeys, snap_primaryKeys);
return snap_table;
}
virtual bool toSqlRecord(string &list[][2])
{
ArrayResize(list, ArrayRange(snap_list, 0));
ArrayCopy(list, snap_list);
return snap_record;
}
};
/**
* Write data to a file format MySQL.
*/
class CSqlFile
{
protected:
int hsql;
string table;
string schema;
string filename;
uint transaction_count;
private:
void classInit()
{
hsql = INVALID_HANDLE;
table = "";
schema = "forex";
filename = "";
transaction_count = 0;
}
public:
int getHandle()
{
return hsql;
}
string getFileName()
{
return filename;
}
string getTable()
{
return table;
}
void setTable(string _table)
{
table = _table;
}
string getSchema()
{
return schema;
}
void setSchema(string _schema)
{
schema = _schema;
}
public:
CSqlFile()
{
classInit();
}
CSqlFile(string _table, bool _append, string _schema = "forex", string _filesuffix = "")
{
classInit();
createFile(_table, _append, _schema, _filesuffix);
}
bool createFile(string _table, bool _append, string _schema = "forex", string _filesuffix = "")
{
table = MQLInfoString(MQL_PROGRAM_NAME) + "_" + _table;
StringReplace(table, " ", "_");
schema = _schema;
filename = schema + "_" + table + _filesuffix + ".sql";
int flags = FILE_TXT|FILE_WRITE|FILE_ANSI|FILE_SHARE_READ|FILE_COMMON;
if (_append) {
flags |= FILE_READ;
}
hsql = FileOpen("SQL/" + filename, flags);
if (_append) {
FileSeek(hsql, 0, SEEK_END);
}
return hsql != INVALID_HANDLE;
}
~CSqlFile()
{
close();
}
void close()
{
if (hsql != INVALID_HANDLE) {
transaction(true);
FileClose(hsql);
hsql = INVALID_HANDLE;
}
}
bool createTable(CSqlObject &sqlobject, bool drop = true)
{
if (hsql == INVALID_HANDLE) {
return false;
}
string types[][2];
string primaryKeys[];
if (sqlobject.toSqlTable(types, primaryKeys) == false) {
return false;
}
if (drop) {
FileWrite(hsql, "DROP TABLE IF EXISTS `", schema, "`.`", table, "`;");
}
FileWrite(hsql, "CREATE TABLE IF NOT EXISTS `", schema, "`.`", table, "` (");
const int typeslast = ArrayRange(types, 0) - 1;
for (int x = 0; x < typeslast; x++) {
FileWrite(hsql, " `", types[x][0], "` ", types[x][1], ",");
}
FileWrite(hsql, " `", types[typeslast][0], "` ", types[typeslast][1]);
for (int x = 0, xc = ArrayRange(primaryKeys, 0); x < xc; x++) {
FileWrite(hsql, " ,PRIMARY KEY (`", primaryKeys[x], "`)");
}
FileWrite(hsql, ") DEFAULT CHARSET=utf8;");
return true;
}
bool insert(CSqlObject &sqlobject)
{
if (hsql == INVALID_HANDLE) {
return false;
}
transaction();
string list[][2];
if (sqlobject.toSqlRecord(list) == false) {
return false;
}
string type = "`" + list[0][0] + "`";
for (int x = 1, xc = ArrayRange(list, 0); x < xc; x++) {
type += ",`" + list[x][0] + "`";
}
string record = "'" + getString(list[0][1]) + "'";
for (int x = 1, xc = ArrayRange(list, 0); x < xc; x++) {
record += ",'" + getString(list[x][1]) + "'";
}
FileWrite(hsql, "INSERT INTO `", schema, "`.`", table, "`");
FileWrite(hsql, " (", type, ")");
FileWrite(hsql, " VALUES(", record, ");");
return true;
}
string getString(string rec)
{
if (rec == "false") {
return "0";
}
if (rec == "true") {
return "1";
}
return rec;
}
void transaction(bool commit = false)
{
if (hsql == INVALID_HANDLE) {
return;
}
if (transaction_count == 0) {
FileWrite(hsql, "START TRANSACTION;");
}
transaction_count++;
if (transaction_count > 500 || commit) {
FileWrite(hsql, "COMMIT;");
transaction_count = 0;
if (commit == false) {
FileWrite(hsql, "START TRANSACTION;");
transaction_count++;
}
}
}
};