Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/tdbcmysql1.1.5/tdbcmysql.tcl @ 68:5028fdace37b
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 16:23:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 68:5028fdace37b |
---|---|
1 # tdbcmysql.tcl -- | |
2 # | |
3 # Class definitions and Tcl-level methods for the tdbc::mysql bridge. | |
4 # | |
5 # Copyright (c) 2008 by Kevin B. Kenny | |
6 # See the file "license.terms" for information on usage and redistribution | |
7 # of this file, and for a DISCLAIMER OF ALL WARRANTIES. | |
8 # | |
9 # RCS: @(#) $Id: tdbcmysql.tcl,v 1.47 2008/02/27 02:08:27 kennykb Exp $ | |
10 # | |
11 #------------------------------------------------------------------------------ | |
12 | |
13 package require tdbc | |
14 | |
15 ::namespace eval ::tdbc::mysql { | |
16 | |
17 namespace export connection datasources drivers | |
18 | |
19 } | |
20 | |
21 #------------------------------------------------------------------------------ | |
22 # | |
23 # tdbc::mysql::connection -- | |
24 # | |
25 # Class representing a connection to a database through MYSQL. | |
26 # | |
27 #------------------------------------------------------------------------------- | |
28 | |
29 ::oo::class create ::tdbc::mysql::connection { | |
30 | |
31 superclass ::tdbc::connection | |
32 | |
33 # The constructor is written in C. It takes alternating keywords | |
34 # and values pairs as its argumenta. (See the manual page for the | |
35 # available options.) | |
36 | |
37 variable foreignKeysStatement | |
38 | |
39 # The 'statementCreate' method delegates to the constructor of the | |
40 # statement class | |
41 | |
42 forward statementCreate ::tdbc::mysql::statement create | |
43 | |
44 # The 'columns' method returns a dictionary describing the tables | |
45 # in the database | |
46 | |
47 method columns {table {pattern %}} { | |
48 | |
49 # To return correct lengths of CHARACTER and BINARY columns, | |
50 # we need to know the maximum lengths of characters in each | |
51 # collation. We cache this information only once, on the first | |
52 # call to 'columns'. | |
53 | |
54 if {[my NeedCollationInfo]} { | |
55 my SetCollationInfo {*}[my allrows -as lists { | |
56 SELECT coll.id, cs.maxlen | |
57 FROM INFORMATION_SCHEMA.COLLATIONS coll, | |
58 INFORMATION_SCHEMA.CHARACTER_SETS cs | |
59 WHERE cs.CHARACTER_SET_NAME = coll.CHARACTER_SET_NAME | |
60 ORDER BY coll.id DESC | |
61 }] | |
62 } | |
63 | |
64 return [my Columns $table $pattern] | |
65 } | |
66 | |
67 # The 'preparecall' method gives a portable interface to prepare | |
68 # calls to stored procedures. It delegates to 'prepare' to do the | |
69 # actual work. | |
70 | |
71 method preparecall {call} { | |
72 regexp {^[[:space:]]*(?:([A-Za-z_][A-Za-z_0-9]*)[[:space:]]*=)?(.*)} \ | |
73 $call -> varName rest | |
74 if {$varName eq {}} { | |
75 my prepare "CALL $rest" | |
76 } else { | |
77 my prepare \\{:$varName=$rest\\} | |
78 } | |
79 } | |
80 | |
81 # The 'init', 'begintransaction', 'commit, 'rollback', 'tables' | |
82 # 'NeedCollationInfo', 'SetCollationInfo', and 'Columns' methods | |
83 # are implemented in C. | |
84 | |
85 # The 'BuildForeignKeysStatements' method builds a SQL statement to | |
86 # retrieve the foreign keys from a database. (It executes once the | |
87 # first time the 'foreignKeys' method is executed, and retains the | |
88 # prepared statements for reuse.) It is slightly nonstandard because | |
89 # MYSQL doesn't name the PRIMARY constraints uniquely. | |
90 | |
91 method BuildForeignKeysStatement {} { | |
92 | |
93 foreach {exists1 clause1} { | |
94 0 {} | |
95 1 { AND fkc.REFERENCED_TABLE_NAME = :primary} | |
96 } { | |
97 foreach {exists2 clause2} { | |
98 0 {} | |
99 1 { AND fkc.TABLE_NAME = :foreign} | |
100 } { | |
101 set stmt [my prepare " | |
102 SELECT rc.CONSTRAINT_SCHEMA AS \"foreignConstraintSchema\", | |
103 rc.CONSTRAINT_NAME AS \"foreignConstraintName\", | |
104 rc.UPDATE_RULE AS \"updateAction\", | |
105 rc.DELETE_RULE AS \"deleteAction\", | |
106 fkc.REFERENCED_TABLE_SCHEMA AS \"primarySchema\", | |
107 fkc.REFERENCED_TABLE_NAME AS \"primaryTable\", | |
108 fkc.REFERENCED_COLUMN_NAME AS \"primaryColumn\", | |
109 fkc.TABLE_SCHEMA AS \"foreignSchema\", | |
110 fkc.TABLE_NAME AS \"foreignTable\", | |
111 fkc.COLUMN_NAME AS \"foreignColumn\", | |
112 fkc.ORDINAL_POSITION AS \"ordinalPosition\" | |
113 FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc | |
114 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE fkc | |
115 ON fkc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME | |
116 AND fkc.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA | |
117 WHERE 1=1 | |
118 $clause1 | |
119 $clause2 | |
120 "] | |
121 dict set foreignKeysStatement $exists1 $exists2 $stmt | |
122 } | |
123 } | |
124 } | |
125 } | |
126 | |
127 #------------------------------------------------------------------------------ | |
128 # | |
129 # tdbc::mysql::statement -- | |
130 # | |
131 # The class 'tdbc::mysql::statement' models one statement against a | |
132 # database accessed through an MYSQL connection | |
133 # | |
134 #------------------------------------------------------------------------------ | |
135 | |
136 ::oo::class create ::tdbc::mysql::statement { | |
137 | |
138 superclass ::tdbc::statement | |
139 | |
140 # The 'resultSetCreate' method forwards to the constructor of the | |
141 # result set. | |
142 | |
143 forward resultSetCreate ::tdbc::mysql::resultset create | |
144 | |
145 # Methods implemented in C: | |
146 # | |
147 # constructor connection SQLCode | |
148 # The constructor accepts the handle to the connection and the SQL code | |
149 # for the statement to prepare. It creates a subordinate namespace to | |
150 # hold the statement's active result sets, and then delegates to the | |
151 # 'init' method, written in C, to do the actual work of preparing the | |
152 # statement. | |
153 # params | |
154 # Returns descriptions of the parameters of a statement. | |
155 # paramtype paramname ?direction? type ?precision ?scale?? | |
156 # Declares the type of a parameter in the statement | |
157 | |
158 } | |
159 | |
160 #------------------------------------------------------------------------------ | |
161 # | |
162 # tdbc::mysql::resultset -- | |
163 # | |
164 # The class 'tdbc::mysql::resultset' models the result set that is | |
165 # produced by executing a statement against an MYSQL database. | |
166 # | |
167 #------------------------------------------------------------------------------ | |
168 | |
169 ::oo::class create ::tdbc::mysql::resultset { | |
170 | |
171 superclass ::tdbc::resultset | |
172 | |
173 # Methods implemented in C include: | |
174 | |
175 # constructor statement ?dictionary? | |
176 # -- Executes the statement against the database, optionally providing | |
177 # a dictionary of substituted parameters (default is to get params | |
178 # from variables in the caller's scope). | |
179 # columns | |
180 # -- Returns a list of the names of the columns in the result. | |
181 # nextdict | |
182 # -- Stores the next row of the result set in the given variable in | |
183 # the caller's scope as a dictionary whose keys are | |
184 # column names and whose values are column values, or else | |
185 # as a list of cells. | |
186 # nextlist | |
187 # -- Stores the next row of the result set in the given variable in | |
188 # the caller's scope as a list of cells. | |
189 # rowcount | |
190 # -- Returns a count of rows affected by the statement, or -1 | |
191 # if the count of rows has not been determined. | |
192 | |
193 } |