Skip to content

Commit

Permalink
Allow godror/knownpb/timestamppb.Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
tgulacsi committed Nov 18, 2021
1 parent c69929f commit d66e9f2
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 12 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module github.com/godror/godror
go 1.14

require (
github.com/UNO-SOFT/knownpb v0.0.2 // indirect
github.com/go-logfmt/logfmt v0.5.0
github.com/google/go-cmp v0.5.5
github.com/godror/knownpb v0.1.0
github.com/google/go-cmp v0.5.6
github.com/oklog/ulid/v2 v2.0.2
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
github.com/UNO-SOFT/knownpb v0.0.2 h1:ABfezjmPyj85MaPHXhgQeS8W1Fp0XQJxax33FM32lzg=
github.com/UNO-SOFT/knownpb v0.0.2/go.mod h1:p80FhK7Efqtw1I44+KdbwHKT2Fg2KluTHKtkGN8YXfE=
github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih4=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/godror/knownpb v0.1.0 h1:dJPK8s/I3PQzGGaGcUStL2zIaaICNzKKAK8BzP1uLio=
github.com/godror/knownpb v0.1.0/go.mod h1:4nRFbQo1dDuwKnblRXDxrfCFYeT4hjg3GjMqef58eRE=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/oklog/ulid/v2 v2.0.2 h1:r4fFzBm+bv0wNKNh5eXTwU7i85y5x+uwkxCUTNVQqLc=
github.com/oklog/ulid/v2 v2.0.2/go.mod h1:mtBL0Qe/0HAx6/a4Z30qxVIAL1eQDweXq5lxOEiwQ68=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (j JSON) String() string {
// Returning empty string for error case, fix?

jScalar, err := j.GetJSONScalar(JSONOptNumberAsString)
logger := getLogger()
logger := getLogger()
if err != nil {
if logger != nil {
logger.Log("msg", "JSON.String", "Error", err.Error())
Expand Down
87 changes: 78 additions & 9 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"sync"
"time"
"unsafe"

"github.com/godror/knownpb/timestamppb"
)

type NullTime = sql.NullTime
Expand Down Expand Up @@ -466,6 +468,9 @@ func (st *statement) ExecContext(ctx context.Context, args []driver.NamedValue)
}
return nil, closeIfBadConn(fmt.Errorf("%d. get[%d]: %w", i, 0, err))
}
if logger != nil {
logger.Log("msg", "get-not-slice", "i", i, "dest", dest)
}
continue
}
var n C.uint32_t = 1
Expand All @@ -482,6 +487,9 @@ func (st *statement) ExecContext(ctx context.Context, args []driver.NamedValue)
}
return nil, closeIfBadConn(fmt.Errorf("%d. get: %w", i, err))
}
if false && logger != nil {
logger.Log("msg", "get-slice", "i", i, "dest", dest)
}
}
var count C.uint64_t
if st.checkExec(func() C.int { return C.dpiStmt_getRowCount(st.dpiStmt, &count) }) != nil {
Expand Down Expand Up @@ -894,7 +902,7 @@ func (st *statement) bindVarTypeSwitch(info *argInfo, get *dataGetter, value int
vlr, isValuer := value.(driver.Valuer)

switch value.(type) {
case *driver.Rows, *Object:
case *driver.Rows, *Object, *timestamppb.Timestamp:
default:
if rv := reflect.ValueOf(value); rv.Kind() == reflect.Ptr {
if nilPtr = rv.IsNil(); nilPtr {
Expand Down Expand Up @@ -1104,16 +1112,15 @@ func (st *statement) bindVarTypeSwitch(info *argInfo, get *dataGetter, value int
*get = dataGetBytes
}

case time.Time, NullTime:
case time.Time, NullTime, *timestamppb.Timestamp:
info.typ, info.natTyp = C.DPI_ORACLE_TYPE_TIMESTAMP_TZ, C.DPI_NATIVE_TYPE_TIMESTAMP
info.set = st.conn.dataSetTime
if info.isOut {
*get = st.conn.dataGetTime
}

case []time.Time, []NullTime:
case []time.Time, []NullTime, []*timestamppb.Timestamp:
info.typ, info.natTyp = C.DPI_ORACLE_TYPE_TIMESTAMP_TZ, C.DPI_NATIVE_TYPE_TIMESTAMP
// info.Typ should be C.DPI_ORACLE_TYPE_TIMESTAMP_TZ, but it does not work for DATE PL/SQL associative arrays
if st.plSQLArrays {
info.typ = C.DPI_ORACLE_TYPE_DATE
}
Expand Down Expand Up @@ -1174,6 +1181,9 @@ func (st *statement) bindVarTypeSwitch(info *argInfo, get *dataGetter, value int
}

default:
if logger != nil {
logger.Log("msg", "bindVarTypeSwitch default", "value", value)
}
if !isValuer {
return value, fmt.Errorf("unknown type %T", value)
}
Expand All @@ -1182,7 +1192,7 @@ func (st *statement) bindVarTypeSwitch(info *argInfo, get *dataGetter, value int
if value, err = vlr.Value(); err != nil {
return value, fmt.Errorf("arg.Value(): %w", err)
}
if logger != nil {
if logger != nil {
logger.Log("msg", "valuer", "old", fmt.Sprintf("[%T]%#v.Value()", oval, oval), "new", fmt.Sprintf("[%T]%#v", value, value))
}
return st.bindVarTypeSwitch(info, get, value)
Expand Down Expand Up @@ -1255,13 +1265,33 @@ func dataSetBool(dv *C.dpiVar, data []C.dpiData, vv interface{}) error {
var _ = sql.Scanner((*NullTime)(nil))

func (c *conn) dataGetTime(v interface{}, data []C.dpiData) error {
logger := getLogger()
if logger != nil {
logger.Log("msg", "dataGetTime", "v", fmt.Sprintf("%[1]T %[1]#v", v))
}
switch x := v.(type) {
case *time.Time:
if len(data) == 0 || data[0].isNull == 1 {
*x = time.Time{}
return nil
}
c.dataGetTimeC(x, &data[0])
case *timestamppb.Timestamp:
if len(data) == 0 || data[0].isNull == 1 {
x.Reset()
return nil
}
var t time.Time
c.dataGetTimeC(&t, &data[0])
if t.IsZero() {
x.Reset()
} else {
if x == nil {
x = timestamppb.New(t)
} else {
*x = *timestamppb.New(t)
}
}

case *NullTime:
if x.Valid = !(len(data) == 0 || data[0].isNull == 1); x.Valid {
Expand All @@ -1278,6 +1308,26 @@ func (c *conn) dataGetTime(v interface{}, data []C.dpiData) error {
for i := range data {
c.dataGetTimeC(&((*x)[i]), &data[i])
}
case *[]*timestamppb.Timestamp:
n := len(data)
if cap(*x) >= n {
*x = (*x)[:n]
} else {
*x = make([]*timestamppb.Timestamp, n)
}
for i := range data {
var t time.Time
c.dataGetTimeC(&t, &data[i])
if t.IsZero() {
(*x)[i].Reset()
} else {
if (*x)[i] == nil {
(*x)[i] = timestamppb.New(t)
} else {
*((*x)[i]) = *timestamppb.New(t)
}
}
}
case *[]NullTime:
n := len(data)
if cap(*x) >= n {
Expand All @@ -1291,10 +1341,17 @@ func (c *conn) dataGetTime(v interface{}, data []C.dpiData) error {
}
}

default:
return fmt.Errorf("dataGetTime(%T): %w", v, errUnknownType)
}
if logger != nil {
logger.Log("msg", "dataGetTime", "v", fmt.Sprintf("%[1]T %[1]#v", v))
}
return nil
}

var errUnknownType = errors.New("unknown type")

func (c *conn) dataGetTimeC(t *time.Time, data *C.dpiData) {
if data.isNull == 1 {
*t = time.Time{}
Expand Down Expand Up @@ -1325,6 +1382,10 @@ func (c *conn) dataSetTime(dv *C.dpiVar, data []C.dpiData, vv interface{}) error
if data[0].isNull = C.int(b2i(!x.Valid)); x.Valid {
times[0] = x.Time
}
case *timestamppb.Timestamp:
times[0] = x.AsTime()
data[0].isNull = C.int(b2i(times[0].IsZero()))

case []time.Time:
times = x
for i, t := range times {
Expand All @@ -1341,11 +1402,19 @@ func (c *conn) dataSetTime(dv *C.dpiVar, data []C.dpiData, vv interface{}) error
times[i] = x[i].Time
}
}
default:
for i := range data {
data[i].isNull = 1
case []*timestamppb.Timestamp:
if cap(times) < len(x) {
times = make([]time.Time, len(x))
} else {
times = times[:len(x)]
}
return nil
for i, n := range x {
times[i] = n.AsTime()
data[i].isNull = C.int(b2i(times[i].IsZero()))
}

default:
return fmt.Errorf("dataSetTime(%T): %w", vv, errUnknownType)
}

//tzHour, tzMin := C.int8_t(c.tzOffSecs/3600), C.int8_t((c.tzOffSecs%3600)/60)
Expand Down
2 changes: 1 addition & 1 deletion subscr.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (s *Subscription) Register(qry string, params ...interface{}) error {
if C.dpiStmt_getSubscrQueryId(dpiStmt, &queryID) == C.DPI_FAILURE {
return fmt.Errorf("getSubscrQueryId: %w", s.conn.getError())
}
logger := getLogger()
logger := getLogger()
if logger != nil {
logger.Log("msg", "subscribed", "query", qry, "id", queryID)
}
Expand Down

0 comments on commit d66e9f2

Please sign in to comment.