While looking to implement the ODBC replace() function (replace occurences
of $2 in $1 by $3), I found that it could be expressed as:

CREATE FUNCTION replace(text, text, text) RETURNS text AS '
    select
        case when position($2 in $1) = 0 or char_length($2) = 0
            then $1
            else substring($1 from 1 for position($2 in $1) - 1)
                 || $3
                 || replace(substring($1 from position($2 in $1) + char_length($2)), 
$2, $3)
        end;
' LANGUAGE SQL WITH (isstrict);

Now this command doesn't actually work because it requires the replace()
function to exist already.  But it does work if one first creates a stub
replace() function and then uses CREATE OR REPLACE.

(So much about the claim that procedural languages are a security hole
because they allow infinite loops.)

I was wondering whether, as a future project, we could make this more
convenient by parsing the body of the function with the binding of the
function already in effect.

Comments?

-- 
Peter Eisentraut   [EMAIL PROTECTED]   http://funkturm.homeip.net/~peter


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

Reply via email to