github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/query/client.go (about)

     1  package query
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    11  )
    12  
    13  type (
    14  	// Executor is an interface for execute queries
    15  	Executor interface {
    16  		// Exec execute query without result
    17  		//
    18  		// Exec used by default:
    19  		// - DefaultTxControl
    20  		Exec(ctx context.Context, query string, opts ...options.Execute) error
    21  
    22  		// Query execute query with result
    23  		//
    24  		// Exec used by default:
    25  		// - DefaultTxControl
    26  		Query(ctx context.Context, query string, opts ...options.Execute) (Result, error)
    27  
    28  		// QueryResultSet execute query and take the exactly single materialized result set from result
    29  		//
    30  		// Exec used by default:
    31  		// - DefaultTxControl
    32  		QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (ClosableResultSet, error)
    33  
    34  		// QueryRow execute query and take the exactly single row from exactly single result set from result
    35  		//
    36  		// Exec used by default:
    37  		// - DefaultTxControl
    38  		QueryRow(ctx context.Context, query string, opts ...options.Execute) (Row, error)
    39  	}
    40  	// Client defines API of query client
    41  	Client interface {
    42  		Executor
    43  
    44  		// Do provide the best effort for execute operation.
    45  		//
    46  		// Do implements internal busy loop until one of the following conditions is met:
    47  		// - deadline was canceled or deadlined
    48  		// - retry operation returned nil as error
    49  		//
    50  		// Warning: if context without deadline or cancellation func than Do can run indefinitely.
    51  		Do(ctx context.Context, op Operation, opts ...DoOption) error
    52  
    53  		// DoTx provide the best effort for execute transaction.
    54  		//
    55  		// DoTx implements internal busy loop until one of the following conditions is met:
    56  		// - deadline was canceled or deadlined
    57  		// - retry operation returned nil as error
    58  		//
    59  		// DoTx makes auto selector (with TransactionSettings, by default - SerializableReadWrite), commit and
    60  		// rollback (on error) of transaction.
    61  		//
    62  		// If op TxOperation returns nil - transaction will be committed
    63  		// If op TxOperation return non nil - transaction will be rollback
    64  		// Warning: if context without deadline or cancellation func than DoTx can run indefinitely
    65  		DoTx(ctx context.Context, op TxOperation, opts ...DoTxOption) error
    66  
    67  		// Exec execute query without result
    68  		//
    69  		// Exec used by default:
    70  		// - DefaultTxControl
    71  		Exec(ctx context.Context, query string, opts ...options.Execute) error
    72  
    73  		// Query execute query with materialized result
    74  		//
    75  		// Warning: the large result from query will be materialized and can happened to "OOM killed" problem
    76  		//
    77  		// Exec used by default:
    78  		// - DefaultTxControl
    79  		Query(ctx context.Context, query string, opts ...options.Execute) (Result, error)
    80  
    81  		// QueryResultSet is a helper which read all rows from first result set in result
    82  		//
    83  		// Warning: the large result set from query will be materialized and can happened to "OOM killed" problem
    84  		QueryResultSet(ctx context.Context, query string, opts ...options.Execute) (ClosableResultSet, error)
    85  
    86  		// QueryRow is a helper which read only one row from first result set in result
    87  		//
    88  		// ReadRow returns error if result contains more than one result set or more than one row
    89  		QueryRow(ctx context.Context, query string, opts ...options.Execute) (Row, error)
    90  
    91  		// ExecuteScript starts long executing script with polling results later
    92  		//
    93  		// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
    94  		ExecuteScript(
    95  			ctx context.Context, query string, ttl time.Duration, ops ...options.Execute,
    96  		) (*options.ExecuteScriptOperation, error)
    97  
    98  		// FetchScriptResults fetching the script results
    99  		//
   100  		// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
   101  		FetchScriptResults(
   102  			ctx context.Context, opID string, opts ...options.FetchScriptOption,
   103  		) (*options.FetchScriptResult, error)
   104  	}
   105  )
   106  
   107  func WithFetchToken(fetchToken string) options.FetchScriptOption {
   108  	return options.WithFetchToken(fetchToken)
   109  }
   110  
   111  func WithResultSetIndex(resultSetIndex int64) options.FetchScriptOption {
   112  	return options.WithResultSetIndex(resultSetIndex)
   113  }
   114  
   115  func WithRowsLimit(rowsLimit int64) options.FetchScriptOption {
   116  	return options.WithRowsLimit(rowsLimit)
   117  }
   118  
   119  type (
   120  	// Operation is the interface that holds an operation for retry.
   121  	// if Operation returns not nil - operation will retry
   122  	// if Operation returns nil - retry loop will break
   123  	Operation func(ctx context.Context, s Session) error
   124  
   125  	// TxOperation is the interface that holds an operation for retry.
   126  	// if TxOperation returns not nil - operation will retry
   127  	// if TxOperation returns nil - retry loop will break
   128  	TxOperation func(ctx context.Context, tx TxActor) error
   129  
   130  	ClosableSession interface {
   131  		closer.Closer
   132  
   133  		Session
   134  	}
   135  	DoOption   = options.DoOption
   136  	DoTxOption = options.DoTxOption
   137  )
   138  
   139  func WithIdempotent() options.RetryOptionsOption {
   140  	return options.WithIdempotent()
   141  }
   142  
   143  func WithTrace(t *trace.Query) options.TraceOption {
   144  	return options.WithTrace(t)
   145  }
   146  
   147  func WithLabel(lbl string) options.RetryOptionsOption {
   148  	return options.WithLabel(lbl)
   149  }
   150  
   151  // WithRetryBudget creates option with external budget
   152  func WithRetryBudget(b budget.Budget) options.RetryOptionsOption {
   153  	return options.WithRetryBudget(b)
   154  }