· 7 years ago · Jan 15, 2019, 12:40 PM
1CREATE PROCEDURE xxx_update_or_insert (IN value_val mediumtext)
2 BEGIN
3 IF EXISTS (SELECT 1=1 FROM xxx_infos WHERE xx=yy AND foo=bar) THEN
4 UPDATE xxx_infos SET
5 value=value_val
6 WHERE xx=yy AND foo=bar;
7 ELSE
8 INSERT INTO xxx_infos(value)
9 VALUES (value_val);
10 END IF;
11 END;;
12
13CREATE TABLE `xxx_infos` (
14 `id` int(11) NOT NULL AUTO_INCREMENT,
15 `value` mediumtext NOT NULL, -- note: ~8KB json or raw txt
16 -- other columns...
17 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
18
19stmt, _ := db.Prepare('call xxx_update_or_insert(?)')
20defer stmt.Close()
21for _, val := range values {
22 if dropData {
23 continue // If continue here, no memory leak, but when get down, leak.
24 }
25
26 _, err := stmt.Exec(Value)
27 if err != nil { ... } // no error throw here!
28}