-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtr.func.sql
63 lines (55 loc) · 1.67 KB
/
tr.func.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* strtr.func.sql
* MySQL UDF implementation of strtr C function, added the
* functionality to remove characters when dict_to
* parameter is NULL
*
* Copyright (C) 2012 Felipe Alcacibar <[email protected]>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* License available in the following URL
* http://www.gnu.org/licenses/gpl-2.0.html
*
**/
DROP FUNCTION IF EXISTS `tr`;
DELIMITER //
CREATE FUNCTION `tr`(`str` TEXT, `dict_from` VARCHAR(1024), `dict_to` VARCHAR(1024))
RETURNS text
LANGUAGE SQL
DETERMINISTIC
NO SQL
SQL SECURITY INVOKER
COMMENT ''
BEGIN
DECLARE len INTEGER;
DECLARE i INTEGER;
IF dict_to IS NOT NULL AND (CHAR_LENGTH(dict_from) != CHAR_LENGTH(dict_to)) THEN
SET @error = CONCAT('Length of dicts does not match.');
SIGNAL SQLSTATE '49999'
SET MESSAGE_TEXT = @error;
END IF;
SET len = CHAR_LENGTH(dict_from);
SET i = 1;
WHILE len >= i DO
SET @f = SUBSTR(dict_from, i, 1);
SET @t = IF(dict_to IS NULL, '', SUBSTR(dict_to, i, 1));
SET str = REPLACE(str, @f, @t);
SET i = i + 1;
END WHILE;
RETURN str;
END
//
DELIMITER ;