diff options
-rw-r--r-- | src/Jamfile.jam | 2 | ||||
-rw-r--r-- | src/ingestor.cpp | 26 | ||||
-rw-r--r-- | src/ingestor.hpp | 4 | ||||
-rw-r--r-- | src/sql.cpp | 16 | ||||
-rw-r--r-- | src/sql.hpp | 13 | ||||
-rw-r--r-- | src/sql/entityInsert.sql | 1 |
6 files changed, 61 insertions, 1 deletions
diff --git a/src/Jamfile.jam b/src/Jamfile.jam index adbdc02..a6abc93 100644 --- a/src/Jamfile.jam +++ b/src/Jamfile.jam @@ -1,4 +1,4 @@ -lib webstat : ingestor.cpp logTypes.cpp : +lib webstat : ingestor.cpp logTypes.cpp sql.cpp : <include>. <library>..//adhocutil <library>..//dbppcore diff --git a/src/ingestor.cpp b/src/ingestor.cpp index d5dd4e2..a5a2e57 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -1,4 +1,7 @@ #include "ingestor.hpp" +#include "sql.hpp" +#include <connection.h> +#include <modifycommand.h> #include <scn/scan.h> #include <syslog.h> #include <utility> @@ -74,4 +77,27 @@ namespace WebStat { } } } + + template<typename T> + void + Ingestor::storeEntity(const T &) const + { + } + + void + Ingestor::storeEntity(const Entity entity) const + { + auto insert = dbconn->modify(SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS); + insert->bindParamI(0, entity.first); + insert->bindParamS(1, entity.second); + insert->execute(); + } + + void + Ingestor::storeEntity(const std::optional<Entity> entity) const + { + if (entity) { + storeEntity(*entity); + } + } } diff --git a/src/ingestor.hpp b/src/ingestor.hpp index 8450e4f..a7a57a6 100644 --- a/src/ingestor.hpp +++ b/src/ingestor.hpp @@ -23,6 +23,10 @@ namespace WebStat { void ingestLog(std::FILE *); + template<typename T> void storeEntity(const T &) const; + void storeEntity(Entity) const; + void storeEntity(std::optional<Entity>) const; + protected: size_t linesRead = 0; size_t linesParsed = 0; diff --git a/src/sql.cpp b/src/sql.cpp new file mode 100644 index 0000000..8512657 --- /dev/null +++ b/src/sql.cpp @@ -0,0 +1,16 @@ +#include "sql.hpp" +#include <command.h> + +namespace WebStat::SQL { + // ccache doesn't play nicely with #embed + // https://github.com/ccache/ccache/issues/1540 + // ccache:disable + + const std::string ENTITY_INSERT { +#embed "sql/entityInsert.sql" + }; +#define HASH_OPTS(VAR) \ + const DB::CommandOptionsPtr VAR##_OPTS = std::make_shared<DB::CommandOptions>(std::hash<std::string> {}(VAR)) + HASH_OPTS(ENTITY_INSERT); +#undef HASH_OPTS +} diff --git a/src/sql.hpp b/src/sql.hpp new file mode 100644 index 0000000..4b598e7 --- /dev/null +++ b/src/sql.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include <command_fwd.h> +#include <string> + +namespace WebStat::SQL { +#define EMBED_DECLARE(Name) \ + extern const std::string Name; \ + extern const DB::CommandOptionsPtr Name##_OPTS + + EMBED_DECLARE(ENTITY_INSERT); +#undef EMBED_DECLARE +} diff --git a/src/sql/entityInsert.sql b/src/sql/entityInsert.sql new file mode 100644 index 0000000..ac443e3 --- /dev/null +++ b/src/sql/entityInsert.sql @@ -0,0 +1 @@ +INSERT INTO entities(id, value) VALUES(?, ?) ON CONFLICT DO NOTHING |