need trigger help - Mailing list pgsql-general

From Abdul-Wahid Paterson
Subject need trigger help
Date
Msg-id 995fcdb0050509073934d4fb75@mail.gmail.com
Whole thread Raw
Responses Re: need trigger help
List 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

pgsql-general by date:

Previous
From: Neil Conway
Date:
Subject: Re: "Hash index" vs. "b-tree index" (PostgreSQL 8.0)
Next
From: r92921004@ntu.edu.tw
Date:
Subject: createdb rftDatabase error and error of standard in must be a tty