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

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  )
     7  
     8  type OperationExecutor struct {
     9  	store           *DocumentStore
    10  	databaseName    string
    11  	requestExecutor *RequestExecutor
    12  }
    13  
    14  func NewOperationExecutor(store *DocumentStore, databaseName string) *OperationExecutor {
    15  	res := &OperationExecutor{
    16  		store:        store,
    17  		databaseName: databaseName,
    18  	}
    19  	if res.databaseName == "" {
    20  		res.databaseName = store.GetDatabase()
    21  	}
    22  	panicIf(res.databaseName == "", "databaseName is empty")
    23  	res.requestExecutor = store.GetRequestExecutor(res.databaseName)
    24  	return res
    25  }
    26  
    27  func (e *OperationExecutor) ForDatabase(databaseName string) *OperationExecutor {
    28  	if strings.EqualFold(e.databaseName, databaseName) {
    29  		return e
    30  	}
    31  
    32  	return NewOperationExecutor(e.store, databaseName)
    33  }
    34  
    35  // Note: we don't return a result because we could only return interface{}
    36  // The caller has access to operation and can access strongly typed
    37  // command and its result
    38  // sessionInfo can be nil
    39  func (e *OperationExecutor) Send(operation IOperation, sessionInfo *SessionInfo) error {
    40  	command, err := operation.GetCommand(e.store, e.requestExecutor.GetConventions(), e.requestExecutor.Cache)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	return e.requestExecutor.ExecuteCommand(command, sessionInfo)
    45  }
    46  
    47  // sessionInfo can be nil
    48  func (e *OperationExecutor) SendAsync(operation IOperation, sessionInfo *SessionInfo) (*Operation, error) {
    49  	command, err := operation.GetCommand(e.store, e.requestExecutor.GetConventions(), e.requestExecutor.Cache)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	if err = e.requestExecutor.ExecuteCommand(command, sessionInfo); err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	changes := func() *DatabaseChanges {
    59  		return e.store.Changes("")
    60  	}
    61  	result := getCommandOperationIDResult(command)
    62  
    63  	return NewOperation(e.requestExecutor, changes, e.requestExecutor.GetConventions(), result.OperationID), nil
    64  }
    65  
    66  // Note: use SendPatchOperation() instead and check PatchOperationResult.Status
    67  // public PatchStatus send(PatchOperation operation) {
    68  // public PatchStatus send(PatchOperation operation, SessionInfo sessionInfo) {
    69  
    70  func (e *OperationExecutor) SendPatchOperation(operation *PatchOperation, sessionInfo *SessionInfo) (*PatchOperationResult, error) {
    71  	conventions := e.requestExecutor.GetConventions()
    72  	cache := e.requestExecutor.Cache
    73  	command, err := operation.GetCommand(e.store, conventions, cache)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	if err = e.requestExecutor.ExecuteCommand(command, sessionInfo); err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	cmdResult := operation.Command.Result
    82  	result := &PatchOperationResult{
    83  		Status:   cmdResult.Status,
    84  		Document: cmdResult.ModifiedDocument,
    85  	}
    86  	switch operation.Command.StatusCode {
    87  	case http.StatusNotModified:
    88  		result.Status = PatchStatusNotModified
    89  	case http.StatusNotFound:
    90  		result.Status = PatchStatusDocumentDoesNotExist
    91  	}
    92  	return result, nil
    93  }