Răsfoiți Sursa

add stored procedure to generate prayer based off parameters

Daniel Sheffield 1 an în urmă
părinte
comite
b41d0307ac
1 a modificat fișierele cu 49 adăugiri și 22 ștergeri
  1. 49 22
      create-views.sql

+ 49 - 22
create-views.sql

@@ -18,31 +18,58 @@ WHERE text IS NOT NULL
 ORDER BY ordinal ASC LIMIT 1;
 $$;
 
+CREATE TABLE IF NOT EXISTS public.pg_categories(
+    tableid int NOT NULL,
+    name text NOT NULL,
+    tablename text NOT NULL,
+    PRIMARY KEY(name)
+);
+ALTER TABLE public.pg_categories OWNER TO pgdb;
+
+INSERT INTO public.pg_categories(tablename, name, tableid)
+VALUES
+    ('c_name', 'name', 10), ('c_z_kingdom', 'z_kingdom', 15), ('c_a_kingdom', 'a_kingdom', 20),
+    ('c_will', 'will', 25), ('c_bread', 'bread', 30), ('c_because', 'because', 35),
+    ('c_forgive', 'forgive', 40), ('c_debtors', 'debtors', 45), ('c_tempt', 'tempt', 50),
+    ('c_deliver', 'deliver', 60), ('c_glory', 'glory', 70)
+ON CONFLICT DO NOTHING;
+
+CREATE OR REPLACE FUNCTION public.getprayer(p_name text[], p_translation character varying [], p_reference text [])
+RETURNS TABLE(ordinal integer, category text, translation character varying, reference text, txt text)
+    LANGUAGE plpgsql
+    AS $$
+DECLARE
+  r record;
+BEGIN
+FOR r IN
+  SELECT tableid, c.name, p.translation AS translation, p.reference AS reference FROM pg_categories AS c JOIN UNNEST(p_name, p_translation, p_reference) AS p(name, translation, reference) ON c.name = p.name
+LOOP
+  SELECT t.translation, t.text INTO translation, txt FROM getfallbacktext(r.translation, getdefaulttranslation(), r.reference) AS t;
+  RETURN QUERY SELECT r.tableid, r.name, translation, reference, txt;
+END LOOP;
+RETURN;
+END
+$$;
+
+
 --
 -- Name: getrandomsample(text, bigint); Type: FUNCTION; Schema: public; Owner: pgdb
 --
 
-CREATE OR REPLACE FUNCTION public.getrandomsample(p_table text, p_count bigint) RETURNS TABLE(ordinal integer, category text, translation character varying, reference text)
+CREATE OR REPLACE FUNCTION public.getrandomsample(p_name text, p_count bigint) RETURNS TABLE(ordinal integer, category text, translation character varying, reference text)
     LANGUAGE plpgsql
     AS $$
 DECLARE
   v_estimate bigint;
   v_percentage double precision;
   v_table_id int;
+  v_table text;
 BEGIN
-SELECT num INTO v_table_id FROM (VALUES 
-    ('c_name', 10), ('c_z_kingdom', 15), ('c_a_kingdom', 20),
-    ('c_will', 25), ('c_bread', 30), ('c_because', 35),
-    ('c_forgive',40), ('c_debtors', 45), ('c_tempt', 50),
-    ('c_deliver', 60), ('c_glory', 70)
-    
-) AS q(name, num)
-WHERE name = p_table;
-SELECT pg_class.reltuples INTO v_estimate FROM pg_class WHERE pg_class.relname = p_table;
+SELECT tableid, tablename, reltuples INTO v_table_id, v_table, v_estimate FROM pg_categories JOIN pg_class ON tablename = relname;
 SELECT 1000::double precision / v_estimate INTO v_percentage;
 RETURN QUERY EXECUTE 'SELECT '|| v_table_id ||' AS ordinal,
-  '''|| REPLACE(p_table, 'c_', '') ||''' AS category, translation, reference
-FROM ' || p_table || ' TABLESAMPLE bernoulli ('|| v_percentage ||')
+  '''|| p_name ||''' AS category, translation, reference
+FROM ' || v_table || ' TABLESAMPLE bernoulli ('|| v_percentage ||')
 LIMIT ' || p_count;
 END
 $$;
@@ -61,70 +88,70 @@ CREATE OR REPLACE VIEW public.pg_random_view AS
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_name'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('name'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_bread'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('bread'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_debtors'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('debtors'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_because'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('because'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_tempt'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('tempt'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_deliver'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('deliver'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_glory'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('glory'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_will'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('will'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_a_kingdom'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('a_kingdom'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
 UNION
  SELECT getrandomsample.ordinal,
     getrandomsample.category,
     getrandomsample.translation,
     getrandomsample.reference,
     public.gettext(getrandomsample.translation, (getrandomsample.reference)::character varying) AS txt
-   FROM public.getrandomsample('c_z_kingdom'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
+   FROM public.getrandomsample('z_kingdom'::text, (1)::bigint) getrandomsample(ordinal, category, translation, reference)
   ORDER BY 1;
 
 ALTER TABLE public.pg_random_view OWNER TO pgdb;