need trigger help

From: Abdul-Wahid Paterson <abdulwahid(at)gmail(dot)com>
To: PgSql General <pgsql-general(at)postgresql(dot)org>
Subject: need trigger help
Date: 2005-05-09 14:39:57
Message-ID: 995fcdb0050509073934d4fb75@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I have created a simple plpgsql function as follows as included below.
The function works fine but I need to have this done automatically on
updates and inserts. So I have a table

CREATE TABLE addresses (
address_id serial PRIMARY KEY,
company varchar(250),
fname varchar(100),
lname varcahr(100,
...etc...
hash_company varchar(250),
hash_fname varchar(100),
hash_lname varchar(100)
);

The idea is, that the hashify_text function below is called for each
(required) field e.g. comapany, fname, lname etc. and a hash version
(using the word hash in a loose sense here) is created. The hash
version is then used for quick db selects so that searchin for...

'A B C Ltd.' would find the strings 'ABC ltd', 'A.B.C. ltd.', 'A B C LTD' etc.

So how can I create a trigger to automatically update the hash fields
on updates and inserts?

CREATE FUNCTION hashify_text(TEXT) RETURNS TEXT AS '
DECLARE
out_text TEXT := '''';
in_text TEXT;
index INTEGER := 0;
max INTEGER;
tmp CHAR;

BEGIN
in_text := $1;
max = char_length(in_text);

FOR i IN 1 .. max LOOP
tmp = upper(substring(in_text from i for 1));
IF ( strpos(''01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'', tmp) > 0 ) THEN
out_text := out_text || tmp;
END IF;
END LOOP;

RETURN out_text;

END;

' LANGUAGE 'plpgsql';

Regards,

Abdul-Wahid

Responses

Browse pgsql-general by date

  From Date Subject
Next Message r92921004 2005-05-09 14:45:57 createdb rftDatabase error and error of standard in must be a tty
Previous Message Sim Zacks 2005-05-09 14:38:42 Re: function call error