Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSADBTable.h
1 /* ===================================================================
2 *
3 * T R A D I N G S Y S T E M A P I ™
4 * Copyright © 1999 - 2014 by Peter Ritter ( TradingSystemAPI.com )
5 * A L L R I G H T S R E S E R V E D
6 *
7 * Consult your license regarding permissions and restrictions.
8 * You may obtain a copy of the License at:
9 * http://www.TradingSystemAPI.com/licenses/LICENSE-TSAPI-3.0.html
10 *
11 * ====================================================================
12 */
13 
14 #ifndef TSA_TABLE__INCLUDED
15 #define TSA_TABLE__INCLUDED
16 
17 #include "TSAStreams.h"
18 #include "TSADatabase.h"
19 #include "TSADBRecord.h"
20 
21 namespace tsa {
22  class path;
23  class record;
24  struct rec_proxy;
25 
28 
29  namespace fast{
30  /*
31  ** ===================================================
32  ** > > > class table < < <
33  ** ===================================================
34  */
35 
41  class dll_export table :
42  public out_stream_adaptor,
43  public appendable,
44  public tsa::object {
45  tsa_declare_testable;
46  friend class database;
47  friend class record;
48  friend void out_stream::connect(const std::string& db_name, const std::string& table_name);
49  private:
50  table_imp_ref m_table_imp;
51  variant m_ret_val;
52  database* m_database_ptr;
53  void prepare_current_record_update(void);
54  size_t m_append_rec_pos;
55  void verify_init(void) const;
56  private:
57  table(void);
58  table(database*);
59  void init(const std::string&, database*);
60  public:
61  table(const table&); // note: for regression tests
62  public:
63  table(const table_imp_ref&);
64  void operator= (const table_imp_ref&);
65  public:
66  virtual void osa__append(const date_time&, std::vector<variant>&) override;
67  virtual void osa__prepare_stream(const std::string& source_name, const tsa::column_defs&) override;
68  virtual void cmp__finalize(void) override;
69  public:
70  // internal
71  table(database& db, const std::string& tableName);
72 
76  virtual ~table(void);
77  public:
78 
84  bool is_init(void) const;
85 
90  void close(void);
91 
96  const std::string& name(void) const;
97  public:
106  const std::string& column_name(size_t position) const;
107 
113  size_t column_number(const std::string& column_name) const;
114 
120  virtual size_t column_count(void) const; /*class appendable*/
121 
127  type_t column_type(size_t position) const;
128 
134  size_t column_field_length(size_t column_position) const; //IU
135 
136  // Internal; Returns the name under which the column_defs object for this table is stored.
137  const std::string& column_defs__name(void) const;
138 
142  const class tsa::column_defs columns(void) const;
144  public:
151  class iterator {
152  friend class table;
153  bool m_is_end;
154  table* m_table_ptr;
155  iterator(table*, bool is_end);
156  iterator(void);
157  public:
158 
163  iterator(const iterator& other);
164 
169  void operator= (const iterator& other);
170 
174  ~iterator(void);
175  public:
176 
181  db_record* operator-> (void);
182 
184  db_record* operator* (void);
185 
189  void operator++ (int);
190 
194  void advance(void);
195 
200  const date_time& timestamp(void) const;
201 
207  const variant operator[](size_t position) const;
208 
214  bool operator== (const iterator& rhs) const;
215 
221  bool operator!= (const iterator& rhs) const;
222  private:
223  void table::iterator::verify_not_end(void) const;
224  };
225 
226  iterator end_iterator;
227 
236  iterator seek(const date_time& target_timestamp);
237 
244  iterator seek(size_t record_position);
245 
251  iterator begin(void);
252 
265  const iterator& end(void);
266 
267  // internal
268  virtual class record record(void); /*class appendable*/
269 
276  virtual void append(const class record& record, append_flag_type flag = error_on_duplicate_timestamp);
277 
278  // internal
279  void push_back(const class record& rec, append_flag_type flag = error_on_duplicate_timestamp);
280 
285  size_t size(void) const;
286 
291  bool empty(void) const;
292 
297  const date_time& first_timestamp(void) const;
298 
303  const date_time& last_timestamp(void) const;
304 
309  const date_time& timestamp(void) const;
310 
316  void commit(void);
317 
323  void read(class record& rec) const;
324 
328  void clear();
329 
335  void truncate(size_t new_size);
336 
342  void truncate(const date_time& new_highest_timestamp);
344  private:
345  iterator* m_iterator_ptr;
346  void register_iterator(iterator*);
347  void deregister_iterator(iterator*);
348  bool cursor_next(void);
349  bool cursor_seek_first(void);
350  const variant cursor_at(size_t index);
351  public:
352  bool cursor_seek(size_t index);
353  bool cursor_seek_last(void);
354  void cursor_update(void);
355  void cursor_set_at(size_t index, const variant& value);
356 
357  /* internal
358  * @brief The function returns false if @b timestamp is out of bounds. If a record with the
359  * given timestamp does not exist, the cursor will be positioned on the record with the next
360  * lower timestamp, and return true. The cursor will thus be positioned on a record with a
361  * timestamp lower or equal to the given @b timestamp.
362  * @param timestamp The timestamp.
363  * @return bool.
364  */
365  bool cursor_seek(const date_time& timestamp);
366  bool b_next(void);
367  void Read(date_time& timestamp, std::vector<variant>& record) const;
368  const variant operator[](size_t fieldIndex);
369  const variant operator[](const std::string& fieldName);
370  public:
371  // internal Updates the table record at current cursor position with values in given @b record
372  void update(std::vector<variant>& record);
373  void append(const date_time& timestamp, const std::vector<variant>& record,
375  );
376  void append(const date_time& timestamp, append_flag_type);
377  void append(const date& date, append_flag_type);
378  void Read(std::vector<variant>& record) const;
379  private:
380  size_t m_float_precision;
381  size_t m_print_limit;
382  bool m_print_timestamp;
383  date_time m_tPrintStart;
384  date_time m_tPrintEnd;
385  public:
389  void print_timestamp(bool _b);
390 
391  // @brief Sets the floating point precision to be used by subsequent calls to print(), printv(), html() and csv().
392  void print_fp_precision(size_t _n);
393 
394  // @brief Sets the number of records to be printed by subsequent calls to print(), printv(), html() and csv().
395  void print_limit(size_t _n);
396 
397  // @brief Sets the range of timestamps to be printed by subsequent calls to print(), printv(), html() and csv().
398  void print_range(const date_time& _start, const date_time& _end);
399  public:
400  // @brief Prints records to @b stream. Upon completion the member will reset any range requests
401  // set via print_range() or print_limit().
402  void print(std::ostream& stream);
403 
404  // @brief Prints records to @b stream vertically, with a single field printed in each line.
405  // This is useful for tables that have many fields. Upon completion the member will reset any range requests
406  // set via print_range() or print_limit().
407  void printv(std::ostream& stream);
408 
409  // @brief Prints records to an html file with same name as table in given @b dir, unless an @b alt_name is given.
410  void html(const os::path& dir, const std::string& alt_name = "");
411 
412  // @b Prints records as comma separated values. Upon completion the member will reset any range requests
413  // set via print_range() or print_limit().
414  void csv(std::ostream&);
415 
417  public:
418  // Multiplies all numeric (non NIL) values in @b column by @b number.
419  void column_multiply(const std::string& column, double number);
420 
421  // Divides all numeric (non NIL) values in @b column by @b number.
422  void column_divide(const std::string& column, double number);
423 
424  // Adds @b number to all numeric (non NIL) values in @b column.
425  void column_add(const std::string& column, double number);
426 
427  // Caps all numeric (non NIL) values in @b column at @b number. No value in @b column will be larger than @b number.
428  void column_cap(const std::string& column, double number);
429 
430  // Floors all numeric (non NIL) values in @b column at @b number. No value in @b column will be smaller than @b number.
431  void column_floor(const std::string& column, double number);
432 
433  // Returns true if any value in @b column is NIL, else false.
434  bool column_contains_undefined(const std::string& column);
435 
436  // Returns true if any value in any column of the table is NIL.
437  bool table_contains_undefined(void);
438 
439  // @brief Returns the average numeric value in @b column_name. If @b raiseNIL is true,
440  // any NIL values encountered is raised as an exception, else NIL values are ignored.
441  double column_average(const std::string& column_name, bool raiseNIL = false);
442 
443  // @brief Returns the maximum numeric value in @b column_name. If @b raiseNIL is true,
444  // any NIL values encountered is raised as an exception, else NIL values are ignored.
445  double column_max(const std::string& column_name, bool raiseNIL = false);
446 
447  // @brief Returns the minimum numeric value in @b column_name. If @b raiseNIL is true,
448  // any NIL values encountered is raised as an exception, else NIL values are ignored.
449  double column_min(const std::string& column_name, bool raiseNIL = false);
450 
451  void UpdateAllValuesInColumn(const std::string& _columnName,
452  double _transformValue, double (table::*_func)(double value, double by));
453  double _Multiply(double value, double by);
454  double _Divide(double value, double by);
455  double _Add(double value, double by);
456  double _Cap(double value, double at);
457  double _Floor(double value, double at);
458  bool _TypeIsNumeric(type_t type);
459  double _ApplyValues(_MetricCalc& calc, const std::string& _columnName, bool _raiseNIL);
460  };
461  }//namespace fast
463 } //tsa
464 #endif
append_flag_type
Values that represent append flag types.
Definition: TSADBRecord.h:32
Utility class to manipulate file system paths. This class is intended to fulfil the requirements of t...
Definition: TSAOS.h:36
Throw an exception when a duplicate timestamp is encountered. This is normally the default behaviour...
Definition: TSADBRecord.h:36
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
Class for managing native timeseries database files. Extremely high speed data access.
Definition: TSADatabase.h:118
sref< double > operator*(numeric_series_cref a, numeric_series_cref b)
Multiplies a by b.
Definition: TSADSLBase.cpp:42
Defines the columnar structure of a table. Each column has a name, data type and field size...
Definition: TSADataDef.h:88
Abstract base class for delegates of class out_stream.
Definition: TSAStreams.h:553
variant objects can represent values of different types.
Definition: TSAVariant.h:140
_value_types_type
Data type enumeration used throughout the library. Intended to be used via type_t.
Definition: TSATypeDef.h:166
Table iterator. Supports forward iteration only.
Definition: TSADBTable.h:151
Parent class for many library classes.
Definition: TSATypeDef.h:462
A date of the Gregorian calendar.
Definition: TSATime.h:119
sref< bool > operator==(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:130
Class representing a database record.
Definition: TSADBRecord.h:52
Represents a database table in fast::database.
Definition: TSADBTable.h:41
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
Represents an object to which a record object can be appended.
Definition: TSADBRecord.h:140