-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_replace.sql
More file actions
73 lines (55 loc) · 2.42 KB
/
str_replace.sql
File metadata and controls
73 lines (55 loc) · 2.42 KB
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
64
65
66
67
68
69
70
71
72
73
CREATE DEFINER=`root`@`localhost` FUNCTION `str_replace`(string_search TEXT CHARSET utf8, string_replace TEXT CHARSET utf8, string_subject TEXT CHARSET utf8) RETURNS text CHARSET utf8
BEGIN
-- string_search/string_replace items separator
DECLARE ws TEXT CHARSET utf8 default ',';
-- ghost letter - one char that can't exist in string_subject
DECLARE ghost TEXT CHARSET utf8 default '\0';
-- string in which I will search items and replace them with ghosts
DECLARE string_test TEXT CHARSET utf8 default '';
-- result string
DECLARE string_output TEXT CHARSET utf8 default '';
-- how many items to search
DECLARE string_search_items INT DEFAULT 0;
-- how many items to replace
DECLARE string_replace_items INT DEFAULT 0;
-- iterator
DECLARE iterator INT DEFAULT 0;
-- position of replacement
DECLARE pos INT DEFAULT 0;
-- one item to search
DECLARE replace_from TEXT CHARSET utf8 default '';
-- corresponding item to replace
DECLARE replace_to TEXT CHARSET utf8 default '';
-- length of item to search
DECLARE replace_from_length INT DEFAULT 0;
-- replacement in string_test
DECLARE replace_to_ghost TEXT CHARSET utf8 default '';
SET string_test = string_subject;
SET string_output = string_subject;
SET string_search_items = char_length(string_search)-char_length(replace(string_search, ws, ''));
SET string_replace_items = char_length(string_replace)-char_length(replace(string_replace, ws, ''));
-- lengths of string_search and string_replace have to fit together
IF string_search_items > string_replace_items THEN
SET string_replace = concat(string_replace, REPEAT(ws, string_search_items-string_replace_items));
END IF;
-- iteration over string_search items
iteration: LOOP
IF iterator > string_search_items THEN
LEAVE iteration;
END IF;
SET iterator = iterator + 1;
SET replace_from = substring_index(substring_index(string_search, ws, iterator), ws, -1);
SET replace_to = substring_index(substring_index(string_replace, ws, iterator), ws, -1);
SET replace_from_length = char_length(replace_from);
SET replace_to_ghost = repeat(ghost, char_length(replace_to));
replacing: LOOP
SET pos = LOCATE(replace_from, string_test);
IF pos = 0 THEN
LEAVE replacing;
END IF;
SET string_output = insert(string_output, pos, replace_from_length, replace_to);
SET string_test = insert(string_test, pos, replace_from_length, replace_to_ghost);
END LOOP replacing;
END LOOP iteration;
RETURN string_output;
END