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

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"github.com/cyverse/go-irodsclient/irods/util"
     8  	"golang.org/x/xerrors"
     9  )
    10  
    11  // IRODSMessageQueryRequest stores query
    12  type IRODSMessageQueryRequest struct {
    13  	XMLName           xml.Name             `xml:"GenQueryInp_PI"`
    14  	MaxRows           int                  `xml:"maxRows"`
    15  	ContinueIndex     int                  `xml:"continueInx"`       // 1 for continueing, 0 for end
    16  	PartialStartIndex int                  `xml:"partialStartIndex"` // unknown
    17  	Options           int                  `xml:"options"`
    18  	KeyVals           IRODSMessageSSKeyVal `xml:"KeyValPair_PI"`
    19  	Selects           IRODSMessageIIKeyVal `xml:"InxIvalPair_PI"`
    20  	Conditions        IRODSMessageISKeyVal `xml:"InxValPair_PI"`
    21  }
    22  
    23  // NewIRODSMessageQueryRequest creates a IRODSMessageQueryRequest message
    24  func NewIRODSMessageQueryRequest(maxRows int, continueIndex int, partialStartIndex int, options int) *IRODSMessageQueryRequest {
    25  	return &IRODSMessageQueryRequest{
    26  		MaxRows:           maxRows,
    27  		ContinueIndex:     continueIndex,
    28  		PartialStartIndex: partialStartIndex,
    29  		Options:           options,
    30  		KeyVals: IRODSMessageSSKeyVal{
    31  			Length: 0,
    32  		},
    33  		Selects: IRODSMessageIIKeyVal{
    34  			Length: 0,
    35  		},
    36  		Conditions: IRODSMessageISKeyVal{
    37  			Length: 0,
    38  		},
    39  	}
    40  }
    41  
    42  // AddSelect adds a column to select
    43  func (msg *IRODSMessageQueryRequest) AddSelect(key common.ICATColumnNumber, val int) {
    44  	msg.Selects.Add(int(key), val)
    45  }
    46  
    47  // AddCondition adds a condition
    48  func (msg *IRODSMessageQueryRequest) AddCondition(key common.ICATColumnNumber, val string) {
    49  	escapedVal := util.EscapeXMLSpecialChars(val)
    50  	msg.Conditions.Add(int(key), escapedVal)
    51  }
    52  
    53  // AddKeyVal adds a key-value pair
    54  func (msg *IRODSMessageQueryRequest) AddKeyVal(key common.KeyWord, val string) {
    55  	escapedVal := util.EscapeXMLSpecialChars(val)
    56  	msg.KeyVals.Add(string(key), escapedVal)
    57  }
    58  
    59  // GetBytes returns byte array
    60  func (msg *IRODSMessageQueryRequest) GetBytes() ([]byte, error) {
    61  	xmlBytes, err := xml.Marshal(msg)
    62  	if err != nil {
    63  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    64  	}
    65  	return xmlBytes, nil
    66  }
    67  
    68  // FromBytes returns struct from bytes
    69  func (msg *IRODSMessageQueryRequest) FromBytes(bytes []byte) error {
    70  	err := xml.Unmarshal(bytes, msg)
    71  	if err != nil {
    72  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    73  	}
    74  	return nil
    75  }
    76  
    77  // GetMessage builds a message
    78  func (msg *IRODSMessageQueryRequest) GetMessage() (*IRODSMessage, error) {
    79  	bytes, err := msg.GetBytes()
    80  	if err != nil {
    81  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    82  	}
    83  
    84  	msgBody := IRODSMessageBody{
    85  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    86  		Message: bytes,
    87  		Error:   nil,
    88  		Bs:      nil,
    89  		IntInfo: int32(common.GEN_QUERY_AN),
    90  	}
    91  
    92  	msgHeader, err := msgBody.BuildHeader()
    93  	if err != nil {
    94  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    95  	}
    96  
    97  	return &IRODSMessage{
    98  		Header: msgHeader,
    99  		Body:   &msgBody,
   100  	}, nil
   101  }