Q9.6: Sybtcl FAQ

This is Tom Poindexter http://www.nyx.net/~tpoindex/ FAQ.

Index of Sections


Overview

Sybtcl is an extension to Tcl (Tool Command Language) that allows Tcl programs to access Sybase databases. Sybtcl adds additional Tcl commands to login to a Sybase server, send SQL statements, retrieve result sets, execute stored procedures, etc. Sybtcl simplifies Sybase programming by creating a high level interface on top of DB-Library. Sybtcl can be used to program a wide variety of applications, from system administration procedures to end-user applications.

Sybtcl runs on Unix, Windows NT and 95, and Macintosh platforms.


The enabling language platform

Tool Command Language, often abbreviated "Tcl" and pronounced as "tickle", was created by Dr. John Ousterhout at the University of California-Berkeley. Tcl is an interpreted script language, similar to Unix shell, Awk, Perl, and others. Tcl was designed to be easily extended, where new commands are added to the base interpreter to provide additional functionality. Core Tcl commands contain all of the usual constructs provided by most programming languages: setting and accessing variables, file read/write, if-then-else, do-while, function calls. Tcl also contains many productivity enhancing commands: list manipulation, associative arrays, and regular expression processing.

Tcl has several features that make it a highly productive language. First, the language is interpreted. Interpreters allow execution without a compile and link step. Code can be developed with immediate feedback. Second, Tcl has a single data type: string. While this might at first glance seem to a deficiency, it avoids problems of data conversion and memory management. (This feature doesn't preclude Tcl from performing arithmetic operations.) Last, Tcl has a consistent and simple syntax, much the same as the Unix shell. Every Tcl statement is a command name, followed by arguments.

Dr. Ousterhout also developed a companion Tcl extension, called Tk. Tk provides simplified programming of X11 applications with a Motif look and feel. X11 applications can be programmed with 60%-80% less code than equivalent Xt, Motif, or Xview programs using C or C++.

Dr. Ousterhout now leads Tcl/Tk development at Sun Microsystems.


Design and commands

Sybtcl was designed to fill the gap between pure applications development tools (e.g. Apt, Powerbuilder, et.al.) and database administration tools, often Unix shell scripts consisting of 'isql' and Awk pipelines. Sybtcl extends the Tcl language with specialized commands for Sybase access. Sybtcl consists of a set of C language functions that interface DB-Library calls to the Tcl language.

Instead of a simple one-to-one interface to DB-Library, Sybtcl provides a high-level Sybase programming interface of its own. The following example is a complete Sybtcl program that illustrates the simplified interface. It relies on the Tcl interpreter, "tclsh", that has been extended with Sybtcl.

  #!/usr/local/bin/tclsh
  set hand [sybconnect "mysybid" "mysybpasswd"]
  sybuse $hand pubs2
  sybsql $hand "select au_lname, au_fname from authors order by au_lname"
  sybnext $hand {
    puts [format "%s, %s" @1 @2]
  }
  sybclose $hand
  exit
In this example, a Sybase server connection is established ("sybconnect"), and the "pubs" sample database is accessed ("sybuse"). An SQL statement is sent to the server ("sybsql"), and all rows returned are fetched and printed ("sybnext"). Finally, the connection is closed ("sybclose").

The same program can be made to display its output in an X11 window, with a few changes. The Tcl/Tk windowing shell, "wish", also extended with Sybtcl is used.

  #!/usr/local/bin/wish
  listbox .sql_output
  button  .exit -text exit -command exit 
  pack .sql_output .exit
  set hand [sybconnect "mysybid" "mysybpasswd"]
  sybuse $hand pubs2
  sybsql $hand "select au_lname, au_fname from authors order by au_lname"
  sybnext $hand {
    .sql_output insert end  [format "%s, %s" @1 @2]
  }
  sybclose $hand
In addition to these commands, Sybtcl includes commands to access return column names and datatypes ("sybcols"), return values from stored procedures ("sybretval"), reading and writing of "text" or "image" columns ("sybreadtext", "sybwritetext"), canceling pending results ("sybcancel"), and polling asynchronous SQL execution ("sybpoll").

Full access to Sybase server messages is also provided. Sybtcl maintains a Tcl array variable which contains server messages, output from stored procedures ("print"), DB-Library and OS error message.


Applications

The Sybtcl distribution includes "Wisqlite", an X11 SQL command processor. Wisqlite provides a typical windowing style environment to enter and edit SQL statements, list results of the SQL execution in a scrollable listbox, save or print output. In addition, menu access to the Sybase data dictionary is provided, listing tables in a database, the column names and datatypes of a table, text of stored procedures and triggers.

For a snapshot of Wisqlite in action, look here.

Other applications included in the Sybtcl distribution include:

Sybtcl users have reported a wide variety of applications written in Sybtcl, ranging from end user applications to database administration utilities.

Information Sources

Sybtcl is extensively documented in "Tcl/Tk Tools", edited by Mark Harrison, published by O'Reilly and Associates, 1997, ISBN: 1-56592-218-2.

Tcl/Tk is described in detail in "Tcl and the Tk Toolkit" by Dr. John Ousterhout, Addison-Wesley Publishing 1994 ISBN: 0-201-63337-X . Another recent publication is "Practical Programming in Tcl and Tk" by Brent Welch, Prentice Hall 1995 ISBN 0-13-182007-9.

A wealth of information on Tcl/Tk is available via Internet sources:

news:comp.lang.tcl
http://sunscript.sun.com/
http://www.neosoft.com/tcl/
http://www.sco.com/Technology/tcl/Tcl.html
ftp://ftp.sunlabs.com/pub/tcl
ftp://ftp.neosoft.com/pub/tcl/

Download

Download Sybtcl in tar.gz format for Unix.
Download Sybtcl in zip format for Windows NT and 95.

Tcl/Tk and Sybtcl are both released in source code form under a "BSD" style license. Tcl/Tk and Sybtcl may be freely used for any purpose, as long as copyright credit is given to the respective owners. Tcl/Tk can be obtained from either anonymous FTP site listed above.

Tcl/Tk and Sybtcl can be easily configured under most modern Unix systems including SunOS, Solaris, HP-UX, Irix, OSF/1, AIX, SCO, et.al. Sybtcl also runs under Windows NT and 95; pre-compiled DLL's are include in the distribution. Sybtcl requires Sybase's DB-Library, from Sybase's Open Client bundle.

Current versions are:

The Internet newsgroup comp.lang.tcl is the focal point for support. The group is regularly read by developers and users alike. Authors may also be reached via email. Sun has committed to keeping Tcl/Tk as freely available software.

About the Author

Tom Poindexter is a consultant with expertise in Unix, relational databases, systems and application programming. He holds a B.S. degree from the University of Missouri, and an M.B.A. degree from Illinois State University. He can be reached at tpoindex@nyx.net.