trader  v0.1a
A framework to build trading applications
genericdatabase.h
1 
2 #pragma once
3 
4 
5 namespace trader {
6 
7  namespace GenericDatabase {
8 
9  class Trade_History : public Poco::RefCountedObject {
10  public:
11 
12  struct Record {
13 
14  //
15  Poco::Int32 tradeId;
16 
17  bool isSetTradeId() {
18  return (tradeId != std::numeric_limits<Poco::Int32>::max());
19  }
20 
21  // Current Unix Timestamp
22  Poco::Int32 timeStamp;
23 
24  bool isSetTimeStamp() {
25  return (timeStamp != std::numeric_limits<Poco::Int32>::max());
26  }
27 
28  //
29  double volume;
30 
31  bool isSetVolume() {
32  return (volume != std::numeric_limits<double>::max());
33  }
34 
35  //
36  double price;
37 
38  bool isSetPrice() {
39  return (price != std::numeric_limits<double>::max());
40  }
41 
42  //
43  double total;
44 
45  bool isSetTotal() {
46  return (total != std::numeric_limits<double>::max());
47  }
48 
49  //
50  std::string fillType;
51 
52  bool isSetFillType() {
53  return (fillType != "Empty");
54  }
55 
56  //
57  std::string orderType;
58 
59  bool isSetOrderType() {
60  return (orderType != "Empty");
61  }
62 
63  Record()
64  : tradeId(std::numeric_limits<Poco::Int32>::max())
65  , timeStamp(std::numeric_limits<Poco::Int32>::max())
66  , volume(std::numeric_limits<double>::max())
67  , price(std::numeric_limits<double>::max())
68  , total(std::numeric_limits<double>::max())
69  , fillType("Empty")
70  , orderType("Empty")
71  {
72  }
73 
74  };
75 
76 
77  struct RecordWithId {
78 
79  //
80  Poco::Int32 tradeId;
81 
82  bool isSetTradeId() {
83  return (tradeId != std::numeric_limits<Poco::Int32>::max());
84  }
85 
86  // Current Unix Timestamp
87  Poco::Int32 timeStamp;
88 
89  bool isSetTimeStamp() {
90  return (timeStamp != std::numeric_limits<Poco::Int32>::max());
91  }
92 
93  //
94  double volume;
95 
96  bool isSetVolume() {
97  return (volume != std::numeric_limits<double>::max());
98  }
99 
100  //
101  double price;
102 
103  bool isSetPrice() {
104  return (price != std::numeric_limits<double>::max());
105  }
106 
107  //
108  double total;
109 
110  bool isSetTotal() {
111  return (total != std::numeric_limits<double>::max());
112  }
113 
114  //
115  std::string fillType;
116 
117  bool isSetFillType() {
118  return (fillType != "Empty");
119  }
120 
121  //
122  std::string orderType;
123 
124  bool isSetOrderType() {
125  return (orderType != "Empty");
126  }
127 
128  //Record ID in database
129  Poco::Int32 id;
130 
131  RecordWithId()
132  : tradeId(std::numeric_limits<Poco::Int32>::max())
133  , timeStamp(std::numeric_limits<Poco::Int32>::max())
134  , volume(std::numeric_limits<double>::max())
135  , price(std::numeric_limits<double>::max())
136  , total(std::numeric_limits<double>::max())
137  , fillType("Empty")
138  , orderType("Empty")
139  , id(std::numeric_limits<Poco::Int32>::max())
140  {
141  }
142 
143  };
144 
145 
146  Trade_History(Poco::Data::Session* _db, std::string _suffix = "");
147 
148  ~Trade_History();
149 
150  void init();
151 
152  void clear();
153 
154  void insert(Trade_History::Record& record);
155 
156  void insertOnce(Trade_History::Record& record);
157 
158  void insertMultiple(std::vector<Trade_History::Record>& records);
159 
160  void insertMultiple(std::vector<Trade_History::RecordWithId>& records);
161 
162  void insertMultipleUnique(std::vector<Trade_History::Record>& records);
163 
164  void deleteMultiple(std::vector<Trade_History::RecordWithId>& records);
165 
166  void insertAndDeleteUnchanged(Trade_History::Record& record);
167 
168  void insertUnique(Trade_History::Record& record);
169 
170  void getLatest(Trade_History::RecordWithId& rec);
171 
172  std::size_t getAll(std::vector<Trade_History::RecordWithId>& records, std::string condition);
173 
174  Poco::Data::Session* db;
175 
176  std::string tableName;
177 
178  }; //Trade_History
179 
180  class Market_List : public Poco::RefCountedObject {
181  public:
182 
183  struct Record {
184 
185  //
186  Poco::Int32 created;
187 
188  bool isSetCreated() {
189  return (created != std::numeric_limits<Poco::Int32>::max());
190  }
191 
192  //
193  std::string marketName;
194 
195  bool isSetMarketName() {
196  return (marketName != "Empty");
197  }
198 
199  //
200  std::string marketCurrency;
201 
202  bool isSetMarketCurrency() {
203  return (marketCurrency != "Empty");
204  }
205 
206  //
207  std::string baseCurrency;
208 
209  bool isSetBaseCurrency() {
210  return (baseCurrency != "Empty");
211  }
212 
213  //
214  std::string marketCurrencyLong;
215 
216  bool isSetMarketCurrencyLong() {
217  return (marketCurrencyLong != "Empty");
218  }
219 
220  //
221  double baseCurrencyLong;
222 
223  bool isSetBaseCurrencyLong() {
224  return (baseCurrencyLong != std::numeric_limits<double>::max());
225  }
226 
227  //
228  double minTradeSize;
229 
230  bool isSetMinTradeSize() {
231  return (minTradeSize != std::numeric_limits<double>::max());
232  }
233 
234  Record()
235  : created(std::numeric_limits<Poco::Int32>::max())
236  , marketName("Empty")
237  , marketCurrency("Empty")
238  , baseCurrency("Empty")
239  , marketCurrencyLong("Empty")
240  , baseCurrencyLong(std::numeric_limits<double>::max())
241  , minTradeSize(std::numeric_limits<double>::max())
242  {
243  }
244 
245  };
246 
247 
248  struct RecordWithId {
249 
250  //
251  Poco::Int32 created;
252 
253  bool isSetCreated() {
254  return (created != std::numeric_limits<Poco::Int32>::max());
255  }
256 
257  //
258  std::string marketName;
259 
260  bool isSetMarketName() {
261  return (marketName != "Empty");
262  }
263 
264  //
265  std::string marketCurrency;
266 
267  bool isSetMarketCurrency() {
268  return (marketCurrency != "Empty");
269  }
270 
271  //
272  std::string baseCurrency;
273 
274  bool isSetBaseCurrency() {
275  return (baseCurrency != "Empty");
276  }
277 
278  //
279  std::string marketCurrencyLong;
280 
281  bool isSetMarketCurrencyLong() {
282  return (marketCurrencyLong != "Empty");
283  }
284 
285  //
286  double baseCurrencyLong;
287 
288  bool isSetBaseCurrencyLong() {
289  return (baseCurrencyLong != std::numeric_limits<double>::max());
290  }
291 
292  //
293  double minTradeSize;
294 
295  bool isSetMinTradeSize() {
296  return (minTradeSize != std::numeric_limits<double>::max());
297  }
298 
299  //Record ID in database
300  Poco::Int32 id;
301 
302  RecordWithId()
303  : created(std::numeric_limits<Poco::Int32>::max())
304  , marketName("Empty")
305  , marketCurrency("Empty")
306  , baseCurrency("Empty")
307  , marketCurrencyLong("Empty")
308  , baseCurrencyLong(std::numeric_limits<double>::max())
309  , minTradeSize(std::numeric_limits<double>::max())
310  , id(std::numeric_limits<Poco::Int32>::max())
311  {
312  }
313 
314  };
315 
316 
317  Market_List(Poco::Data::Session* _db, std::string _suffix = "");
318 
319  ~Market_List();
320 
321  void init();
322 
323  void clear();
324 
325  void insert(Market_List::Record& record);
326 
327  void insertOnce(Market_List::Record& record);
328 
329  void insertMultiple(std::vector<Market_List::Record>& records);
330 
331  void insertMultiple(std::vector<Market_List::RecordWithId>& records);
332 
333  void insertMultipleUnique(std::vector<Market_List::Record>& records);
334 
335  void deleteMultiple(std::vector<Market_List::RecordWithId>& records);
336 
337  void insertAndDeleteUnchanged(Market_List::Record& record);
338 
339  void insertUnique(Market_List::Record& record);
340 
341  void getLatest(Market_List::RecordWithId& rec);
342 
343  std::size_t getAll(std::vector<Market_List::RecordWithId>& records, std::string condition);
344 
345  Poco::Data::Session* db;
346 
347  std::string tableName;
348 
349  }; //Market_List
350 
351  class Tables : public Poco::RefCountedObject {
352  public:
353 
354  Tables(Poco::Data::Session* _db);
355 
356  ~Tables();
357 
358  void init();
359 
360  void clear();
361 
362  Poco::Data::Session* db;
363 
364  Poco::AutoPtr<Trade_History> trade_HistoryTable;
365 
366  Poco::AutoPtr<Market_List> market_ListTable;
367 
368  }; //Tables
369 
370  } //GenericDatabase
371 } //trader
372 
373 template <>
374 class Poco::Data::TypeHandler<trader::GenericDatabase::Trade_History::Record>
375 {
376  public:
377  static std::size_t size()
378  {
379  return 7;
380  }
381 
382  static void bind(std::size_t pos, const trader::GenericDatabase::Trade_History::Record& record, Poco::Data::AbstractBinder::Ptr pBinder, Poco::Data::AbstractBinder::Direction dir)
383  {
384  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.tradeId , pBinder, dir);
385  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.timeStamp , pBinder, dir);
386  Poco::Data::TypeHandler<double>::bind(pos++, record.volume , pBinder, dir);
387  Poco::Data::TypeHandler<double>::bind(pos++, record.price , pBinder, dir);
388  Poco::Data::TypeHandler<double>::bind(pos++, record.total , pBinder, dir);
389  Poco::Data::TypeHandler<std::string>::bind(pos++, record.fillType , pBinder, dir);
390  Poco::Data::TypeHandler<std::string>::bind(pos++, record.orderType , pBinder, dir);
391  }
392 
393  static void extract(std::size_t pos, trader::GenericDatabase::Trade_History::Record& record, trader::GenericDatabase::Trade_History::Record& deflt, Poco::Data::AbstractExtractor::Ptr pExtr)
394  {
395  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.tradeId, deflt.tradeId, pExtr);
396  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.timeStamp, deflt.timeStamp, pExtr);
397  Poco::Data::TypeHandler<double>::extract(pos++, record.volume, deflt.volume, pExtr);
398  Poco::Data::TypeHandler<double>::extract(pos++, record.price, deflt.price, pExtr);
399  Poco::Data::TypeHandler<double>::extract(pos++, record.total, deflt.total, pExtr);
400  Poco::Data::TypeHandler<std::string>::extract(pos++, record.fillType, deflt.fillType, pExtr);
401  Poco::Data::TypeHandler<std::string>::extract(pos++, record.orderType, deflt.orderType, pExtr);
402  }
403 
404  static void prepare(std::size_t pos, trader::GenericDatabase::Trade_History::Record& record, Poco::Data::AbstractPreparator::Ptr pPrep)
405  {
406  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.tradeId, pPrep);
407  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.timeStamp, pPrep);
408  Poco::Data::TypeHandler<double>::prepare(pos++, record.volume, pPrep);
409  Poco::Data::TypeHandler<double>::prepare(pos++, record.price, pPrep);
410  Poco::Data::TypeHandler<double>::prepare(pos++, record.total, pPrep);
411  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.fillType, pPrep);
412  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.orderType, pPrep);
413  }
414 }
415 ;
416 
417 template <>
418 class Poco::Data::TypeHandler<trader::GenericDatabase::Trade_History::RecordWithId>
419 {
420  public:
421  static std::size_t size()
422  {
423  return 8;
424  }
425 
426  static void bind(std::size_t pos, const trader::GenericDatabase::Trade_History::RecordWithId& record, Poco::Data::AbstractBinder::Ptr pBinder, Poco::Data::AbstractBinder::Direction dir)
427  {
428  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.tradeId , pBinder, dir);
429  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.timeStamp , pBinder, dir);
430  Poco::Data::TypeHandler<double>::bind(pos++, record.volume , pBinder, dir);
431  Poco::Data::TypeHandler<double>::bind(pos++, record.price , pBinder, dir);
432  Poco::Data::TypeHandler<double>::bind(pos++, record.total , pBinder, dir);
433  Poco::Data::TypeHandler<std::string>::bind(pos++, record.fillType , pBinder, dir);
434  Poco::Data::TypeHandler<std::string>::bind(pos++, record.orderType , pBinder, dir);
435  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.id, pBinder, dir);
436  }
437 
438  static void extract(std::size_t pos, trader::GenericDatabase::Trade_History::RecordWithId& record, trader::GenericDatabase::Trade_History::RecordWithId& deflt, Poco::Data::AbstractExtractor::Ptr pExtr)
439  {
440  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.tradeId, deflt.tradeId, pExtr);
441  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.timeStamp, deflt.timeStamp, pExtr);
442  Poco::Data::TypeHandler<double>::extract(pos++, record.volume, deflt.volume, pExtr);
443  Poco::Data::TypeHandler<double>::extract(pos++, record.price, deflt.price, pExtr);
444  Poco::Data::TypeHandler<double>::extract(pos++, record.total, deflt.total, pExtr);
445  Poco::Data::TypeHandler<std::string>::extract(pos++, record.fillType, deflt.fillType, pExtr);
446  Poco::Data::TypeHandler<std::string>::extract(pos++, record.orderType, deflt.orderType, pExtr);
447  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.id, deflt.id, pExtr);
448  }
449 
450  static void prepare(std::size_t pos, trader::GenericDatabase::Trade_History::RecordWithId& record, Poco::Data::AbstractPreparator::Ptr pPrep)
451  {
452  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.tradeId, pPrep);
453  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.timeStamp, pPrep);
454  Poco::Data::TypeHandler<double>::prepare(pos++, record.volume, pPrep);
455  Poco::Data::TypeHandler<double>::prepare(pos++, record.price, pPrep);
456  Poco::Data::TypeHandler<double>::prepare(pos++, record.total, pPrep);
457  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.fillType, pPrep);
458  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.orderType, pPrep);
459  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.id, pPrep);
460  }
461 }
462 ;
463 
464 template <>
465 class Poco::Data::TypeHandler<trader::GenericDatabase::Market_List::Record>
466 {
467  public:
468  static std::size_t size()
469  {
470  return 7;
471  }
472 
473  static void bind(std::size_t pos, const trader::GenericDatabase::Market_List::Record& record, Poco::Data::AbstractBinder::Ptr pBinder, Poco::Data::AbstractBinder::Direction dir)
474  {
475  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.created , pBinder, dir);
476  Poco::Data::TypeHandler<std::string>::bind(pos++, record.marketName , pBinder, dir);
477  Poco::Data::TypeHandler<std::string>::bind(pos++, record.marketCurrency , pBinder, dir);
478  Poco::Data::TypeHandler<std::string>::bind(pos++, record.baseCurrency , pBinder, dir);
479  Poco::Data::TypeHandler<std::string>::bind(pos++, record.marketCurrencyLong , pBinder, dir);
480  Poco::Data::TypeHandler<double>::bind(pos++, record.baseCurrencyLong , pBinder, dir);
481  Poco::Data::TypeHandler<double>::bind(pos++, record.minTradeSize , pBinder, dir);
482  }
483 
484  static void extract(std::size_t pos, trader::GenericDatabase::Market_List::Record& record, trader::GenericDatabase::Market_List::Record& deflt, Poco::Data::AbstractExtractor::Ptr pExtr)
485  {
486  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.created, deflt.created, pExtr);
487  Poco::Data::TypeHandler<std::string>::extract(pos++, record.marketName, deflt.marketName, pExtr);
488  Poco::Data::TypeHandler<std::string>::extract(pos++, record.marketCurrency, deflt.marketCurrency, pExtr);
489  Poco::Data::TypeHandler<std::string>::extract(pos++, record.baseCurrency, deflt.baseCurrency, pExtr);
490  Poco::Data::TypeHandler<std::string>::extract(pos++, record.marketCurrencyLong, deflt.marketCurrencyLong, pExtr);
491  Poco::Data::TypeHandler<double>::extract(pos++, record.baseCurrencyLong, deflt.baseCurrencyLong, pExtr);
492  Poco::Data::TypeHandler<double>::extract(pos++, record.minTradeSize, deflt.minTradeSize, pExtr);
493  }
494 
495  static void prepare(std::size_t pos, trader::GenericDatabase::Market_List::Record& record, Poco::Data::AbstractPreparator::Ptr pPrep)
496  {
497  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.created, pPrep);
498  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.marketName, pPrep);
499  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.marketCurrency, pPrep);
500  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.baseCurrency, pPrep);
501  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.marketCurrencyLong, pPrep);
502  Poco::Data::TypeHandler<double>::prepare(pos++, record.baseCurrencyLong, pPrep);
503  Poco::Data::TypeHandler<double>::prepare(pos++, record.minTradeSize, pPrep);
504  }
505 }
506 ;
507 
508 template <>
509 class Poco::Data::TypeHandler<trader::GenericDatabase::Market_List::RecordWithId>
510 {
511  public:
512  static std::size_t size()
513  {
514  return 8;
515  }
516 
517  static void bind(std::size_t pos, const trader::GenericDatabase::Market_List::RecordWithId& record, Poco::Data::AbstractBinder::Ptr pBinder, Poco::Data::AbstractBinder::Direction dir)
518  {
519  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.created , pBinder, dir);
520  Poco::Data::TypeHandler<std::string>::bind(pos++, record.marketName , pBinder, dir);
521  Poco::Data::TypeHandler<std::string>::bind(pos++, record.marketCurrency , pBinder, dir);
522  Poco::Data::TypeHandler<std::string>::bind(pos++, record.baseCurrency , pBinder, dir);
523  Poco::Data::TypeHandler<std::string>::bind(pos++, record.marketCurrencyLong , pBinder, dir);
524  Poco::Data::TypeHandler<double>::bind(pos++, record.baseCurrencyLong , pBinder, dir);
525  Poco::Data::TypeHandler<double>::bind(pos++, record.minTradeSize , pBinder, dir);
526  Poco::Data::TypeHandler<Poco::Int32>::bind(pos++, record.id, pBinder, dir);
527  }
528 
529  static void extract(std::size_t pos, trader::GenericDatabase::Market_List::RecordWithId& record, trader::GenericDatabase::Market_List::RecordWithId& deflt, Poco::Data::AbstractExtractor::Ptr pExtr)
530  {
531  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.created, deflt.created, pExtr);
532  Poco::Data::TypeHandler<std::string>::extract(pos++, record.marketName, deflt.marketName, pExtr);
533  Poco::Data::TypeHandler<std::string>::extract(pos++, record.marketCurrency, deflt.marketCurrency, pExtr);
534  Poco::Data::TypeHandler<std::string>::extract(pos++, record.baseCurrency, deflt.baseCurrency, pExtr);
535  Poco::Data::TypeHandler<std::string>::extract(pos++, record.marketCurrencyLong, deflt.marketCurrencyLong, pExtr);
536  Poco::Data::TypeHandler<double>::extract(pos++, record.baseCurrencyLong, deflt.baseCurrencyLong, pExtr);
537  Poco::Data::TypeHandler<double>::extract(pos++, record.minTradeSize, deflt.minTradeSize, pExtr);
538  Poco::Data::TypeHandler<Poco::Int32>::extract(pos++, record.id, deflt.id, pExtr);
539  }
540 
541  static void prepare(std::size_t pos, trader::GenericDatabase::Market_List::RecordWithId& record, Poco::Data::AbstractPreparator::Ptr pPrep)
542  {
543  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.created, pPrep);
544  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.marketName, pPrep);
545  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.marketCurrency, pPrep);
546  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.baseCurrency, pPrep);
547  Poco::Data::TypeHandler<std::string>::prepare(pos++, record.marketCurrencyLong, pPrep);
548  Poco::Data::TypeHandler<double>::prepare(pos++, record.baseCurrencyLong, pPrep);
549  Poco::Data::TypeHandler<double>::prepare(pos++, record.minTradeSize, pPrep);
550  Poco::Data::TypeHandler<Poco::Int32>::prepare(pos++, record.id, pPrep);
551  }
552 }
553 ;
Definition: genericdatabase.h:9
Definition: genericdatabase.h:180
Definition: genericdatabase.h:351
Definition: genericdatabase.h:183
Definition: app.h:7
Definition: genericdatabase.h:12