package pgx

import (
	
	
	

	
)

type extendedQueryBuilder struct {
	paramValues     [][]byte
	paramValueBytes []byte
	paramFormats    []int16
	resultFormats   []int16

	resetCount int
}

func ( *extendedQueryBuilder) ( *pgtype.ConnInfo,  uint32,  interface{}) error {
	 := chooseParameterFormatCode(, , )
	.paramFormats = append(.paramFormats, )

	,  := .encodeExtendedParamValue(, , , )
	if  != nil {
		return 
	}
	.paramValues = append(.paramValues, )

	return nil
}

func ( *extendedQueryBuilder) ( int16) {
	.resultFormats = append(.resultFormats, )
}

func ( *extendedQueryBuilder) () {
	.paramValues = .paramValues[0:0]
	.paramValueBytes = .paramValueBytes[0:0]
	.paramFormats = .paramFormats[0:0]
	.resultFormats = .resultFormats[0:0]

	.resetCount++
Every so often shrink our reserved memory if it is abnormally high
	if .resetCount%128 == 0 {
		if cap(.paramValues) > 64 {
			.paramValues = make([][]byte, 0, cap(.paramValues)/2)
		}

		if cap(.paramValueBytes) > 256 {
			.paramValueBytes = make([]byte, 0, cap(.paramValueBytes)/2)
		}

		if cap(.paramFormats) > 64 {
			.paramFormats = make([]int16, 0, cap(.paramFormats)/2)
		}
		if cap(.resultFormats) > 64 {
			.resultFormats = make([]int16, 0, cap(.resultFormats)/2)
		}
	}

}

func ( *extendedQueryBuilder) ( *pgtype.ConnInfo,  uint32,  int16,  interface{}) ([]byte, error) {
	if  == nil {
		return nil, nil
	}

	 := reflect.ValueOf()
	 := .Kind() == reflect.Ptr

	if  && .IsNil() {
		return nil, nil
	}

	if .paramValueBytes == nil {
		.paramValueBytes = make([]byte, 0, 128)
	}

	var  error
	var  []byte
	 := len(.paramValueBytes)

	if ,  := .(string);  {
		return []byte(), nil
	}

	if  == TextFormatCode {
		if ,  := .(pgtype.TextEncoder);  {
			,  = .EncodeText(, .paramValueBytes)
			if  != nil {
				return nil, 
			}
			if  == nil {
				return nil, nil
			}
			.paramValueBytes = 
			return .paramValueBytes[:], nil
		}
	} else if  == BinaryFormatCode {
		if ,  := .(pgtype.BinaryEncoder);  {
			,  = .EncodeBinary(, .paramValueBytes)
			if  != nil {
				return nil, 
			}
			if  == nil {
				return nil, nil
			}
			.paramValueBytes = 
			return .paramValueBytes[:], nil
		}
	}

We have already checked that arg is not pointing to nil, so it is safe to dereference here.
		 = .Elem().Interface()
		return .(, , , )
	}

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

			return nil, 
		}

		return .(, , , )
	}
There is no data type registered for the destination OID, but maybe there is data type registered for the arg type. If so use it's text encoder (if available).
	if ,  := .DataTypeForValue();  {
		 := .Value
		if ,  := .(pgtype.TextEncoder);  {
			 := .Set()
			if  != nil {
				return nil, 
			}

			,  = .EncodeText(, .paramValueBytes)
			if  != nil {
				return nil, 
			}
			if  == nil {
				return nil, nil
			}
			.paramValueBytes = 
			return .paramValueBytes[:], 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", , , ))