summaryrefslogtreecommitdiff
path: root/sql/modules
diff options
context:
space:
mode:
authoreinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-11-18 18:19:52 +0000
committereinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-11-18 18:19:52 +0000
commit5398dde91bacbc86db0614214d0bdcaedf142e67 (patch)
tree84769c263a6ccc69d63737b1afba796870f656db /sql/modules
parentee03e8e74922d77cd6cd6114166f2a3a537f17c3 (diff)
Location_save rewritten to be safe for the many-many mappings
git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1878 4979c152-3d1c-0410-bac9-87ea11338e46
Diffstat (limited to 'sql/modules')
-rw-r--r--sql/modules/Location.sql31
1 files changed, 21 insertions, 10 deletions
diff --git a/sql/modules/Location.sql b/sql/modules/Location.sql
index a379fefb..fb93cf82 100644
--- a/sql/modules/Location.sql
+++ b/sql/modules/Location.sql
@@ -1,23 +1,25 @@
-- VERSION 1.3.0
CREATE OR REPLACE FUNCTION location_save
-(in_id int, in_companyname text, in_address1 text, in_address2 text,
+(in_companyname text, in_address1 text, in_address2 text,
in_city text, in_state text, in_zipcode text, in_country text)
returns integer AS
$$
DECLARE
location_id integer;
+ location_row RECORD;
BEGIN
- UPDATE location
- SET companyname = in_companyname,
- address1 = in_address1,
- address2 = in_address2,
- city = in_city,
- state = in_state,
- zipcode = in_zipcode,
+
+ SELECT * INTO location_row FROM location
+ WHERE companyname = in_companyname AND
+ address1 = in_address1 AND
+ address2 = in_address2 AND
+ city = in_city AND
+ state = in_state AND
+ zipcode = in_zipcode AND
country = in_country
- WHERE id = in_id;
+ LIMIT 1;
IF FOUND THEN
- return in_id;
+ return location_row.id;
END IF;
INSERT INTO location
(companyname, address1, address2, city, state, zipcode, country)
@@ -29,6 +31,15 @@ BEGIN
END;
$$ LANGUAGE PLPGSQL;
+COMMENT ON function location_save
+(in_companyname text, in_address1 text, in_address2 text,
+ in_city text, in_state text, in_zipcode text, in_country text) IS
+$$ Note that this does NOT override the data in the database.
+Instead we search for locations matching the desired specifications and if none
+are found, we insert one. Either way, the return value of the location can be
+used for mapping to other things. This is necessary because locations are
+only loosly coupled with entities, etc.$$;
+
CREATE OR REPLACE FUNCTION location_get (in_id integer) returns location AS
$$
DECLARE