OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [database/] [sql/] [driver/] [types.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package driver
6
 
7
import (
8
        "fmt"
9
        "reflect"
10
        "strconv"
11
        "time"
12
)
13
 
14
// ValueConverter is the interface providing the ConvertValue method.
15
//
16
// Various implementations of ValueConverter are provided by the
17
// driver package to provide consistent implementations of conversions
18
// between drivers.  The ValueConverters have several uses:
19
//
20
//  * converting from the subset types as provided by the sql package
21
//    into a database table's specific column type and making sure it
22
//    fits, such as making sure a particular int64 fits in a
23
//    table's uint16 column.
24
//
25
//  * converting a value as given from the database into one of the
26
//    subset types.
27
//
28
//  * by the sql package, for converting from a driver's subset type
29
//    to a user's type in a scan.
30
type ValueConverter interface {
31
        // ConvertValue converts a value to a restricted subset type.
32
        ConvertValue(v interface{}) (interface{}, error)
33
}
34
 
35
// SubsetValuer is the interface providing the SubsetValue method.
36
//
37
// Types implementing SubsetValuer interface are able to convert
38
// themselves to one of the driver's allowed subset values.
39
type SubsetValuer interface {
40
        // SubsetValue returns a driver parameter subset value.
41
        SubsetValue() (interface{}, error)
42
}
43
 
44
// Bool is a ValueConverter that converts input values to bools.
45
//
46
// The conversion rules are:
47
//  - booleans are returned unchanged
48
//  - for integer types,
49
//       1 is true
50
//       0 is false,
51
//       other integers are an error
52
//  - for strings and []byte, same rules as strconv.ParseBool
53
//  - all other types are an error
54
var Bool boolType
55
 
56
type boolType struct{}
57
 
58
var _ ValueConverter = boolType{}
59
 
60
func (boolType) String() string { return "Bool" }
61
 
62
func (boolType) ConvertValue(src interface{}) (interface{}, error) {
63
        switch s := src.(type) {
64
        case bool:
65
                return s, nil
66
        case string:
67
                b, err := strconv.ParseBool(s)
68
                if err != nil {
69
                        return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
70
                }
71
                return b, nil
72
        case []byte:
73
                b, err := strconv.ParseBool(string(s))
74
                if err != nil {
75
                        return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
76
                }
77
                return b, nil
78
        }
79
 
80
        sv := reflect.ValueOf(src)
81
        switch sv.Kind() {
82
        case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
83
                iv := sv.Int()
84
                if iv == 1 || iv == 0 {
85
                        return iv == 1, nil
86
                }
87
                return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", iv)
88
        case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
89
                uv := sv.Uint()
90
                if uv == 1 || uv == 0 {
91
                        return uv == 1, nil
92
                }
93
                return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", uv)
94
        }
95
 
96
        return nil, fmt.Errorf("sql/driver: couldn't convert %v (%T) into type bool", src, src)
97
}
98
 
99
// Int32 is a ValueConverter that converts input values to int64,
100
// respecting the limits of an int32 value.
101
var Int32 int32Type
102
 
103
type int32Type struct{}
104
 
105
var _ ValueConverter = int32Type{}
106
 
107
func (int32Type) ConvertValue(v interface{}) (interface{}, error) {
108
        rv := reflect.ValueOf(v)
109
        switch rv.Kind() {
110
        case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
111
                i64 := rv.Int()
112
                if i64 > (1<<31)-1 || i64 < -(1<<31) {
113
                        return nil, fmt.Errorf("sql/driver: value %d overflows int32", v)
114
                }
115
                return i64, nil
116
        case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
117
                u64 := rv.Uint()
118
                if u64 > (1<<31)-1 {
119
                        return nil, fmt.Errorf("sql/driver: value %d overflows int32", v)
120
                }
121
                return int64(u64), nil
122
        case reflect.String:
123
                i, err := strconv.Atoi(rv.String())
124
                if err != nil {
125
                        return nil, fmt.Errorf("sql/driver: value %q can't be converted to int32", v)
126
                }
127
                return int64(i), nil
128
        }
129
        return nil, fmt.Errorf("sql/driver: unsupported value %v (type %T) converting to int32", v, v)
130
}
131
 
132
// String is a ValueConverter that converts its input to a string.
133
// If the value is already a string or []byte, it's unchanged.
134
// If the value is of another type, conversion to string is done
135
// with fmt.Sprintf("%v", v).
136
var String stringType
137
 
138
type stringType struct{}
139
 
140
func (stringType) ConvertValue(v interface{}) (interface{}, error) {
141
        switch v.(type) {
142
        case string, []byte:
143
                return v, nil
144
        }
145
        return fmt.Sprintf("%v", v), nil
146
}
147
 
148
// Null is a type that implements ValueConverter by allowing nil
149
// values but otherwise delegating to another ValueConverter.
150
type Null struct {
151
        Converter ValueConverter
152
}
153
 
154
func (n Null) ConvertValue(v interface{}) (interface{}, error) {
155
        if v == nil {
156
                return nil, nil
157
        }
158
        return n.Converter.ConvertValue(v)
159
}
160
 
161
// NotNull is a type that implements ValueConverter by disallowing nil
162
// values but otherwise delegating to another ValueConverter.
163
type NotNull struct {
164
        Converter ValueConverter
165
}
166
 
167
func (n NotNull) ConvertValue(v interface{}) (interface{}, error) {
168
        if v == nil {
169
                return nil, fmt.Errorf("nil value not allowed")
170
        }
171
        return n.Converter.ConvertValue(v)
172
}
173
 
174
// IsParameterSubsetType reports whether v is of a valid type for a
175
// parameter. These types are:
176
//
177
//   int64
178
//   float64
179
//   bool
180
//   nil
181
//   []byte
182
//   time.Time
183
//   string
184
//
185
// This is the same list as IsScanSubsetType, with the addition of
186
// string.
187
func IsParameterSubsetType(v interface{}) bool {
188
        if IsScanSubsetType(v) {
189
                return true
190
        }
191
        if _, ok := v.(string); ok {
192
                return true
193
        }
194
        return false
195
}
196
 
197
// IsScanSubsetType reports whether v is of a valid type for a
198
// value populated by Rows.Next. These types are:
199
//
200
//   int64
201
//   float64
202
//   bool
203
//   nil
204
//   []byte
205
//   time.Time
206
//
207
// This is the same list as IsParameterSubsetType, without string.
208
func IsScanSubsetType(v interface{}) bool {
209
        if v == nil {
210
                return true
211
        }
212
        switch v.(type) {
213
        case int64, float64, []byte, bool, time.Time:
214
                return true
215
        }
216
        return false
217
}
218
 
219
// DefaultParameterConverter is the default implementation of
220
// ValueConverter that's used when a Stmt doesn't implement
221
// ColumnConverter.
222
//
223
// DefaultParameterConverter returns the given value directly if
224
// IsSubsetType(value).  Otherwise integer type are converted to
225
// int64, floats to float64, and strings to []byte.  Other types are
226
// an error.
227
var DefaultParameterConverter defaultConverter
228
 
229
type defaultConverter struct{}
230
 
231
var _ ValueConverter = defaultConverter{}
232
 
233
func (defaultConverter) ConvertValue(v interface{}) (interface{}, error) {
234
        if IsParameterSubsetType(v) {
235
                return v, nil
236
        }
237
 
238
        if svi, ok := v.(SubsetValuer); ok {
239
                sv, err := svi.SubsetValue()
240
                if err != nil {
241
                        return nil, err
242
                }
243
                if !IsParameterSubsetType(sv) {
244
                        return nil, fmt.Errorf("non-subset type %T returned from SubsetValue", sv)
245
                }
246
                return sv, nil
247
        }
248
 
249
        rv := reflect.ValueOf(v)
250
        switch rv.Kind() {
251
        case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
252
                return rv.Int(), nil
253
        case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
254
                return int64(rv.Uint()), nil
255
        case reflect.Uint64:
256
                u64 := rv.Uint()
257
                if u64 >= 1<<63 {
258
                        return nil, fmt.Errorf("uint64 values with high bit set are not supported")
259
                }
260
                return int64(u64), nil
261
        case reflect.Float32, reflect.Float64:
262
                return rv.Float(), nil
263
        }
264
        return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
265
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.