github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/table/statement.go (about)

     1  package table
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     7  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
     8  	"google.golang.org/grpc"
     9  
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/operation"
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
    13  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
    14  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    16  	"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
    17  	"github.com/ydb-platform/ydb-go-sdk/v3/table/result"
    18  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    19  )
    20  
    21  type statement struct {
    22  	session *session
    23  	query   query
    24  	params  map[string]*Ydb.Type
    25  }
    26  
    27  // Execute executes prepared data query.
    28  func (s *statement) Execute(
    29  	ctx context.Context, txControl *table.TransactionControl,
    30  	parameters *params.Parameters,
    31  	opts ...options.ExecuteDataQueryOption,
    32  ) (
    33  	txr table.Transaction, r result.Result, err error,
    34  ) {
    35  	var (
    36  		a       = allocator.New()
    37  		request = options.ExecuteDataQueryDesc{
    38  			ExecuteDataQueryRequest: a.TableExecuteDataQueryRequest(),
    39  			IgnoreTruncated:         s.session.config.IgnoreTruncated(),
    40  		}
    41  		callOptions []grpc.CallOption
    42  	)
    43  	defer a.Free()
    44  
    45  	request.SessionId = s.session.id
    46  	request.TxControl = txControl.Desc()
    47  	request.Parameters = parameters.ToYDB(a)
    48  	request.Query = s.query.toYDB(a)
    49  	request.QueryCachePolicy = a.TableQueryCachePolicy()
    50  	request.QueryCachePolicy.KeepInCache = len(request.Parameters) > 0
    51  	request.OperationParams = operation.Params(ctx,
    52  		s.session.config.OperationTimeout(),
    53  		s.session.config.OperationCancelAfter(),
    54  		operation.ModeSync,
    55  	)
    56  
    57  	for _, opt := range opts {
    58  		if opt != nil {
    59  			callOptions = append(callOptions, opt.ApplyExecuteDataQueryOption(&request, a)...)
    60  		}
    61  	}
    62  
    63  	onDone := trace.TableOnSessionQueryExecute(
    64  		s.session.config.Trace(), &ctx,
    65  		stack.FunctionID(""),
    66  		s.session, s.query, parameters,
    67  		request.QueryCachePolicy.GetKeepInCache(),
    68  	)
    69  	defer func() {
    70  		onDone(txr, true, r, err)
    71  	}()
    72  
    73  	return s.execute(ctx, a, &request, request.TxControl, callOptions...)
    74  }
    75  
    76  // execute executes prepared query without any tracing.
    77  func (s *statement) execute(
    78  	ctx context.Context, a *allocator.Allocator,
    79  	request *options.ExecuteDataQueryDesc, txControl *Ydb_Table.TransactionControl,
    80  	callOptions ...grpc.CallOption,
    81  ) (
    82  	txr table.Transaction, r result.Result, err error,
    83  ) {
    84  	res, err := s.session.executeDataQuery(ctx, a, request.ExecuteDataQueryRequest, callOptions...)
    85  	if err != nil {
    86  		return nil, nil, xerrors.WithStackTrace(err)
    87  	}
    88  
    89  	return s.session.executeQueryResult(res, txControl, request.IgnoreTruncated)
    90  }
    91  
    92  func (s *statement) NumInput() int {
    93  	return len(s.params)
    94  }
    95  
    96  func (s *statement) Text() string {
    97  	return s.query.YQL()
    98  }