github.com/cyverse/go-irodsclient@v0.13.2/irods/message/query_specific_request.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"golang.org/x/xerrors"
     8  )
     9  
    10  // IRODSMessageQuerySpecificRequest stores specific query request
    11  type IRODSMessageQuerySpecificRequest struct {
    12  	// str *sql; str *arg1; str *arg2; str *arg3; str *arg4; str *arg5; str *arg6; str *arg7; str *arg8; str *arg9; str *arg10; int maxRows; int continueInx; int rowOffset; int options; struct KeyValPair_PI;
    13  	XMLName       xml.Name             `xml:"specificQueryInp_PI"`
    14  	SQL           string               `xml:"sql"`
    15  	Arg1          string               `xml:"arg1"`
    16  	Arg2          string               `xml:"arg2"`
    17  	Arg3          string               `xml:"arg3"`
    18  	Arg4          string               `xml:"arg4"`
    19  	Arg5          string               `xml:"arg5"`
    20  	Arg6          string               `xml:"arg6"`
    21  	Arg7          string               `xml:"arg7"`
    22  	Arg8          string               `xml:"arg8"`
    23  	Arg9          string               `xml:"arg9"`
    24  	Arg10         string               `xml:"arg10"`
    25  	MaxRows       int                  `xml:"maxRows"`
    26  	ContinueIndex int                  `xml:"continueInx"` // 1 for continueing, 0 for end
    27  	RowOffset     int                  `xml:"rowOffset"`
    28  	Options       int                  `xml:"options"`
    29  	KeyVals       IRODSMessageSSKeyVal `xml:"KeyValPair_PI"`
    30  }
    31  
    32  // NewIRODSMessageQuerySpecificRequest creates a IRODSMessageQuerySpecificRequest message
    33  func NewIRODSMessageQuerySpecificRequest(sqlQuery string, args []string, maxRows int, continueIndex int, rowOffset int, options int) *IRODSMessageQuerySpecificRequest {
    34  	q := &IRODSMessageQuerySpecificRequest{
    35  		SQL:           sqlQuery,
    36  		MaxRows:       maxRows,
    37  		ContinueIndex: continueIndex,
    38  		RowOffset:     rowOffset,
    39  		Options:       options,
    40  		KeyVals: IRODSMessageSSKeyVal{
    41  			Length: 0,
    42  		},
    43  	}
    44  
    45  	argMap := []*string{&q.Arg1, &q.Arg2, &q.Arg3, &q.Arg4, &q.Arg5, &q.Arg6, &q.Arg7, &q.Arg8, &q.Arg9, &q.Arg10}
    46  	for i, ptr := range argMap {
    47  		if len(args) > i {
    48  			*ptr = args[i]
    49  		}
    50  	}
    51  
    52  	return q
    53  }
    54  
    55  // AddKeyVal adds a key-value pair
    56  func (msg *IRODSMessageQuerySpecificRequest) AddKeyVal(key common.KeyWord, val string) {
    57  	msg.KeyVals.Add(string(key), val)
    58  }
    59  
    60  // GetBytes returns byte array
    61  func (msg *IRODSMessageQuerySpecificRequest) GetBytes() ([]byte, error) {
    62  	xmlBytes, err := xml.Marshal(msg)
    63  	if err != nil {
    64  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    65  	}
    66  	return xmlBytes, nil
    67  }
    68  
    69  // FromBytes returns struct from bytes
    70  func (msg *IRODSMessageQuerySpecificRequest) FromBytes(bytes []byte) error {
    71  	err := xml.Unmarshal(bytes, msg)
    72  	if err != nil {
    73  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    74  	}
    75  	return nil
    76  }
    77  
    78  // GetMessage builds a message
    79  func (msg *IRODSMessageQuerySpecificRequest) GetMessage() (*IRODSMessage, error) {
    80  	bytes, err := msg.GetBytes()
    81  	if err != nil {
    82  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    83  	}
    84  
    85  	msgBody := IRODSMessageBody{
    86  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    87  		Message: bytes,
    88  		Error:   nil,
    89  		Bs:      nil,
    90  		IntInfo: int32(common.SPECIFIC_QUERY_AN),
    91  	}
    92  
    93  	msgHeader, err := msgBody.BuildHeader()
    94  	if err != nil {
    95  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    96  	}
    97  
    98  	return &IRODSMessage{
    99  		Header: msgHeader,
   100  		Body:   &msgBody,
   101  	}, nil
   102  }