52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
|
|
package stardb
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"strconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
// GetString returns column value as string with error
|
||
|
|
func (r *StarResult) GetString(name string) (string, error) {
|
||
|
|
index, ok := r.columnIndex[name]
|
||
|
|
if !ok {
|
||
|
|
return "", errors.New("column not found: " + name)
|
||
|
|
}
|
||
|
|
return ConvertToStringSafe(r.Result()[index])
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetInt64 returns column value as int64 with error
|
||
|
|
func (r *StarResult) GetInt64(name string) (int64, error) {
|
||
|
|
index, ok := r.columnIndex[name]
|
||
|
|
if !ok {
|
||
|
|
return 0, errors.New("column not found: " + name)
|
||
|
|
}
|
||
|
|
return ConvertToInt64Safe(r.Result()[index])
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetFloat64 returns column value as float64 with error
|
||
|
|
func (r *StarResult) GetFloat64(name string) (float64, error) {
|
||
|
|
index, ok := r.columnIndex[name]
|
||
|
|
if !ok {
|
||
|
|
return 0, errors.New("column not found: " + name)
|
||
|
|
}
|
||
|
|
|
||
|
|
switch v := r.Result()[index].(type) {
|
||
|
|
case nil:
|
||
|
|
return 0, nil
|
||
|
|
case float64:
|
||
|
|
return v, nil
|
||
|
|
case float32:
|
||
|
|
return float64(v), nil
|
||
|
|
case int, int32, int64, uint64:
|
||
|
|
val, err := ConvertToInt64Safe(v)
|
||
|
|
return float64(val), err
|
||
|
|
case string:
|
||
|
|
return strconv.ParseFloat(v, 64)
|
||
|
|
case []byte:
|
||
|
|
return strconv.ParseFloat(string(v), 64)
|
||
|
|
default:
|
||
|
|
return 0, fmt.Errorf("cannot convert %T to float64", v)
|
||
|
|
}
|
||
|
|
}
|