summaryrefslogtreecommitdiff
path: root/sql/modules/Entity.sql
blob: 9f7a5ac83e885519d58cafc7fc534c422013e75b (plain)
  1. --
  2. BEGIN;
  3. CREATE OR REPLACE FUNCTION entity_save(
  4. in_entity_id int, in_name text, in_entity_class INT
  5. ) RETURNS INT AS $$
  6. DECLARE
  7. e entity;
  8. e_id int;
  9. BEGIN
  10. select * into e from entity where id = in_entity_id;
  11. update
  12. entity
  13. SET
  14. name = in_name,
  15. entity_class = in_entity_class
  16. WHERE
  17. id = in_entity_id;
  18. IF NOT FOUND THEN
  19. -- do the insert magic.
  20. e_id = nextval('entity_id_seq');
  21. insert into entity (id, name, entity_class) values
  22. (e_id,
  23. in_name,
  24. in_entity_class
  25. );
  26. return e_id;
  27. END IF;
  28. return in_entity_id;
  29. END;
  30. $$ language 'plpgsql';
  31. CREATE OR REPLACE FUNCTION entity__list_classes ()
  32. RETURNS SETOF entity_class AS $$
  33. DECLARE out_row entity_class;
  34. BEGIN
  35. FOR out_row IN
  36. SELECT * FROM entity_class
  37. WHERE active
  38. ORDER BY id
  39. LOOP
  40. RETURN NEXT out_row;
  41. END LOOP;
  42. END;
  43. $$ LANGUAGE PLPGSQL;
  44. CREATE OR REPLACE FUNCTION entity__get_entity (
  45. in_entity_id int
  46. ) RETURNS setof entity AS $$
  47. declare
  48. v_row entity;
  49. BEGIN
  50. SELECT * INTO v_row FROM entity WHERE id = in_entity_id;
  51. IF NOT FOUND THEN
  52. raise exception 'Could not find entity with ID %', in_entity_id;
  53. ELSE
  54. return next v_row;
  55. END IF;
  56. END;
  57. $$ language plpgsql;
  58. commit;