mirror of https://github.com/perkeep/perkeep.git
camli/db: move SubsetValue into dbimpl
Change-Id: I81e965fd3e24d642925651225e1cc968ab015a72
This commit is contained in:
parent
3a29e1c245
commit
7095174178
|
@ -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())
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue