File: binarytest.tcl

package info (click to toggle)
mysqltcl 3.051-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 844 kB
  • sloc: ansic: 1,916; tcl: 1,276; sh: 142; makefile: 26; sql: 20
file content (111 lines) | stat: -rwxr-xr-x 4,343 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/tcl
# Write and read file into database
# using 
# binarytest.tcl file
# The output file will be written with file.bin

if {[file exists libload.tcl]} {
    source libload.tcl
} else {
    source [file join [file dirname [info script]] libload.tcl]
}


if {[llength $argv]==0} {
    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"
    exit
}

set file [lindex $argv 0]
set fhandle [open $file r]
fconfigure $fhandle -translation binary -encoding binary
set binary [read $fhandle]
close $fhandle

puts "test conection without encoding option"

# What is saved in mysql is dependend from your system encoding
# if system encoding is not utf-8 then data could not be safed correctly
# It is also not so good idea to safe binary data as utf-8.
# I recommend always to use -encoding binary if you handle binary data
# You can alway build multiple handles if you want handle binary and utf-8 data.

set handle [mysqlconnect -user root -db uni]
mysqlexec $handle "INSERT INTO Binarytest (data) VALUES ('[mysqlescape $binary]')"
set id [mysqlinsertid $handle]

set nfile [file tail $file].bin
set fhandle [open $nfile w]
fconfigure $fhandle -translation binary -encoding binary
#set nbinary [lindex [lindex [mysqlsel $handle "SELECT data from Binarytest where id=$id" -list] 0] 0]
mysqlsel $handle "SELECT data from Binarytest where id=$id"
#set nbinary [encoding convertfrom [encoding system] [lindex [mysqlnext $handle] 0]]
set nbinary [lindex [mysqlnext $handle] 0]
puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary]  [string length $nbinary]"
puts -nonewline $fhandle $nbinary
close $fhandle
if {[catch {exec cmp $file $nfile}]} {
   puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
} else {
   puts "binary comparing ok"
}
puts "Length in Mysql [mysqlsel $handle "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"


puts "test with -encoding binary"
set handle2 [mysqlconnect -user root -db uni -encoding binary]
mysqlexec $handle2 "Update Binarytest set data = '[mysqlescape $binary]' where id=$id"
mysqlsel $handle2 "SELECT data from Binarytest where id=$id"
set nbinary [lindex [mysqlnext $handle2] 0]

set nfile [file tail $file].bin
set fhandle [open $nfile w]
fconfigure $fhandle -translation binary -encoding binary
puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary]  [string length $nbinary]"
puts -nonewline $fhandle $nbinary
close $fhandle
if {[catch {exec cmp $file $nfile}]} {
   puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
} else {
   puts "binary comparing ok"
}
puts "Length in Mysql [mysqlsel $handle2 "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"


puts "test reading binary data but do not use -binary option but iso8859-1"
# please do not try to read binary data if your system encoding is set to
# utf-8. The converting from it will crash system
set handle2 [mysqlconnect -user root -db uni -encoding iso8859-1]
mysqlsel $handle2 "SELECT data from Binarytest where id=$id"
set nbinary [lindex [mysqlnext $handle2] 0]
set nfile [file tail $file].bin
set fhandle [open $nfile w]
fconfigure $fhandle -translation binary -encoding binary
puts "primary length [string bytelength $binary] new length [string bytelength $nbinary] - [string length $binary]  [string length $nbinary]"
puts -nonewline $fhandle $nbinary
close $fhandle
if {[catch {exec cmp $file $nfile}]} {
   puts "binary comparing failed primary length [file size $file] new length [file size $nfile]"
} else {
   puts "binary comparing ok"
}


puts "test with -encoding iso8859-15"
set handle3 [mysqlconnect -user root -db uni -encoding iso8859-15]
# iso8859-1]
set umlaute "   Deutsch Umlaute 26"
mysqlexec $handle3 "Update Binarytest set data = '$umlaute' where id=$id"
mysqlsel $handle3 "SELECT data from Binarytest where id=$id"
set umlauteOut [lindex [mysqlnext $handle3] 0]

puts "$umlaute $umlauteOut"
if {$umlaute!=$umlauteOut} {
    puts "Umlaut Test Failed"
}

puts "Length in Mysql [mysqlsel $handle3 "SELECT LENGTH(data) from Binarytest where id=$id" -flatlist]"



puts "Testing finished"