From 1adff774d81a9b9b317c43039c29726ff18a79e0 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Sun, 6 Sep 2020 11:39:53 -0700 Subject: [PATCH] Test query made --- .gitignore | 1 + factoidtest.sql | 27 +++++++++++++++++++++++++++ postgresql.sql | 25 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 factoidtest.sql diff --git a/.gitignore b/.gitignore index 0519846..82ec254 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ modules.lst jail.ext4 var/asn.db langs/ +*.swp diff --git a/factoidtest.sql b/factoidtest.sql new file mode 100644 index 0000000..82e257d --- /dev/null +++ b/factoidtest.sql @@ -0,0 +1,27 @@ +WITH RECURSIVE factoid_lookup_order (depth, namespace, server, alias_namespace, alias_server, parent_namespace, parent_server, recursive) AS ( + SELECT 0 AS depth, namespace, server, alias_namespace, alias_server, parent_namespace, parent_server, recursive + FROM factoid_config + WHERE namespace = '#perlbot' AND server = 'freenode.net' -- PLACEHOLDER TARGET + UNION ALL + SELECT p.depth+1 AS depth, m.namespace, m.server, m.alias_namespace, m.alias_server, m.parent_namespace, m.parent_server, m.recursive + FROM factoid_config m + INNER JOIN factoid_lookup_order p + ON m.namespace = p.parent_namespace AND m.server = p.parent_server AND p.recursive +), +get_latest_factoid (depth, factoid_id, subject, copula, predicate, author, modified_time, compose_macro, protected, original_subject, deleted, server, namespace) AS ( + SELECT DISTINCT ON(lo.depth) lo.depth, factoid_id, subject, copula, predicate, author, modified_time, compose_macro, protected, original_subject, f.deleted, f.server, f.namespace + FROM factoid f + INNER JOIN factoid_lookup_order lo + ON COALESCE(f.server, '') = COALESCE(lo.alias_server, lo.server) + AND COALESCE(f.namespace, '') = COALESCE(lo.alias_namespace, lo.namespace) + WHERE original_subject = 'hi' -- PLACEHOLDER TARGET + ORDER BY depth ASC, factoid_id DESC +) +SELECT * FROM get_latest_factoid WHERE NOT deleted ORDER BY depth ASC, factoid_id DESC LIMIT 1; + + + +-- SELECT factoid_id, subject, copula, predicate, author, modified_time, compose_macro, protected, original_subject +-- FROM factoid +-- WHERE original_subject = ? +-- ORDER BY factoid_id DESC diff --git a/postgresql.sql b/postgresql.sql index fe7699a..6a0534d 100644 --- a/postgresql.sql +++ b/postgresql.sql @@ -6,6 +6,7 @@ ALTER TABLE public.factoid ALTER COLUMN original_subject TYPE text; ALTER TABLE public.factoid ALTER COLUMN subject TYPE text; ALTER TABLE public.factoid ALTER COLUMN copula TYPE text; ALTER TABLE public.factoid ALTER COLUMN author TYPE text; +ALTER TABLE public.factoid ADD COLUMN deleted boolean DEFAULT false; ALTER TABLE public.factoid ADD COLUMN namespace text; ALTER TABLE public.factoid ADD COLUMN server text; @@ -14,5 +15,29 @@ UPDATE public.factoid SET namespace=NULL WHERE namespace = ''; UPDATE public.factoid SET server=NULL WHERE server = ''; UPDATE public.factoid SET original_subject=split_part(original_subject, E'\034', 4), subject=split_part(subject, E'\034', 4) WHERE namespace IS NOT NULL and server IS NOT NULL; +DROP TABLE IF EXISTS public.factoid_namespace_config; +DROP TABLE IF EXISTS public.factoid_config; +CREATE TABLE public.factoid_config ( + server text NOT NULL, + namespace text NOT NULL, + +-- this lets me set the explicit name used in the rest of the database + alias_server text, + alias_namespace text, +-- this lets me set the explicit name for the parent namespace, this only refers to the server+namespace values, not the alias_* they set + parent_server text NOT NULL DEFAULT '', + parent_namespace text NOT NULL DEFAULT '', +-- Should we do the recursive lookup into the parent_*, this is needed because NULL is a valid value for parent_* + recursive boolean DEFAULT false, + command_prefix text, + + CONSTRAINT unique_config UNIQUE (server, namespace) +); + +INSERT INTO public.factoid_config (server, namespace, alias_server, alias_namespace, recursive, command_prefix) + VALUES ('freenode.net', '#perlbot', 'freenode.net', '#perlbot', true, NULL), + ('', '', '', '', false, NULL), -- the parent of all + ('freenode.net', '#regex', 'freenode.net', '#regex', false, '!'), + ('freenode.net', '#regexen', 'freenode.net', '#regex', false, '!'); COMMIT;