1
source include/master-slave.inc;
3
# It is not possible to replicate FOUND_ROWS() using statement-based
4
# replication, but there is a workaround that stores the result of
5
# FOUND_ROWS() into a user variable and then replicates this instead.
7
# The purpose of this test case is to test that the workaround
8
# function properly even when inside stored programs (i.e., stored
9
# routines and triggers).
11
--echo ==== 0. Setting it all up ====
13
SET BINLOG_FORMAT=STATEMENT;
15
--echo **** On Master ****
17
CREATE TABLE t1 (a INT);
18
CREATE TABLE logtbl (sect INT, test INT, count INT);
20
INSERT INTO t1 VALUES (1),(2),(3);
21
INSERT INTO t1 SELECT 2*a+3 FROM t1;
22
INSERT INTO t1 SELECT 2*a+3 FROM t1;
23
INSERT INTO t1 SELECT 2*a+3 FROM t1;
24
INSERT INTO t1 SELECT 2*a+3 FROM t1;
25
INSERT INTO t1 SELECT 2*a+3 FROM t1;
26
INSERT INTO t1 SELECT 2*a+3 FROM t1;
28
--echo #### 1. Using statement mode ####
30
--echo ==== 1.1. Simple test ====
32
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
35
# INSERT INTO logtbl VALUES(1, 1, FOUND_ROWS());
37
SELECT FOUND_ROWS() INTO @a;
38
INSERT INTO logtbl VALUES(1,1,@a);
40
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a < 5 ORDER BY a LIMIT 1;
42
# INSERT INTO logtbl VALUES(1, 2, FOUND_ROWS());
44
SELECT FOUND_ROWS() INTO @a;
45
INSERT INTO logtbl VALUES(1,2,@a);
47
SELECT * FROM logtbl WHERE sect = 1 ORDER BY sect,test;
48
--echo **** On Slave ****
49
sync_slave_with_master;
50
SELECT * FROM logtbl WHERE sect = 1 ORDER BY sect,test;
52
--echo ==== 1.2. Stored procedure ====
54
# Here we do both the calculation and the logging. We also do it twice
55
# to make sure that there are no limitations on how many times it can
58
--echo **** On Master ****
61
CREATE PROCEDURE calc_and_log(sect INT, test INT) BEGIN
63
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a < 5 ORDER BY a LIMIT 1;
64
SELECT FOUND_ROWS() INTO cnt;
65
INSERT INTO logtbl VALUES(sect,test,cnt);
66
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
67
SELECT FOUND_ROWS() INTO cnt;
68
INSERT INTO logtbl VALUES(sect,test+1,cnt);
72
CALL calc_and_log(2,1);
75
CREATE PROCEDURE just_log(sect INT, test INT, found_rows INT) BEGIN
76
INSERT INTO logtbl VALUES (sect,test,found_rows);
80
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
81
SELECT FOUND_ROWS() INTO @found_rows;
82
CALL just_log(2,3,@found_rows);
84
SELECT * FROM logtbl WHERE sect = 2 ORDER BY sect,test;
85
--echo **** On Slave ****
86
sync_slave_with_master;
87
SELECT * FROM logtbl WHERE sect = 2 ORDER BY sect,test;
89
--echo ==== 1.3. Stored functions ====
90
--echo **** On Master ****
93
CREATE FUNCTION log_rows(sect INT, test INT, found_rows INT)
96
INSERT INTO logtbl VALUES(sect,test,found_rows);
101
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
102
SELECT FOUND_ROWS() INTO @found_rows;
103
SELECT log_rows(3,1,@found_rows), log_rows(3,2,@found_rows);
105
SELECT * FROM logtbl WHERE sect = 3 ORDER BY sect,test;
106
--echo **** On Slave ****
107
sync_slave_with_master;
108
SELECT * FROM logtbl WHERE sect = 3 ORDER BY sect,test;
110
--echo ==== 1.9. Cleanup ====
111
--echo **** On Master ****
114
DROP PROCEDURE just_log;
115
DROP PROCEDURE calc_and_log;
116
DROP FUNCTION log_rows;
117
sync_slave_with_master;
119
source include/reset_master_and_slave.inc;
121
--echo #### 2. Using mixed mode ####
123
--echo ==== 2.1. Checking a procedure ====
125
--echo **** On Master ****
127
SET BINLOG_FORMAT=MIXED;
129
# We will now check some stuff that will not work in statement-based
130
# replication, but which should cause the binary log to switch to
134
CREATE PROCEDURE just_log(sect INT, test INT) BEGIN
135
INSERT INTO logtbl VALUES (sect,test,FOUND_ROWS());
138
sync_slave_with_master;
140
--echo **** On Master 1 ****
142
SET BINLOG_FORMAT=MIXED;
143
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
146
--echo **** On Master ****
148
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
151
--echo **** On Master 1 ****
154
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a < 5 ORDER BY a LIMIT 1;
156
sync_slave_with_master;
158
--echo **** On Master ****
160
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
162
sync_slave_with_master;
165
SELECT * FROM logtbl WHERE sect = 1 ORDER BY sect,test;
166
--echo **** On Slave ****
167
sync_slave_with_master;
168
SELECT * FROM logtbl WHERE sect = 1 ORDER BY sect,test;
170
--echo ==== 2.1. Checking a stored function ====
171
--echo **** On Master ****
174
CREATE FUNCTION log_rows(sect INT, test INT)
177
DECLARE found_rows INT;
178
SELECT FOUND_ROWS() INTO found_rows;
179
INSERT INTO logtbl VALUES(sect,test,found_rows);
184
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a < 5 ORDER BY a LIMIT 1;
185
SELECT log_rows(2,1), log_rows(2,2);
187
CREATE TABLE t2 (a INT, b INT);
189
# Trying with referencing FOUND_ROWS() directly in the trigger.
192
CREATE TRIGGER t2_tr BEFORE INSERT ON t2 FOR EACH ROW
194
INSERT INTO logtbl VALUES (NEW.a, NEW.b, FOUND_ROWS());
198
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a < 5 ORDER BY a LIMIT 1;
199
INSERT INTO t2 VALUES (2,3), (2,4);
201
# Referencing FOUND_ROWS() indirectly.
206
CREATE TRIGGER t2_tr BEFORE INSERT ON t2 FOR EACH ROW
209
SELECT log_rows(NEW.a, NEW.b) INTO dummy;
213
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
214
INSERT INTO t2 VALUES (2,5), (2,6);
216
# Putting FOUND_ROWS() even lower in the call chain.
222
CREATE PROCEDURE log_me_inner(sect INT, test INT)
225
SELECT log_rows(sect, test) INTO dummy;
226
SELECT log_rows(sect, test+1) INTO dummy;
229
CREATE PROCEDURE log_me(sect INT, test INT)
231
CALL log_me_inner(sect,test);
236
CREATE TRIGGER t2_tr BEFORE INSERT ON t2 FOR EACH ROW
238
CALL log_me(NEW.a, NEW.b);
242
SELECT SQL_CALC_FOUND_ROWS * FROM t1 WHERE a > 5 ORDER BY a LIMIT 1;
243
INSERT INTO t2 VALUES (2,5), (2,6);
245
SELECT * FROM logtbl WHERE sect = 2 ORDER BY sect,test;
246
sync_slave_with_master;
247
SELECT * FROM logtbl WHERE sect = 2 ORDER BY sect,test;
250
DROP TABLE t1, logtbl;
251
DROP PROCEDURE just_log;
252
DROP PROCEDURE log_me;
253
DROP PROCEDURE log_me_inner;
254
DROP FUNCTION log_rows;
255
sync_slave_with_master;