MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ps_modify.inc
1 ###################### ps_modify.inc #########################
2 # #
3 # Tests for prepared statements: INSERT/DELETE/UPDATE... #
4 # #
5 ##############################################################
6 
7 #
8 # NOTE: PLEASE SEE ps_1general.test (bottom)
9 # BEFORE ADDING NEW TEST CASES HERE !!!
10 
11 #
12 # Please be aware, that this file will be sourced by several test case files
13 # stored within the subdirectory 't'. So every change here will affect
14 # several test cases.
15 #
16 # Please do not modify the structure (DROP/ALTER..) of the tables
17 # 't1' and 't9'.
18 #
19 # But you are encouraged to use these two tables within your statements
20 # (DELETE/UPDATE/...) whenever possible.
21 # t1 - very simple table
22 # t9 - table with nearly all available column types
23 #
24 # The structure and the content of these tables can be found in
25 # include/ps_create.inc CREATE TABLE ...
26 # include/ps_renew.inc DELETE all rows and INSERT some rows
27 #
28 # Both tables are managed by the same storage engine.
29 # The type of the storage engine is stored in the variable '$type' .
30 
31 
32 
33 #------------------- Please insert your test cases here -------------------#
34 
35 
36 
37 #-------- Please be very carefull when editing behind this line ----------#
38 
39 --disable_query_log
40 select '------ delete tests ------' as test_sequence ;
41 --enable_query_log
42 --source include/ps_renew.inc
43 
44 ## delete without parameter
45 prepare stmt1 from 'delete from t1 where a=2' ;
46 execute stmt1;
47 select a,b from t1 where a=2;
48 # delete with row not found
49 execute stmt1;
50 
51 ## delete with one parameter in the where clause
52 insert into t1 values(0,NULL);
53 set @arg00=NULL;
54 prepare stmt1 from 'delete from t1 where b=?' ;
55 execute stmt1 using @arg00;
56 select a,b from t1 where b is NULL ;
57 set @arg00='one';
58 execute stmt1 using @arg00;
59 select a,b from t1 where b=@arg00;
60 
61 ## truncate a table
62 prepare stmt1 from 'truncate table t1' ;
63 
64 
65 --disable_query_log
66 select '------ update tests ------' as test_sequence ;
67 --enable_query_log
68 --source include/ps_renew.inc
69 
70 ## update without parameter
71 prepare stmt1 from 'update t1 set b=''a=two'' where a=2' ;
72 execute stmt1;
73 select a,b from t1 where a=2;
74 # dummy update
75 execute stmt1;
76 select a,b from t1 where a=2;
77 
78 ## update with one parameter in the set clause
79 set @arg00=NULL;
80 prepare stmt1 from 'update t1 set b=? where a=2' ;
81 execute stmt1 using @arg00;
82 select a,b from t1 where a=2;
83 set @arg00='two';
84 execute stmt1 using @arg00;
85 select a,b from t1 where a=2;
86 
87 ## update with one parameter in the where cause
88 set @arg00=2;
89 prepare stmt1 from 'update t1 set b=NULL where a=?' ;
90 execute stmt1 using @arg00;
91 select a,b from t1 where a=@arg00;
92 update t1 set b='two' where a=@arg00;
93 # row not found in update
94 set @arg00=2000;
95 execute stmt1 using @arg00;
96 select a,b from t1 where a=@arg00;
97 
98 ## update on primary key column (two parameters)
99 set @arg00=2;
100 set @arg01=22;
101 prepare stmt1 from 'update t1 set a=? where a=?' ;
102 # dummy update
103 execute stmt1 using @arg00, @arg00;
104 select a,b from t1 where a=@arg00;
105 execute stmt1 using @arg01, @arg00;
106 select a,b from t1 where a=@arg01;
107 execute stmt1 using @arg00, @arg01;
108 select a,b from t1 where a=@arg00;
109 set @arg00=NULL;
110 set @arg01=2;
111 execute stmt1 using @arg00, @arg01;
112 select a,b from t1 order by a;
113 set @arg00=0;
114 execute stmt1 using @arg01, @arg00;
115 select a,b from t1 order by a;
116 
117 ## update with subquery and several parameters
118 set @arg00=23;
119 set @arg01='two';
120 set @arg02=2;
121 set @arg03='two';
122 set @arg04=2;
123 --disable_warnings
124 drop table if exists t2;
125 --enable_warnings
126 # t2 will be of table type 'MYISAM'
127 create table t2 as select a,b from t1 ;
128 prepare stmt1 from 'update t1 set a=? where b=?
129  and a in (select ? from t2
130  where b = ? or a = ?)';
131 --enable_info
132 execute stmt1 using @arg00, @arg01, @arg02, @arg03, @arg04 ;
133 --disable_info
134 select a,b from t1 where a = @arg00 ;
135 prepare stmt1 from 'update t1 set a=? where b=?
136  and a not in (select ? from t2
137  where b = ? or a = ?)';
138 --enable_info
139 execute stmt1 using @arg04, @arg01, @arg02, @arg03, @arg00 ;
140 --disable_info
141 select a,b from t1 order by a ;
142 drop table t2 ;
143 # t2 is now of table type '$type'
144 # The test battery for table type 'MERGE' gets here only a 'MYISAM' table
145 #
146 # Test UPDATE with SUBQUERY in prepared mode
147 #
148 eval create table t2
149 (
150  a int, b varchar(30),
151  primary key(a)
152 ) engine = $type ;
153 insert into t2(a,b) select a, b from t1 ;
154 prepare stmt1 from 'update t1 set a=? where b=?
155  and a in (select ? from t2
156  where b = ? or a = ?)';
157 --enable_info
158 execute stmt1 using @arg00, @arg01, @arg02, @arg03, @arg04 ;
159 --disable_info
160 select a,b from t1 where a = @arg00 ;
161 prepare stmt1 from 'update t1 set a=? where b=?
162  and a not in (select ? from t2
163  where b = ? or a = ?)';
164 --enable_info
165 execute stmt1 using @arg04, @arg01, @arg02, @arg03, @arg00 ;
166 --disable_info
167 select a,b from t1 order by a ;
168 drop table t2 ;
169 
170 ## update with parameters in limit
171 set @arg00=1;
172 prepare stmt1 from 'update t1 set b=''bla''
173 where a=2
174 limit 1';
175 execute stmt1 ;
176 select a,b from t1 where b = 'bla' ;
177 prepare stmt1 from 'update t1 set b=''bla'' where a=2 limit ?';
178 execute stmt1 using @arg00;
179 
180 --disable_query_log
181 select '------ insert tests ------' as test_sequence ;
182 --enable_query_log
183 --source include/ps_renew.inc
184 
185 ## insert without parameter
186 prepare stmt1 from 'insert into t1 values(5, ''five'' )';
187 execute stmt1;
188 select a,b from t1 where a = 5;
189 
190 ## insert with one parameter in values part
191 set @arg00='six' ;
192 prepare stmt1 from 'insert into t1 values(6, ? )';
193 execute stmt1 using @arg00;
194 select a,b from t1 where b = @arg00;
195 # the second insert fails, because the first column is primary key
196 --error ER_DUP_ENTRY
197 execute stmt1 using @arg00;
198 set @arg00=NULL ;
199 prepare stmt1 from 'insert into t1 values(0, ? )';
200 execute stmt1 using @arg00;
201 select a,b from t1 where b is NULL;
202 
203 ## insert with two parameter in values part
204 set @arg00=8 ;
205 set @arg01='eight' ;
206 prepare stmt1 from 'insert into t1 values(?, ? )';
207 execute stmt1 using @arg00, @arg01 ;
208 select a,b from t1 where b = @arg01;
209 # cases derived from client_test.c: test_null()
210 set @NULL= null ;
211 set @arg00= 'abc' ;
212 # execute must fail, because first column is primary key (-> not null)
213 --error 1048
214 execute stmt1 using @NULL, @NULL ;
215 --error 1048
216 execute stmt1 using @NULL, @NULL ;
217 --error 1048
218 execute stmt1 using @NULL, @arg00 ;
219 --error 1048
220 execute stmt1 using @NULL, @arg00 ;
221 let $1 = 2;
222 while ($1)
223 {
224  eval set @arg01= 10000 + $1 ;
225  execute stmt1 using @arg01, @arg00 ;
226  dec $1;
227 }
228 select * from t1 where a > 10000 order by a ;
229 delete from t1 where a > 10000 ;
230 let $1 = 2;
231 while ($1)
232 {
233  eval set @arg01= 10000 + $1 ;
234  execute stmt1 using @arg01, @NULL ;
235  dec $1;
236 }
237 select * from t1 where a > 10000 order by a ;
238 delete from t1 where a > 10000 ;
239 let $1 = 10;
240 while ($1)
241 {
242  eval set @arg01= 10000 + $1 ;
243  execute stmt1 using @arg01, @arg01 ;
244  dec $1;
245 }
246 select * from t1 where a > 10000 order by a ;
247 delete from t1 where a > 10000 ;
248 
249 
250 ## insert with two rows in values part
251 set @arg00=81 ;
252 set @arg01='8-1' ;
253 set @arg02=82 ;
254 set @arg03='8-2' ;
255 prepare stmt1 from 'insert into t1 values(?,?),(?,?)';
256 execute stmt1 using @arg00, @arg01, @arg02, @arg03 ;
257 --sorted_result
258 select a,b from t1 where a in (@arg00,@arg02) ;
259 
260 ## insert with two parameter in the set part
261 set @arg00=9 ;
262 set @arg01='nine' ;
263 prepare stmt1 from 'insert into t1 set a=?, b=? ';
264 execute stmt1 using @arg00, @arg01 ;
265 select a,b from t1 where a = @arg00 ;
266 
267 ## insert with parameters in the ON DUPLICATE KEY part
268 set @arg00=6 ;
269 set @arg01=1 ;
270 prepare stmt1 from 'insert into t1 set a=?, b=''sechs''
271  on duplicate key update a=a + ?, b=concat(b,''modified'') ';
272 execute stmt1 using @arg00, @arg01;
273 select * from t1 order by a;
274 set @arg00=81 ;
275 set @arg01=1 ;
276 --error ER_DUP_ENTRY
277 execute stmt1 using @arg00, @arg01;
278 
279 ## insert, autoincrement column and ' SELECT LAST_INSERT_ID() '
280 # cases derived from client_test.c: test_bug3117()
281 --disable_warnings
282 drop table if exists t2 ;
283 --enable_warnings
284 # The test battery for table type 'MERGE' gets here only a 'MYISAM' table
285 eval create table t2 (id int auto_increment primary key)
286 ENGINE= $type ;
287 prepare stmt1 from ' select last_insert_id() ' ;
288 insert into t2 values (NULL) ;
289 execute stmt1 ;
290 insert into t2 values (NULL) ;
291 # bug#3117
292 execute stmt1 ;
293 drop table t2 ;
294 
295 ## many parameters
296 set @1000=1000 ;
297 set @x1000_2="x1000_2" ;
298 set @x1000_3="x1000_3" ;
299 
300 set @x1000="x1000" ;
301 set @1100=1100 ;
302 set @x1100="x1100" ;
303 set @100=100 ;
304 set @updated="updated" ;
305 insert into t1 values(1000,'x1000_1') ;
306 insert into t1 values(@1000,@x1000_2),(@1000,@x1000_3)
307  on duplicate key update a = a + @100, b = concat(b,@updated) ;
308 select a,b from t1 where a >= 1000 order by a ;
309 delete from t1 where a >= 1000 ;
310 insert into t1 values(1000,'x1000_1') ;
311 prepare stmt1 from ' insert into t1 values(?,?),(?,?)
312  on duplicate key update a = a + ?, b = concat(b,?) ';
313 execute stmt1 using @1000, @x1000_2, @1000, @x1000_3, @100, @updated ;
314 select a,b from t1 where a >= 1000 order by a ;
315 delete from t1 where a >= 1000 ;
316 insert into t1 values(1000,'x1000_1') ;
317 execute stmt1 using @1000, @x1000_2, @1100, @x1000_3, @100, @updated ;
318 select a,b from t1 where a >= 1000 order by a ;
319 delete from t1 where a >= 1000 ;
320 
321 ## replace
322 prepare stmt1 from ' replace into t1 (a,b) select 100, ''hundred'' ';
323 execute stmt1;
324 execute stmt1;
325 execute stmt1;
326 
327 
328 
329 ## multi table statements
330 --disable_query_log
331 select '------ multi table tests ------' as test_sequence ;
332 --enable_query_log
333 # cases derived from client_test.c: test_multi
334 delete from t1 ;
335 delete from t9 ;
336 insert into t1(a,b) values (1, 'one'), (2, 'two'), (3, 'three') ;
337 insert into t9 (c1,c21)
338  values (1, 'one'), (2, 'two'), (3, 'three') ;
339 prepare stmt_delete from " delete t1, t9
340  from t1, t9 where t1.a=t9.c1 and t1.b='updated' ";
341 prepare stmt_update from " update t1, t9
342  set t1.b='updated', t9.c21='updated'
343  where t1.a=t9.c1 and t1.a=? ";
344 prepare stmt_select1 from " select a, b from t1 order by a" ;
345 prepare stmt_select2 from " select c1, c21 from t9 order by c1" ;
346 set @arg00= 1 ;
347 let $1= 3 ;
348 while ($1)
349 {
350  execute stmt_update using @arg00 ;
351  execute stmt_delete ;
352  execute stmt_select1 ;
353  execute stmt_select2 ;
354  set @arg00= @arg00 + 1 ;
355  dec $1 ;
356 }
357