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