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 {setserveroption-1.0} {set multistatment off} -body {
194 mysql::setserveroption $conn -multi_statment_off
196 insert into transtest (name,id) values ('row1',31);
197 insert into transtest (name,id) values ('row2',32);
198 insert into transtest (name,id) values ('row3',33);
200 } -returnCodes error -match glob -result "mysql::exec/db server*"
202 tcltest::test {setserveroption-1.1} {set multistatment on} -body {
203 mysql::setserveroption $conn -multi_statment_on
205 insert into transtest (name,id) values ('row1',31);
206 insert into transtest (name,id) values ('row2',32);
207 insert into transtest (name,id) values ('row3',33);
212 tcltest::test {info-1.0} {asking about host} -body {
213 set res [mysql::info $conn host]
214 expr {[string length $res]>0}
217 tcltest::test {info-1.1} {serverversion} -body {
218 mysql::info $conn serverversion
219 expr {[mysql::info $conn serverversionid]>0}
222 tcltest::test {info-1.2} {sqlstate} -body {
223 mysql::info $conn sqlstate
227 tcltest::test {info-1.3} {sqlstate} -body {
228 mysql::info $conn state
232 tcltest::test {state-1.0} {reported bug in 3.51} -body {
233 mysql::state nothandle -numeric
236 tcltest::test {state-1.1} {reported bug in 3.51} -body {
237 mysql::state nothandle
238 } -result NOT_A_HANDLE
240 tcltest::test {null-2.0} {reading and checking null from database} -body {
242 mysql::autocommit $conn 1
244 delete from transtest where name="nulltest"
247 insert into transtest (name,id) values ('nulltest',NULL);
249 mysql::sel $conn {select id from transtest where name='nulltest'}
250 set res [lindex [mysql::fetch $conn] 0]
254 tcltest::test {baseinfo-1.0} {clientversionid} -body {
255 expr {[mysql::baseinfo clientversionid]>0}
258 tcltest::test {encoding-1.0} {read system encoding} -body {
259 mysql::encoding $conn
260 } -result [encoding system]
262 tcltest::test {encoding-1.1} {change to binary} -body {
263 mysql::encoding $conn binary
264 mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
265 mysql::encoding $conn
268 tcltest::test {encoding-1.2} {change to binary} -body {
269 mysql::encoding $conn [encoding system]
270 mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
271 mysql::encoding $conn
272 } -result [encoding system]
274 tcltest::test {encoding-1.3} {change to binary} -body {
275 mysql::encoding $conn iso8859-1
276 mysql::exec $conn "INSERT INTO Student (Name,Semester) VALUES ('Test',4)"
277 mysql::encoding $conn
280 tcltest::test {encoding-1.4} {unknown encoding} -body {
281 mysql::encoding $conn unknown
282 } -returnCodes error -match glob -result "unknown encoding*"
284 tcltest::test {encoding-1.5} {changing encoding of query handle} -body {
285 set q [mysql::query $conn "select * from Student"]
286 mysql::encoding $q iso8859-1
289 } -returnCodes error -result "encoding set can be used only on connection handle"
291 tcltest::test {encoding-1.6} {changing encoding of handle} -body {
292 mysql::encoding $conn iso8859-1
293 set q [mysql::query $conn "select * from Student"]
299 tcltest::test {encoding-1.7} {changing encoding of handle} -body {
300 set q [mysql::query $conn "select * from Student"]
301 mysql::encoding $conn iso8859-1
307 tcltest::test {encoding-1.8} {changing encoding of handle} -body {
308 mysql::encoding $conn utf-8
309 set q [mysql::query $conn "select * from Student"]
310 mysql::encoding $conn iso8859-1
316 tcltest::test {encoding-1.8} {changing encoding of handle} -body {
317 mysql::encoding $conn iso8859-5
318 set q [mysql::query $conn "select Name from Student"]
319 mysql::encoding $conn utf-8
325 # no prepared statements in this version
328 tcltest::test {preparedstatment-1.0} {create test} -body {
329 set phandle [mysql::prepare $conn {insert into transtest (id,name) values (?,?)}]
330 mysql::close $phandle
334 tcltest::test {preparedstatment-1.1} {create errortest} -body {
335 set phandle [mysql::prepare $conn {nosql command ?,?}]
336 mysql::close $phandle
338 } -returnCodes error -match glob -result "*SQL*"
340 tcltest::test {preparedstatment-1.3} {select} -body {
341 set phandle [mysql::prepare $conn {select id,name from transtest}]
342 mysql::pselect $phandle
344 while {[llength [set row [mysql::fetch $phandle]]]>0} {
347 mysql::close $phandle
352 tcltest::test {preparedstatment-1.2} {insert} -body {
353 set phandle [mysql::prepare $conn {insert into transtest (id,name) values (?,?)}]
354 set count [mysql::param $phandle count]
355 mysql::param $phandle type 0
356 mysql::param $phandle type 1
357 mysql::param $phandle type
358 mysql::pexecute $phandle 2 Artur
359 mysql::close $phandle
364 tcltest::test {preparedstatment-1.4} {select mit bind} -body {
365 set phandle [mysql::prepare $conn {select id,name from transtest where id=?}]
366 set countin [mysql::paramin $phandle count]
367 set countout [mysql::paramin $phandle count]
368 mysql::paramin $phandle type 0
369 mysql::paramin $phandle type
370 mysql::paramout $phandle type 0
371 mysql::paramout $phandle type 1
372 mysql::paramout $phandle type
373 mysql::execute $phandle
374 mysql::close $phandle
375 list $countin $countout
380 tcltest::cleanupTests