-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbc.tcl
318 lines (243 loc) · 7.86 KB
/
dbc.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# dbc.tcl --
#
# Database Connectivity module
#
# Copyright (c) 2013 by Nagarajan Chinnasamy <[email protected]>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
namespace eval ::tdao {}
namespace eval ::tdao::dbc {
variable commands
set commands [list load]
variable driver_commands
set driver_commands [list open]
variable conn_commands
set conn_commands [list insert get mget update delete begin commit rollback close]
variable drivers
set drivers [dict create]
variable connections
set connections [dict create -count 0]
namespace export dbc
}
proc ::tdao::dbc::dbc {cmd args} {
variable commands
if {[lsearch $commands $cmd] < 0} {
return -code error "Sub command \"$cmd\" is not recognized. Must be [join $commands ,]"
}
set cmd [string totitle "$cmd"]
return [uplevel 1 ::tdao::dbc::${cmd} $args]
}
proc ::tdao::dbc::Load {driver {version ""}} {
variable drivers
if {[dict exists $drivers $driver]} {
return [dict get $dbms $driver -cmd]
}
if {[catch {package require tdao::dbc::${driver} {*}$version} err]} {
return -code error "Unable to load dbc driver $driver.\nError: $err"
}
if {[catch {[namespace current]::${driver}::Load} err]} {
return -code error "Unable to load dbc driver $driver.\nError: $err"
}
set driver_cmd [format "%s%s%s" [namespace current] "___" $driver]
uplevel #0 [list interp alias {} $driver_cmd {} [namespace current]::DriverCmd $driver]
dict set drivers $driver -cmd $driver_cmd
return $driver_cmd
}
proc ::tdao::dbc::DriverCmd {driver cmd args} {
variable driver_commands
if {[lsearch $driver_commands $cmd] < 0} {
return -code error "Sub command \"$cmd\" is not recognized. Must be [join $driver_commands ,]"
}
return [uplevel 1 [namespace current]::${cmd} $driver $args]
}
proc ::tdao::dbc::open {driver args} {
variable drivers
variable connections
if {![dict exists $drivers $driver]} {
return -code error "GDBC driver $driver not loaded"
}
dict incr connections -count
set conn_cmd [format "%s%s" "::tdao::dbc::conncmd" [dict get $connections -count]]
if {[catch {uplevel 1 [namespace current]::${driver}::open $args} result]} {
return -code error $result
}
set conn $result
uplevel #0 [list interp alias {} $conn_cmd {} [namespace current]::ConnCmd $conn_cmd]
dict set connections $conn_cmd -driver $driver
dict set connections $conn_cmd -conn $conn
return $conn_cmd
}
proc ::tdao::dbc::ConnCmd {conn_cmd cmd args} {
variable connections
variable conn_commands
if {![dict exists $connections $conn_cmd]} {
return -code error "Connection $conn_cmd does not exist"
}
if {[lsearch $conn_commands $cmd] < 0} {
return -code error "Sub command \"$cmd\" is not recognized. Must be [join $conn_commands ,]"
}
set driver [dict get $connections $conn_cmd -driver]
set conn [dict get $connections $conn_cmd -conn]
if {$cmd == "close"} {
return [uplevel 1 [namespace current]::${cmd} $conn_cmd $driver $conn $args]
}
return [uplevel 1 [namespace current]::${cmd} $driver $conn $args]
}
proc ::tdao::dbc::close {conn_cmd driver conn} {
variable connections
if {[catch {uplevel 1 [namespace current]::${driver}::close $conn} err]} {
return -code error $err
}
if {[dict exists $connections $conn_cmd]} {
dict unset connections $conn_cmd
}
}
proc ::tdao::dbc::get {driver conn schema_name fieldslist condition {format "dict"}} {
set stmt [_prepare_select_stmt $schema_name $fieldslist -condition [_prepare_condition $condition]]
if {[catch {uplevel 1 [list [namespace current]::${driver}::get $conn $stmt $fieldslist $format]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::mget {driver conn schema_name fieldslist {format "dict"} args} {
set stmt [_prepare_select_stmt $schema_name $fieldslist {*}$args]]
if {[catch {uplevel 1 [list [namespace current]::${driver}::get $conn $stmt $fieldslist $format]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::insert {driver conn schema_name namevaluepairs {sequence_fields ""}} {
set stmt [_prepare_insert_stmt $schema_name $namevaluepairs]
if {[catch {uplevel 1 [list [namespace current]::${driver}::insert $conn $stmt $sequence_fields]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::update {driver conn schema_name namevaluepairs {condition ""}} {
set stmt [_prepare_update_stmt $schema_name $namevaluepairs $condition]
if {[catch {uplevel 1 [list [namespace current]::${driver}::update $conn $stmt]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::delete {driver conn schema_name {condition ""}} {
set stmt [_prepare_delete_stmt $schema_name $condition]
if {[catch {uplevel 1 [list [namespace current]::${driver}::delete $conn $stmt]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::begin {driver conn args} {
if {[catch {uplevel 1 [list [namespace current]::${driver}::begin $conn {*}$args]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::commit {driver conn args} {
if {[catch {uplevel 1 [list [namespace current]::${driver}::commit $conn {*}$args]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::rollback {driver conn args} {
if {[catch {uplevel 1 [list [namespace current]::${driver}::rollback $conn {*}$args]} result]} {
return -code error $result
}
return $result
}
proc ::tdao::dbc::_prepare_insert_stmt {schema_name namevaluepairs} {
set fnamelist [join [dict keys $namevaluepairs] ", "]
set valuelist [list]
foreach value [dict values $namevaluepairs] {
lappend valuelist '$value'
}
set valuelist [join $valuelist ", "]
set stmt "INSERT INTO $schema_name ($fnamelist) VALUES ($valuelist)"
return $stmt
}
proc ::tdao::dbc::_prepare_select_stmt {schema_name fieldslist args} {
set fieldslist [join $fieldslist ", "]
set condition ""
set groupby ""
set orderby ""
foreach {opt val} $args {
switch $opt {
-condition {
set condition $val
}
-groupby {
set groupby $val
}
-orderby {
set orderby $val
}
default {
return -code error "Unknown option: $opt"
}
}
}
set stmt "SELECT $fieldslist FROM $schema_name"
if [string length $condition] {
append stmt " WHERE $condition"
}
if [string length $groupby] {
append stmt " GROUP BY $groupby"
}
if [string length $orderby] {
append stmt " ORDER BY $orderby"
}
return $stmt
}
proc ::tdao::dbc::_prepare_update_stmt {schema_name namevaluepairs {condition ""}} {
set setlist ""
foreach {name val} $namevaluepairs {
lappend setlist "$name='$val'"
}
set setlist [join $setlist ", "]
set stmt "UPDATE $schema_name SET $setlist"
if [string length $condition] {
append stmt " WHERE [_prepare_condition $condition]"
}
return $stmt
}
proc ::tdao::dbc::_prepare_delete_stmt {schema_name {condition ""}} {
set stmt "DELETE FROM $schema_name"
if {[string length $condition]} {
append stmt " WHERE [_prepare_condition $condition]"
}
return $stmt
}
proc ::tdao::dbc::_prepare_condition {conditionlist} {
set sqlcondition [list]
foreach condition $conditionlist {
set complist [list]
foreach {fname val} $condition {
lappend complist "$fname='$val'"
}
if {$complist != ""} {
lappend sqlcondition "([join $complist " AND "])"
}
}
if {$sqlcondition == ""} {
return
}
set sqlcondition [join $sqlcondition " OR "]
return "($sqlcondition)"
}
proc ::tdao::dbc::_qualify {item {level 2}} {
if {![string match "::*" "$item"]} {
set ns [uplevel $level [list namespace current]]
if {![string match "::" "$ns"]} {
append ns "::"
}
set item "$ns${item}"
}
return "$item"
}
namespace eval ::tdao {
# Get 'dbc::dbc' into the general structure namespace.
namespace import -force dbc::dbc
namespace export dbc
}
package provide tdao::dbc 0.1.1