github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/sugar/result.go (about)

     1  package sugar
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"io"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/scanner"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/query"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/table/result/indexed"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
    12  )
    13  
    14  type result struct {
    15  	r   query.Result
    16  	rs  query.ResultSet
    17  	row query.Row
    18  }
    19  
    20  func (r *result) NextResultSet(ctx context.Context) bool {
    21  	var err error
    22  	r.rs, err = r.r.NextResultSet(ctx)
    23  	if err != nil && errors.Is(err, io.EOF) {
    24  		return false
    25  	}
    26  
    27  	return err == nil && r.rs != nil && r.r.Err() == nil
    28  }
    29  
    30  func (r *result) NextRow() bool {
    31  	if r.rs == nil {
    32  		return false
    33  	}
    34  
    35  	var err error
    36  	r.row, err = r.rs.NextRow(context.Background())
    37  	if err != nil && errors.Is(err, io.EOF) {
    38  		return false
    39  	}
    40  
    41  	return r.row != nil && r.r.Err() == nil
    42  }
    43  
    44  func (r *result) Scan(indexedValues ...indexed.RequiredOrOptional) error {
    45  	values := make([]interface{}, 0, len(indexedValues))
    46  	for _, value := range indexedValues {
    47  		values = append(values, value)
    48  	}
    49  
    50  	return r.row.Scan(values...)
    51  }
    52  
    53  func (r *result) ScanNamed(namedValues ...named.Value) error {
    54  	values := make([]scanner.NamedDestination, 0, len(namedValues))
    55  	for i := range namedValues {
    56  		values = append(values, scanner.NamedRef(namedValues[i].Name, namedValues[i].Value))
    57  	}
    58  
    59  	return r.row.ScanNamed(values...)
    60  }
    61  
    62  func (r *result) ScanStruct(dst interface{}) error {
    63  	return r.row.ScanStruct(dst)
    64  }
    65  
    66  func (r *result) Err() error {
    67  	return r.r.Err()
    68  }
    69  
    70  func (r *result) Close() error {
    71  	return r.r.Close(context.Background())
    72  }
    73  
    74  // Result converts query.Result to iterable result for compatibility with table/result.Result usage
    75  //
    76  // # Experimental
    77  //
    78  // Notice: This API is EXPERIMENTAL and may be changed or removed in a later release.
    79  func Result(r query.Result) *result {
    80  	return &result{
    81  		r: r,
    82  	}
    83  }