github.com/altipla-consulting/ravendb-go-client@v0.1.3/explain_query_command.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  var (
     8  	_ RavenCommand = &ExplainQueryCommand{}
     9  )
    10  
    11  type ExplainQueryResult struct {
    12  	Index  string `json:"Index"`
    13  	Reason string `json:"Reason"`
    14  }
    15  
    16  type ExplainQueryCommand struct {
    17  	RavenCommandBase
    18  
    19  	_conventions *DocumentConventions
    20  	_indexQuery  *IndexQuery
    21  
    22  	Result []*ExplainQueryResult
    23  }
    24  
    25  func NewExplainQueryCommand(conventions *DocumentConventions, indexQuery *IndexQuery) *ExplainQueryCommand {
    26  	panicIf(conventions == nil, "Conventions cannot be null")
    27  	panicIf(indexQuery == nil, "IndexQuery cannot be null")
    28  	cmd := &ExplainQueryCommand{
    29  		RavenCommandBase: NewRavenCommandBase(),
    30  
    31  		_conventions: conventions,
    32  		_indexQuery:  indexQuery,
    33  	}
    34  	cmd.IsReadRequest = true
    35  	return cmd
    36  }
    37  
    38  func (c *ExplainQueryCommand) createRequest(node *ServerNode) (*http.Request, error) {
    39  	url := node.URL + "/databases/" + node.Database + "/queries?debug=explain"
    40  
    41  	v := jsonExtensionsWriteIndexQuery(c._conventions, c._indexQuery)
    42  	d, err := jsonMarshal(v)
    43  	panicIf(err != nil, "jsonMarshal() failed with %s", err)
    44  	return newHttpPost(url, d)
    45  }
    46  
    47  func (c *ExplainQueryCommand) setResponse(response []byte, fromCache bool) error {
    48  	var res struct {
    49  		Results []*ExplainQueryResult
    50  	}
    51  	err := jsonUnmarshal(response, &res)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	if res.Results == nil {
    56  		return throwInvalidResponse()
    57  	}
    58  	c.Result = res.Results
    59  	return nil
    60  }