address alert : DB func added to schema
authorBill Erickson <berick@esilibrary.com>
Fri, 9 Dec 2011 14:48:36 +0000 (09:48 -0500)
committerBill Erickson <berick@esilibrary.com>
Fri, 9 Dec 2011 14:48:36 +0000 (09:48 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/sql/Pg/999.functions.global.sql

index 4eff147..1ce50e1 100644 (file)
@@ -2035,3 +2035,35 @@ for my $key ( keys %map ) {
 return $default;
 
 $f$ LANGUAGE PLPERLU;
+
+CREATE OR REPLACE FUNCTION actor.address_alert_matches 
+    (org_unit INT, street1 TEXT, street2 TEXT, city TEXT, county TEXT, state TEXT, country TEXT, post_code TEXT) 
+    RETURNS SETOF actor.address_alert AS $$
+SELECT *
+FROM actor.address_alert
+WHERE
+    owner IN (SELECT id FROM actor.org_unit_ancestors($1)) AND (
+        (
+            match_all
+            AND LOWER(COALESCE($2, '')) ~ LOWER(COALESCE(street1,   '.*'))
+            AND LOWER(COALESCE($3, '')) ~ LOWER(COALESCE(street2,   '.*'))
+            AND LOWER(COALESCE($4, '')) ~ LOWER(COALESCE(city,      '.*'))
+            AND LOWER(COALESCE($5, '')) ~ LOWER(COALESCE(county,    '.*'))
+            AND LOWER(COALESCE($6, '')) ~ LOWER(COALESCE(state,     '.*'))
+            AND LOWER(COALESCE($7, '')) ~ LOWER(COALESCE(country,   '.*'))
+            AND LOWER(COALESCE($8, '')) ~ LOWER(COALESCE(post_code, '.*'))
+        ) OR (
+            NOT match_all AND (
+               LOWER($2) ~ LOWER(street1)
+            OR LOWER($3) ~ LOWER(street2)
+            OR LOWER($4) ~ LOWER(city)
+            OR LOWER($5) ~ LOWER(county)
+            OR LOWER($6) ~ LOWER(state)
+            OR LOWER($7) ~ LOWER(country)
+            OR LOWER($8) ~ LOWER(post_code)
+            )
+        )
+    )
+    ORDER BY actor.org_unit_proximity(owner, $1)
+$$ LANGUAGE SQL;
+