From 2db6c4f42a20c0c2a9b32545f4d6fab90be2c26d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 23 Aug 2025 16:52:38 +0100 Subject: Add helpers and SQL for storing an entity --- src/Jamfile.jam | 2 +- src/ingestor.cpp | 26 ++++++++++++++++++++++++++ src/ingestor.hpp | 4 ++++ src/sql.cpp | 16 ++++++++++++++++ src/sql.hpp | 13 +++++++++++++ src/sql/entityInsert.sql | 1 + 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/sql.cpp create mode 100644 src/sql.hpp create mode 100644 src/sql/entityInsert.sql 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 : . ..//adhocutil ..//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 +#include #include #include #include @@ -74,4 +77,27 @@ namespace WebStat { } } } + + template + 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) 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 void storeEntity(const T &) const; + void storeEntity(Entity) const; + void storeEntity(std::optional) 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 + +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(std::hash {}(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 +#include + +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 -- cgit v1.2.3