File: dio_Oracle.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 (241 lines) | stat: -rw-r--r-- 5,688 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
# dio_Mysql.tcl -- Mysql backend.

# Copyright 2006 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.

# $Id: dio_Oracle.tcl 265421 2004-10-29 20:17:54Z karl $

package provide dio_Oracle 0.1

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

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

	    eval configure $args

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

	destructor {
	    close
	}

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

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

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

	    set conn $error

	    if {![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 {[lempty $offset]} {
		return " LIMIT $limit"
	    }
	    return " LIMIT [expr $offset - 1],$limit"
	}

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

	method makeDBFieldValue {table_name field_name val {convert_to {}}} {
		if {[info exists specialFields(${table_name}@${field_name})]} {
			switch $specialFields(${table_name}@${field_name}) {
			DATE {
			  	set secs [clock scan $val]
				set my_val [clock format $secs -format {%Y-%m-%d}]
				return "to_date('$my_val', 'YYYY-MM-DD')"
			}
			DATETIME {
			  	set secs [clock scan $val]
				set my_val [clock format $secs -format {%Y-%m-%d %T}]
				return "to_date('$my_val', 'YYYY-MM-DD HH24:MI:SS')"
			}
			NOW {
			    switch $convert_to {
				SECS {
				    if {[::string compare $val "now"] == 0} {
					set secs [clock seconds]
				    	set my_val [clock format $secs -format {%Y%m%d%H%M%S}]
					return $my_val
				    } else {
                                        return "($field_name - to_date('1970-01-01')) * 86400"
				    	#return "to_char($field_name, 'YYYYMMDDHH24MISS')"
				    }
				}
				default {
				    if {[::string compare $val "now"] == 0} {
					set secs [clock seconds]
				    } else {
					set secs [clock scan $val]
				    }
				    set my_val [clock format $secs -format {%Y-%m-%d %T}]
				    return "to_date('$my_val', 'YYYY-MM-DD HH24:MI:SS')"
				}
			    }
			}
			default {
			  	# no special cod for that type!!
				return "'[quote $val]'"
			  }
			}
		} else {
			return "'[quote $val]'"
		}
	}

	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

}