-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Database.mqh
329 lines (299 loc) · 8.59 KB
/
Database.mqh
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* Implements Database class.
*
* The methods for working with databases uses SQLite engine.
*
* @docs https://www.mql5.com/en/docs/database
*/
// Prevents processing this includes file for the second time.
#ifndef DATABASE_MQH
#define DATABASE_MQH
// Includes.
#include "DictStruct.mqh"
#include "MiniMatrix.h"
// Enums.
enum DATABASE_COLUMN_FLAGS {
DATABASE_COLUMN_FLAG_NONE = 0,
DATABASE_COLUMN_FLAG_IS_KEY = 1,
DATABASE_COLUMN_FLAG_IS_NULL = 2,
};
// Structs.
struct DatabaseTableColumnEntry {
string name;
ENUM_DATATYPE type;
unsigned short flags;
unsigned short char_size;
// Getter methods;
string GetDatatype() {
switch (type) {
case TYPE_BOOL:
return "BOOL";
case TYPE_CHAR:
return StringFormat("CHAR(%d)", char_size);
case TYPE_DOUBLE:
return "REAL";
case TYPE_INT:
return "INT";
case TYPE_LONG:
return "LONG";
case TYPE_STRING:
return "TEXT";
}
return "UNKNOWN";
}
string GetFlags() { return GetKey() + " " + GetNull(); }
string GetName() { return name; }
string GetNull() { return !IsNull() ? "NOT NULL" : ""; }
string GetKey() { return IsKey() ? "KEY" : ""; }
// State methods.
bool IsKey() { return bool(flags & DATABASE_COLUMN_FLAG_IS_KEY); }
bool IsNull() { return bool(flags & DATABASE_COLUMN_FLAG_IS_NULL); }
};
struct DatabaseTableSchema {
DictStruct<short, DatabaseTableColumnEntry> columns;
// Constructor.
DatabaseTableSchema() {}
DatabaseTableSchema(DatabaseTableColumnEntry &_columns[]) {
for (int i = 0; i < ArraySize(_columns); i++) {
columns.Push(_columns[i]);
}
}
// Methods.
bool AddColumn(DatabaseTableColumnEntry &column) { return columns.Push(column); }
};
// Struct table entry for SymbolInfo.
#ifdef SYMBOLINFO_MQH
struct DbSymbolInfoEntry : public SymbolInfoEntry {
DatabaseTableSchema schema;
// Constructors.
DbSymbolInfoEntry() { DefineSchema(); }
DbSymbolInfoEntry(const MqlTick &_tick, const string _symbol = NULL) : SymbolInfoEntry(_tick, _symbol) {
DefineSchema();
}
// Methods.
void DefineSchema() {
DatabaseTableColumnEntry _columns[] = {
{"bid", TYPE_DOUBLE}, {"ask", TYPE_DOUBLE}, {"last", TYPE_DOUBLE},
{"spread", TYPE_DOUBLE}, {"volume", TYPE_INT},
};
for (int i = 0; i < ArraySize(_columns); i++) {
schema.columns.Push(_columns[i]);
}
}
};
#endif
class Database {
private:
int handle;
DictStruct<string, DatabaseTableSchema> tables;
public:
/**
* Class constructor.
*/
#ifndef __MQL4__
Database(string _filename, unsigned int _flags = DATABASE_OPEN_CREATE){
#else
Database(string _filename, unsigned int _flags = 0) {
#endif
#ifndef __MQL4__
handle = DatabaseOpen(_filename, _flags);
#else
handle = -1;
SetUserError(ERR_USER_NOT_SUPPORTED);
#endif
}
/**
* Class deconstructor.
*/
~Database() {
#ifndef __MQL4__
DatabaseClose(handle);
#endif
}
/* Table methods */
/**
* Checks if table exists.
*/
bool TableExists(string _name) {
#ifndef __MQL4__
return DatabaseTableExists(handle, _name);
#else
SetUserError(ERR_USER_NOT_SUPPORTED);
return false;
#endif
}
/**
* Creates table if not yet exist.
*/
bool CreateTableIfNotExist(string _name, DatabaseTableSchema &_schema) {
if (TableExists(_name)) {
return true;
}
return CreateTable(_name, _schema);
}
/**
* Creates table.
*/
bool CreateTable(string _name, DatabaseTableSchema &_schema) {
bool _result = false;
#ifndef __MQL4__
if (DatabaseTableExists(handle, _name)) {
// Generic error (ERR_DATABASE_ERROR).
SetUserError(5601);
return _result;
}
string query = "", subquery = "";
if (_schema.columns.Size() == 0) {
// SQLite does'nt allow tables without columns;
subquery = "`dummy` INTEGER";
} else {
for (DictStructIterator<short, DatabaseTableColumnEntry> iter = _schema.columns.Begin(); iter.IsValid(); ++iter) {
subquery +=
StringFormat("`%s` %s %s,", iter.Value().GetName(), iter.Value().GetDatatype(), iter.Value().GetFlags());
}
subquery = StringSubstr(subquery, 0, StringLen(subquery) - 1); // Removes extra comma.
}
query = StringFormat("CREATE TABLE `%s`(%s);", _name, subquery);
#ifdef __debug__
Print("Database: Executing query:\n", query);
#endif
if (_result = DatabaseExecute(handle, query)) {
ResetLastError();
SetTableSchema(_name, _schema);
} else {
#ifdef __debug__
Print("Database: Query failed with error ", _LastError);
DebugBreak();
#endif
}
#endif
return _result;
}
/**
* Drops table.
*/
bool DropTable(string _name) {
tables.Unset(_name);
#ifndef __MQL4__
return DatabaseExecute(handle, "DROP TABLE IF EXISTS `" + _name + "`");
#else
return false;
#endif
}
/* Import methods */
/**
* Imports data into table. First row must contain column names. Strings must be enclosed with double quotes.
*/
bool ImportData(const string _name, MiniMatrix2d<string> &data) {
if (data.SizeY() < 2 || data.SizeX() == 0) {
// No data to import or there are no columns in input data (Serialize() serialized no fields).
return true;
}
int x;
bool _result = true;
DatabaseTableSchema _schema = GetTableSchema(_name);
string _query = "", _cols = "", _vals = "";
for (x = 0; x < data.SizeX(); ++x) {
const string key = data.Get(x, 0);
_cols += "`" + StringSubstr(key, 1, StringLen(key) - 2) + "`,";
}
_cols = StringSubstr(_cols, 0, StringLen(_cols) - 1); // Removes extra comma.
#ifndef __MQL4__
if (DatabaseTransactionBegin(handle)) {
_query = StringFormat("INSERT INTO `%s`(%s) VALUES\n", _name, _cols);
for (int y = 1; y < data.SizeY(); ++y) {
_query += "(";
for (x = 0; x < data.SizeX(); ++x) {
_query += data.Get(x, y) + (x < data.SizeX() - 1 ? ", " : "");
}
_query += ")" + (y < data.SizeY() - 1 ? ",\n" : "");
}
#ifdef __debug__
Print("Database: Executing query:\n", _query);
#endif
_result &= DatabaseExecute(handle, _query);
}
if (_result) {
DatabaseTransactionCommit(handle);
} else {
Print("Database: Query failed with error ", _LastError);
DebugBreak();
DatabaseTransactionRollback(handle);
}
#else
return false;
#endif
return _result;
}
#ifdef BUFFER_STRUCT_MQH
/**
* Imports BufferStruct records into a table.
*/
template <typename TStruct>
bool Import(const string _name, BufferStruct<TStruct> &_bstruct) {
bool _result = true;
DatabaseTableSchema _schema = GetTableSchema(_name);
string _query = "", _cols = "", _vals = "";
for (DictStructIterator<short, DatabaseTableColumnEntry> iter = _schema.columns.Begin(); iter.IsValid(); ++iter) {
_cols += iter.Value().name + ",";
}
_cols = StringSubstr(_cols, 0, StringLen(_cols) - 1); // Removes extra comma.
#ifndef __MQL4__
if (DatabaseTransactionBegin(handle)) {
for (DictStructIterator<long, TStruct> iter = _bstruct.Begin(); iter.IsValid(); ++iter) {
_query = StringFormat("INSERT INTO %s(%s) VALUES (%s)", _name, _cols, iter.Value().ToCSV());
_result &= DatabaseExecute(handle, _query);
}
}
if (_result) {
DatabaseTransactionCommit(handle);
} else {
DatabaseTransactionRollback(handle);
}
#else
return false;
#endif
return _result;
}
#endif
/* Getters */
/**
* Gets database handle.
*/
int GetHandle() { return handle; }
/**
* Gets table schema.
*/
DatabaseTableSchema GetTableSchema(string _name) { return tables.GetByKey(_name); }
/**
* Checks if table schema exists.
*/
bool SchemaExists(string _name) { return tables.KeyExists(_name); }
/* Setters */
/**
* Sets table schema.
*/
bool SetTableSchema(string _name, DatabaseTableSchema &_schema) { return tables.Set(_name, _schema); }
}
;
#endif // DATABASE_MQH