Fledge
An open source edge computing platform for industrial users
connection.h
1 #ifndef _CONNECTION_H
2 #define _CONNECTION_H
3 /*
4  * Fledge storage service.
5  *
6  * Copyright (c) 2018 OSisoft, LLC
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Massimiliano Pinto
11  */
12 
13 #include <sql_buffer.h>
14 #include <string>
15 #include <rapidjson/document.h>
16 #include <sqlite3.h>
17 
18 WARNING: THIS FILE IS NOT USED
19 
20 #define READINGS_TABLE "readings"
21 #define READINGS_TABLE_MEM READINGS_TABLE "_1"
22 
23 class Connection {
24  public:
25  Connection();
26  ~Connection();
27  bool retrieveReadings(const std::string& condition,
28  std::string& resultSet);
29  int appendReadings(const char *readings);
30  bool fetchReadings(unsigned long id, unsigned int blksize,
31  std::string& resultSet);
32  unsigned int purgeReadings(unsigned long age, unsigned int flags,
33  unsigned long sent, std::string& results);
34  long tableSize(const std::string& table);
35  void setTrace(bool flag) { m_logSQL = flag; };
36  static bool formatDate(char *formatted_date, size_t formatted_date_size, const char *date);
37  unsigned int purgeReadingsAsset(const std::string& asset);
38  bool vacuum();
39  bool loadDatabase(const std::string& filname);
40  bool saveDatabase(const std::string& filname);
41  void setPurgeBlockSize(unsigned long purgeBlockSize)
42  {
43  m_purgeBlockSize = purgeBlockSize;
44  }
45  private:
46  int SQLexec(sqlite3 *db, const char *sql,
47  int (*callback)(void*,int,char**,char**),
48  void *cbArg, char **errmsg);
49  bool m_logSQL;
50  void raiseError(const char *operation, const char *reason,...);
51  sqlite3 *inMemory; // Handle for :memory: database
52  int mapResultSet(void *res, std::string& resultSet);
53  bool jsonWhereClause(const rapidjson::Value& whereClause, SQLBuffer&, bool convertLocaltime = false);
54  bool jsonModifiers(const rapidjson::Value&, SQLBuffer&);
55  bool jsonAggregates(const rapidjson::Value&,
56  const rapidjson::Value&,
57  SQLBuffer&,
58  SQLBuffer&,
59  bool isTableReading = false);
60  bool returnJson(const rapidjson::Value&, SQLBuffer&, SQLBuffer&);
61  char *trim(char *str);
62  const std::string escape(const std::string&);
63  bool applyColumnDateTimeFormat(sqlite3_stmt *pStmt,
64  int i,
65  std::string& newDate);
66  void logSQL(const char *, const char *);
67  unsigned long m_purgeBlockSize;
68 };
69 #endif
~Connection()
Destructor for the database connection.
Definition: connection.cpp:373
Buffer class designed to hold SQL statement that can as required but have minimal copy semantics...
Definition: sql_buffer.h:22
static bool formatDate(char *formatted_date, size_t formatted_date_size, const char *date)
Format a date to a fixed format with milliseconds, microseconds and timezone expressed, examples :
Definition: connection.cpp:1496
Connection()
Create a database connection.
Definition: connection.cpp:335
bool vacuum()
Execute a SQLite VACUUM command on the database.
Definition: connection.cpp:3849
void setTrace(bool flag)
Enable or disable the tracing of SQL statements.
Definition: connection.h:50
Definition: connection.h:32
unsigned int purgeReadings(unsigned long age, unsigned int flags, unsigned long sent, std::string &results)
Purge readings from the reading table.
Definition: connection.cpp:1782
int appendReadings(const char *readings)
Append a set of readings to the readings table.
Definition: connection.cpp:1597
bool fetchReadings(unsigned long id, unsigned int blksize, std::string &resultSet)
Fetch a block of readings from the reading table.
Definition: connection.cpp:1757