github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/query_tx_execute_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/ydb-platform/ydb-go-sdk/v3"
    14  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/version"
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/query"
    16  )
    17  
    18  func TestQueryTxExecute(t *testing.T) {
    19  	if version.Lt(os.Getenv("YDB_VERSION"), "24.1") {
    20  		t.Skip("query service not allowed in YDB version '" + os.Getenv("YDB_VERSION") + "'")
    21  	}
    22  
    23  	ctx, cancel := context.WithCancel(context.Background())
    24  	defer cancel()
    25  
    26  	db, err := ydb.Open(ctx,
    27  		os.Getenv("YDB_CONNECTION_STRING"),
    28  		ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
    29  	)
    30  	require.NoError(t, err)
    31  	err = db.Query().DoTx(ctx, func(ctx context.Context, tx query.TxActor) (err error) {
    32  		res, err := tx.Execute(ctx, "SELECT 1 AS col1")
    33  		if err != nil {
    34  			return err
    35  		}
    36  		rs, err := res.NextResultSet(ctx)
    37  		if err != nil {
    38  			return err
    39  		}
    40  		row, err := rs.NextRow(ctx)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		var col1 int
    45  		err = row.ScanNamed(query.Named("col1", &col1))
    46  		if err != nil {
    47  			return err
    48  		}
    49  		return res.Err()
    50  	}, query.WithIdempotent())
    51  	require.NoError(t, err)
    52  }