package types

import (
	
	
	
	

	

	
)

func ( interface{},  Reader,  int) error {
	var  error
	switch v := .(type) {
	case *string:
		*,  = ScanString(, )
		return 
	case *[]byte:
		*,  = ScanBytes(, )
		return 
	case *int:
		*,  = ScanInt(, )
		return 
	case *int64:
		*,  = ScanInt64(, )
		return 
	case *float32:
		*,  = ScanFloat32(, )
		return 
	case *float64:
		*,  = ScanFloat64(, )
		return 
	case *time.Time:
		*,  = ScanTime(, )
		return 
	}

	 := reflect.ValueOf()
	if !.IsValid() {
		return errors.New("pg: Scan(nil)")
	}

	if .Kind() != reflect.Ptr {
		return fmt.Errorf("pg: Scan(non-pointer %T)", )
	}
	if .IsNil() {
		return fmt.Errorf("pg: Scan(non-settable %T)", )
	}

	 = .Elem()
	if .Kind() == reflect.Interface {
		if .IsNil() {
			return errors.New("pg: Scan(nil)")
		}

		 = .Elem()
		if .Kind() != reflect.Ptr {
			return fmt.Errorf("pg: Decode(non-pointer %s)", .Type().String())
		}
	}

	return ScanValue(, , )
}

func ( Reader,  int) (string, error) {
	if  <= 0 {
		return "", nil
	}

	,  := .ReadFull()
	if  != nil {
		return "", 
	}

	return internal.BytesToString(), nil
}

func ( Reader,  int) ([]byte, error) {
	if  == -1 {
		return nil, nil
	}
	if  == 0 {
		return []byte{}, nil
	}

	 := make([]byte, hex.DecodedLen(-2))
	if  := ReadBytes(, );  != nil {
		return nil, 
	}
	return , nil
}

func ( Reader,  []byte) error {
	,  := .ReadFullTemp()
	if  != nil {
		return 
	}

	if len() < 2 {
		return fmt.Errorf("pg: can't parse bytea: %q", )
	}

	if [0] != '\\' || [1] != 'x' {
		return fmt.Errorf("pg: can't parse bytea: %q", )
	}
	 = [2:] // Trim off "\\x".

	if len() != hex.DecodedLen(len()) {
		return fmt.Errorf("pg: too small buf to decode hex")
	}

	if ,  := hex.Decode(, );  != nil {
		return 
	}

	return nil
}

func ( Reader,  int) (int, error) {
	if  <= 0 {
		return 0, nil
	}

	,  := .ReadFullTemp()
	if  != nil {
		return 0, 
	}

	,  := internal.Atoi()
	if  != nil {
		return 0, 
	}

	return , nil
}

func ( Reader,  int) (int64, error) {
	return scanInt64(, , 64)
}

func ( Reader,  int,  int) (int64, error) {
	if  <= 0 {
		return 0, nil
	}

	,  := .ReadFullTemp()
	if  != nil {
		return 0, 
	}

	,  := internal.ParseInt(, 10, )
	if  != nil {
		return 0, 
	}

	return , nil
}

func ( Reader,  int) (uint64, error) {
	if  <= 0 {
		return 0, nil
	}

	,  := .ReadFullTemp()
	if  != nil {
		return 0, 
	}

	// PostgreSQL does not natively support uint64 - only int64.
	// Be nice and accept negative int64.
	if len() > 0 && [0] == '-' {
		,  := internal.ParseInt(, 10, 64)
		if  != nil {
			return 0, 
		}
		return uint64(), nil
	}

	,  := internal.ParseUint(, 10, 64)
	if  != nil {
		return 0, 
	}

	return , nil
}

func ( Reader,  int) (float32, error) {
	if  <= 0 {
		return 0, nil
	}

	,  := .ReadFullTemp()
	if  != nil {
		return 0, 
	}

	,  := internal.ParseFloat(, 32)
	if  != nil {
		return 0, 
	}

	return float32(), nil
}

func ( Reader,  int) (float64, error) {
	if  <= 0 {
		return 0, nil
	}

	,  := .ReadFullTemp()
	if  != nil {
		return 0, 
	}

	,  := internal.ParseFloat(, 64)
	if  != nil {
		return 0, 
	}

	return , nil
}

func ( Reader,  int) (time.Time, error) {
	if  <= 0 {
		return time.Time{}, nil
	}

	,  := .ReadFullTemp()
	if  != nil {
		return time.Time{}, 
	}

	return ParseTime()
}

func ( Reader,  int) (bool, error) {
	,  := .ReadFullTemp()
	if  != nil {
		return false, 
	}
	return len() == 1 && ([0] == 't' || [0] == '1'), nil
}