jpayne@68
|
1 # tdbcpostgres.tcl --
|
jpayne@68
|
2 #
|
jpayne@68
|
3 # Class definitions and Tcl-level methods for the tdbc::postgres bridge.
|
jpayne@68
|
4 #
|
jpayne@68
|
5 # Copyright (c) 2009 by Slawomir Cygan
|
jpayne@68
|
6 #
|
jpayne@68
|
7 # See the file "license.terms" for information on usage and redistribution
|
jpayne@68
|
8 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
jpayne@68
|
9 #
|
jpayne@68
|
10 #------------------------------------------------------------------------------
|
jpayne@68
|
11
|
jpayne@68
|
12 package require tdbc
|
jpayne@68
|
13
|
jpayne@68
|
14 ::namespace eval ::tdbc::mypostgres {
|
jpayne@68
|
15
|
jpayne@68
|
16 namespace export connection datasources drivers
|
jpayne@68
|
17
|
jpayne@68
|
18 }
|
jpayne@68
|
19
|
jpayne@68
|
20 #------------------------------------------------------------------------------
|
jpayne@68
|
21 #
|
jpayne@68
|
22 # tdbc::postgres::connection --
|
jpayne@68
|
23 #
|
jpayne@68
|
24 # Class representing a connection to a Postgres database.
|
jpayne@68
|
25 #
|
jpayne@68
|
26 #-------------------------------------------------------------------------------
|
jpayne@68
|
27
|
jpayne@68
|
28 ::oo::class create ::tdbc::postgres::connection {
|
jpayne@68
|
29
|
jpayne@68
|
30 superclass ::tdbc::connection
|
jpayne@68
|
31
|
jpayne@68
|
32 # The constructor is written in C. It takes alternating keywords
|
jpayne@68
|
33 # and values pairs as its arguments. (See the manual page for the
|
jpayne@68
|
34 # available options.)
|
jpayne@68
|
35
|
jpayne@68
|
36 # The 'statementCreate' method delegates to the constructor of the
|
jpayne@68
|
37 # statement class
|
jpayne@68
|
38
|
jpayne@68
|
39 forward statementCreate ::tdbc::postgres::statement create
|
jpayne@68
|
40
|
jpayne@68
|
41
|
jpayne@68
|
42 # The 'prepareCall' method gives a portable interface to prepare
|
jpayne@68
|
43 # calls to stored procedures. It delegates to 'prepare' to do the
|
jpayne@68
|
44 # actual work.
|
jpayne@68
|
45
|
jpayne@68
|
46 method preparecall {call} {
|
jpayne@68
|
47 regexp {^[[:space:]]*(?:([A-Za-z_][A-Za-z_0-9]*)[[:space:]]*=)?(.*)} \
|
jpayne@68
|
48 $call -> varName rest
|
jpayne@68
|
49 if {$varName eq {}} {
|
jpayne@68
|
50 my prepare \\{$rest\\}
|
jpayne@68
|
51 } else {
|
jpayne@68
|
52 my prepare \\{:$varName=$rest\\}
|
jpayne@68
|
53 }
|
jpayne@68
|
54 }
|
jpayne@68
|
55
|
jpayne@68
|
56 # The 'init', 'begintransaction', 'commit, 'rollback', 'tables'
|
jpayne@68
|
57 # and 'columns' methods are implemented in C.
|
jpayne@68
|
58
|
jpayne@68
|
59 }
|
jpayne@68
|
60
|
jpayne@68
|
61 #------------------------------------------------------------------------------
|
jpayne@68
|
62 #
|
jpayne@68
|
63 # tdbc::postgres::statement --
|
jpayne@68
|
64 #
|
jpayne@68
|
65 # The class 'tdbc::postgres::statement' models one statement against a
|
jpayne@68
|
66 # database accessed through a Postgres connection
|
jpayne@68
|
67 #
|
jpayne@68
|
68 #------------------------------------------------------------------------------
|
jpayne@68
|
69
|
jpayne@68
|
70 ::oo::class create ::tdbc::postgres::statement {
|
jpayne@68
|
71
|
jpayne@68
|
72 superclass ::tdbc::statement
|
jpayne@68
|
73
|
jpayne@68
|
74 # The 'resultSetCreate' method forwards to the constructor of the
|
jpayne@68
|
75 # result set.
|
jpayne@68
|
76
|
jpayne@68
|
77 forward resultSetCreate ::tdbc::postgres::resultset create
|
jpayne@68
|
78
|
jpayne@68
|
79 # Methods implemented in C:
|
jpayne@68
|
80 #
|
jpayne@68
|
81 # constructor connection SQLCode
|
jpayne@68
|
82 # The constructor accepts the handle to the connection and the SQL code
|
jpayne@68
|
83 # for the statement to prepare. It creates a subordinate namespace to
|
jpayne@68
|
84 # hold the statement's active result sets, and then delegates to the
|
jpayne@68
|
85 # 'init' method, written in C, to do the actual work of preparing the
|
jpayne@68
|
86 # statement.
|
jpayne@68
|
87 # params
|
jpayne@68
|
88 # Returns descriptions of the parameters of a statement.
|
jpayne@68
|
89 # paramtype paramname ?direction? type ?precision ?scale??
|
jpayne@68
|
90 # Declares the type of a parameter in the statement
|
jpayne@68
|
91
|
jpayne@68
|
92 }
|
jpayne@68
|
93
|
jpayne@68
|
94 #------------------------------------------------------------------------------
|
jpayne@68
|
95 #
|
jpayne@68
|
96 # tdbc::postgres::resultset --
|
jpayne@68
|
97 #
|
jpayne@68
|
98 # The class 'tdbc::postgres::resultset' models the result set that is
|
jpayne@68
|
99 # produced by executing a statement against a Postgres database.
|
jpayne@68
|
100 #
|
jpayne@68
|
101 #------------------------------------------------------------------------------
|
jpayne@68
|
102
|
jpayne@68
|
103 ::oo::class create ::tdbc::postgres::resultset {
|
jpayne@68
|
104
|
jpayne@68
|
105 superclass ::tdbc::resultset
|
jpayne@68
|
106
|
jpayne@68
|
107 # The 'nextresults' method is stubbed out; tdbcpostgres does not
|
jpayne@68
|
108 # allow a single call to return multiple results.
|
jpayne@68
|
109
|
jpayne@68
|
110 method nextresults {} {
|
jpayne@68
|
111 while {[my nextdict rubbish]} {}
|
jpayne@68
|
112 return 0
|
jpayne@68
|
113 }
|
jpayne@68
|
114
|
jpayne@68
|
115 # Methods implemented in C include:
|
jpayne@68
|
116
|
jpayne@68
|
117 # constructor statement ?dictionary?
|
jpayne@68
|
118 # -- Executes the statement against the database, optionally providing
|
jpayne@68
|
119 # a dictionary of substituted parameters (default is to get params
|
jpayne@68
|
120 # from variables in the caller's scope).
|
jpayne@68
|
121 # columns
|
jpayne@68
|
122 # -- Returns a list of the names of the columns in the result.
|
jpayne@68
|
123 # nextdict
|
jpayne@68
|
124 # -- Stores the next row of the result set in the given variable in
|
jpayne@68
|
125 # the caller's scope as a dictionary whose keys are
|
jpayne@68
|
126 # column names and whose values are column values, or else
|
jpayne@68
|
127 # as a list of cells.
|
jpayne@68
|
128 # nextlist
|
jpayne@68
|
129 # -- Stores the next row of the result set in the given variable in
|
jpayne@68
|
130 # the caller's scope as a list of cells.
|
jpayne@68
|
131 # rowcount
|
jpayne@68
|
132 # -- Returns a count of rows affected by the statement, or -1
|
jpayne@68
|
133 # if the count of rows has not been determined.
|
jpayne@68
|
134
|
jpayne@68
|
135 }
|