MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
storedproc_10.inc
1 #### suite/funcs_1/storedproc/storedproc_10.inc
2 
3 # This test cannot be used for the embedded server because we check here
4 # privilgeges.
5 --source include/not_embedded.inc
6 
7 --source suite/funcs_1/storedproc/load_sp_tb.inc
8 
9 # ==============================================================================
10 # (numbering from requirement document TP v1.0, Last updated: 25 Jan 2005 01:00)
11 #
12 # 3.1.10 CALL checks:
13 #
14 ## 1. Ensure that a properly defined procedure can always be called, assuming
15 # the appropriate privileges exist.
16 #- 2. Ensure that a procedure cannot be called if the appropriate privileges
17 # do not exist.
18 ## 3. Ensure that a function can never be called.
19 ## 4. Ensure that a properly defined function can always be executed, assuming
20 # the appropriate privileges exist.
21 #- 5. Ensure that a function cannot be executed if the appropriate privileges
22 # do not exist.
23 ## 6. Ensure that a procedure can never be executed.
24 ## 7. Ensure that the ROW_COUNT() SQL function always returns the correct
25 # number of rows affected by the execution of a stored procedure.
26 ## 8. Ensure that the mysql_affected_rows() C API function always returns
27 # the correct number of rows affected by the execution of a
28 # stored procedure.
29 #
30 # ==============================================================================
31 let $message= Section 3.1.10 - CALL checks:;
32 --source include/show_msg80.inc
33 
34 
35 USE db_storedproc;
36 
37 # ------------------------------------------------------------------------------
38 let $message= Testcase 3.1.10.2 + 3.1.10.5:;
39 --source include/show_msg.inc
40 let $message=
41 2. Ensure that a procedure cannot be called if the appropriate privileges do not
42  exist.
43 5. Ensure that a function cannot be executed if the appropriate privileges do
44  not exist.;
45 --source include/show_msg80.inc
46 
47 --disable_warnings
48 DROP PROCEDURE IF EXISTS sp31102;
49 DROP FUNCTION IF EXISTS fn31105;
50 --enable_warnings
51 
52 # DEFINER
53 create user 'user_1'@'localhost';
54 # INVOKER
55 create user 'user_2'@'localhost';
56 
57 GRANT CREATE ROUTINE ON db_storedproc.* TO 'user_1'@'localhost';
58 GRANT SELECT ON db_storedproc.* TO 'user_2'@'localhost';
59 FLUSH PRIVILEGES;
60 
61 connect (user2_1, localhost, user_1, , db_storedproc);
62 --source suite/funcs_1/include/show_connection.inc
63 
64 delimiter //;
65 CREATE PROCEDURE sp31102 () SQL SECURITY INVOKER
66 BEGIN
67  SELECT * FROM db_storedproc.t1 WHERE f4=-5000 LIMIT 1;
68 END//
69 delimiter ;//
70 
71 delimiter //;
72 CREATE FUNCTION fn31105(n INT) RETURNS INT
73  BEGIN
74  DECLARE res INT;
75  SET res = n * n;
76  RETURN res;
77 END//
78 delimiter ;//
79 
80 disconnect user2_1;
81 
82 connect (user2_2, localhost, user_2, , db_storedproc);
83 --source suite/funcs_1/include/show_connection.inc
84 
85 # no privileges exist
86 --error ER_PROCACCESS_DENIED_ERROR
87 CALL sp31102();
88 SELECT fn31105( 9 );
89 
90 # now 'add' EXECUTE to INVOKER
91 --echo connection default;
92 connection default;
93 USE db_storedproc;
94 --source suite/funcs_1/include/show_connection.inc
95 # root can execute ...
96 CALL sp31102();
97 SELECT fn31105( 9 );
98 GRANT EXECUTE ON db_storedproc.* TO 'user_2'@'localhost';
99 FLUSH PRIVILEGES;
100 disconnect user2_2;
101 
102 # new connection
103 connect (user2_3, localhost, user_2, , db_storedproc);
104 --source suite/funcs_1/include/show_connection.inc
105 CALL sp31102();
106 SELECT fn31105( 9 );
107 disconnect user2_3;
108 
109 # now 'remove' SELECT from INVOKER
110 --echo connection default;
111 connection default;
112 USE db_storedproc;
113 --source suite/funcs_1/include/show_connection.inc
114 REVOKE EXECUTE ON db_storedproc.* FROM 'user_2'@'localhost';
115 FLUSH PRIVILEGES;
116 
117 # root can still execute
118 CALL sp31102();
119 SELECT fn31105( 9 );
120 
121 connect (user2_4, localhost, user_2, , db_storedproc);
122 --source suite/funcs_1/include/show_connection.inc
123 CALL sp31102();
124 SELECT fn31105( 9 );
125 disconnect user2_4;
126 
127 # cleanup
128 connection default;
129 USE db_storedproc;
130 
131 --source suite/funcs_1/include/show_connection.inc
132 DROP PROCEDURE sp31102;
133 DROP FUNCTION fn31105;
134 DROP USER 'user_1'@'localhost';
135 DROP USER 'user_2'@'localhost';
136 
137 
138 # ------------------------------------------------------------------------------
139 let $message= Testcase 3.1.10.3:;
140 --source include/show_msg.inc
141 let $message=
142 Ensure that a function can never be called.;
143 --source include/show_msg80.inc
144 
145 --disable_warnings
146 DROP FUNCTION IF EXISTS fn1;
147 --enable_warnings
148 
149 delimiter //;
150 CREATE FUNCTION fn1(a int) returns int
151 BEGIN
152  set @b = 0.9 * a;
153  return @b;
154 END//
155 delimiter ;//
156 
157 --error ER_SP_DOES_NOT_EXIST
158 CALL fn1();
159 
160 # cleanup
161 DROP FUNCTION fn1;
162 
163 
164 # ------------------------------------------------------------------------------
165 let $message= Testcase 3.1.10.6:;
166 --source include/show_msg.inc
167 let $message=
168 Ensure that a procedure can never be executed.;
169 --source include/show_msg80.inc
170 
171 --disable_warnings
172 DROP PROCEDURE IF EXISTS sp1;
173 DROP FUNCTION IF EXISTS sp1;
174 --enable_warnings
175 
176 delimiter //;
177 CREATE PROCEDURE sp1()
178 BEGIN
179  SELECT * from t10;
180 END//
181 delimiter ;//
182 
183 --error ER_SP_DOES_NOT_EXIST
184  SELECT sp1();
185 
186 # cleanup
187 DROP PROCEDURE sp1;
188 
189 
190 # ------------------------------------------------------------------------------
191 let $message= Testcase 3.1.10.7:;
192 --source include/show_msg.inc
193 let $message=
194 Ensure that the ROW_COUNT() SQL function always returns the correct number of
195 rows affected by the execution of a stored procedure.;
196 --source include/show_msg80.inc
197 # Note(mleich): Information taken from a comments in
198 # Bug#21818 Return value of ROW_COUNT() is incorrect for
199 # ALTER TABLE, LOAD DATA
200 # ROW_COUNT() is -1 following any statement which is not DELETE, INSERT
201 # or UPDATE.
202 # Also, after a CALL statement, ROW_COUNT() will return the value of the
203 # last statement in the stored procedure.
204 
205 --disable_warnings
206 DROP PROCEDURE IF EXISTS sp_ins_1;
207 DROP PROCEDURE IF EXISTS sp_ins_3;
208 DROP PROCEDURE IF EXISTS sp_upd;
209 DROP PROCEDURE IF EXISTS sp_ins_upd;
210 DROP PROCEDURE IF EXISTS sp_del;
211 DROP PROCEDURE IF EXISTS sp_with_rowcount;
212 --enable_warnings
213 
214 CREATE TABLE temp(f1 CHAR(20),f2 CHAR(25),f3 DATE,f4 INT,f5 CHAR(25),f6 INT);
215 INSERT INTO temp SELECT * FROM t10;
216 
217 delimiter //;
218 CREATE PROCEDURE sp_ins_1()
219 BEGIN
220  INSERT INTO temp VALUES ('abc', 'abc', '20051003', 100, 'uvw', 1000);
221 END//
222 
223 CREATE PROCEDURE sp_ins_3()
224 BEGIN
225  INSERT INTO temp VALUES ('abc', 'xyz', '19490523', 100, 'uvw', 1000);
226  INSERT INTO temp VALUES ('abc', 'xyz', '1989-11-09', 100, 'uvw', 1000);
227  INSERT INTO temp VALUES ('abc', 'xyz', '2005-10-24', 100, 'uvw', 1000);
228 END//
229 
230 CREATE PROCEDURE sp_upd()
231 BEGIN
232  UPDATE temp SET temp.f1 = 'updated' WHERE temp.f1 ='abc';
233 END//
234 
235 CREATE PROCEDURE sp_ins_upd()
236 BEGIN
237  BEGIN
238  INSERT INTO temp VALUES ('qwe', 'abc', '1989-11-09', 100, 'uvw', 1000);
239  INSERT INTO temp VALUES ('qwe', 'xyz', '1998-03-26', 100, 'uvw', 1000);
240  INSERT INTO temp VALUES ('qwe', 'abc', '2000-11-09', 100, 'uvw', 1000);
241  INSERT INTO temp VALUES ('qwe', 'abc', '2005-11-07', 100, 'uvw', 1000);
242  END;
243  SELECT COUNT( f1 ), f1 FROM temp GROUP BY f1;
244  UPDATE temp SET temp.f1 = 'updated_2' WHERE temp.f1 ='qwe' AND temp.f2 = 'abc';
245 END//
246 
247 CREATE PROCEDURE sp_del()
248 BEGIN
249  DELETE FROM temp WHERE temp.f1 ='qwe' OR temp.f1 = 'updated_2';
250 END//
251 
252 CREATE PROCEDURE sp_with_rowcount()
253 BEGIN
254  BEGIN
255  INSERT INTO temp VALUES ('qwe', 'abc', '1989-11-09', 100, 'uvw', 1000),
256  ('qwe', 'xyz', '1998-03-26', 100, 'uvw', 1000),
257  ('qwe', 'abc', '2000-11-09', 100, 'uvw', 1000),
258  ('qwe', 'xyz', '2005-11-07', 100, 'uvw', 1000);
259  END;
260  SELECT row_count() AS 'row_count() after insert';
261  SELECT row_count() AS 'row_count() after select row_count()';
262  SELECT f1,f2,f3 FROM temp ORDER BY f1,f2,f3;
263  UPDATE temp SET temp.f1 = 'updated_2' WHERE temp.f2 = 'abc';
264  SELECT row_count() AS 'row_count() after update';
265  SELECT f1,f2,f3 FROM temp ORDER BY f1,f2,f3;
266  DELETE FROM temp WHERE temp.f1 = 'updated_2';
267  SELECT row_count() AS 'row_count() after delete';
268 END//
269 delimiter ;//
270 
271 CALL sp_ins_1();
272 SELECT row_count();
273 --sorted_result
274 SELECT * FROM temp;
275 
276 CALL sp_ins_3();
277 SELECT row_count();
278 --sorted_result
279 SELECT * FROM temp;
280 
281 CALL sp_upd();
282 SELECT row_count();
283 --sorted_result
284 SELECT * FROM temp;
285 
286 CALL sp_ins_upd();
287 SELECT row_count();
288 --sorted_result
289 SELECT * FROM temp;
290 
291 CALL sp_del();
292 SELECT row_count();
293 --sorted_result
294 SELECT * FROM temp;
295 
296 DELETE FROM temp;
297 CALL sp_with_rowcount();
298 SELECT row_count();
299 --sorted_result
300 SELECT * FROM temp;
301 
302 
303 # cleanup
304 DROP PROCEDURE sp_ins_1;
305 DROP PROCEDURE sp_ins_3;
306 DROP PROCEDURE sp_upd;
307 DROP PROCEDURE sp_ins_upd;
308 DROP PROCEDURE sp_del;
309 DROP PROCEDURE sp_with_rowcount;
310 DROP TABLE temp;
311 
312 
313 # ------------------------------------------------------------------------------
314 let $message= Testcase 3.1.10.8:;
315 --source include/show_msg.inc
316 let $message=
317 Ensure that the mysql_affected_rows() C API function always returns the correct
318 number of rows affected by the execution of a stored procedure.;
319 --source include/show_msg80.inc
320 
321 #FIXME: 3.1.10.8: to be added later.
322 
323 # ==============================================================================
324 # USE the same .inc to cleanup before and after the test
325 --source suite/funcs_1/storedproc/cleanup_sp_tb.inc
326 
327 # ==============================================================================
328 --echo
329 --echo . +++ END OF SCRIPT +++
330 --echo --------------------------------------------------------------------------------
331 # ==============================================================================