Fledge
An open source edge computing platform for industrial users
schema.h
1 #ifndef _SCHEMAS_H
2 #define _SCHEMAS_H
3 /*
4  * Fledge utilities functions for handling stringa
5  *
6  * Copyright (c) 2022 Dianomic Systems
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch
11  */
12 
13 #include <sql_buffer.h>
14 #include <string>
15 #include <rapidjson/document.h>
16 #include <sqlite3.h>
17 #include <logger.h>
18 #include <map>
19 
20 #define DDL_BACKOFF 50 // Microseconds to backoff between DDL retries
21 
35 class Schema {
36  public:
37  Schema(const std::string& name, const std::string& service, int version,
38  const std::string& definition);
39  Schema(sqlite3 *db, const rapidjson::Document& doc);
40  ~Schema();
41  int getVersion() { return m_version; };
42  std::string getService() { return m_service; };
43  bool upgrade(sqlite3 *db, const rapidjson::Document& doc, const std::string& definition);
44  bool attach(sqlite3 *db);
45  private:
46  std::string m_name;
47  std::string m_service;
48  int m_version;
49  std::string m_definition;
50  int m_indexNo;
51  std::string m_schemaPath;
52  std::map<sqlite3 *, bool>
53  m_attached;
54  private:
55  bool createTable(sqlite3 *db, const rapidjson::Value& table);
56  bool createIndex(sqlite3 *db, const std::string& table,
57  const rapidjson::Value& index);
58  bool hasTable(const rapidjson::Document& doc, const std::string& table);
59  bool hasColumn(const rapidjson::Document& doc, const std::string& table,
60  const std::string& column);
61  bool addTableColumn(sqlite3 *db, const std::string& table,
62  const rapidjson::Value& column);
63  bool executeDDL(sqlite3 *db, SQLBuffer& sql);
64 
65  bool hasString(const rapidjson::Value& value, const char *key)
66  {
67  return (value.HasMember(key) && value[key].IsString());
68  };
69  bool hasInt(const rapidjson::Value& value, const char *key)
70  {
71  return (value.HasMember(key) && value[key].IsInt());
72  };
73  bool hasArray(const rapidjson::Value& value, const char *key)
74  {
75  return (value.HasMember(key) && value[key].IsArray());
76  };
77  bool createDatabase();
78  void setDatabasePath();
79 };
80 
86  public:
87  static SchemaManager *getInstance();
88  void load(sqlite3 *db);
89  bool create(sqlite3 *db, const std::string& definition);
90  bool exists(sqlite3 *db, const std::string& schema);
91  public:
92  static SchemaManager *instance;
93  private:
94  SchemaManager();
95  private:
96  Logger *m_logger;
97  std::map<std::string, Schema *> m_schema;
98  bool m_loaded;
99 };
100 #endif
Fledge Logger class used to log to syslog.
Definition: logger.h:26
Representation of an extension schema.
Definition: schema.h:35
Buffer class designed to hold SQL statement that can as required but have minimal copy semantics...
Definition: sql_buffer.h:22
bool attach(sqlite3 *db)
Attach the schema to the database handle if not already attached.
Definition: schema.cpp:825
The singleton SchemaManager class used to interact with the extension schemas created by various exte...
Definition: schema.h:85
bool upgrade(sqlite3 *db, const rapidjson::Document &doc, const std::string &definition)
Upgrade an existing schema.
Definition: schema.cpp:420