diff --git a/lib/go/camli/db/convert.go b/lib/go/camli/db/convert.go deleted file mode 100644 index 1d857b2ee..000000000 --- a/lib/go/camli/db/convert.go +++ /dev/null @@ -1,57 +0,0 @@ -/* -Copyright 2011 Google Inc. - -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 db - -import ( - "fmt" - "os" - "reflect" -) - -// valueToImpl converts v to one of the restricted subset types that -// dbimpl drivers need to support: int64, float64, bool, nil, []byte -func valueToImpl(v interface{}) (interface{}, os.Error) { - if v == nil { - return nil, nil - } - if _, ok := v.([]byte); ok { - return v, nil - } - if _, ok := v.(bool); ok { - return v, nil - } - if s, ok := v.(string); ok { - return []byte(s), nil - } - - rv := reflect.ValueOf(v) - switch rv.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return rv.Int(), nil - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32: - return int64(rv.Uint()), nil - case reflect.Uint64: - u64 := rv.Uint() - if u64 >= 1 << 63 { - return nil, fmt.Errorf("uint64 values with high bit set are not supported") - } - return int64(u64), nil - case reflect.Float32, reflect.Float64: - return rv.Float(), nil - } - return nil, fmt.Errorf("unsupported type %s", rv.Kind()) -} diff --git a/lib/go/camli/db/db.go b/lib/go/camli/db/db.go index b1217d8d1..9cd6d9bdb 100644 --- a/lib/go/camli/db/db.go +++ b/lib/go/camli/db/db.go @@ -246,7 +246,7 @@ func (s *Stmt) Exec(args ...interface{}) os.Error { // all integers -> int64, etc for n, arg := range args { var err os.Error - args[n], err = valueToImpl(arg) + args[n], err = dbimpl.SubsetValue(arg) if err != nil { return fmt.Errorf("db: error converting index %d: %v", n, err) } diff --git a/lib/go/camli/db/dbimpl/types.go b/lib/go/camli/db/dbimpl/types.go index 572ff1a8f..020635ec1 100644 --- a/lib/go/camli/db/dbimpl/types.go +++ b/lib/go/camli/db/dbimpl/types.go @@ -70,3 +70,37 @@ func (stringType) ConvertValue(v interface{}) (interface{}, os.Error) { } return fmt.Sprintf("%v", v), nil } + +// SubsetValue converts v to one of the restricted subset types that +// dbimpl drivers need to support: int64, float64, bool, nil, []byte +func SubsetValue(v interface{}) (interface{}, os.Error) { + if v == nil { + return nil, nil + } + if _, ok := v.([]byte); ok { + return v, nil + } + if _, ok := v.(bool); ok { + return v, nil + } + if s, ok := v.(string); ok { + return []byte(s), nil + } + + rv := reflect.ValueOf(v) + switch rv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return rv.Int(), nil + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32: + return int64(rv.Uint()), nil + case reflect.Uint64: + u64 := rv.Uint() + if u64 >= 1 << 63 { + return nil, fmt.Errorf("uint64 values with high bit set are not supported") + } + return int64(u64), nil + case reflect.Float32, reflect.Float64: + return rv.Float(), nil + } + return nil, fmt.Errorf("unsupported type %s", rv.Kind()) +}