23 #ifndef TSA_SQLITE__INCLUDED 24 #define TSA_SQLITE__INCLUDED 33 #include "TSADataDef.h" 34 #include "TSADBBase.h" 40 template<
typename T>
class function;
44 class out_stream_adaptor;
45 class in_stream_adaptor;
60 class error :
public std::runtime_error {
62 error(
const int error_code,
const std::string& msg = std::string());
63 error(
const std::string& msg);
65 const std::shared_ptr<sqlite3_stmt>& stmt,
66 const std::string& msg = std::string()
69 const int sqlite_error_code() const {
return code; }
91 field(
const std::shared_ptr<sqlite3_stmt>& statement,
92 const std::size_t& parameter_index
95 explicit operator bool()
const;
96 type column_type(
void)
const;
97 std::string column_name(
void)
const;
98 template<
typename T> T as()
const;
101 std::shared_ptr<sqlite3_stmt> m_stmt_ptr;
102 const std::size_t m_index;
112 row(
const std::shared_ptr<sqlite3_stmt>& statement);
113 row(
const row& other) =
default;
115 row& operator=(
const row& other) =
default;
117 std::size_t column_count()
const;
118 field operator[](
const std::string& column_name)
const;
119 field operator[](std::size_t column_index)
const;
120 field at(
const std::string& column_name)
const;
121 field at(std::size_t column_index)
const;
122 void print_row(std::ostream&)
const;
123 void print_row_v(std::ostream&)
const;
125 bool is_valid_index(
const std::size_t& index)
const;
127 std::shared_ptr<sqlite3_stmt> m_stmt_ptr;
130 class transaction_failed :
public error {
132 transaction_failed(
const int status) : error(status) {}
142 class const_iterator {
144 const_iterator(
const std::shared_ptr<sqlite3_stmt>& statement,
bool& at_end);
145 const_iterator(
const const_iterator& other) =
delete;
146 const_iterator(
const const_iterator&& other);
147 const_iterator(const_iterator&& other);
148 const_iterator& operator=(
const const_iterator& other) =
delete;
150 bool operator==(
const const_iterator& other)
const;
151 bool operator!=(
const const_iterator& other)
const;
152 const_iterator& operator++();
154 const row* operator->()
const;
156 std::shared_ptr<sqlite3_stmt> stmt;
161 bool is_valid(
void)
const;
164 result(
const std::shared_ptr<sqlite3_stmt>& statement);
165 result(
const result& other) =
delete;
166 result(result&& other);
168 result& operator=(
const result& other) =
delete;
169 result& operator=(result&& other);
171 size_t calculate_row_count()
const;
172 std::size_t row_modification_count()
const;
173 const_iterator begin()
const;
174 const_iterator end()
const;
176 void print_rows(std::ostream&)
const;
177 void print_rows_v(std::ostream&)
const;
179 std::shared_ptr<sqlite3_stmt> m_stmt_ptr;
180 mutable bool m_end_reached =
false;
189 static const null_t null = null_t::null;
198 statement(
const std::shared_ptr<sqlite3_stmt>& statement);
199 statement(
const statement& other) =
delete;
200 statement(statement&& other);
201 statement& operator=(
const statement& other) =
delete;
202 statement& operator=(statement&& other);
203 std::size_t parameter_count()
const;
206 void bind(
const std::string& parameter,
const T& value);
207 void bind(
const std::string& parameter,
const char* value);
208 void clear_bindings();
209 friend std::ostream&
operator<<(std::ostream& os,
const statement& statement);
211 void throw_on_bind_error(
const int status,
const std::string& parameter)
const;
212 friend result make_result(
const statement& statement);
214 std::shared_ptr<sqlite3_stmt> m_stmt_ptr;
217 static char const* temporary =
"";
219 enum class special_t {
222 static const special_t in_memory = special_t::in_memory;
227 read_write_create = 0x06
231 shared_cache = 0x00020000,
232 private_cache = 0x00040000
241 friend class database;
242 database* m_db_ptr =
nullptr;
243 bool m_is_open =
true;
245 transaction(database* db_ptr);
246 transaction(
const transaction&);
257 struct field_info_t {
264 std::string default_value;
269 friend class sqlite::database;
270 std::string m_table_name;
271 std::vector <field_info_t> m_fields;
273 table_info_t(
void) =
default;
274 table_info_t(
const std::string& table_name) { m_table_name = table_name; }
275 table_info_t(table_info_t&) =
default;
276 table_info_t(table_info_t&&);
277 const std::string& name(
void)
const {
return m_table_name; }
278 const field_info_t& operator[](
size_t pos);
279 size_t size(
void)
const;
280 void print(std::ostream&);
296 std::shared_ptr<sqlite3_stmt> create_statement(
const std::string& sql)
const;
298 sqlite3* m_db_ptr =
nullptr;
300 virtual std::string type(
void)
const override;
305 const access_mode& permissions = sqlite::read_write_create,
306 const cache_type& visibility = private_cache
310 const special_t& in_memory,
311 const access_mode& permissions = sqlite::read_write_create,
312 const cache_type& visibility = private_cache
319 in_stream_adaptor* database::make_input_stream_adaptor(
const std::string& stream)
override;
321 virtual bool table_exists(
const std::string&)
const override;
322 void drop_table(
const std::string&,
bool throw_on_error =
false);
323 virtual bool drop_series_table(
const std::string&)
override;
324 virtual void create_series_table(
const std::string& table_name,
const tsa::column_defs&)
override;
325 virtual tsa::column_defs series_table_column_defs(
const std::string& table_name)
override;
326 void series_insert(
const std::string& table_name,
const tsa::mem_table& data);
327 virtual void series_append(
const std::string& table_name,
const tsa::mem_table& data)
override;
328 virtual void series_load(
const std::string& table_name,
tsa::mem_table& data,
329 const date_time& from_timestamp,
size_t num_records)
override;
330 void series_load(
const std::string& table_name,
tsa::mem_table& data);
331 void clear_table(
const std::string& table_name,
bool throw_on_error =
false);
332 virtual date_time series_table_first_timestamp(
const std::string& table_name)
override;
333 virtual date_time series_table_last_timestamp(
const std::string& table_name)
override;
334 virtual int64_t table_record_count(
const std::string& table_name);
335 virtual bool table_has_data(
const std::string& table_name)
override;
336 virtual date_time series_table_closest_timestamp_LOE(
const date_time& ts,
const std::string& table_name)
override;
337 virtual void truncate_series_table(
const std::string& table_name,
const date_time& new_max_timestamp =
date_time::min);
338 virtual bool table_record_count_is_known(
void)
const;
343 statement prepare_statement(
const std::string& sql)
const;
345 transaction begin(
void);
346 bool in_transaction(
void)
const;
348 friend class transaction;
352 result select_all_from(
const std::string& table_name);
353 result execute(
const std::string& sql)
const;
354 result execute(
const statement& statement);
355 template<
typename T> T execute_scalar(
const std::string& sql)
const {
356 result r = execute(sql);
357 if (r.begin() == r.end()){
358 throw tsa::exception(
"sqlite::result",
"Scalar query returned no results: " + sql,
SLOC);
360 return (*r.begin())[0].as<T>();
362 template<
typename T> T execute_scalar(
const statement& statement)
const {
363 result results = make_result(statement);
364 if (results.begin() == results.end()){
367 return (*results.begin())[0].as<T>();
369 std::size_t size_on_disk()
const;
372 table_info_t table_info(
const std::string& table_name);
377 void print_table(std::ostream&,
const std::string& table_name);
378 void print_table_v(std::ostream&,
const std::string& table_name);
Class for generating pseudo random price series (o,h,l,c,v).
Utility class to manipulate file system paths. This class is intended to fulfil the requirements of t...
Definition: TSAOS.h:36
Namespace for the 'Trading System API' library.
Definition: original1.TSA3Core.cpp:20
Base class for all other database classes such as:
Definition: TSADBBase.h:47
sref< double > operator*(numeric_series_cref a, numeric_series_cref b)
Multiplies a by b.
Definition: TSADSLBase.cpp:42
Definition: database.cpp:26
Defines the columnar structure of a table. Each column has a name, data type and field size...
Definition: TSADataDef.h:88
Definition: TSASQLite.h:39
#define SLOC
Macro to be passed as argument to Exception constructors. Contains information about the source locat...
Definition: TSAError.h:92
Parent class for 'in-stream adaptors'. in_stream object rely on adaptors for access to underlying dat...
Definition: TSAStreams.h:52
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
Represents a duration - the difference between two date_time values.
Definition: TSATime.h:945
std::ostream & operator<<(std::ostream &s, const transaction &t)
Writes human readable transaction information to stream.
Definition: TSAOrder.cpp:779
_value_types_type
Data type enumeration used throughout the library. Intended to be used via type_t.
Definition: TSATypeDef.h:166
Class tsa::exception used by most classes of the Trading System API library. The class inherits from ...
Definition: TSAError.h:37
SQLite database object. Object of this class allow ts-api to read and write time series tables in an ...
Definition: TSASQLite.h:293
double min(const series< double > &series, size_t period)
Returns the smallest value found in series over given .
Definition: TSAFunctionsScalar.cpp:218
sref< bool > operator==(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:130
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
Class mem_table represents a memory based table. mem_table objects can be used in strategies both for...
Definition: TSAMemTable.h:48
static duration minutes(uint64_t)
Returns a duration equivalent to a given number of minutes.
Definition: TSATime.cpp:1412