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

     1  package ravendb
     2  
     3  var _ ILazyOperation = &LazyQueryOperation{}
     4  
     5  // LazyQueryOperation describes server operation for lazy queries
     6  type LazyQueryOperation struct {
     7  	_conventions        *DocumentConventions
     8  	_queryOperation     *queryOperation
     9  	_afterQueryExecuted []func(*QueryResult)
    10  
    11  	queryResult   *QueryResult
    12  	requiresRetry bool
    13  }
    14  
    15  // newLazyQueryOperation returns new LazyQueryOperation
    16  func newLazyQueryOperation(conventions *DocumentConventions, queryOperation *queryOperation, afterQueryExecuted []func(*QueryResult)) *LazyQueryOperation {
    17  	return &LazyQueryOperation{
    18  		_conventions:        conventions,
    19  		_queryOperation:     queryOperation,
    20  		_afterQueryExecuted: afterQueryExecuted,
    21  	}
    22  }
    23  
    24  // needed for ILazyOperation
    25  func (o *LazyQueryOperation) createRequest() *getRequest {
    26  	return &getRequest{
    27  		url:     "/queries",
    28  		method:  "POST",
    29  		query:   "?queryHash=" + o._queryOperation.indexQuery.GetQueryHash(),
    30  		content: NewIndexQueryContent(o._conventions, o._queryOperation.indexQuery),
    31  	}
    32  }
    33  
    34  // needed for ILazyOperation
    35  func (o *LazyQueryOperation) getResult(result interface{}) error {
    36  	return o._queryOperation.complete(result)
    37  }
    38  
    39  // needed for ILazyOperation
    40  func (o *LazyQueryOperation) getQueryResult() *QueryResult {
    41  	return o.queryResult
    42  }
    43  
    44  // needed for ILazyOperation
    45  func (o *LazyQueryOperation) isRequiresRetry() bool {
    46  	return o.requiresRetry
    47  }
    48  
    49  // needed for ILazyOperation
    50  func (o *LazyQueryOperation) handleResponse(response *GetResponse) error {
    51  	if response.IsForceRetry {
    52  		o.requiresRetry = true
    53  		return nil
    54  	}
    55  
    56  	var queryResult *QueryResult
    57  	if len(response.Result) != 0 {
    58  		err := jsonUnmarshal(response.Result, &queryResult)
    59  		if err != nil {
    60  			return err
    61  		}
    62  	}
    63  	return o.handleResponse2(queryResult)
    64  }
    65  
    66  func (o *LazyQueryOperation) handleResponse2(queryResult *QueryResult) error {
    67  	err := o._queryOperation.ensureIsAcceptableAndSaveResult(queryResult)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	for _, e := range o._afterQueryExecuted {
    73  		e(queryResult)
    74  	}
    75  	o.queryResult = queryResult
    76  	return nil
    77  }