Thursday, June 23, 2005
Using the LOOP construct
Here's a version using the LOOP construct:
DROP FUNCTION encrypto;
delimiter //
CREATE FUNCTION encrypto (s CHAR(255)) RETURNS char(255)
BEGIN
DECLARE mylen INT;
DECLARE cnt INT;
DECLARE result CHAR(255);
SET result = "";
SET mylen = LENGTH(s); -- oohh, this hurts multibyte!
SET cnt = 1; -- SUBSTRING starts from index 1!
crypto: LOOP
SET result = CONCAT(result,CHAR(ORD(SUBSTRING(s,cnt,1))+1));
SET cnt = cnt + 1;
IF cnt > mylen THEN LEAVE crypto; ELSE ITERATE crypto; END IF;
END LOOP crypto;
RETURN result;
END
//