2 # Simple Test file to test all mysqltcl commands and parameters
3 # up from version mysqltcl 3.0 and mysql 4.1
5 # please adapt the parameters for mysqlconnect some lines above
7 if {[file exists libload.tcl]} {
10 source [file join [file dirname [info script]] libload.tcl]
13 # global connect variables
16 set dbank mysqltcltest
18 package require tcltest
19 variable SETUP {#common setup code}
20 variable CLEANUP {#common cleanup code}
21 tcltest::configure -verbose bet
23 proc getConnection {{addOptions {}} {withDB 1}} {
24 global dbuser dbpassword dbank
26 append addOptions " -db $dbank"
28 if {$dbpassword ne ""} {
29 append addOptions " -password $dbpassword"
31 return [eval mysqlconnect -user $dbuser $addOptions]
33 proc prepareTestDB {} {
35 set handle [getConnection {} 0]
36 if {[lsearch [mysqlinfo $handle databases] $dbank]<0} {
37 puts "Testdatabase $dbank does not exist. Create it"
38 mysqlexec $handle "CREATE DATABASE $dbank"
40 mysqluse $handle $dbank
42 catch {mysql::exec $handle {drop table transtest}}
44 create table transtest (
50 catch {mysql::exec $handle {drop table Student}}
52 CREATE TABLE Student (
53 MatrNr int NOT NULL auto_increment,
59 mysql::exec $handle "INSERT INTO Student VALUES (1,'Sojka',4)"
60 mysql::exec $handle "INSERT INTO Student VALUES (2,'Preisner',2)"
61 mysql::exec $handle "INSERT INTO Student VALUES (3,'Killar',2)"
62 mysql::exec $handle "INSERT INTO Student VALUES (4,'Penderecki',10)"
63 mysql::exec $handle "INSERT INTO Student VALUES (5,'Turnau',2)"
64 mysql::exec $handle "INSERT INTO Student VALUES (6,'Grechuta',3)"
65 mysql::exec $handle "INSERT INTO Student VALUES (7,'Gorniak',1)"
66 mysql::exec $handle "INSERT INTO Student VALUES (8,'Niemen',3)"
67 mysql::exec $handle "INSERT INTO Student VALUES (9,'Bem',5)"
72 set conn [getConnection {-multistatement 1 -multiresult 1}]
75 tcltest::test {null-1.0} {creating of null} {
76 set null [mysql::newnull]
80 tcltest::test {null-1.1} {null checking} {
84 tcltest::test {null-1.2} {null checking} {
85 mysql::isnull [mysql::newnull]
88 tcltest::test {null-1.3} {null checking} {
92 tcltest::test {null-1.4} {null checking} {
93 mysql::isnull [lindex [list [mysql::newnull]] 0]
97 tcltest::test {autocommit} {setting autocommit} -body {
98 mysql::autocommit $conn 0
100 tcltest::test {autocommit} {setting autocommit} -body {
101 mysql::autocommit $conn 1
103 tcltest::test {autocommit} {setting false autocommit} -body {
104 mysql::autocommit $conn nobool
105 } -returnCodes error -match glob -result "expected boolean value*"
107 mysql::autocommit $conn 0
109 tcltest::test {commit} {commit} -body {
110 mysql::autocommit $conn 0
111 mysqlexec $conn {delete from transtest where name='committest'}
112 mysqlexec $conn {insert into transtest (name,id) values ('committest',2)}
114 set res [mysqlexec $conn {delete from transtest where name='committest'}]
119 tcltest::test {rollback-1.0} {roolback} -body {
120 mysql::autocommit $conn 0
121 mysqlexec $conn {delete from transtest where name='committest'}
122 mysqlexec $conn {insert into transtest (name,id) values ('committest',2)}
123 mysql::rollback $conn
124 set res [mysqlexec $conn {delete from transtest where name='committest'}]
129 tcltest::test {rollback-1.1} {roolback by auto-commit 1} -body {
130 mysql::autocommit $conn 1
131 mysqlexec $conn {delete from transtest where name='committest'}
132 mysqlexec $conn {insert into transtest (name,id) values ('committest',2)}
133 # rollback should not affect
134 mysql::rollback $conn
135 set res [mysqlexec $conn {delete from transtest where name='committest'}]
140 tcltest::test {warning-count-1.0} {check mysql::warningcount} -body {
141 set list [mysql::sel $conn {select * from Student} -list]
142 mysql::warningcount $conn
145 tcltest::test {multistatement-1.0} {inserting multi rows} -body {
147 insert into transtest (name,id) values ('row1',31);
148 insert into transtest (name,id) values ('row2',32);
149 insert into transtest (name,id) values ('row3',33),('row4',34);
154 tcltest::test {moreresult-1.3} {arg counts} -body {
156 } -returnCodes error -match glob -result "wrong # args:*"
158 tcltest::test {moreresult-1.0} {only one result} -body {
160 mysql::sel $conn {select * from transtest}
161 mysql::moreresult $conn
164 tcltest::test {moreresult-1.1} {only one result} -body {
167 select * from transtest;
168 select * from Student;
170 while {[llength [mysql::fetch $conn]]>0} {}
171 if {[set ret [mysql::moreresult $conn]]} {
172 # mysql::nextresult $conn
177 tcltest::test {nextresult-1.0} {only one result} -body {
180 select * from transtest;
181 select * from Student;
183 while {[llength [set row [mysql::fetch $conn]]]>0} {
185 mysql::nextresult $conn
187 while {[llength [set row [mysql::fetch $conn]]]>0} {
191 } -result 1 -returnCodes 2
193 tcltest::test {nextresult-rows-1.1} {rows number} -body {
196 select name from Student where name='Sojka';
197 select name,semester from Student;
199 set r1 [mysql::result $conn cols]
200 mysql::nextresult $conn
201 set r2 [mysql::result $conn cols]
205 tcltest::test {setserveroption-1.0} {set multistatment off} -body {
206 mysql::setserveroption $conn -multi_statment_off
208 insert into transtest (name,id) values ('row1',31);
209 insert into transtest (name,id) values ('row2',32);
210 insert into transtest (name,id) values ('row3',33);
212 } -returnCodes error -match glob -result "mysql::exec/db server*"
214 tcltest::test {setserveroption-1.1} {set multistatment on} -body {
215 mysql::setserveroption $conn -multi_statment_on
217 insert into transtest (name,id) values ('row1',31);
218 insert into transtest (name,id) values ('row2',32);
219 insert into transtest (name,id) values ('row3',33);
224 tcltest::test {info-1.0} {asking about host} -body {
225 set res [mysql::info $conn host]
226 expr {[string length $res]>0}
229 tcltest::test {info-1.1} {serverversion} -body {
230 mysql::info $conn serverversion
231 expr {[mysql::info $conn serverversionid]>0}
234 tcltest::test {info-1.2} {sqlstate} -body {
235 mysql::info $conn sqlstate
239 tcltest::test {info-1.3} {sqlstate} -body {
240 mysql::info $conn state
244 tcltest::test {state-1.0} {reported bug in 3.51} -body {
245 mysql::state nothandle -numeric
248 tcltest::test {state-1.1} {reported bug in 3.51} -body {
249 mysql::state nothandle
250 } -result NOT_A_HANDLE
252 tcltest::test {null-2.0} {reading and checking null from database} -body {
254 mysql::autocommit $conn 1
256 delete from transtest where name="nulltest"
259 insert into transtest (name,id) values ('nulltest',NULL);
261 mysql::sel $conn {select id from transtest where name='nulltest'}
262 set res [lindex [mysql::fetch $conn] 0]
266 tcltest::test {baseinfo-1.0} {clientversionid} -body {
267 expr {[mysql::baseinfo clientversionid]>0}
270 tcltest::test {encoding-1.0} {read system encoding} -body {
271 mysql::encoding $conn
272 } -result [encoding system]
274 tcltest::test {encoding-1.1} {change to binary} -body {
275 mysql::encoding $conn binary
276 mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
277 mysql::encoding $conn
280 tcltest::test {encoding-1.2} {change to binary} -body {
281 mysql::encoding $conn [encoding system]
282 mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
283 mysql::encoding $conn
284 } -result [encoding system]
286 tcltest::test {encoding-1.3} {change to binary} -body {
287 mysql::encoding $conn iso8859-1
288 mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
289 mysql::encoding $conn
292 tcltest::test {encoding-1.4} {unknown encoding} -body {
293 mysql::encoding $conn unknown
294 } -returnCodes error -match glob -result "unknown encoding*"
296 tcltest::test {encoding-1.5} {changing encoding of query handle} -body {
297 set q [mysql::query $conn "select * from Student"]
298 mysql::encoding $q iso8859-1
301 } -returnCodes error -result "encoding set can be used only on connection handle"
303 tcltest::test {encoding-1.6} {changing encoding of handle} -body {
304 mysql::encoding $conn iso8859-1
305 set q [mysql::query $conn "select * from Student"]
311 tcltest::test {encoding-1.7} {changing encoding of handle} -body {
312 set q [mysql::query $conn "select * from Student"]
313 mysql::encoding $conn iso8859-1
319 tcltest::test {encoding-1.8} {changing encoding of handle} -body {
320 mysql::encoding $conn utf-8
321 set q [mysql::query $conn "select * from Student"]
322 mysql::encoding $conn iso8859-1
328 tcltest::test {encoding-1.8} {changing encoding of handle} -body {
329 mysql::encoding $conn iso8859-5
330 set q [mysql::query $conn "select Name from Student"]
331 mysql::encoding $conn utf-8
337 # no prepared statements in this version
340 tcltest::test {preparedstatment-1.0} {create test} -body {
341 set phandle [mysql::prepare $conn {insert into transtest (id,name) values (?,?)}]
342 mysql::close $phandle
346 tcltest::test {preparedstatment-1.1} {create errortest} -body {
347 set phandle [mysql::prepare $conn {nosql command ?,?}]
348 mysql::close $phandle
350 } -returnCodes error -match glob -result "*SQL*"
352 tcltest::test {preparedstatment-1.3} {select} -body {
353 set phandle [mysql::prepare $conn {select id,name from transtest}]
354 mysql::pselect $phandle
356 while {[llength [set row [mysql::fetch $phandle]]]>0} {
359 mysql::close $phandle
364 tcltest::test {preparedstatment-1.2} {insert} -body {
365 set phandle [mysql::prepare $conn {insert into transtest (id,name) values (?,?)}]
366 set count [mysql::param $phandle count]
367 mysql::param $phandle type 0
368 mysql::param $phandle type 1
369 mysql::param $phandle type
370 mysql::pexecute $phandle 2 Artur
371 mysql::close $phandle
376 tcltest::test {preparedstatment-1.4} {select mit bind} -body {
377 set phandle [mysql::prepare $conn {select id,name from transtest where id=?}]
378 set countin [mysql::paramin $phandle count]
379 set countout [mysql::paramin $phandle count]
380 mysql::paramin $phandle type 0
381 mysql::paramin $phandle type
382 mysql::paramout $phandle type 0
383 mysql::paramout $phandle type 1
384 mysql::paramout $phandle type
385 mysql::execute $phandle
386 mysql::close $phandle
387 list $countin $countout
392 tcltest::cleanupTests