x
1
BEGIN
2
3
DECLARE table_name varchar(50) DEFAULT '';
4
DECLARE columns_list varchar(2000) DEFAULT '';
5
DECLARE values_list text DEFAULT '';
6
DECLARE cur_position INT DEFAULT 1;
7
DECLARE cur_string VARCHAR(2000);
8
DECLARE cur_table varchar(50) DEFAULT '';
9
DECLARE cur_column varchar(50);
10
DECLARE cur_value VARCHAR(2000);
11
DECLARE delimiter_length TINYINT UNSIGNED;
12
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
13
DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
14
15
START TRANSACTION;
16
17
18
SET cur_position = 1;
19
20
21
SET delimiter_length = CHAR_LENGTH('|');
22
23
24
25
WHILE CHAR_LENGTH(incoming) > 0 AND cur_position > 0 DO
26
SET cur_position = INSTR(incoming, '|');
27
28
IF cur_position = 0 THEN
29
SET cur_string = incoming;
30
ELSE
31
SET cur_string = LEFT(incoming, cur_position - 1);
32
END IF;
33
34
35
36
37
IF TRIM(cur_string) != '' THEN
38
SET cur_table = SUBSTRING_INDEX(cur_string, '.', 1);
39
SET cur_column = SUBSTRING_INDEX(SUBSTRING_INDEX(cur_string, '.', (LENGTH(REPLACE(cur_string, '.', '')) -LENGTH(cur_string))), ':', 1);
40
SET cur_value = CONCAT(''', SUBSTRING_INDEX(cur_string, ':', (LENGTH(REPLACE(cur_string, ':', '')) - LENGTH(cur_string))), ''');
41
42
43
If table_name <> cur_table and table_name <> '' THEN
44
SELECT CONCAT('REPLACE INTO ', table_name, '(', columns_list, ') VALUES (', values_list, ');') into @executestatement;
45
PREPARE stmtinsert FROM @executestatement;
46
EXECUTE stmtinsert;
47
SET @executestatement = '';
48
SET columns_list = '';
49
SET values_list = '';
50
END IF;
51
52
SET table_name = cur_table;
53
IF columns_list = '' THEN
54
SET columns_list = CONCAT(columns_list, cur_column);
55
ELSE
56
SET columns_list = CONCAT(columns_list, ',', cur_column);
57
END IF;
58
IF values_list = '' THEN
59
SET values_list = CONCAT(values_list, cur_value);
60
ELSE
61
SET values_list = CONCAT(values_list, ',', cur_value);
62
END IF;
63
END IF;
64
65
SET incoming = SUBSTRING(incoming, cur_position + delimiter_length);
66
67
68
END WHILE;
69
70
71
SELECT CONCAT('REPLACE INTO ', table_name, '(', columns_list, ') VALUES (', values_list, ');') into @executestatement;
72
PREPARE stmtinsertfinal FROM @executestatement;
73
EXECUTE stmtinsertfinal;
74
SET @executestatement = '';
75
76
COMMIT;
77
78
END