good morning!!!!

Skip to content
Snippets Groups Projects
Commit 2bd78911 authored by Peter Kieltyka's avatar Peter Kieltyka
Browse files

Add Int64Array option type

parent f08732c6
No related branches found
No related tags found
No related merge requests found
...@@ -165,6 +165,9 @@ func fetchResult(itemT reflect.Type, rows *sqlx.Rows, columns []string) (reflect ...@@ -165,6 +165,9 @@ func fetchResult(itemT reflect.Type, rows *sqlx.Rows, columns []string) (reflect
if _, ok := fi.Options["stringarray"]; ok { if _, ok := fi.Options["stringarray"]; ok {
values[i] = &StringArray{} values[i] = &StringArray{}
wrappedValues[f] = values[i] wrappedValues[f] = values[i]
} else if _, ok := fi.Options["int64array"]; ok {
values[i] = &Int64Array{}
wrappedValues[f] = values[i]
} else if _, ok := fi.Options["jsonb"]; ok { } else if _, ok := fi.Options["jsonb"]; ok {
values[i] = &JsonbType{} values[i] = &JsonbType{}
wrappedValues[f] = values[i] wrappedValues[f] = values[i]
......
...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
"database/sql/driver" "database/sql/driver"
"encoding/json" "encoding/json"
"errors" "errors"
"strconv"
"strings" "strings"
"upper.io/db" "upper.io/db"
...@@ -128,3 +129,54 @@ func appendArrayQuotedString(b []byte, v string) []byte { ...@@ -128,3 +129,54 @@ func appendArrayQuotedString(b []byte, v string) []byte {
} }
return append(b, '"') return append(b, '"')
} }
//------
type Int64Array []int64
func (a *Int64Array) Scan(src interface{}) error {
if src == nil {
return nil
}
b, ok := src.([]byte)
if !ok {
return errors.New("Scan source was not []bytes")
}
s := string(b)[1 : len(b)-1]
parts := strings.Split(s, ",")
results := make([]int64, 0)
for _, n := range parts {
i, err := strconv.ParseInt(n, 10, 64)
if err != nil {
return err
}
results = append(results, i)
}
*a = Int64Array(results)
return nil
}
// Value implements the driver.Valuer interface.
func (a Int64Array) Value() (driver.Value, error) {
if a == nil {
return nil, nil
}
if n := len(a); n > 0 {
// There will be at least two curly brackets, N bytes of values,
// and N-1 bytes of delimiters.
b := make([]byte, 1, 1+2*n)
b[0] = '{'
b = strconv.AppendInt(b, a[0], 10)
for i := 1; i < n; i++ {
b = append(b, ',')
b = strconv.AppendInt(b, a[i], 10)
}
return append(b, '}'), nil
}
return []byte{'{', '}'}, nil
}
...@@ -92,6 +92,9 @@ func (t *T) FieldValues(item interface{}) ([]string, []interface{}, error) { ...@@ -92,6 +92,9 @@ func (t *T) FieldValues(item interface{}) ([]string, []interface{}, error) {
var value interface{} var value interface{}
if _, ok := fi.Options["stringarray"]; ok { if _, ok := fi.Options["stringarray"]; ok {
value = StringArray(fld.Interface().([]string)) value = StringArray(fld.Interface().([]string))
} else if _, ok := fi.Options["int64array"]; ok {
value = Int64Array(fld.Interface().([]int64))
} else if _, ok := fi.Options["jsonb"]; ok { } else if _, ok := fi.Options["jsonb"]; ok {
value = JsonbType{fld.Interface()} value = JsonbType{fld.Interface()}
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment