summaryrefslogtreecommitdiff
path: root/utils/custom_fields/custom_fields.sql
blob: 0ee4290be6f1d951e7a47a90d04c95f8504aaf75 (plain)
  1. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  2. RETURNS BOOL AS
  3. '
  4. DECLARE
  5. table_name ALIAS FOR $1;
  6. new_field_name ALIAS FOR $2;
  7. field_datatype ALIAS FOR $3;
  8. BEGIN
  9. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  10. WHERE extends = '''''' || table_name || '''''' '';
  11. IF NOT FOUND THEN
  12. BEGIN
  13. INSERT INTO custom_table_catalog (extends)
  14. VALUES (table_name);
  15. EXECUTE ''CREATE TABLE custom_''||table_name ||
  16. '' (row_id INT)'';
  17. EXCEPTION WHEN duplicate_table THEN
  18. -- do nothing
  19. END;
  20. END IF;
  21. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  22. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  23. WHERE extends = ''''''|| table_name || ''''''))'';
  24. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  25. || new_field_name || '' '' || field_datatype;
  26. RETURN TRUE;
  27. END;
  28. ' LANGUAGE PLPGSQL;
  29. CREATE OR REPLACE FUNCTION drop_custom_field (VARCHAR, VARCHAR)
  30. RETURNS BOOL AS
  31. '
  32. DECLARE
  33. table_name ALIAS FOR $1;
  34. custom_field_name ALIAS FOR $2;
  35. BEGIN
  36. DELETE FROM custom_field_catalog
  37. WHERE field_name = custom_field_name AND
  38. table_id = (SELECT table_id FROM custom_table_catalog
  39. WHERE extends = table_name);
  40. EXECUTE ''ALTER TABLE custom_'' || table_name ||
  41. '' DROP COLUMN '' || custom_field_name;
  42. RETURN TRUE;
  43. END;
  44. ' LANGUAGE PLPGSQL;