]> git.sven.stormbind.net Git - sven/mysqltcl.git/blob - tests/binarytest.tcl
Imported Upstream version 3.05
[sven/mysqltcl.git] / tests / binarytest.tcl
1 #!/usr/bin/tcl
2 # Write and read file into database
3 # using 
4 # binarytest.tcl file
5 # The output file will be written with file.bin
6
7 if {[file exists libload.tcl]} {
8     source libload.tcl
9 } else {
10     source [file join [file dirname [info script]] libload.tcl]
11 }
12
13
14 if {[llength $argv]==0} {
15     puts "usage. binarytest.tcl filename. File name should be binary test file that will be put in Datebase read from it and save als filename.bin"
16     exit
17 }
18
19 set file [lindex $argv 0]
20 set fhandle [open $file r]
21 fconfigure $fhandle -translation binary -encoding binary
22 set binary [read $fhandle]
23 close $fhandle
24
25 puts "test conection without encoding option"
26
27 # What is saved in mysql is dependend from your system encoding
28 # if system encoding is not utf-8 then data could not be safed correctly
29 # It is also not so good idea to safe binary data as utf-8.
30 # I recommend always to use -encoding binary if you handle binary data
31 # You can alway build multiple handles if you want handle binary and utf-8 data.
32
33 set handle [mysqlconnect -user root -db uni]
34 mysqlexec $handle "INSERT INTO Binarytest (data) VALUES ('[mysqlescape $binary]')"
35 set id [mysqlinsertid $handle]
36
37 set nfile [file tail $file].bin
38 set fhandle [open $nfile w]
39 fconfigure $fhandle -translation binary -encoding binary
40 #set nbinary [lindex [lindex [mysqlsel $handle "SELECT data from Binarytest where id=$id" -list] 0] 0]
41 mysqlsel $handle "SELECT data from Binarytest where id=$id"
42 #set nbinary [encoding convertfrom [encoding system] [lindex [mysqlnext $handle] 0]]
43 set nbinary [lindex [mysqlnext $handle] 0]
44 puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary]  [string length $nbinary]"
45 puts -nonewline $fhandle $nbinary
46 close $fhandle
47 if {[catch {exec cmp $file $nfile}]} {
48    puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
49 } else {
50    puts "binary comparing ok"
51 }
52 puts "Length in Mysql [mysqlsel $handle "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"
53
54
55 puts "test with -encoding binary"
56 set handle2 [mysqlconnect -user root -db uni -encoding binary]
57 mysqlexec $handle2 "Update Binarytest set data = '[mysqlescape $binary]' where id=$id"
58 mysqlsel $handle2 "SELECT data from Binarytest where id=$id"
59 set nbinary [lindex [mysqlnext $handle2] 0]
60
61 set nfile [file tail $file].bin
62 set fhandle [open $nfile w]
63 fconfigure $fhandle -translation binary -encoding binary
64 puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary]  [string length $nbinary]"
65 puts -nonewline $fhandle $nbinary
66 close $fhandle
67 if {[catch {exec cmp $file $nfile}]} {
68    puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
69 } else {
70    puts "binary comparing ok"
71 }
72 puts "Length in Mysql [mysqlsel $handle2 "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"
73
74
75 puts "test reading binary data but do not use -binary option but iso8859-1"
76 # please do not try to read binary data if your system encoding is set to
77 # utf-8. The converting from it will crash system
78 set handle2 [mysqlconnect -user root -db uni -encoding iso8859-1]
79 mysqlsel $handle2 "SELECT data from Binarytest where id=$id"
80 set nbinary [lindex [mysqlnext $handle2] 0]
81 set nfile [file tail $file].bin
82 set fhandle [open $nfile w]
83 fconfigure $fhandle -translation binary -encoding binary
84 puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary]  [string length $nbinary]"
85 puts -nonewline $fhandle $nbinary
86 close $fhandle
87 if {[catch {exec cmp $file $nfile}]} {
88    puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
89 } else {
90    puts "binary comparing ok"
91 }
92
93
94 puts "test with -encoding iso8859-15"
95 set handle3 [mysqlconnect -user root -db uni -encoding iso8859-15]
96 # iso8859-1]
97 set umlaute "ÄÖ äö ß Deutsch Umlaute 26"
98 mysqlexec $handle3 "Update Binarytest set data = '$umlaute' where id=$id"
99 mysqlsel $handle3 "SELECT data from Binarytest where id=$id"
100 set umlauteOut [lindex [mysqlnext $handle3] 0]
101
102 puts "$umlaute $umlauteOut"
103 if {$umlaute!=$umlauteOut} {
104     puts "Umlaut Test Failed"
105 }
106
107 puts "Length in Mysql [mysqlsel $handle3 "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"
108
109
110
111 puts "Testing finished"