Fledge
An open source edge computing platform for industrial users
insert.h
1 #ifndef _INSERT_H
2 #define _INSERT_H
3 /*
4  * Fledge storage client.
5  *
6  * Copyright (c) 2018 OSisoft, LLC
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch
11  */
12 #include <string>
13 #include <sstream>
14 #include <iostream>
15 #include <vector>
16 #include <resultset.h>
17 #include "rapidjson/document.h"
18 #include "rapidjson/writer.h"
19 #include "rapidjson/stringbuffer.h"
20 #include "rapidjson/error/error.h"
21 
22 
26 class InsertValue {
27  public:
28  InsertValue(const std::string& column, const std::string& value) :
29  m_column(column)
30  {
31  m_value.str = (char *)malloc(value.length() + 1);
32  strncpy(m_value.str, value.c_str(), value.length() + 1);
33  m_type = STRING_COLUMN;
34  };
35  InsertValue(const std::string& column, const int value) :
36  m_column(column)
37  {
38  m_value.ival = value;
39  m_type = INT_COLUMN;
40  };
41  InsertValue(const std::string& column, const long value) :
42  m_column(column)
43  {
44  m_value.ival = value;
45  m_type = INT_COLUMN;
46  };
47  InsertValue(const std::string& column, const double value) :
48  m_column(column)
49  {
50  m_value.fval = value;
51  m_type = NUMBER_COLUMN;
52  };
53  InsertValue(const std::string& column, const rapidjson::Value& value) :
54  m_column(column)
55  {
56  rapidjson::StringBuffer sb;
57  rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
58  value.Accept(writer);
59  std::string s = sb.GetString();
60  m_value.str = (char *)malloc(s.length() + 1);
61  strncpy(m_value.str, s.c_str(), s.length() + 1);
62  m_type = JSON_COLUMN;
63  };
64 
65  // Insert a NULL value for the given column
66  InsertValue(const std::string& column) :
67  m_column(column)
68  {
69  m_type = NULL_COLUMN;
70  m_value.str = NULL;
71  }
72 
73  InsertValue(const InsertValue& rhs) : m_column(rhs.m_column)
74  {
75  m_type = rhs.m_type;
76  switch (rhs.m_type)
77  {
78  case INT_COLUMN:
79  m_value.ival = rhs.m_value.ival;
80  break;
81  case NUMBER_COLUMN:
82  m_value.fval = rhs.m_value.fval;
83  break;
84  case STRING_COLUMN:
85  m_value.str = strdup(rhs.m_value.str);
86  break;
87  case JSON_COLUMN: // Internally stored a a string
88  m_value.str = strdup(rhs.m_value.str);
89  break;
90  case NULL_COLUMN:
91  m_value.str = NULL;
92  break;
93  case BOOL_COLUMN:
94  // TODO
95  break;
96  }
97  }
98  ~InsertValue()
99  {
100  if (m_type == STRING_COLUMN || m_type == JSON_COLUMN)
101  {
102  free(m_value.str);
103  }
104  };
105  const std::string toJSON() const
106  {
107  std::ostringstream json;
108 
109  json << "\"" << m_column << "\" : ";
110  switch (m_type)
111  {
112  case JSON_COLUMN:
113  json << m_value.str;
114  break;
115  case BOOL_COLUMN:
116  json << m_value.ival;
117  break;
118  case INT_COLUMN:
119  json << m_value.ival;
120  break;
121  case NUMBER_COLUMN:
122  json << m_value.fval;
123  break;
124  case STRING_COLUMN:
125  json << "\"" << m_value.str << "\"";
126  break;
127  case NULL_COLUMN:
128  // JSON output for NULL value
129  json << "null";
130  break;
131  }
132  return json.str();
133  }
134  private:
135  InsertValue& operator=(InsertValue const& rhs);
136  const std::string m_column;
137  ColumnType m_type;
138  union {
139  char *str;
140  long ival;
141  double fval;
142  } m_value;
143 };
144 
145 class InsertValues : public std::vector<InsertValue>
146 {
147  public:
148  const std::string toJSON() const
149  {
150  std::ostringstream json;
151 
152  json << "{ ";
153  for (std::vector<InsertValue>::const_iterator it = this->cbegin();
154  it != this->cend(); ++it)
155 
156  {
157  json << it->toJSON();
158  if (it + 1 != this->cend())
159  json << ", ";
160  else
161  json << " ";
162  }
163  json << "}";
164  return json.str();
165  };
166 };
167 #endif
168 
Class that defines data to be inserted or updated in a column within the table.
Definition: insert.h:26
Definition: insert.h:145