summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ingestor.cpp6
-rw-r--r--src/schema.sql22
-rw-r--r--src/sql/entityInsert.sql2
3 files changed, 23 insertions, 7 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp
index 3e36d85..fe8d38c 100644
--- a/src/ingestor.cpp
+++ b/src/ingestor.cpp
@@ -562,8 +562,10 @@ namespace WebStat {
assert(!entity.id);
const auto & [typeName, onInsert] = ENTITY_TYPE_VALUES[std::to_underlying(entity.type)];
- entity.id = insert(dbconn, SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS, entity.value, typeName);
- if (onInsert) {
+ bool entityNullDetail = true;
+ std::tie(entity.id, entityNullDetail)
+ = insert<EntityId, bool>(dbconn, SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS, entity.value, typeName);
+ if (onInsert && entityNullDetail) {
std::invoke(onInsert, this, entity);
}
stats.entitiesInserted += 1;
diff --git a/src/schema.sql b/src/schema.sql
index 698ea3e..056f7ac 100644
--- a/src/schema.sql
+++ b/src/schema.sql
@@ -43,14 +43,20 @@ CREATE TABLE entities(
CREATE UNIQUE INDEX uni_entities_value ON entities(MD5(value));
CREATE OR REPLACE FUNCTION entity(newValue text, newType entity)
- RETURNS integer
+ RETURNS TABLE(
+ id integer,
+ nulldetail boolean
+ )
AS $$
DECLARE
now timestamp without time zone;
recid integer;
+ nulldetail boolean;
BEGIN
IF newValue IS NULL THEN
- RETURN NULL;
+ RETURN query
+ VALUES (NULL,
+ NULL);
END IF;
INSERT INTO entities(value, type)
SELECT
@@ -66,16 +72,22 @@ BEGIN
ON CONFLICT
DO NOTHING
RETURNING
- id INTO recid;
+ entities.id,
+ entities.detail IS NULL INTO recid,
+ nulldetail;
IF recid IS NULL THEN
+ RETURN query
SELECT
- id INTO recid
+ id,
+ detail IS NULL
FROM
entities
WHERE
md5(value) = md5(newValue);
END IF;
- RETURN recid;
+ RETURN query
+VALUES (recid,
+ nulldetail);
END;
$$
LANGUAGE plpgSQL
diff --git a/src/sql/entityInsert.sql b/src/sql/entityInsert.sql
index f518617..11e60af 100644
--- a/src/sql/entityInsert.sql
+++ b/src/sql/entityInsert.sql
@@ -1,2 +1,4 @@
SELECT
+ *
+FROM
entity(?, ?)