Chapter 4. pgtcl - TCL Binding Library

Introduction

pgtcl is a Tcl package for client programs to interface with Red Hat Database servers. It makes most of the functionality of libpq available to Tcl scripts.

Table 4-1. pgtcl Commands

CommandDescription
pg_connectOpens a connection to the backend server
pg_disconnectCloses a connection
pg_conndefaultsGets connection options and their defaults
pg_execSends a query to the backend
pg_resultManipulates the results of a query
pg_selectLoops over the result of a SELECT statement
pg_listenEstablishes a callback for NOTIFY messages
pg_lo_creatCreates a large object
pg_lo_openOpens a large object
pg_lo_closeCloses a large object
pg_lo_readReads a large object
pg_lo_writeWrites a large object
pg_lo_lseekSeeks to a position in a large object
pg_lo_tellReturns the current seek position of a large object
pg_lo_unlinkDeletes a large object
pg_lo_importImports a Linux file into a large object
pg_lo_exportExports a large object into a Linux file

The pg_lo* routines are interfaces to the Large Object features of Red Hat Database. The functions are designed to mimic the analogous file-system functions in the standard Unix file system interface. The pg_lo* routines should be used within a BEGIN/COMMIT transaction block because the file descriptor returned by pg_lo_open is valid only for the current transaction. pg_lo_import and pg_lo_export must be used in a BEGIN/COMMIT transaction block.

Example of Using pgtcl Routines

Here is a small example of how to use the routines:
# getDBs :
#   get the names of all the databases at a given host and port 
#   number with the defaults being the localhost and port 5432
#   return them in alphabetical order
proc getDBs { {host "localhost"} {port "5432"} } {
    # datnames is the list to be result
    set conn [pg_connect template1 -host $host -port $port]
    set res [pg_exec $conn "SELECT datname FROM pg_database ORDER
      BY datname"]
    set ntups [pg_result $res -numTuples]
    for {set i 0} {$i < $ntups} {incr i} {
	lappend datnames [pg_result $res -getTuple $i]
        }
    pg_result $res -clear
    pg_disconnect $conn
    return $datnames
}