You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0.1.3_01_struct_init.sql 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. --
  2. -- Name: luticate_settings; Type: TABLE; Schema: public; Owner: dev; Tablespace:
  3. --
  4. CREATE TABLE luticate_settings (
  5. name character varying(128) NOT NULL,
  6. type character varying(128) NOT NULL,
  7. value text NOT NULL,
  8. is_blocked boolean NOT NULL,
  9. is_admin boolean NOT NULL
  10. );
  11. --
  12. -- Name: luticate_settings_users; Type: TABLE; Schema: public; Owner: dev; Tablespace:
  13. --
  14. CREATE TABLE luticate_settings_users (
  15. name character varying(128) NOT NULL,
  16. value text NOT NULL,
  17. user_id integer NOT NULL,
  18. is_blocked boolean NOT NULL
  19. );
  20. --
  21. -- Name: luticate_settings_pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace:
  22. --
  23. ALTER TABLE ONLY luticate_settings
  24. ADD CONSTRAINT luticate_settings_pkey PRIMARY KEY (name);
  25. --
  26. -- Name: luticate_settings_users_pkey; Type: CONSTRAINT; Schema: public; Owner: dev; Tablespace:
  27. --
  28. ALTER TABLE ONLY luticate_settings_users
  29. ADD CONSTRAINT luticate_settings_users_pkey PRIMARY KEY (name, user_id);
  30. --
  31. -- Name: luticate_settings_users_name_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dev
  32. --
  33. ALTER TABLE ONLY luticate_settings_users
  34. ADD CONSTRAINT luticate_settings_users_name_fkey FOREIGN KEY (name) REFERENCES luticate_settings(name) ON DELETE CASCADE;
  35. --
  36. -- Name: luticate_settings_users_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: dev
  37. --
  38. ALTER TABLE ONLY luticate_settings_users
  39. ADD CONSTRAINT luticate_settings_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES luticate_users(id) ON DELETE CASCADE;
  40. --
  41. -- Name: sp_lu_get_user_setting(integer, character varying); Type: FUNCTION; Schema: public; Owner: dev
  42. --
  43. CREATE FUNCTION sp_lu_get_user_setting(_user_id integer, _setting_name character varying, OUT setting json) RETURNS json
  44. LANGUAGE plpgsql IMMUTABLE SECURITY DEFINER COST 10
  45. AS $$
  46. DECLARE
  47. _setting luticate_settings;
  48. _setting_user luticate_settings_users;
  49. BEGIN
  50. SELECT * INTO _setting FROM luticate_settings WHERE "name" = _setting_name AND NOT "is_admin";
  51. IF _setting IS NULL THEN
  52. setting := NULL;
  53. ELSE
  54. IF NOT _setting.is_blocked THEN
  55. SELECT * INTO _setting_user FROM luticate_settings_users WHERE "user_id" = _user_id AND "name" = _setting_name;
  56. IF _setting_user IS NOT NULL THEN
  57. _setting.value := _setting_user.value;
  58. END IF;
  59. END IF;
  60. setting := to_json(_setting);
  61. END IF;
  62. END;
  63. $$;
  64. --
  65. -- Name: sp_lu_get_all_user_settings(integer); Type: FUNCTION; Schema: public; Owner: dev
  66. --
  67. CREATE FUNCTION sp_lu_get_all_user_settings(_user_id integer, OUT _settings json) RETURNS SETOF json
  68. LANGUAGE sql IMMUTABLE SECURITY DEFINER COST 10
  69. AS $$
  70. SELECT sp_lu_get_user_setting(_user_id, s.name)
  71. FROM luticate_settings s
  72. WHERE NOT s.is_admin
  73. ORDER BY s.name
  74. $$;