...

Source file src/github.com/go-kivik/kivik/v4/mockdb/rows.go

Documentation: github.com/go-kivik/kivik/v4/mockdb

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package mockdb
    14  
    15  import (
    16  	"context"
    17  	"time"
    18  
    19  	"github.com/go-kivik/kivik/v4/driver"
    20  )
    21  
    22  // Rows is a mocked collection of rows.
    23  type Rows struct {
    24  	iter
    25  	offset    int64
    26  	updateSeq string
    27  	totalRows int64
    28  	warning   string
    29  }
    30  
    31  func coalesceRows(rows *Rows) *Rows {
    32  	if rows != nil {
    33  		return rows
    34  	}
    35  	return &Rows{}
    36  }
    37  
    38  type driverRows struct {
    39  	context.Context
    40  	*Rows
    41  }
    42  
    43  var (
    44  	_ driver.Rows       = &driverRows{}
    45  	_ driver.RowsWarner = &driverRows{}
    46  )
    47  
    48  func (r *driverRows) Offset() int64     { return r.offset }
    49  func (r *driverRows) UpdateSeq() string { return r.updateSeq }
    50  func (r *driverRows) TotalRows() int64  { return r.totalRows }
    51  func (r *driverRows) Warning() string   { return r.warning }
    52  
    53  func (r *driverRows) Next(row *driver.Row) error {
    54  	result, err := r.unshift(r.Context)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	*row = *result.(*driver.Row)
    59  	return nil
    60  }
    61  
    62  // CloseError sets an error to be returned when the rows iterator is closed.
    63  func (r *Rows) CloseError(err error) *Rows {
    64  	r.closeErr = err
    65  	return r
    66  }
    67  
    68  // Offset sets the offset value to be returned by the rows iterator.
    69  func (r *Rows) Offset(offset int64) *Rows {
    70  	r.offset = offset
    71  	return r
    72  }
    73  
    74  // TotalRows sets the total rows value to be returned by the rows iterator.
    75  func (r *Rows) TotalRows(totalRows int64) *Rows {
    76  	r.totalRows = totalRows
    77  	return r
    78  }
    79  
    80  // UpdateSeq sets the update sequence value to be returned by the rows iterator.
    81  func (r *Rows) UpdateSeq(seq string) *Rows {
    82  	r.updateSeq = seq
    83  	return r
    84  }
    85  
    86  // Warning sets the warning value to be returned by the rows iterator.
    87  func (r *Rows) Warning(warning string) *Rows {
    88  	r.warning = warning
    89  	return r
    90  }
    91  
    92  // AddRow adds a row to be returned by the rows iterator. If AddrowError has
    93  // been set, this method will panic.
    94  func (r *Rows) AddRow(row *driver.Row) *Rows {
    95  	if r.resultErr != nil {
    96  		panic("It is invalid to set more rows after AddRowError is defined.")
    97  	}
    98  	r.push(&item{item: row})
    99  	return r
   100  }
   101  
   102  // AddRowError adds an error to be returned during row iteration.
   103  func (r *Rows) AddRowError(err error) *Rows {
   104  	r.resultErr = err
   105  	return r
   106  }
   107  
   108  // AddDelay adds a delay before the next iteration will complete.
   109  func (r *Rows) AddDelay(delay time.Duration) *Rows {
   110  	r.push(&item{delay: delay})
   111  	return r
   112  }
   113  
   114  // Final converts the Rows object to a driver.Rows. This method is intended for
   115  // use within WillExecute() to return results.
   116  func (r *Rows) Final() driver.Rows {
   117  	return &driverRows{Rows: r}
   118  }
   119  

View as plain text