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

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  var (
     8  	_ RavenCommand = &QueryCommand{}
     9  )
    10  
    11  type QueryCommand struct {
    12  	RavenCommandBase
    13  
    14  	conventions      *DocumentConventions
    15  	indexQuery       *IndexQuery
    16  	metadataOnly     bool
    17  	indexEntriesOnly bool
    18  
    19  	Result *QueryResult
    20  }
    21  
    22  func NewQueryCommand(conventions *DocumentConventions, indexQuery *IndexQuery, metadataOnly bool, indexEntriesOnly bool) (*QueryCommand, error) {
    23  	if indexQuery == nil {
    24  		return nil, newIllegalArgumentError("IndexQuery cannot be null")
    25  	}
    26  	cmd := &QueryCommand{
    27  		RavenCommandBase: NewRavenCommandBase(),
    28  
    29  		conventions:      conventions,
    30  		indexQuery:       indexQuery,
    31  		metadataOnly:     metadataOnly,
    32  		indexEntriesOnly: indexEntriesOnly,
    33  	}
    34  	cmd.IsReadRequest = true
    35  	return cmd, nil
    36  }
    37  
    38  func (c *QueryCommand) createRequest(node *ServerNode) (*http.Request, error) {
    39  	c.CanCache = !c.indexQuery.disableCaching
    40  
    41  	// we won't allow aggressive caching of queries with WaitForNonStaleResults
    42  	c.CanCacheAggressively = c.CanCache && !c.indexQuery.waitForNonStaleResults
    43  
    44  	// we need to add a query hash because we are using POST queries
    45  	// so we need to unique parameter per query so the query cache will
    46  	// work properly
    47  	path := node.URL + "/databases/" + node.Database + "/queries?queryHash=" + c.indexQuery.GetQueryHash()
    48  
    49  	if c.metadataOnly {
    50  		path += "&metadataOnly=true"
    51  	}
    52  
    53  	if c.indexEntriesOnly {
    54  		path += "&debug=entries"
    55  	}
    56  
    57  	m := jsonExtensionsWriteIndexQuery(c.conventions, c.indexQuery)
    58  	d, err := jsonMarshal(m)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return newHttpPost(path, d)
    63  }
    64  
    65  func (c *QueryCommand) setResponse(response []byte, fromCache bool) error {
    66  	if len(response) == 0 {
    67  		return nil
    68  	}
    69  
    70  	return jsonUnmarshal(response, &c.Result)
    71  }