-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_encode_utf8.sql
38 lines (34 loc) · 1.28 KB
/
url_encode_utf8.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
DELIMITER $$
DROP FUNCTION IF EXISTS `url_encode` $$
CREATE DEFINER=`root`@`%` FUNCTION `url_encode`(original_text text) RETURNS text CHARSET utf8
BEGIN
declare new_text text DEFAULT NULL;
declare current_char varchar(10) DEFAULT '';
declare ascii_current_char int DEFAULT 0;
declare pointer int DEFAULT 1;
declare hex_pointer int DEFAULT 1;
IF original_text IS NOT NULL then
SET new_text = '';
while pointer <= char_length(original_text) do
SET current_char = mid(original_text,pointer,1);
SET ascii_current_char = ascii(current_char);
IF current_char = ' ' then
SET current_char = '+';
ELSEIF NOT (ascii_current_char BETWEEN 48 AND 57 || ascii_current_char BETWEEN 65 AND 90 || ascii_current_char BETWEEN 97 AND 122
|| ascii_current_char = 45 || ascii_current_char = 95 || ascii_current_char = 46 || ascii_current_char = 126
|| ascii_current_char = 34
) then
SET current_char = hex(current_char);
SET hex_pointer = char_length(current_char)-1;
while hex_pointer > 0 do
SET current_char = INSERT(current_char, hex_pointer, 0, "%");
SET hex_pointer = hex_pointer - 2;
end while;
end IF;
SET new_text = concat(new_text,current_char);
SET pointer = pointer + 1;
end while;
end IF;
RETURN new_text;
END $$
DELIMITER ;