summaryrefslogtreecommitdiff
path: root/sql/modules/Person.sql
blob: 0426045dd9928fe089a22f5ec47320662a5da5b6 (plain)
  1. begin;
  2. CREATE OR REPLACE FUNCTION person_save
  3. (in_id integer, in_salutation int,
  4. in_first_name text, in_last_name text
  5. )
  6. RETURNS INT AS $$
  7. DECLARE
  8. e_id int;
  9. e entity;
  10. loc location;
  11. l_id int;
  12. per person;
  13. p_id int;
  14. BEGIN
  15. select * into e from entity where id = in_id and entity_class = 3;
  16. IF NOT FOUND THEN
  17. RAISE EXCEPTION 'No entity found for ID %', in_id;
  18. END IF;
  19. select * into per FROM person WHERE entity_id = in_id;
  20. IF FOUND THEN
  21. -- do an update
  22. UPDATE person SET
  23. salutation = in_salutation,
  24. first_name = in_first_name,
  25. last_name = in_last_name
  26. WHERE
  27. entity_id = in_id
  28. AND
  29. id = per.id;
  30. ELSE
  31. -- Do an insert
  32. INSERT INTO person (salutation, first_name, last_name) VALUES
  33. (in_salutation, in_first_name, in_last_name);
  34. END IF;
  35. END;
  36. $$ language plpgsql;
  37. commit;