summaryrefslogtreecommitdiff
path: root/sql/modules/Location.sql
blob: 4e0cb6b198daf3bacb36cc5b4f092ff789c7660b (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 name
  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 int)
  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. coalesce(line_two, '') = coalesce(in_address2, '') AND
  38. coalesce(line_three, '') = coalesce(in_address3, '') AND
  39. city = in_city AND
  40. coalesce(state, '') = coalesce(in_state, '') AND
  41. coalesce(mail_code, '') = coalesce(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. (line_one, line_two, line_three, city, state, mail_code, country_id,
  49. created)
  50. VALUES
  51. (in_address1, in_address2, in_address3, in_city, in_state,
  52. in_zipcode, in_country, now());
  53. SELECT currval('location_id_seq') INTO location_id;
  54. return location_id;
  55. END;
  56. $$ LANGUAGE PLPGSQL;
  57. COMMENT ON function location_save
  58. (in_companyname text, in_address1 text, in_address2 text,
  59. in_city text, in_state text, in_zipcode text, in_country int) IS
  60. $$ Note that this does NOT override the data in the database.
  61. Instead we search for locations matching the desired specifications and if none
  62. are found, we insert one. Either way, the return value of the location can be
  63. used for mapping to other things. This is necessary because locations are
  64. only loosly coupled with entities, etc.$$;
  65. CREATE OR REPLACE FUNCTION location_get (in_id integer) returns location AS
  66. $$
  67. DECLARE
  68. out_location location%ROWTYPE;
  69. BEGIN
  70. SELECT * INTO out_location FROM location WHERE id = in_id;
  71. RETURN out_location;
  72. END;
  73. $$ language plpgsql;
  74. CREATE OR REPLACE FUNCTION location_search
  75. (in_companyname varchar, in_address1 varchar, in_address2 varchar,
  76. in_city varchar, in_state varchar, in_zipcode varchar,
  77. in_country varchar)
  78. RETURNS SETOF location
  79. AS
  80. $$
  81. DECLARE
  82. out_location location%ROWTYPE;
  83. BEGIN
  84. FOR out_location IN
  85. SELECT * FROM location
  86. WHERE companyname ilike '%' || in_companyname || '%'
  87. AND address1 ilike '%' || in_address1 || '%'
  88. AND address2 ilike '%' || in_address2 || '%'
  89. AND in_city ilike '%' || in_city || '%'
  90. AND in_state ilike '%' || in_state || '%'
  91. AND in_zipcode ilike '%' || in_zipcode || '%'
  92. AND in_country ilike '%' || in_country || '%'
  93. LOOP
  94. RETURN NEXT out_location;
  95. END LOOP;
  96. END;
  97. $$ LANGUAGE PLPGSQL;
  98. CREATE OR REPLACE FUNCTION location_list_all () RETURNS SETOF location AS
  99. $$
  100. DECLARE
  101. out_location location%ROWTYPE;
  102. BEGIN
  103. FOR out_location IN
  104. SELECT * FROM location
  105. ORDER BY company_name, city, state, country
  106. LOOP
  107. RETURN NEXT out_location;
  108. END LOOP;
  109. END;
  110. $$ LANGUAGE plpgsql;
  111. CREATE OR REPLACE FUNCTION location_delete (in_id integer) RETURNS VOID AS
  112. $$
  113. BEGIN
  114. DELETE FROM location WHERE id = in_id;
  115. END;
  116. $$ language plpgsql;
  117. CREATE TYPE location_result AS (
  118. id int,
  119. line_one text,
  120. line_two text,
  121. line_three text,
  122. city text,
  123. state text,
  124. mail_code text,
  125. country text,
  126. class text
  127. );