aboutsummaryrefslogtreecommitdiff
path: root/apps/SoilMoistureIo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SoilMoistureIo.cpp')
-rw-r--r--apps/SoilMoistureIo.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/apps/SoilMoistureIo.cpp b/apps/SoilMoistureIo.cpp
new file mode 100644
index 0000000..1280fc6
--- /dev/null
+++ b/apps/SoilMoistureIo.cpp
@@ -0,0 +1,107 @@
+#include "SoilMoistureIo.h"
+
+#include "json.hpp"
+#include <set>
+
+namespace trygvis {
+namespace soil_moisture {
+
+using namespace std;
+using json = nlohmann::json;
+
+CsvSampleOutputStream::CsvSampleOutputStream(ostream &stream, vector<string> fields) :
+ stream(stream), fields(fields) {
+
+ auto i = fields.begin();
+ while (true) {
+ stream << *i;
+
+ i++;
+
+ if (i != fields.end()) {
+ stream << ",";
+ } else {
+ break;
+ }
+ }
+
+ stream << endl;
+}
+
+void CsvSampleOutputStream::write(it values) {
+ auto i = fields.begin();
+ while (true) {
+ auto field = *i;
+ auto value = values.find(field);
+
+ if (value != values.end()) {
+ stream << value->second;
+ }
+
+ i++;
+
+ if (i != fields.end()) {
+ stream << ",";
+ } else {
+ break;
+ }
+ }
+
+ stream << endl;
+}
+
+JsonSampleOutputStream::JsonSampleOutputStream(ostream &stream, vector<string> fields) :
+ stream(stream), fields(fields) {
+}
+
+void JsonSampleOutputStream::write(it values) {
+ json doc;
+
+ for (auto &f: fields) {
+ auto value = values.find(f);
+
+ if (value != values.end()) {
+ doc[f] = value->second;
+ }
+ }
+
+ stream << doc << endl;
+}
+
+SqlSampleOutputStream::SqlSampleOutputStream(ostream &stream, vector<string> fields) :
+ stream(stream), fields(fields) {
+}
+
+void SqlSampleOutputStream::write(it values) {
+ auto i = fields.begin();
+
+ stringstream fs, vs;
+
+ while (true) {
+ auto field = *i;
+
+ fs << field;
+
+ auto value = values.find(field);
+
+ if (value != values.end()) {
+ vs << "'" << value->second << "'";
+ } else {
+ vs << "NULL";
+ }
+
+ i++;
+
+ if (i != fields.end()) {
+ fs << ",";
+ vs << ",";
+ } else {
+ break;
+ }
+ }
+
+ stream << "INSERT INTO (" << fs << ") VALUES(" << vs << ")" << endl;
+}
+
+}
+}