File: dio_Oracle.tcl

package info (click to toggle)
libapache2-mod-rivet 3.2.6-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,384 kB
  • sloc: tcl: 8,982; xml: 8,619; ansic: 7,074; sh: 5,039; makefile: 195; sql: 91; lisp: 78
file content (199 lines) | stat: -rw-r--r-- 6,012 bytes parent folder | download
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
# dio_Oracle.tcl -- Oracle (odbc) backend.

# Copyright 2006-2024 The Apache Software Foundation

#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.  The ASF licenses this file
#    to you 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.

package require DIO        1.2
package provide dio_Oracle 1.2

namespace eval DIO {
    ::itcl::class Oracle {
        inherit Database

        constructor {args} {eval configure -interface Oracle $args} {
            if {[catch {package require Oratcl}]} {
                    return -code error "No Oracle Tcl package available"
            }

            eval configure $args

            if {[::rivet::lempty $db]} {
                if {[::rivet::lempty $user]} {:b
                    set user $::env(USER)
                }
                set db $user
            }
        }

        destructor {
            close
        }

        method open {} {
            set command "::oralogon"

            if {![::rivet::lempty $user]} { append command " $user" }
            if {![::rivet::lempty $pass]} { append command "/$pass" }
            if {![::rivet::lempty $host]} { append command "@$host" }
            if {![::rivet::lempty $port]} { append command -port $port }

            if {[catch $command error]} { return -code error $error }

            set conn $error

            if {![::rivet::lempty $db]} { 
                # ??? mysqluse $conn $db 
            }
        }

        method close {} {
            if {![info exists conn]} { return }
            catch {::oraclose $conn}
            unset conn
        }

        method exec {req} {
            if {![info exists conn]} { open }

            set _cur [::oraopen $conn]
            set cmd ::orasql
            set is_select 0
            if {[::string tolower [lindex $req 0]] == "select"} {
                set cmd ::orasql
                set is_select 1
            }
            set errorinfo ""
    #puts "ORA:$is_select:$req:<br>"
            if {[catch {$cmd $_cur $req} error]} {
    #puts "ORA:error:$error:<br>"
                set errorinfo $error
                catch {::oraclose $_cur}
                set obj [result $interface -error 1 -errorinfo [::list $error]]
                return $obj
            }
            if {[catch {::oracols $_cur name} fields]} { set fields "" }
            ::oracommit $conn
            set my_fields $fields
            set fields [::list]
            foreach field $my_fields {
                set field [::string tolower $field]
                lappend fields $field
            }
            set error [::oramsg $_cur rows]
            set res_cmd "result"
            lappend res_cmd $interface -resultid $_cur 
            lappend res_cmd -numrows [::list $error] -fields [::list $fields]
            lappend res_cmd -fetch_first_row $is_select
            set obj [eval $res_cmd]
            if {!$is_select} {
                ::oraclose $_cur
            }
            return $obj
        }

        method lastkey {} {
            if {![info exists conn]} { return }
            return [mysqlinsertid $conn]
        }

        method quote {string} {
            regsub -all {'} $string {\'} string
            return $string
        }

        method sql_limit_syntax {limit {offset ""}} {
            # temporary
            return ""
            if {[::rivet::lempty $offset]} {
                return " LIMIT $limit"
            }
            return " LIMIT [expr $offset - 1],$limit"
        }

        method handle {} {
            if {![info exists conn]} { open }
            return $conn
        }

        public variable db "" {
            if {[info exists conn]} {
                mysqluse $conn $db
            }
        }

        #public variable interface	"Oracle"
        private variable conn
        private variable _cur

    } ; ## ::itcl::class Mysql

    ::itcl::class OracleResult {
        inherit Result

        public variable fetch_first_row 0
        private variable _data ""
        private variable _have_first_row 0

        constructor {args} {
            eval configure $args
            if {$fetch_first_row} {
                if {[llength [nextrow]] == 0} {
                    set _have_first_row 0
                    numrows 0
                } else {
                    set _have_first_row 1
                    numrows 1
                }
            }
            set fetch_first_row 0
        }

        destructor {
            if {[string length $resultid] > 0} {
                catch {::oraclose $resultid}
            }
        }

        method nextrow {} {
            if {[string length $resultid] == 0} {
                return [::list]
            }
            if {$_have_first_row} {
                set _have_first_row 0
                return $_data
            }
            set ret [::orafetch $resultid -datavariable _data]
            switch $ret {
                0 {
                    return $_data
                }
                1403 {
                    ::oraclose $resultid
                    set resultid ""
                    return [::list]
                }
                default {
                    # FIXME!! have to handle error here !!
                    return [::list]
                }
            }
        }
    } ; ## ::itcl::class OracleResult

}