Better loc or group function
authorDoug Kyle <dkyle@grpl.org>
Fri, 11 Apr 2014 14:46:31 +0000 (10:46 -0400)
committerDoug Kyle <dkyle@grpl.org>
Fri, 11 Apr 2014 14:46:31 +0000 (10:46 -0400)
Signed-off-by: Doug Kyle <dkyle@grpl.org>
Open-ILS/src/sql/Pg/smart_float.sql

index 32fcaf4..eaa08d3 100644 (file)
@@ -246,25 +246,19 @@ BEGIN
 END;
 $function$;
 
--- !!!  smart_float.loc_or_locgroup !!!
-CREATE OR REPLACE FUNCTION smart_float.loc_or_locgroup(loc integer)
- RETURNS integer                                                                                                 
+-- !!!  smart_float.loc_or_group !!!
+CREATE OR REPLACE FUNCTION smart_float.loc_or_group(loc integer) RETURNS integer                                                                                 
  LANGUAGE plpgsql
- STABLE COST 10                                                                                                                                                                   AS $function$                                                                                                                                                                     DECLARE                                                                                     
-        found_loc BOOLEAN;
-        loc_group INT;
+ STABLE COST 10                                                                                                                                                                   AS $function$                                                                                                                                                                     DECLARE                                                                                  
+        loc_group INT := 0;
 BEGIN
-        SELECT INTO found_loc loc IN (select shelf_location from config.smart_float where shelf_is_group is FALSE group by 1);
-        IF found_loc IS FALSE THEN
-                SELECT INTO loc_group lgroup from asset.copy_location_group_map where location = loc
-                        and lgroup in (select shelf_location from config.smart_float where shelf_is_group is TRUE group by 1);
-                IF FOUND THEN
-                        RETURN loc_group;
-                END IF;
+        select into loc_group shelf_location from config.smart_float where shelf_is_group is true and shelf_location in (select lgroup from asset.copy_location_group_map where location = loc);
+        IF loc_group != 0 THEN
+                RETURN loc_group;
+        ELSE
+                RETURN loc;
         END IF;
-        RETURN loc;
 END;
-$function$;
 
 -- !!! need some new indexes
 create index concurrently cp_location_idx on asset.copy (location);