Source File
array.go
Belonging Package
github.com/lib/pq
func ( interface{}) interface {
driver.Valuer
sql.Scanner
} {
switch a := .(type) {
case []bool:
return (*BoolArray)(&)
case []float64:
return (*Float64Array)(&)
case []int64:
return (*Int64Array)(&)
case []string:
return (*StringArray)(&)
case *[]bool:
return (*BoolArray)()
case *[]float64:
return (*Float64Array)()
case *[]int64:
return (*Int64Array)()
case *[]string:
return (*StringArray)()
}
return GenericArray{}
}
ArrayDelimiter() string
}
func ( *BoolArray) ( interface{}) error {
switch src := .(type) {
case []byte:
return .scanBytes()
case string:
return .scanBytes([]byte())
case nil:
* = nil
return nil
}
return fmt.Errorf("pq: cannot convert %T to BoolArray", )
}
func ( *BoolArray) ( []byte) error {
, := scanLinearArray(, []byte{','}, "BoolArray")
if != nil {
return
}
if * != nil && len() == 0 {
* = (*)[:0]
} else {
:= make(BoolArray, len())
for , := range {
if len() != 1 {
return fmt.Errorf("pq: could not parse boolean array index %d: invalid boolean %q", , )
}
switch [0] {
case 't':
[] = true
case 'f':
[] = false
default:
return fmt.Errorf("pq: could not parse boolean array index %d: invalid boolean %q", , )
}
}
* =
}
return nil
}
type ByteaArray [][]byte
func ( *ByteaArray) ( interface{}) error {
switch src := .(type) {
case []byte:
return .scanBytes()
case string:
return .scanBytes([]byte())
case nil:
* = nil
return nil
}
return fmt.Errorf("pq: cannot convert %T to ByteaArray", )
}
func ( *ByteaArray) ( []byte) error {
, := scanLinearArray(, []byte{','}, "ByteaArray")
if != nil {
return
}
if * != nil && len() == 0 {
* = (*)[:0]
} else {
:= make(ByteaArray, len())
for , := range {
[], = parseBytea()
if != nil {
return fmt.Errorf("could not parse bytea array index %d: %s", , .Error())
}
}
* =
}
return nil
}
type Float64Array []float64
func ( *Float64Array) ( interface{}) error {
switch src := .(type) {
case []byte:
return .scanBytes()
case string:
return .scanBytes([]byte())
case nil:
* = nil
return nil
}
return fmt.Errorf("pq: cannot convert %T to Float64Array", )
}
func ( *Float64Array) ( []byte) error {
, := scanLinearArray(, []byte{','}, "Float64Array")
if != nil {
return
}
if * != nil && len() == 0 {
* = (*)[:0]
} else {
:= make(Float64Array, len())
for , := range {
if [], = strconv.ParseFloat(string(), 64); != nil {
return fmt.Errorf("pq: parsing array element index %d: %v", , )
}
}
* =
}
return nil
}
{
= func( []byte, reflect.Value) ( error) {
:= .Addr().Interface().(sql.Scanner)
if == nil {
= .Scan(nil)
} else {
= .Scan()
}
return
}
goto
}
= func([]byte, reflect.Value) error {
return fmt.Errorf("pq: scanning to %s is not implemented; only sql.Scanner", )
}
}
:
if , := reflect.Zero().Interface().(ArrayDelimiter); {
= .ArrayDelimiter()
}
return , ,
}
func ( GenericArray) ( interface{}) error {
:= reflect.ValueOf(.A)
switch {
case .Kind() != reflect.Ptr:
return fmt.Errorf("pq: destination %T is not a pointer to array or slice", .A)
case .IsNil():
return fmt.Errorf("pq: destination %T is nil", .A)
}
:= .Elem()
switch .Kind() {
case reflect.Slice:
case reflect.Array:
default:
return fmt.Errorf("pq: destination %T is not a pointer to array or slice", .A)
}
switch src := .(type) {
case []byte:
return .scanBytes(, )
case string:
return .scanBytes([]byte(), )
case nil:
if .Kind() == reflect.Slice {
.Set(reflect.Zero(.Type()))
return nil
}
}
return fmt.Errorf("pq: cannot convert %T to %s", , .Type())
}
func ( GenericArray) ( []byte, reflect.Value) error {
, , := .evaluateDestination(.Type().Elem())
, , := parseArray(, []byte())
if != nil {
return
}
:= make([]byte, 0, 1+2*)
, , := appendArray(, , )
return string(),
}
return "{}", nil
}
type Int64Array []int64
func ( *Int64Array) ( interface{}) error {
switch src := .(type) {
case []byte:
return .scanBytes()
case string:
return .scanBytes([]byte())
case nil:
* = nil
return nil
}
return fmt.Errorf("pq: cannot convert %T to Int64Array", )
}
func ( *Int64Array) ( []byte) error {
, := scanLinearArray(, []byte{','}, "Int64Array")
if != nil {
return
}
if * != nil && len() == 0 {
* = (*)[:0]
} else {
:= make(Int64Array, len())
for , := range {
if [], = strconv.ParseInt(string(), 10, 64); != nil {
return fmt.Errorf("pq: parsing array element index %d: %v", , )
}
}
* =
}
return nil
}
type StringArray []string
func ( *StringArray) ( interface{}) error {
switch src := .(type) {
case []byte:
return .scanBytes()
case string:
return .scanBytes([]byte())
case nil:
* = nil
return nil
}
return fmt.Errorf("pq: cannot convert %T to StringArray", )
}
func ( *StringArray) ( []byte) error {
, := scanLinearArray(, []byte{','}, "StringArray")
if != nil {
return
}
if * != nil && len() == 0 {
* = (*)[:0]
} else {
:= make(StringArray, len())
for , := range {
if [] = string(); == nil {
return fmt.Errorf("pq: parsing array element index %d: cannot convert nil to string", )
}
}
* =
}
return nil
}
func ( []byte, reflect.Value) ([]byte, string, error) {
if := .Kind(); == reflect.Array || == reflect.Slice {
if := .Type(); != typeByteSlice && !.Implements(typeDriverValuer) {
if := .Len(); > 0 {
return appendArray(, , )
}
return , "", nil
}
}
var = ","
var error
var interface{} = .Interface()
if , := .(ArrayDelimiter); {
= .ArrayDelimiter()
}
if , = driver.DefaultParameterConverter.ConvertValue(); != nil {
return , ,
}
switch v := .(type) {
case nil:
return append(, "NULL"...), , nil
case []byte:
return appendArrayQuotedBytes(, ), , nil
case string:
return appendArrayQuotedBytes(, []byte()), , nil
}
, = appendValue(, )
return , ,
}
func (, []byte) []byte {
= append(, '"')
for {
:= bytes.IndexAny(, `"\`)
if < 0 {
= append(, ...)
break
}
if > 0 {
= append(, [:]...)
}
= append(, '\\', [])
= [+1:]
}
return append(, '"')
}
func ( []byte, driver.Value) ([]byte, error) {
return append(, encode(nil, , 0)...), nil
}
func (, []byte) ( []int, [][]byte, error) {
var , int
if len() < 1 || [0] != '{' {
return nil, nil, fmt.Errorf("pq: unable to parse array; expected %q at offset %d", '{', 0)
}
:
for < len() {
switch [] {
case '{':
++
++
case '}':
= make([][]byte, 0)
goto
default:
break
}
}
= make([]int, )
:
for < len() {
switch [] {
case '{':
if == len() {
break
}
++
[-1] = 0
++
case '"':
var = []byte{}
var bool
for ++; < len(); ++ {
if {
= append(, [])
= false
} else {
switch [] {
default:
= append(, [])
case '\\':
= true
case '"':
= append(, )
++
break
}
}
}
default:
for := ; < len(); ++ {
if bytes.HasPrefix([:], ) || [] == '}' {
:= [:]
if len() == 0 {
return nil, nil, fmt.Errorf("pq: unable to parse array; unexpected %q at offset %d", [], )
}
if bytes.Equal(, []byte("NULL")) {
= nil
}
= append(, )
break
}
}
}
}
for < len() {
if bytes.HasPrefix([:], ) && > 0 {
[-1]++
+= len()
goto
} else if [] == '}' && > 0 {
[-1]++
--
++
} else {
return nil, nil, fmt.Errorf("pq: unable to parse array; unexpected %q at offset %d", [], )
}
}
:
for < len() {
if [] == '}' && > 0 {
--
++
} else {
return nil, nil, fmt.Errorf("pq: unable to parse array; unexpected %q at offset %d", [], )
}
}
if > 0 {
= fmt.Errorf("pq: unable to parse array; expected %q at offset %d", '}', )
}
if == nil {
for , := range {
if (len() % ) != 0 {
= fmt.Errorf("pq: multidimensional arrays must have elements with matching dimensions")
}
}
}
return
}
func (, []byte, string) ( [][]byte, error) {
, , := parseArray(, )
if != nil {
return nil,
}
if len() > 1 {
return nil, fmt.Errorf("pq: cannot convert ARRAY%s to %s", strings.Replace(fmt.Sprint(), " ", "][", -1), )
}
return ,
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |