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