summaryrefslogtreecommitdiff
path: root/sql/modules/Settings.sql
blob: e86450c31c77ba41c48105afc7c3bd67f6404f5a (plain)
  1. -- VERSION 1.3.0
  2. CREATE OR REPLACE FUNCTION setting_set (in_key varchar, in_value varchar)
  3. RETURNS VOID AS
  4. $$
  5. BEGIN
  6. UPDATE defaults SET value = in_value WHERE setting_key = in_key;
  7. RETURN;
  8. END;
  9. CREATE OR REPLACE FUNCTION setting_get (in_key varchar) RETURNS varchar AS
  10. $$
  11. DECLARE
  12. out_value varchar;
  13. BEGIN
  14. SELECT value INTO out_value FROM defaults WHERE setting_key = in_key;
  15. RETURN value;
  16. END;
  17. $$ LANGUAGE plpgsql;
  18. CREATE OR REPLACE FUNCTION setting_get_default_accounts ()
  19. RETURNS SETOF defaults AS
  20. $$
  21. DECLARE
  22. account defaults%ROWTYPE;
  23. BEGIN;
  24. FOR account IN
  25. SELECT * FROM defaults
  26. WHERE setting_key like '%accno_id'
  27. LOOP
  28. RETURN NEXT account;
  29. END LOOP;
  30. END;
  31. $$ LANGUAGE plpgsql;
  32. CREATE OR REPLACE FUNCTION setting_incriment (in_key varchar) returns varchar
  33. AS
  34. $$
  35. DECLARE
  36. base_value VARCHAR;
  37. raw_value VARCHAR;
  38. incriment INTEGER;
  39. inc_length INTEGER;
  40. new_value VARCHAR;
  41. BEGIN
  42. SELECT value INTO raw_value FROM defaults
  43. WHERE setting_key = in_key
  44. FOR UPDATE;
  45. SELECT substring(raw_value from '(\\d*)(\\D*|<\\?lsmb [^<>] \\?>)*$')
  46. INTO base_value;
  47. IF base_value like '0%' THEN
  48. incriment := base_value::integer + 1;
  49. SELECT char_length(incriment::text) INTO inc_length;
  50. SELECT overlay(base_value placing incriment::varchar
  51. from (select char_length(base_value)
  52. - inc_length + 1) for inc_length)
  53. INTO new_value;
  54. ELSE
  55. new_value := base_value::integer + 1;
  56. END IF;
  57. SELECT regexp_replace(raw_value, base_value, new_value) INTO new_value;
  58. UPDATE defaults SET value = new_value WHERE setting_key = in_key;
  59. return new_value;
  60. END;
  61. $$ LANGUAGE PLPGSQL;