summaryrefslogtreecommitdiff
path: root/sql/modules/Location.sql
blob: 565b7e1b59455fc8875f87f7bd64a9a4864e9229 (plain)
  1. -- VERSION 1.3.0
  2. CREATE OR REPLACE FUNCTION location_save
  3. (in_id int, in_companyname text, in_address1 text, in_address2 text,
  4. in_city text, in_state text, in_zipcode text, in_country text)
  5. returns integer AS
  6. $$
  7. DECLARE
  8. location_id integer;
  9. BEGIN
  10. UPDATE locations
  11. SET companyname = in_companyname,
  12. address1 = in_address1,
  13. address2 = in_address2,
  14. city = in_city,
  15. state = in_state,
  16. zipcode = in_zipcode,
  17. country = in_country
  18. WHERE id = in_id;
  19. IF FOUND THEN
  20. return in_id;
  21. END IF;
  22. INSERT INTO location
  23. (companyname, address1, address2, city, state, zipcode, country)
  24. VALUES
  25. (in_companyname, in_address1, in_address2, in_city, in_state,
  26. in_zipcode, in_country);
  27. SELECT lastval('location_id_seq') INTO location_id;
  28. return location_id;
  29. END;
  30. $$ LANGUAGE PLPGSQL;
  31. CREATE OR REPLACE FUNCTION location_get (in_id integer) returns locations AS
  32. $$
  33. DECLARE
  34. location locations%ROWTYPE;
  35. BEGIN
  36. SELECT * INTO location FROM locations WHERE id = in_id;
  37. RETURN location;
  38. END;
  39. $$ language plpgsql;
  40. CREATE OR REPLACE FUNCTION location_search
  41. (in_companyname varchar, in_address1 varchar, in_address2 varchar,
  42. in_city varchar, in_state varchar, in_zipcode varchar,
  43. in_country varchar)
  44. RETURNS SETOF locations
  45. AS
  46. $$
  47. DECLARE
  48. location locations%ROWTYPE;
  49. BEGIN
  50. FOR location IN
  51. SELECT * FROM locations
  52. WHERE companyname ilike '%' || in_companyname || '%'
  53. AND address1 ilike '%' || in_address1 || '%'
  54. AND address2 ilike '%' || in_address2 || '%'
  55. AND in_city ilike '%' || in_city || '%'
  56. AND in_state ilike '%' || in_state || '%'
  57. AND in_zipcode ilike '%' || in_zipcode || '%'
  58. AND in_country ilike '%' || in_country || '%'
  59. LOOP
  60. RETURN NEXT location;
  61. END LOOP;
  62. END;
  63. $$ LANGUAGE PLPGSQL;
  64. CREATE OR REPLACE FUNCTION location_list_all () RETURNS SETOF locations AS
  65. $$
  66. DECLARE
  67. location locations%ROWTYPE;
  68. BEGIN
  69. FOR location IN
  70. SELECT * FROM locations
  71. ORDER BY company_name, city, state, country
  72. LOOP
  73. RETURN NEXT location;
  74. END LOOP;
  75. END;
  76. $$ LANGUAGE plpgsql;
  77. CREATE OR REPLACE FUNCTION location_delete (in_id integer) RETURNS VOID AS
  78. $$
  79. BEGIN
  80. DELETE FROM locations WHERE id = in_id;
  81. END;
  82. $$ language plpgsql;