summaryrefslogtreecommitdiff
path: root/src/schema.sql
diff options
context:
space:
mode:
authorDan Goodliffe <dan.goodliffe@octal.co.uk>2026-05-05 12:24:11 +0100
committerDan Goodliffe <dan.goodliffe@octal.co.uk>2026-05-05 12:24:11 +0100
commit200c3636b600ac2f997316612f1b1321112496cd (patch)
tree21a86b460e1dca04d5a403e56133383ab78881b6 /src/schema.sql
parentf9b4422198281410218e371ffc3e1e4513a9ba7f (diff)
downloadwebstat-200c3636b600ac2f997316612f1b1321112496cd.tar.bz2
webstat-200c3636b600ac2f997316612f1b1321112496cd.tar.xz
webstat-200c3636b600ac2f997316612f1b1321112496cd.zip
Only call entity insert handler if detail is null
Improves handling of entity inserts where the entity already exists and already has detail; does not call the onInsert handler. This avoids repeatedly fetching UA detail every time the UA is first seen by a process.
Diffstat (limited to 'src/schema.sql')
-rw-r--r--src/schema.sql22
1 files changed, 17 insertions, 5 deletions
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