File: sql.tcl

package info (click to toggle)
libapache2-mod-rivet 2.3.3-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,156 kB
  • ctags: 1,093
  • sloc: xml: 7,696; tcl: 6,939; ansic: 5,682; sh: 4,862; makefile: 199; sql: 91; lisp: 78
file content (309 lines) | stat: -rw-r--r-- 9,090 bytes parent folder | download | duplicates (2)
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
# sql.tcl -- SQL code generator

# Copyright 2002-2004 The Apache Software Foundation

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#       http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This class provides a way to abstract to some extent the
# SQL code generation. It's supposed to provide a bridge to
# different implementation in various backends for specific
# functionalities
#
# $Id: sql.tcl 1596885 2014-05-22 14:19:10Z mxmanghi $

package require Itcl

### 
catch { ::itcl::delete class ::DIO::Sql }
###
namespace eval ::Aida {

    proc generator {backend} {
        
    }

    ::itcl::class Sql {

        public variable backend
        public variable what
        public variable table

        constructor { backend } {

        }

        private method where_clause {where_arguments}

        public method build_select_query {table row_d}
        public method quote {field_value}

        protected method field_value {table_name field_name val} {
            return "'[quote $val]'"
        }

        public method build_insert_query {table row_d}
        public method build_update_query {table row_d}

    }
    
    # -- build_insert_query
    #
    #

    ::itcl::body Sql::build_insert_query {table row_d} {

        set vars [dict keys $row_d]
        foreach field $vars {
        
            lappend vals [$this field_value $table $field [dict get $row_d $field]]

        }

        return "INSERT INTO $table ([join $vars {,}]) VALUES ([join $vals {,}])"
    }

    # -- build_update_query
    #
    #

    ::itcl::body Sql::build_update_query {table row_d} {
      
        foreach field [dict keys $row_d] {
            lappend rowfields "$field=[field_value $table $field [dict get $row_d $field]]"
        }

        return "UPDATE $table SET [join $rowfields {,}]"
    }


    # build_where_clause 
    #
    #
    ::itcl::body Sql::where_clause {where_expr} {

        set sql ""
        for {set i 0} {$i < [llength [dict keys $where_expr]]} {incr i} {

            set d [dict get $where_expr $i]

            set col [dict get $d column]
            set op  [dict get $d operator]
            if {$i > 0} {

                append sql " [dict get $d logical]"

            }
            switch $op {

                "eq" {
                    set sqlop "="
                }
                "ne" {
                    set sqlop "!="
                }
                "lt" {
                    set sqlop "<"
                }
                "gt" {
                    set sqlop ">"
                }
                "le" {
                    set sqlop "<="
                }
                "ge" {
                    set sqlop ">="
                }
                "notnull" {

                    append sql " $col IS NOT NULL"
                    continue

                }
                "null" {
                    append sql " $col IS NULL"
                    continue

                }

            }

            set predicate [dict get $d predicate]
            if {[::string first {%} $predicate] != -1} {
                append sql " $col LIKE [$this field_value $table $col [[string range $predicate 1 end]]"
            } else {
                append sql " $col$sqlop[$this field_value $table $col $predicate]"
            }
        }

        return $sql
    }    


    #
    # quote - given a string, return the same string with any single
    #  quote characters preceded by a backslash
    #
    ::itcl::body Sql::quote {field_value} {
        regsub -all {'} $field_value {\'} field_value
        return $field_value
    }

    # build_select_query - build a select query based on given arguments,
    # which can include a table name, a select statement, switches to
    # turn on boolean AND or OR processing, and possibly
    # some key-value pairs that cause the where clause to be
    # generated accordingly

    ::itcl::body Sql::build_select_query {args} {

        set bool    AND
        set first   1
        set req     ""
        set table   $from_table
        set what    "*"

        set parser_st   state0
        set condition_count 0
        set where_expr [dict create]

        # for each argument passed us...
        # (we go by integers because we mess with the index depending on
        # what we find)
        #puts "args: $args"
        for {set i 0} {$i < [llength $args]} {incr i} {

            # fetch the argument we're currently processing
            set elem [lindex $args $i]
            # puts "cycle: $i (elem: $elem, status: $parser_st, first: $first)"
            
            switch $parser_st {
                state0 {

                    switch -- [::string tolower $elem] {

                        # -table and -select don't drive the parser state machine
                        # and whatever they have as arguments on the command
                        # line they're set 

                        "-table" { 
                            # -table -- identify which table the query is about
                            set table [lindex $args [incr i]]
                        }
                        "-select" {
                            # -select - 
                            set what [lindex $args [incr i]]
                        }
                        "-or" - 
                        "-and" {
                            if {$first} {
                                return -code error "$elem can not be the first element of a where clause"
                            } else {
                                incr condition_count
                                dict set where_expr $condition_count logical [string range $elem 1 end] 
                                set parser_st where_op
                            }
                        }    
                        default {
                            
                            if {[::string index $elem 0] == "-"} {
                                if {!$first} { 
                                    incr condition_count 
                                }
                                dict set where_expr $condition_count column [string range $elem 1 end]
                                set first 0
                                set parser_st where_op
                            } else {

                                return -code error "Error: expected -<column_name>"
                            }

                        }

                    }

                }

                where_op {

                    switch -- [string tolower $elem] {

                        "-lt" -
                        "-gt" -
                        "-ne" -
                        "-eq" {

                            dict set where_expr $condition_count operator [string range $elem 1 end]
                            set parser_st cond_predicate

                        }
                         
                        "-null" -
                        "-notnull" {

                            dict set where_expr $condition_count operator [string range $elem 1 end]
                            set parser_st state0

                        }

                        default {
                            if {[::string index $elem 0] == "-"} {
                                dict set where_expr $condition_count column [string range $elem 1 end]
                            } else {
                                dict set where_expr $condition_count operator "eq"
                                dict set where_expr $condition_count predicate $elem 
                                set parser_st state0
                            }
                        }

                    }
                }

                cond_predicate {
                    
                    switch -- [string tolower $elem] {

                        "-expr" {
                            dict set where_expr $condition_count predicate [lindex $args [incr i]] 
                        }
                        default  {

                            # convert any asterisks to percent signs in the
                            # value field
                            regsub -all {\*} $elem {%} elem

                            dict set where_expr $condition_count predicate $elem

                        }

                    }
                    set parser_st state0
                }
                default {
                    return -code error "invalid parser status"
                }
            }
        }

        set sql "SELECT $what from $table WHERE[$this where_clause $where_expr]"

        return $sql
    }




}