summaryrefslogtreecommitdiff
path: root/sql/modules/Location.sql
blob: 51fb1d02b75f819ffdb2075c252984613aff26b0 (plain)
  1. -- VERSION 1.3.0
  2. CREATE OR REPLACE FUNCTION location_list_class()
  3. RETURNS SETOF location_class AS
  4. $$
  5. DECLARE out_row RECORD;
  6. BEGIN
  7. FOR out_row IN
  8. SELECT * FROM location_class ORDER BY id
  9. LOOP
  10. RETURN NEXT out_row;
  11. END LOOP;
  12. END;
  13. $$ language plpgsql;
  14. CREATE OR REPLACE FUNCTION location_list_country()
  15. RETURNS SETOF country AS
  16. $$
  17. DECLARE out_row RECORD;
  18. BEGIN
  19. FOR out_row IN
  20. SELECT * FROM country ORDER BY id
  21. LOOP
  22. RETURN NEXT out_row;
  23. END LOOP;
  24. END;
  25. $$ language plpgsql;
  26. CREATE OR REPLACE FUNCTION location_save
  27. (in_address1 text, in_address2 text, in_address3 text,
  28. in_city text, in_state text, in_zipcode text, in_country text)
  29. returns integer AS
  30. $$
  31. DECLARE
  32. location_id integer;
  33. location_row RECORD;
  34. BEGIN
  35. SELECT * INTO location_row FROM location
  36. WHERE line_one = in_address1 AND
  37. line_two = in_address2 AND
  38. line_three = in_address3 AND
  39. city = in_city AND
  40. state = in_state AND
  41. zipcode = in_zipcode AND
  42. country_id = in_country
  43. LIMIT 1;
  44. IF FOUND THEN
  45. return location_row.id;
  46. END IF;
  47. INSERT INTO location
  48. (companyname, address1, address2, city, state, zipcode, country_id)
  49. VALUES
  50. (in_companyname, in_address1, in_address2, in_city, in_state,
  51. in_zipcode, in_country);
  52. SELECT lastval('location_id_seq') INTO location_id;
  53. return location_id;
  54. END;
  55. $$ LANGUAGE PLPGSQL;
  56. COMMENT ON function location_save
  57. (in_companyname text, in_address1 text, in_address2 text,
  58. in_city text, in_state text, in_zipcode text, in_country text) IS
  59. $$ Note that this does NOT override the data in the database.
  60. Instead we search for locations matching the desired specifications and if none
  61. are found, we insert one. Either way, the return value of the location can be
  62. used for mapping to other things. This is necessary because locations are
  63. only loosly coupled with entities, etc.$$;
  64. CREATE OR REPLACE FUNCTION location_get (in_id integer) returns location AS
  65. $$
  66. DECLARE
  67. out_location location%ROWTYPE;
  68. BEGIN
  69. SELECT * INTO out_location FROM location WHERE id = in_id;
  70. RETURN out_location;
  71. END;
  72. $$ language plpgsql;
  73. CREATE OR REPLACE FUNCTION location_search
  74. (in_companyname varchar, in_address1 varchar, in_address2 varchar,
  75. in_city varchar, in_state varchar, in_zipcode varchar,
  76. in_country varchar)
  77. RETURNS SETOF location
  78. AS
  79. $$
  80. DECLARE
  81. out_location location%ROWTYPE;
  82. BEGIN
  83. FOR out_location IN
  84. SELECT * FROM location
  85. WHERE companyname ilike '%' || in_companyname || '%'
  86. AND address1 ilike '%' || in_address1 || '%'
  87. AND address2 ilike '%' || in_address2 || '%'
  88. AND in_city ilike '%' || in_city || '%'
  89. AND in_state ilike '%' || in_state || '%'
  90. AND in_zipcode ilike '%' || in_zipcode || '%'
  91. AND in_country ilike '%' || in_country || '%'
  92. LOOP
  93. RETURN NEXT out_location;
  94. END LOOP;
  95. END;
  96. $$ LANGUAGE PLPGSQL;
  97. CREATE OR REPLACE FUNCTION location_list_all () RETURNS SETOF location AS
  98. $$
  99. DECLARE
  100. out_location location%ROWTYPE;
  101. BEGIN
  102. FOR out_location IN
  103. SELECT * FROM location
  104. ORDER BY company_name, city, state, country
  105. LOOP
  106. RETURN NEXT out_location;
  107. END LOOP;
  108. END;
  109. $$ LANGUAGE plpgsql;
  110. CREATE OR REPLACE FUNCTION location_delete (in_id integer) RETURNS VOID AS
  111. $$
  112. BEGIN
  113. DELETE FROM location WHERE id = in_id;
  114. END;
  115. $$ language plpgsql;