package pgx

import (
	
	
	
	
	

	
	
)
PostgreSQL format codes
const (
	TextFormatCode   = 0
	BinaryFormatCode = 1
)
SerializationError occurs on failure to encode or decode a value
type SerializationError string

func ( SerializationError) () string {
	return string()
}

func ( *pgtype.ConnInfo,  interface{}) (interface{}, error) {
	if  == nil {
		return nil, nil
	}

	 := reflect.ValueOf()
	if .Kind() == reflect.Ptr && .IsNil() {
		return nil, nil
	}

	switch arg := .(type) {
https://github.com/jackc/pgx/issues/409 Changed JSON and JSONB to surface []byte to database/sql instead of string. But that caused problems with the simple protocol because the driver.Valuer case got taken before the pgtype.TextEncoder case. And driver.Valuer needed to be first in the usual case because of https://github.com/jackc/pgx/issues/339. So instead we special case JSON and JSONB.
	case *pgtype.JSON:
		,  := .EncodeText(, nil)
		if  != nil {
			return nil, 
		}
		if  == nil {
			return nil, nil
		}
		return string(), nil
	case *pgtype.JSONB:
		,  := .EncodeText(, nil)
		if  != nil {
			return nil, 
		}
		if  == nil {
			return nil, nil
		}
		return string(), nil

	case driver.Valuer:
		return callValuerValue()
	case pgtype.TextEncoder:
		,  := .EncodeText(, nil)
		if  != nil {
			return nil, 
		}
		if  == nil {
			return nil, nil
		}
		return string(), nil
	case float32:
		return float64(), nil
	case float64:
		return , nil
	case bool:
		return , nil
	case time.Time:
		return , nil
	case string:
		return , nil
	case []byte:
		return , nil
	case int8:
		return int64(), nil
	case int16:
		return int64(), nil
	case int32:
		return int64(), nil
	case int64:
		return , nil
	case int:
		return int64(), nil
	case uint8:
		return int64(), nil
	case uint16:
		return int64(), nil
	case uint32:
		return int64(), nil
	case uint64:
		if  > math.MaxInt64 {
			return nil, fmt.Errorf("arg too big for int64: %v", )
		}
		return int64(), nil
	case uint:
		if uint64() > math.MaxInt64 {
			return nil, fmt.Errorf("arg too big for int64: %v", )
		}
		return int64(), nil
	}

	if ,  := .DataTypeForValue();  {
		 := .Value
		 := .Set()
		if  != nil {
			return nil, 
		}
		,  := .(pgtype.TextEncoder).EncodeText(, nil)
		if  != nil {
			return nil, 
		}
		if  == nil {
			return nil, nil
		}
		return string(), nil
	}

	if .Kind() == reflect.Ptr {
		 = .Elem().Interface()
		return (, )
	}

	if ,  := stripNamedType(&);  {
		return (, )
	}
	return nil, SerializationError(fmt.Sprintf("Cannot encode %T in simple protocol - %T must implement driver.Valuer, pgtype.TextEncoder, or be a native type", , ))
}

func ( *pgtype.ConnInfo,  []byte,  uint32,  interface{}) ([]byte, error) {
	if  == nil {
		return pgio.AppendInt32(, -1), nil
	}

	switch arg := .(type) {
	case pgtype.BinaryEncoder:
		 := len()
		 = pgio.AppendInt32(, -1)
		,  := .EncodeBinary(, )
		if  != nil {
			return nil, 
		}
		if  != nil {
			 = 
			pgio.SetInt32([:], int32(len([:])-4))
		}
		return , nil
	case pgtype.TextEncoder:
		 := len()
		 = pgio.AppendInt32(, -1)
		,  := .EncodeText(, )
		if  != nil {
			return nil, 
		}
		if  != nil {
			 = 
			pgio.SetInt32([:], int32(len([:])-4))
		}
		return , nil
	case string:
		 = pgio.AppendInt32(, int32(len()))
		 = append(, ...)
		return , nil
	}

	 := reflect.ValueOf()

	if .Kind() == reflect.Ptr {
		if .IsNil() {
			return pgio.AppendInt32(, -1), nil
		}
		 = .Elem().Interface()
		return (, , , )
	}

	if ,  := .DataTypeForOID();  {
		 := .Value
		 := .Set()
		if  != nil {
			{
				if ,  := .(driver.Valuer);  {
					,  := callValuerValue()
					if  != nil {
						return nil, 
					}
					return (, , , )
				}
			}

			return nil, 
		}

		 := len()
		 = pgio.AppendInt32(, -1)
		,  := .(pgtype.BinaryEncoder).EncodeBinary(, )
		if  != nil {
			return nil, 
		}
		if  != nil {
			 = 
			pgio.SetInt32([:], int32(len([:])-4))
		}
		return , nil
	}

	if ,  := stripNamedType(&);  {
		return (, , , )
	}
	return nil, SerializationError(fmt.Sprintf("Cannot encode %T into oid %v - %T must implement Encoder or be converted to a string", , , ))
}
chooseParameterFormatCode determines the correct format code for an argument to a prepared statement. It defaults to TextFormatCode if no determination can be made.
func ( *pgtype.ConnInfo,  uint32,  interface{}) int16 {
	switch arg := .(type) {
	case pgtype.ParamFormatPreferrer:
		return .PreferredParamFormat()
	case pgtype.BinaryEncoder:
		return BinaryFormatCode
	case string, *string, pgtype.TextEncoder:
		return TextFormatCode
	}

	return .ParamFormatCodeForOID()
}

func ( *reflect.Value) (interface{}, bool) {
	switch .Kind() {
	case reflect.Int:
		 := int(.Int())
		return , reflect.TypeOf() != .Type()
	case reflect.Int8:
		 := int8(.Int())
		return , reflect.TypeOf() != .Type()
	case reflect.Int16:
		 := int16(.Int())
		return , reflect.TypeOf() != .Type()
	case reflect.Int32:
		 := int32(.Int())
		return , reflect.TypeOf() != .Type()
	case reflect.Int64:
		 := int64(.Int())
		return , reflect.TypeOf() != .Type()
	case reflect.Uint:
		 := uint(.Uint())
		return , reflect.TypeOf() != .Type()
	case reflect.Uint8:
		 := uint8(.Uint())
		return , reflect.TypeOf() != .Type()
	case reflect.Uint16:
		 := uint16(.Uint())
		return , reflect.TypeOf() != .Type()
	case reflect.Uint32:
		 := uint32(.Uint())
		return , reflect.TypeOf() != .Type()
	case reflect.Uint64:
		 := uint64(.Uint())
		return , reflect.TypeOf() != .Type()
	case reflect.String:
		 := .String()
		return , reflect.TypeOf() != .Type()
	}

	return nil, false