???? Tcl ( Tool Command Language ) ??? ????????
John Ousterhout [1]. ?? ????? ???? Tcl ??? ?????????? ??? ???? ? ?????????????.
??? ??????????? ???? ???????????? ??? ???? ??????: ??????, ?????? ? ???????. ??????? ??
??? ????? ???????????? ?????????? ????????? [2], ????????? ?????????? ?????????? ?
Tk, ????? ??? ???????? ??????????? ?????????? ?? Tcl.
???? MySQL ????? ?????????? ?????? ??? ??????
? ?????????? ???????? ????????????????? ???????????? ???????????, ??????? ?? ????????? ?
?????????????.
MySQLTcl ??? ?????????? ??? ????? Tcl ? ???????
??????? ????? ???????? ? ???? MySQL ?? ????????, ?????????? ?? ????? Tcl. ? ????????? ?????
???????? ? ?????????????? ???? ?????????? ???????? Paolo Brutti (Paolo.Bruti at tlsoft.it),
Tobias Ritzau (tobri at ida.liu.se) ? Artur Trzewick (mail at xdobry.de).
_________________ _________________ _________________
|
????????? ?????????? MySQLTcl
???? ???? Linux ??? *BSD ??????? ???????????? ?????? ( ??????? RPM ??? DEB ) ??? ????? ???
Crux Linux ??? FreeBSD,
?? ?????? ???????????? ?? ??? ????????? ?????????? MySQLTcl ? ?????????? ???? ??????.
???? ??? ?? ??? ??? ?? ?????? ????????????? ????????????? ??? "??????", ? ??????????????? ???
??? ??? ????? ???. ?? ?????? ???????????? ??? ??? ??????????, ?? ?? ??? ????????? ???????????
?????????. ????????? bash ???????????? Linux Mandrake
?????? 9.2 ?????? ?????????:
$ ./configure --with-mysql-lib=/usr/lib
$ make
$ make install
|
???? ???-?? ?????????? ?? ??? ?? ????? ???? "configure", ?????????? ?? ??????? ????????
??????? ??? ?? ???????????. ?????? ?????????? ???????? ??????????? ? ????????? - ??????
configure ?? ??????? ???????????? ???????? ??? ?????. ????????? ??? ????? ?????????
??????? ???? ? ???? ?????? ??? ????????? ? ???????? ??????????. ????????, ????????????
MySQLTcl ?? FreeBSD 5.0 ? ??????????? ????????? ?????:
$ export CPP=/usr/bin/cpp
$ ./configure --with-tcl=/usr/lib/local/tcl8.3
--with-tclinclude=/usr/local/include/tcl8.3
--with-mysql-include=/usr/local/include/mysql
--with-mysql-lib=/usr/local/lib/mysql
$ make
$ make install
|
??????? ?? ????????, ??? ? ???? ??????? ???????????? Tcl ?????? 8.3, ????? ???? - ??????
?????????? MySQLTcl 2.15, ? ??????? MySQL 3.23.54.
?????? Tcl
? ???? ??????? ?????????? ?????? Tcl ??? ???, ???? ????????? ?????? ???????, ?? ??? ??
??????? ? ???. ???? ?? ?????????????? ?? Tcl - ?????? ?????????? ???? ??????.
?? ?????? ??????????????? ?? ???? ???????? ?? ???????? Tcl (tclsh).
??????????. ??????? ? ??????????? ??????????.
?????????? Tcl ????????? ? ??????? ??????? set. ?????????? ????????? ????????:
% set address {Edison Avenue, 83}
Edison Avenue, 83
% set zip_code 48631
48631
|
? ???? ???? ???????? ?? ??????? ??? ?????????? - address ? zip_code, ? ????????? ?? ????????
Edison Avenue, 83 ? 48361, ??????? ????? ??? string. ???????? ???????? ?? ??,
??? ??? ?????????? ?????????? address ???????? ?? ???????????? ???????? ?????? ??????, ??? ?
??? ???? ??????. ???????? ???????? ?????????? ????? ???????? set:
% set address
Edison Avenue, 83
% set zip_code
48631
????? ??????? ???????? ?????????? address ?? ????? ????????????? ???????? puts:
% puts stdout [set address]
Edison Avenue, 83
???????? stdout ???????????? ??????? puts ?????? ??? ???????? ??????
? ??????????? ????? ?????? - ?? ?????. ?????? ???????????? ???????? ???????
puts ??? [set address]. ?????????? ?????? ????????? ??????????????
Tcl, ??? ??? ?????? ??????? ????? Tcl, ??????? ?????? ???? ????????? ????? ????????
puts - ??? ?????????? ??????????? ??????. ?? ?? ????? ????? ??????? ?????????
???????:
% puts stdout $address
Edison Avenue, 83
? ???? ??????? ?? ????????? ??????????? ??????????: ?????? $ ????? ??????????
????????? ?? ???.
? ?????????? ??????? ?? ????????, ??? ????????? ???????? ?????? ????? ??????? ?????
? ??????. ?????? ??????????? ??????? ?? ?? ????? - ????????????? ??????? ( ?????? " ).
??? ?? ?????, ??? ??? ??????? ????? ???? ??-???????. ????????? ?? ???????:
% puts stdout "the zip code is [set address]"
the zip code is 48631
% puts stdout "the zip code is $address"
the zip code is 48631
% puts stdout {the zip code is [set address]}
the zip code is [set address]
% puts stdout {the zip code is $address}
the zip code is $address
????? ?????, ??? ??? ????????????? ???????? ?????? ??????????? ?????????? ?? ??????????,
?? ?????????? ??? ????????????? ???????.
??????? ?????????? ????? ???????? unset:
% unset address
% set address
can't read "address": no such variable
% unset zip_code
% set zip_code
can't read "zip_code": no such variable
?????? ? Tcl
?????? ???????? ????? ?? ???? ???????? ????? ?????? ? Tcl. ?????? - ??? ????? ????????.
?????? ????????? ? ??????? ??????? set.
% set surname Westmoreland
Westmoreland
% set number 46.625
46.625
??? ?????????? - surname ? number ????? ??? ??????. ???????? ? ??????????? ?????
???? ????? ???????? string. ????????? ???? ??????? ????????? - string
???????? ???????? ??????_?????????. ??????? ?????????:
% string length $surname
12
% set surname [string range $surname 0 3]
West
% puts stdout $surname
West
% string tolower $surname
west
? ??????? ?? Java ??? Pascal Tcl ?? ????? ??????? ???? ????????????????. ????????? ??????
????????????? ???:
% set number [expr $number + 24.5]
70.125
% string range $number 2 5
.125
? ??????? ??????? expr ?? ????????? ???????? ?????????? number ?? 24.5.
????? ????? ?? ??????????? ?????????? number ????????? ? ? ??????? ??????? string
?????? ????????? ?????? ???????.
?????? ? Tcl
?????? ? Tcl ???????? ?????????????? ?????, ??????? ????????? ???????? ? ????????????????
??????????????? ??-??????.
% set friends_list {Fany John Lisa Jack Michael}
Fany John Lisa Jack Michael
% set friends_list [string tolower $friends_list]
fany john lisa jack michael
% set friends_list
fany john lisa jack michael
? Tcl ?????????? ????? ?????? ??? ?????? ?? ????????. ?????????? ????????? ????????:
% lindex 2 $friends_list
lisa
% lrange $friends_list 2 4
lisa jack michael
% set friends_list [lsort -ascii $friends_list]
fany jack john lisa michael
% set friends_list
fany jack john lisa michael
% set friends_list [linsert $friends_list 2 Peter]
fany jack Peter john lisa michael
% string toupper $friends_list
FANY JACK PETER JOHN LISA MICHAEL
?? ?????????? ??????? ?????, ??? ?????? ? ?????? ????? ?????????? ?????????.
??????? ? Tcl
?????? ????? ????????????? ?????? ????? ?????? ? ???????? ? ???????? ????????
????????? ?????????? ???? "??????". ?????????? ?????? ???????? ???????:
% set phone_numbers(fany) 629
629
% set phone_numbers(michael) 513
513
% set phone_numbers(john) 286
286
???????? ???????? ?? ??????? ????? ????????? ??????? set ??? ? ? ?????????? ???????,
?????? ??????????? ? ??????? ??????.
% set $phone_numers(michael)
513
?????????? ? ??????? ????? ???????? ? ??????? ??????? array. ?????????? ?????????
???????? ????????????? ?????? ???????:
% array size phone_numbers
3
% array names phone_numbers
fany john michael
??????????? ? ???? ??????
??? ???? ????? ????????? ??????? ? ???? ?????? ?? ???????? Tcl ??????? ???? ??????????
?????????? ? ???????? ??? ??????. ? ???? ????? ??????? ?? ?????????? ???? ??????, ? ?????
????????? ??????, ??????? ????? ?????????? ? ???????? ??????????.
?????????? ? ???????? ??
?????????? ?????? ??????????? ? ??????? MySQL ?? Tcl ???????:
0: #!/usr/bin/tclsh8.4
1:
2: package require mysqltcl
3: global mysqlstatus
4:
5: set port {3306}
6: set host {127.0.0.1}
7: set user {john_smith}
8: set password {jsmith_password}
9:
10: set mysql_handler [mysqlconnect -host $host
-port $port -user $user -password $password]
11:
12: mysqlclose $mysql_handler
???????? ???????? ?? ????? ??????? ???? - ??? ?? ???????? ?????? ?????? ???????, ????? ???????
?? ?????? ????????????? ??????. ?????, ? ??????????? ?? ????????????? ???? ????????????
Linux, ??? ???????? ???????? ???????? ?????? #0, ??????????? ???? ? Tcl shell.
?????? #0 ????????? shell'?, ??? ?????? ???? ???????? ???????? Tcl ? ????????? ??? ??????
????????????? Tcl.
? ?????? #2 ????? ???????? ?????????????? ???????????? ?????????? MySQLTcl. ???????? ? ??????
#10 ?????????? ??????? mysqlconnect, ???? ?? ?? ?? ??????? ? ?????? #2 ??????????, ?????
??? ?????? ?????? ??????? ????????????? ????? ?? ?????? "??????? ?? ???????" ( command not found ).
? ??????? #5 ? #6 ???????????????? ?????????? port ? host. ? ????? ??????? port ????????????????
????????? 3306 ( ?? ????????? MySQL ???????????? ???? ???? ), ? host - ??????? ??????????
??????????.
? ??????? #7 ? #8 ???????????????? ?????????? user ? password.
? ?????? #10 ??????????????? ???? ?????????? ? handler ??????????? ? ??????????
mysql_handler, ??? ?????????? ??? ???????? ? ?? ? ???????? ??????????, ??? ????????
? ?????? #12.
????????? ??????
? ?????????? ????? ?? ?? ??????????? ?????? #3. ??????? ??? ??????.
?????????? ?????? ?????????? MySQLTcl ????? ???????? ??????. ???? ?? ?? ??????? ??? ??????,
?????? ????? ?????????? ???? ??????. ???????? ??????? ??? ?????? ????????? ???????:
10: if [catch {mysqlconnect -host $host -port $port
-user $user -password $password} mysql_handler] {
11: puts stderr {error, the database connection could not be established}
12: exit
13: } else {
14: mysqlclose mysql_handler
15: }
???? ??????? set mysql_handler [mysqlconnect -host $host... ??????? ?????? - ??? ?????
??????????? ???????? catch. ??????? catch ?????????? 1 ? ?????? ?????????????
?????? ? 0 ? ???????????????.
? ?????? ????????????? ?????? ????????? ????????? ? ??????????? ????? ??????, ? ?????
?????? ??? ?????. ???????? ?????? ????????? ? ?????? ??????????????? - ???????????? ??????,
????, ???? ? ?.?. ? ???????? ?????? ????? ????????? ???????? ?????? ????? ????????.
? ?????? #3 ?? ???????? ?????????? ?????????? mysqlstatus. ?????????? ??????????
???????? ? ????? ????? Tcl ???????. ?????????? MySQLTcl ??????? ?????????? ??????
mysqlstatus, ? ??????? ?????????? ????????? ????????:
??????? |
???????? |
code |
???? ?????? ?? ????????? mysqlstatus(code) ????? ????, ? ????????? ???????
??????????????? ? ??? ?????? ??????? MySQL. ???? ?????? ????????, ?? ?? ???????? ???????
MySQL, ?? ???????? mysqlstatus(code) ??????????????? ? -1.
???????? mysqlstatus(code) ??????????? ????? ??????? ????????????? ??????? ??????????
MySQLTcl.
|
command |
??????? ?????????? MySQLTcl, ?????????? ??????? ???????? ??????, ??????????? ?
mysqlstatus(command).
???????? mysqlstatus(command) ??????????? ????? ?????? ??????? ????????? ??????,
??????? ??????? - mysqlstatus(command) ?? ??????????? ????? ???????? ???????.
|
message |
???????? mysqltcl(message) ??????????? ????? ?????? ??????? ????????? ??????
? ??????????? ?????????? ?? ??????. ????? ??? ? mysqlstatus(command),
mysqlstatus(message) ?? ??????????? ????? ???????? ???????.
|
?????????? ??? ???? ??????? ? ?????????? ??????? mysqlstatus, ??????? ?? ?????? ?
?????????? ??????:
??????? |
???????? |
nullvalue |
??????, ???????????? ??? ???????? null ??? ?????????? ?????????? SQL ???????. ?? ?????????
???????????? ?????? ??????; musqlstatus(nullvalue) ????? ???? ??????????? ? ?????
????????? ????????.
|
????? ???????, ????????? ?????????? ?????? mysqlstatus, ?????????? ??? ????? ??????????
???, ????? ???? ??????? ??? ????????? ? ?????? ???????????? ?????????? ? ???????? ??:
10: catch {mysqlconnect -host $host -port $port
-user $user -password $password} mysql_handler
11: if {$mysqltatus(code) != 0} {
12: puts stderr $mysqlstatus(message)
13: } else {
14: mysqlclose mysql_handler
15: }
?????????? ?????? mysqlstatus ????? ???????????? ??? ????????? ???????? ????? ??????,
?? ?????? ??? ???????????? ?????????? ? ??.
???????? ??????? ?????????? MySQLTcl
? ???? ????? ?????????? ???????? ??????? ?????????? MySQLTcl ? ????????? ??????? ??
?????????????. ?????? ?????????? ?? ??????? ??????? ???????? ?? man ???????? MySQLTcl.
??????? ???????? ??????? ? ??????? ?????????. ????????? ???????????. ???? ???????? ????? ?????
"?" - ?? ????????????, ?????? "|" ?????????? "???".
???????
|
??????? ????????
|
mysqlconnect ?option value ...?
|
??????????? ? ??; ?????????? handler, ??????? ?????????? ???????????? ? ?????? ???????? mysqltcl
|
mysqluse handle dbname
|
??????????? MySQL handler ? ???????????? ??
|
mysqsel handle sql_statement ?-list | -flatlist?
|
???????? SQL ??????? ? ??
|
mysqlexec handle sql_statement
|
???????? ??_select SQL ??????? ? ??
|
mysqlclose handle
|
???????? ?????????? ? ??
|
mysqlconnect
??? ??????? ?? ??? ????????? ? ??????? "?????????? ? ???????? ??", ?? ???? ???? ??????,
??????? ?? ?? ????????? - ???????? -db, ?? ????? ??? ???????? ???????????? ?? ???
???????????? ?????????????. ????????? ??????:
% set mysql_handler [mysqlconnect -h 127.0.0.1 -p 3306 \
-user john_smith -password jsmith_password -db jsmith_database]
? ???? ?????? ??? ??????????? ????????????? mysql_handler ??????? ????? ?????
?????????? ? ?? jsmith_database.
( ??????????, ???????? ???????? ?? ??????? \, ??? ?? ???????? ?????? ???????; ??? ??????????,
??? ??????? ???????????? ?? ????????? ?????? ).
mysqluse
??????? ?????? ??? ????? ?? ??????????????? ? handler'??, ?? ??????????? ?????? ??????????.
mysqlsel
? ??????? ?????? ??????? ?????????? SQL select ?????? ? ??. ???? ???????? sql_statement
?? ???????? SQL select ???????? - ?????????? ??????.
????? ???????????? ? ???? ??????? ?????? ( ???????????? ) ???????? - list ??? flat_list.
????????? ?? ??????? ??? ?????????? ? ???????, ??? ????????????? ????? ?????????. ???????????
? ?? ???? ??????? people:
id |
first_name |
last_ name |
phone |
26 |
Karl |
Bauer |
8245 |
47 |
James |
Brooks |
1093 |
61 |
Roberto |
Castro Portela |
6507 |
? ??????? ??????? mysqlsel ???????? SQL ?????? ? ??:
% mysqlsel $mysql_handler {select first_name, last_name from people order by id asc} -list
{Karl Bauer} {James Brooks} {Roberto {Castro Portela}}
% mysqlsel $mysql_handler {select first_name, last_name from people order by id asc} -flatlist
Karl Bauer James Brooks Roberto {Castro Portela}
? ?????? ?????? (-list parameter) ??????? ?????????? ??????, ???????? ???????? ??????.
?? ?????? ?????? (-flatlist parameter) ??????? ?????????? ??????, ???????? ????????
???? ??? ??????.
??? ?????????? ???? ???????????? ??????? mysqlsel ??? ???????? ?????????? ? ???? ??????
??????????? ?????? ??????? ????? ????? ?????, ???????????? ????????:
% mysqlsel $mysql_handler {select first_name, last_name from people order by id asc}
3
mysqlexec
? ??????? ??????? mysqlexec ????? ??????? ??_select SQL ?????? ? ??. ???? ? ???????
???? ??????? ?? ??????? select SQL ?????? - ?????? ?? ??????????, ?? ? ?????? ?? ????? ???????.
????????? ??? ??? ?? ?????????? ??????:
% mysqlexec $mysql_handler {delete from people where id=26}
1
% mysqlsel $mysql_handler {select first_name, last_name, phone from people order by id asc} -list
{James Brooks 1093} {Roberto {Castro Portela} 6507}
% mysqlexec $mysql_handler \
{insert into people (id, first_name, last_name, phone) values (58, "Carla", "di Bella", 8925)}
1
% mysqlsel $mysql_handler {select first_name, last_name, phone from people order by id asc} -list
{James Brooks 1093} {Carla {di Bella} 8925} {Roberto {Castro Portela} 6507}
??????????? SQL ??????? ???????? ?? delete ??? insert ????? ???? ??????? ? ??????? ???? ???????.
???????? update ??????:
% mysqlexec $mysql_handler {update people set phone=3749 where first_name="James"}
1
% mysqlsel $mysql_handler {select first_name, last_name, phone from people order by id asc} -list
{James Brooks 3749} {Carla {di Bella} 8925} {Roberto {Castro Portela} 6507}
??????????? ?????? ??????? mysqlexec ???????? ????? ?????, ??????? ???? ??????????
??_select SQL ????????.
mysqlclose
??????? mysqlclose ????????? ?????????? ? ??. ?????????? ???? ??????? ????????
MySQL handler ??????????, ??????? ?? ????? ???????.
?????? ??????? ?????????? MySQLTcl
????? ?????? ?????????? MySQLTcl ?? ?????????????? ?????????????? ???? - ???? ??????? ???
????????? ?????????? ? ??, ????????? ????? ??? ????????????? ? ????????, ???????? ?????????
????????. ?????????? ?????? ??? ???????? ?????????? MySQLTcl ???????? ?? man ????????.
??????
[1] a slightly skeptical view on John K. Ousterhout and Tcl:
http://www.softpanorama.org/People/Ousterhout/index.shtml>
[2] ??????????? ?? ?????????? ?????????? Tcl:
http://www.mapfree.com/sbf/tcl/book/select/Html/7.html>
TclTutor - ?????????? ?????????? ??? ???????? Tcl:
http://www.msen.com/~clif/TclTutor.html>
???????????? MySQL ? ?????? ???????? ( HTML, PDF... ):
http://www.mysql.com/documentation/index.html>
www.linuxfocus.org
 |