2017-07-07 20:11:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 SAP SE
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
p "github.com/SAP/go-hdb/internal/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minTinyint = 0
|
|
|
|
maxTinyint = math.MaxUint8
|
|
|
|
minSmallint = math.MinInt16
|
|
|
|
maxSmallint = math.MaxInt16
|
|
|
|
minInteger = math.MinInt32
|
|
|
|
maxInteger = math.MaxInt32
|
|
|
|
minBigint = math.MinInt64
|
|
|
|
maxBigint = math.MaxInt64
|
|
|
|
maxReal = math.MaxFloat32
|
|
|
|
maxDouble = math.MaxFloat64
|
|
|
|
)
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// ErrIntegerOutOfRange means that an integer exceeds the size of the hdb integer field.
|
2017-07-07 20:11:23 +00:00
|
|
|
var ErrIntegerOutOfRange = errors.New("integer out of range error")
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// ErrFloatOutOfRange means that a float exceeds the size of the hdb float field.
|
2017-07-07 20:11:23 +00:00
|
|
|
var ErrFloatOutOfRange = errors.New("float out of range error")
|
|
|
|
|
|
|
|
var typeOfTime = reflect.TypeOf((*time.Time)(nil)).Elem()
|
|
|
|
var typeOfBytes = reflect.TypeOf((*[]byte)(nil)).Elem()
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
func checkNamedValue(prmFieldSet *p.ParameterFieldSet, nv *driver.NamedValue) error {
|
|
|
|
idx := nv.Ordinal - 1
|
|
|
|
|
|
|
|
if idx >= prmFieldSet.NumInputField() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f := prmFieldSet.Field(idx)
|
|
|
|
dt := f.TypeCode().DataType()
|
|
|
|
|
|
|
|
value, err := convertNamedValue(idx, f, dt, nv.Value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nv.Value = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertNamedValue(idx int, f *p.ParameterField, dt p.DataType, v driver.Value) (driver.Value, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// let fields with own Value converter convert themselves first (e.g. NullInt64, ...)
|
|
|
|
if _, ok := v.(driver.Valuer); ok {
|
|
|
|
if v, err = driver.DefaultParameterConverter.ConvertValue(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
switch dt {
|
|
|
|
|
|
|
|
default:
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("convert named value datatype error: %[1]d - %[1]s", dt)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
case p.DtTinyint:
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvInteger(v, minTinyint, maxTinyint)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
case p.DtSmallint:
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvInteger(v, minSmallint, maxSmallint)
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
case p.DtInteger:
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvInteger(v, minInteger, maxInteger)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
case p.DtBigint:
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvInteger(v, minBigint, maxBigint)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
case p.DtReal:
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvFloat(v, maxReal)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
case p.DtDouble:
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvFloat(v, maxDouble)
|
|
|
|
|
2017-07-07 20:11:23 +00:00
|
|
|
case p.DtTime:
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvTime(v)
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
case p.DtDecimal:
|
|
|
|
return convertNvDecimal(v)
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
case p.DtString:
|
|
|
|
return convertNvString(v)
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
case p.DtBytes:
|
|
|
|
return convertNvBytes(v)
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
case p.DtLob:
|
|
|
|
return convertNvLob(idx, f, v)
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// integer types
|
|
|
|
func convertNvInteger(v interface{}, min, max int64) (driver.Value, error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
switch rv.Kind() {
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// bool is represented in HDB as tinyint
|
|
|
|
case reflect.Bool:
|
|
|
|
return rv.Bool(), nil
|
2017-07-07 20:11:23 +00:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
i64 := rv.Int()
|
2018-07-06 16:09:34 +00:00
|
|
|
if i64 > max || i64 < min {
|
2017-07-07 20:11:23 +00:00
|
|
|
return nil, ErrIntegerOutOfRange
|
|
|
|
}
|
|
|
|
return i64, nil
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
u64 := rv.Uint()
|
2018-07-06 16:09:34 +00:00
|
|
|
if u64 > uint64(max) {
|
2017-07-07 20:11:23 +00:00
|
|
|
return nil, ErrIntegerOutOfRange
|
|
|
|
}
|
|
|
|
return int64(u64), nil
|
|
|
|
case reflect.Ptr:
|
|
|
|
// indirect pointers
|
|
|
|
if rv.IsNil() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvInteger(rv.Elem().Interface(), min, max)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("unsupported integer conversion type error %[1]T %[1]v", v)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// float types
|
|
|
|
func convertNvFloat(v interface{}, max float64) (driver.Value, error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
switch rv.Kind() {
|
|
|
|
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
f64 := rv.Float()
|
2018-07-06 16:09:34 +00:00
|
|
|
if math.Abs(f64) > max {
|
2017-07-07 20:11:23 +00:00
|
|
|
return nil, ErrFloatOutOfRange
|
|
|
|
}
|
|
|
|
return f64, nil
|
|
|
|
case reflect.Ptr:
|
|
|
|
// indirect pointers
|
|
|
|
if rv.IsNil() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvFloat(rv.Elem().Interface(), max)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("unsupported float conversion type error %[1]T %[1]v", v)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// time
|
|
|
|
func convertNvTime(v interface{}) (driver.Value, error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
|
|
|
|
case time.Time:
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
|
|
|
|
switch rv.Kind() {
|
|
|
|
|
|
|
|
case reflect.Ptr:
|
|
|
|
// indirect pointers
|
|
|
|
if rv.IsNil() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvTime(rv.Elem().Interface())
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rv.Type().ConvertibleTo(typeOfTime) {
|
|
|
|
tv := rv.Convert(typeOfTime)
|
|
|
|
return tv.Interface().(time.Time), nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("unsupported time conversion type error %[1]T %[1]v", v)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// decimal
|
|
|
|
func convertNvDecimal(v interface{}) (driver.Value, error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := v.([]byte); ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("unsupported decimal conversion type error %[1]T %[1]v", v)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// string
|
|
|
|
func convertNvString(v interface{}) (driver.Value, error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
|
|
|
|
case string, []byte:
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
|
|
|
|
switch rv.Kind() {
|
|
|
|
|
|
|
|
case reflect.String:
|
|
|
|
return rv.String(), nil
|
|
|
|
|
|
|
|
case reflect.Slice:
|
|
|
|
if rv.Type() == typeOfBytes {
|
|
|
|
return rv.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case reflect.Ptr:
|
|
|
|
// indirect pointers
|
|
|
|
if rv.IsNil() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvString(rv.Elem().Interface())
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rv.Type().ConvertibleTo(typeOfBytes) {
|
|
|
|
bv := rv.Convert(typeOfBytes)
|
|
|
|
return bv.Interface().([]byte), nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("unsupported character conversion type error %[1]T %[1]v", v)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// bytes
|
|
|
|
func convertNvBytes(v interface{}) (driver.Value, error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := v.([]byte); ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
|
|
|
|
switch rv.Kind() {
|
|
|
|
|
|
|
|
case reflect.Slice:
|
|
|
|
if rv.Type() == typeOfBytes {
|
|
|
|
return rv.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case reflect.Ptr:
|
|
|
|
// indirect pointers
|
|
|
|
if rv.IsNil() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
return convertNvBytes(rv.Elem().Interface())
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rv.Type().ConvertibleTo(typeOfBytes) {
|
|
|
|
bv := rv.Convert(typeOfBytes)
|
|
|
|
return bv.Interface().([]byte), nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("unsupported bytes conversion type error %[1]T %[1]v", v)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// Lob
|
|
|
|
func convertNvLob(idx int, f *p.ParameterField, v interface{}) (driver.Value, error) {
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
if v == nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
switch v := v.(type) {
|
|
|
|
case Lob:
|
|
|
|
if v.rd == nil {
|
|
|
|
return nil, fmt.Errorf("lob error: initial reader %[1]T %[1]v", v)
|
|
|
|
}
|
|
|
|
f.SetLobReader(v.rd)
|
|
|
|
return fmt.Sprintf("<lob %d", idx), nil
|
|
|
|
case *Lob:
|
|
|
|
if v.rd == nil {
|
|
|
|
return nil, fmt.Errorf("lob error: initial reader %[1]T %[1]v", v)
|
|
|
|
}
|
|
|
|
f.SetLobReader(v.rd)
|
|
|
|
return fmt.Sprintf("<lob %d", idx), nil
|
|
|
|
case NullLob:
|
|
|
|
if !v.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if v.Lob.rd == nil {
|
|
|
|
return nil, fmt.Errorf("lob error: initial reader %[1]T %[1]v", v)
|
|
|
|
}
|
|
|
|
f.SetLobReader(v.Lob.rd)
|
|
|
|
return fmt.Sprintf("<lob %d", idx), nil
|
|
|
|
case *NullLob:
|
|
|
|
if !v.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if v.Lob.rd == nil {
|
|
|
|
return nil, fmt.Errorf("lob error: initial reader %[1]T %[1]v", v)
|
|
|
|
}
|
|
|
|
f.SetLobReader(v.Lob.rd)
|
|
|
|
return fmt.Sprintf("<lob %d", idx), nil
|
|
|
|
}
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
rv := reflect.ValueOf(v)
|
2017-07-07 20:11:23 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
switch rv.Kind() {
|
|
|
|
|
|
|
|
case reflect.Ptr:
|
|
|
|
// indirect pointers
|
|
|
|
if rv.IsNil() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return convertNvLob(idx, f, rv.Elem().Interface())
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
return nil, fmt.Errorf("unsupported lob conversion type error %[1]T %[1]v", v)
|
2017-07-07 20:11:23 +00:00
|
|
|
}
|