From 5398dde91bacbc86db0614214d0bdcaedf142e67 Mon Sep 17 00:00:00 2001 From: einhverfr Date: Sun, 18 Nov 2007 18:19:52 +0000 Subject: 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 --- sql/modules/Location.sql | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'sql/modules') 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 -- cgit v1.2.3