package pgproto3

import (
	
	
	
	
)

type ErrorResponse struct {
	Severity            string
	SeverityUnlocalized string // only in 9.6 and greater
	Code                string
	Message             string
	Detail              string
	Hint                string
	Position            int32
	InternalPosition    int32
	InternalQuery       string
	Where               string
	SchemaName          string
	TableName           string
	ColumnName          string
	DataTypeName        string
	ConstraintName      string
	File                string
	Line                int32
	Routine             string

	UnknownFields map[byte]string
}

// Backend identifies this message as sendable by the PostgreSQL backend.
func (*ErrorResponse) () {}

// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
// type identifier and 4 byte message length.
func ( *ErrorResponse) ( []byte) error {
	* = ErrorResponse{}

	 := bytes.NewBuffer()

	for {
		,  := .ReadByte()
		if  != nil {
			return 
		}
		if  == 0 {
			break
		}

		,  := .ReadBytes(0)
		if  != nil {
			return 
		}
		 := string([:len()-1])

		switch  {
		case 'S':
			.Severity = 
		case 'V':
			.SeverityUnlocalized = 
		case 'C':
			.Code = 
		case 'M':
			.Message = 
		case 'D':
			.Detail = 
		case 'H':
			.Hint = 
		case 'P':
			 := 
			,  := strconv.ParseInt(, 10, 32)
			.Position = int32()
		case 'p':
			 := 
			,  := strconv.ParseInt(, 10, 32)
			.InternalPosition = int32()
		case 'q':
			.InternalQuery = 
		case 'W':
			.Where = 
		case 's':
			.SchemaName = 
		case 't':
			.TableName = 
		case 'c':
			.ColumnName = 
		case 'd':
			.DataTypeName = 
		case 'n':
			.ConstraintName = 
		case 'F':
			.File = 
		case 'L':
			 := 
			,  := strconv.ParseInt(, 10, 32)
			.Line = int32()
		case 'R':
			.Routine = 

		default:
			if .UnknownFields == nil {
				.UnknownFields = make(map[byte]string)
			}
			.UnknownFields[] = 
		}
	}

	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func ( *ErrorResponse) ( []byte) []byte {
	return append(, .marshalBinary('E')...)
}

func ( *ErrorResponse) ( byte) []byte {
	var  BigEndianBuf
	 := &bytes.Buffer{}

	.WriteByte()
	.Write(.Uint32(0))

	if .Severity != "" {
		.WriteByte('S')
		.WriteString(.Severity)
		.WriteByte(0)
	}
	if .SeverityUnlocalized != "" {
		.WriteByte('V')
		.WriteString(.SeverityUnlocalized)
		.WriteByte(0)
	}
	if .Code != "" {
		.WriteByte('C')
		.WriteString(.Code)
		.WriteByte(0)
	}
	if .Message != "" {
		.WriteByte('M')
		.WriteString(.Message)
		.WriteByte(0)
	}
	if .Detail != "" {
		.WriteByte('D')
		.WriteString(.Detail)
		.WriteByte(0)
	}
	if .Hint != "" {
		.WriteByte('H')
		.WriteString(.Hint)
		.WriteByte(0)
	}
	if .Position != 0 {
		.WriteByte('P')
		.WriteString(strconv.Itoa(int(.Position)))
		.WriteByte(0)
	}
	if .InternalPosition != 0 {
		.WriteByte('p')
		.WriteString(strconv.Itoa(int(.InternalPosition)))
		.WriteByte(0)
	}
	if .InternalQuery != "" {
		.WriteByte('q')
		.WriteString(.InternalQuery)
		.WriteByte(0)
	}
	if .Where != "" {
		.WriteByte('W')
		.WriteString(.Where)
		.WriteByte(0)
	}
	if .SchemaName != "" {
		.WriteByte('s')
		.WriteString(.SchemaName)
		.WriteByte(0)
	}
	if .TableName != "" {
		.WriteByte('t')
		.WriteString(.TableName)
		.WriteByte(0)
	}
	if .ColumnName != "" {
		.WriteByte('c')
		.WriteString(.ColumnName)
		.WriteByte(0)
	}
	if .DataTypeName != "" {
		.WriteByte('d')
		.WriteString(.DataTypeName)
		.WriteByte(0)
	}
	if .ConstraintName != "" {
		.WriteByte('n')
		.WriteString(.ConstraintName)
		.WriteByte(0)
	}
	if .File != "" {
		.WriteByte('F')
		.WriteString(.File)
		.WriteByte(0)
	}
	if .Line != 0 {
		.WriteByte('L')
		.WriteString(strconv.Itoa(int(.Line)))
		.WriteByte(0)
	}
	if .Routine != "" {
		.WriteByte('R')
		.WriteString(.Routine)
		.WriteByte(0)
	}

	for ,  := range .UnknownFields {
		.WriteByte()
		.WriteString()
		.WriteByte(0)
	}

	.WriteByte(0)

	binary.BigEndian.PutUint32(.Bytes()[1:5], uint32(.Len()-1))

	return .Bytes()
}

// MarshalJSON implements encoding/json.Marshaler.
func ( ErrorResponse) () ([]byte, error) {
	return json.Marshal(struct {
		                string
		            string
		 string // only in 9.6 and greater
		                string
		             string
		              string
		                string
		            int32
		    int32
		       string
		               string
		          string
		           string
		          string
		        string
		      string
		                string
		                int32
		             string

		 map[byte]string
	}{
		:                "ErrorResponse",
		:            .Severity,
		: .SeverityUnlocalized,
		:                .Code,
		:             .Message,
		:              .Detail,
		:                .Hint,
		:            .Position,
		:    .InternalPosition,
		:       .InternalQuery,
		:               .Where,
		:          .SchemaName,
		:           .TableName,
		:          .ColumnName,
		:        .DataTypeName,
		:      .ConstraintName,
		:                .File,
		:                .Line,
		:             .Routine,
		:       .UnknownFields,
	})
}

// UnmarshalJSON implements encoding/json.Unmarshaler.
func ( *ErrorResponse) ( []byte) error {
	// Ignore null, like in the main JSON package.
	if string() == "null" {
		return nil
	}

	var  struct {
		                string
		            string
		 string // only in 9.6 and greater
		                string
		             string
		              string
		                string
		            int32
		    int32
		       string
		               string
		          string
		           string
		          string
		        string
		      string
		                string
		                int32
		             string

		 map[byte]string
	}
	if  := json.Unmarshal(, &);  != nil {
		return 
	}

	.Severity = .
	.SeverityUnlocalized = .
	.Code = .
	.Message = .
	.Detail = .
	.Hint = .
	.Position = .
	.InternalPosition = .
	.InternalQuery = .
	.Where = .
	.SchemaName = .
	.TableName = .
	.ColumnName = .
	.DataTypeName = .
	.ConstraintName = .
	.File = .
	.Line = .
	.Routine = .

	.UnknownFields = .

	return nil
}