jpayne@69: # tdbcpostgres.tcl -- jpayne@69: # jpayne@69: # Class definitions and Tcl-level methods for the tdbc::postgres bridge. jpayne@69: # jpayne@69: # Copyright (c) 2009 by Slawomir Cygan jpayne@69: # jpayne@69: # See the file "license.terms" for information on usage and redistribution jpayne@69: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. jpayne@69: # jpayne@69: #------------------------------------------------------------------------------ jpayne@69: jpayne@69: package require tdbc jpayne@69: jpayne@69: ::namespace eval ::tdbc::mypostgres { jpayne@69: jpayne@69: namespace export connection datasources drivers jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #------------------------------------------------------------------------------ jpayne@69: # jpayne@69: # tdbc::postgres::connection -- jpayne@69: # jpayne@69: # Class representing a connection to a Postgres database. jpayne@69: # jpayne@69: #------------------------------------------------------------------------------- jpayne@69: jpayne@69: ::oo::class create ::tdbc::postgres::connection { jpayne@69: jpayne@69: superclass ::tdbc::connection jpayne@69: jpayne@69: # The constructor is written in C. It takes alternating keywords jpayne@69: # and values pairs as its arguments. (See the manual page for the jpayne@69: # available options.) jpayne@69: jpayne@69: # The 'statementCreate' method delegates to the constructor of the jpayne@69: # statement class jpayne@69: jpayne@69: forward statementCreate ::tdbc::postgres::statement create jpayne@69: jpayne@69: jpayne@69: # The 'prepareCall' method gives a portable interface to prepare jpayne@69: # calls to stored procedures. It delegates to 'prepare' to do the jpayne@69: # actual work. jpayne@69: jpayne@69: method preparecall {call} { jpayne@69: regexp {^[[:space:]]*(?:([A-Za-z_][A-Za-z_0-9]*)[[:space:]]*=)?(.*)} \ jpayne@69: $call -> varName rest jpayne@69: if {$varName eq {}} { jpayne@69: my prepare \\{$rest\\} jpayne@69: } else { jpayne@69: my prepare \\{:$varName=$rest\\} jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: # The 'init', 'begintransaction', 'commit, 'rollback', 'tables' jpayne@69: # and 'columns' methods are implemented in C. jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #------------------------------------------------------------------------------ jpayne@69: # jpayne@69: # tdbc::postgres::statement -- jpayne@69: # jpayne@69: # The class 'tdbc::postgres::statement' models one statement against a jpayne@69: # database accessed through a Postgres connection jpayne@69: # jpayne@69: #------------------------------------------------------------------------------ jpayne@69: jpayne@69: ::oo::class create ::tdbc::postgres::statement { jpayne@69: jpayne@69: superclass ::tdbc::statement jpayne@69: jpayne@69: # The 'resultSetCreate' method forwards to the constructor of the jpayne@69: # result set. jpayne@69: jpayne@69: forward resultSetCreate ::tdbc::postgres::resultset create jpayne@69: jpayne@69: # Methods implemented in C: jpayne@69: # jpayne@69: # constructor connection SQLCode jpayne@69: # The constructor accepts the handle to the connection and the SQL code jpayne@69: # for the statement to prepare. It creates a subordinate namespace to jpayne@69: # hold the statement's active result sets, and then delegates to the jpayne@69: # 'init' method, written in C, to do the actual work of preparing the jpayne@69: # statement. jpayne@69: # params jpayne@69: # Returns descriptions of the parameters of a statement. jpayne@69: # paramtype paramname ?direction? type ?precision ?scale?? jpayne@69: # Declares the type of a parameter in the statement jpayne@69: jpayne@69: } jpayne@69: jpayne@69: #------------------------------------------------------------------------------ jpayne@69: # jpayne@69: # tdbc::postgres::resultset -- jpayne@69: # jpayne@69: # The class 'tdbc::postgres::resultset' models the result set that is jpayne@69: # produced by executing a statement against a Postgres database. jpayne@69: # jpayne@69: #------------------------------------------------------------------------------ jpayne@69: jpayne@69: ::oo::class create ::tdbc::postgres::resultset { jpayne@69: jpayne@69: superclass ::tdbc::resultset jpayne@69: jpayne@69: # The 'nextresults' method is stubbed out; tdbcpostgres does not jpayne@69: # allow a single call to return multiple results. jpayne@69: jpayne@69: method nextresults {} { jpayne@69: while {[my nextdict rubbish]} {} jpayne@69: return 0 jpayne@69: } jpayne@69: jpayne@69: # Methods implemented in C include: jpayne@69: jpayne@69: # constructor statement ?dictionary? jpayne@69: # -- Executes the statement against the database, optionally providing jpayne@69: # a dictionary of substituted parameters (default is to get params jpayne@69: # from variables in the caller's scope). jpayne@69: # columns jpayne@69: # -- Returns a list of the names of the columns in the result. jpayne@69: # nextdict jpayne@69: # -- Stores the next row of the result set in the given variable in jpayne@69: # the caller's scope as a dictionary whose keys are jpayne@69: # column names and whose values are column values, or else jpayne@69: # as a list of cells. jpayne@69: # nextlist jpayne@69: # -- Stores the next row of the result set in the given variable in jpayne@69: # the caller's scope as a list of cells. jpayne@69: # rowcount jpayne@69: # -- Returns a count of rows affected by the statement, or -1 jpayne@69: # if the count of rows has not been determined. jpayne@69: jpayne@69: }