-
Notifications
You must be signed in to change notification settings - Fork 66
/
defTime.go
196 lines (185 loc) · 5.65 KB
/
defTime.go
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
// Copyright 2014 Rana Ian. All rights reserved.
// Use of this source code is governed by The MIT License
// found in the accompanying LICENSE file.
package ora
/*
#include <stdlib.h>
#include <oci.h>
#include "version.h"
*/
import "C"
import (
"bytes"
"math"
"strings"
"time"
"unsafe"
)
type defTime struct {
ociDef
isNullable bool
dates []*C.OCIDateTime
}
func (def *defTime) define(position int, isNullable bool, rset *Rset) error {
def.rset = rset
def.isNullable = isNullable
if def.dates != nil {
C.free(unsafe.Pointer(&def.dates[0]))
}
def.dates = (*((*[MaxFetchLen]*C.OCIDateTime)(C.malloc(C.size_t(rset.fetchLen) * C.sof_DateTimep))))[:rset.fetchLen]
def.ensureAllocatedLength(len(def.dates))
return def.ociDef.defineByPos(position, unsafe.Pointer(&def.dates[0]), int(C.sof_DateTimep), C.SQLT_TIMESTAMP_TZ)
}
func (def *defTime) value(offset int) (value interface{}, err error) {
if def.nullInds[offset] < 0 {
if def.isNullable {
return Time{IsNull: true}, nil
}
return nil, nil
}
t, err := getTime(def.rset.stmt.ses.srv.env, def.dates[offset])
if def.isNullable {
return Time{Value: t}, err
}
return t, err
}
func (def *defTime) alloc() error {
for i := range def.dates {
def.allocated[i] = false
r := C.OCIDescriptorAlloc(
unsafe.Pointer(def.rset.stmt.ses.srv.env.ocienv), //CONST dvoid *parenth,
(*unsafe.Pointer)(unsafe.Pointer(&def.dates[i])), //dvoid **descpp,
C.OCI_DTYPE_TIMESTAMP_TZ, //ub4 type,
0, //size_t xtramem_sz,
nil) //dvoid **usrmempp);
if r == C.OCI_ERROR {
return def.rset.stmt.ses.srv.env.ociError()
} else if r == C.OCI_INVALID_HANDLE {
return errNew("unable to allocate oci timestamp handle during define")
}
def.allocated[i] = true
}
return nil
}
func (def *defTime) free() {
for i, d := range def.dates {
if d == nil {
continue
}
def.dates[i] = nil
if !def.allocated[i] {
continue
}
C.OCIDescriptorFree(
unsafe.Pointer(d), //void *descp,
C.OCI_DTYPE_TIMESTAMP_TZ) //timeDefine.descTypeCode) //ub4 type );
}
def.arrHlp.close()
}
func (def *defTime) close() (err error) {
defer func() {
if value := recover(); value != nil {
err = errR(value)
}
}()
def.free()
rset := def.rset
def.rset = nil
if def.dates != nil {
C.free(unsafe.Pointer(&def.dates[0]))
def.dates = nil
}
def.ocidef = nil
rset.putDef(defIdxTime, def)
return nil
}
func getTime(env *Env, ociDateTime *C.OCIDateTime) (result time.Time, err error) {
var year C.sb2
var month C.ub1
var day C.ub1
var hour C.ub1
var minute C.ub1
var second C.ub1
var fsec C.ub4
var location *time.Location
r := C.OCIDateTimeGetDate(
unsafe.Pointer(env.ocienv), //void *hndl,
env.ocierr, //OCIError *err,
ociDateTime, //const OCIDateTime *datetime,
&year, //sb2 *year,
&month, //ub1 *month,
&day) //ub1 *day );
if r == C.OCI_ERROR {
return result, env.ociError()
}
r = C.OCIDateTimeGetTime(
unsafe.Pointer(env.ocienv), //void *hndl,
env.ocierr, //OCIError *err,
ociDateTime, //OCIDateTime *datetime,
&hour, //ub1 *hour,
&minute, //ub1 *min,
&second, //ub1 *sec,
&fsec) //ub4 *fsec );
if r == C.OCI_ERROR {
return result, env.ociError()
}
var buf [32]byte
var buflen C.ub4 = 32
r = C.OCIDateTimeGetTimeZoneName(
unsafe.Pointer(env.ocienv), //void *hndl,
env.ocierr, //OCIError *err,
ociDateTime, //const OCIDateTime *datetime,
(*C.ub1)(&buf[0]), //ub1 *buf,
&buflen) //ub4 *buflen, );
if r != C.OCI_ERROR {
var buffer bytes.Buffer
for n := 0; n < int(buflen); n++ {
buffer.WriteByte(buf[n])
}
locName := buffer.String()
_drv.locationsMu.RLock()
location = _drv.locations[locName]
_drv.locationsMu.RUnlock()
if location == nil {
// timestamp_ltz returns numeric offset
// time.Time's lookup for numeric offset is unknown;
// therefore, create a fixed location for the offset
var offsetHour C.sb1
var offsetMinute C.sb1
if strings.ContainsAny(locName, "-0123456789") {
r = C.OCIDateTimeGetTimeZoneOffset(
unsafe.Pointer(env.ocienv), //void *hndl,
env.ocierr, //OCIError *err,
ociDateTime, //const OCIDateTime *datetime,
&offsetHour, //sb1 *hour,
&offsetMinute) //sb1 *min, );
if r == C.OCI_ERROR {
return result, env.ociError()
}
seconds := math.Abs(float64(offsetHour)) * 60 * 60
seconds += math.Abs(float64(offsetMinute)) * 60
if offsetHour < 0 {
seconds *= -1
}
location = time.FixedZone(locName, int(seconds))
} else {
location, err = time.LoadLocation(locName)
if err != nil {
return result, err
}
}
// stored location for future reference
// important that FixedZone is called as few times as possible
// to reduce significant memory allocation
_drv.locationsMu.Lock()
_drv.locations[locName] = location
_drv.locationsMu.Unlock()
}
} else {
// Date Oracle type doesn't have timezone info
// no timezone information available from server
location = time.Local
}
result = time.Date(int(year), time.Month(int(month)), int(day), int(hour), int(minute), int(second), int(fsec), location)
return result, nil
}