github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/scripting/example_test.go (about)

     1  package scripting_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/ydb-platform/ydb-go-sdk/v3"
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/retry"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    11  )
    12  
    13  func Example_execute() {
    14  	ctx := context.TODO()
    15  	db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
    16  	if err != nil {
    17  		fmt.Printf("failed to connect: %v", err)
    18  
    19  		return
    20  	}
    21  	defer db.Close(ctx) // cleanup resources
    22  	if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
    23  		res, err := db.Scripting().Execute(
    24  			ctx,
    25  			"SELECT 1+1",
    26  			table.NewQueryParameters(),
    27  		)
    28  		if err != nil {
    29  			return err
    30  		}
    31  		defer res.Close() // cleanup resources
    32  		if !res.NextResultSet(ctx) {
    33  			return retry.RetryableError(
    34  				fmt.Errorf("no result sets"),
    35  				retry.WithBackoff(retry.TypeNoBackoff),
    36  			)
    37  		}
    38  		if !res.NextRow() {
    39  			return retry.RetryableError(
    40  				fmt.Errorf("no rows"),
    41  				retry.WithBackoff(retry.TypeSlowBackoff),
    42  			)
    43  		}
    44  		var sum int32
    45  		if err = res.Scan(&sum); err != nil {
    46  			return fmt.Errorf("scan failed: %w", err)
    47  		}
    48  		if sum != 2 {
    49  			return fmt.Errorf("unexpected sum: %v", sum)
    50  		}
    51  
    52  		return res.Err()
    53  	}, retry.WithIdempotent(true)); err != nil {
    54  		fmt.Printf("Execute failed: %v", err)
    55  	}
    56  }
    57  
    58  func Example_streamExecute() {
    59  	ctx := context.TODO()
    60  	db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
    61  	if err != nil {
    62  		fmt.Printf("failed to connect: %v", err)
    63  
    64  		return
    65  	}
    66  	defer db.Close(ctx) // cleanup resources
    67  	if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
    68  		res, err := db.Scripting().StreamExecute(
    69  			ctx,
    70  			"SELECT 1+1",
    71  			table.NewQueryParameters(),
    72  		)
    73  		if err != nil {
    74  			return err
    75  		}
    76  		defer res.Close() // cleanup resources
    77  		if !res.NextResultSet(ctx) {
    78  			return retry.RetryableError(
    79  				fmt.Errorf("no result sets"),
    80  				retry.WithBackoff(retry.TypeNoBackoff),
    81  				retry.WithDeleteSession(),
    82  			)
    83  		}
    84  		if !res.NextRow() {
    85  			return retry.RetryableError(
    86  				fmt.Errorf("no rows"),
    87  				retry.WithBackoff(retry.TypeFastBackoff),
    88  			)
    89  		}
    90  		var sum int32
    91  		if err = res.Scan(&sum); err != nil {
    92  			return fmt.Errorf("scan failed: %w", err)
    93  		}
    94  		if sum != 2 {
    95  			return fmt.Errorf("unexpected sum: %v", sum)
    96  		}
    97  
    98  		return res.Err()
    99  	}, retry.WithIdempotent(true)); err != nil {
   100  		fmt.Printf("StreamExecute failed: %v", err)
   101  	}
   102  }
   103  
   104  func Example_explainPlan() {
   105  	ctx := context.TODO()
   106  	db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
   107  	if err != nil {
   108  		fmt.Printf("failed to connect: %v", err)
   109  
   110  		return
   111  	}
   112  	defer db.Close(ctx) // cleanup resources
   113  	res, err := db.Scripting().Explain(
   114  		ctx,
   115  		"SELECT 1+1",
   116  		scripting.ExplainModePlan,
   117  	)
   118  	if err != nil {
   119  		fmt.Printf("Explain failed: %v", err)
   120  
   121  		return
   122  	}
   123  	if res.Plan == "" {
   124  		fmt.Printf("Unexpected empty plan")
   125  
   126  		return
   127  	}
   128  	fmt.Printf("")
   129  }
   130  
   131  func Example_explainValidate() {
   132  	ctx := context.TODO()
   133  	db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
   134  	if err != nil {
   135  		fmt.Printf("failed to connect: %v", err)
   136  
   137  		return
   138  	}
   139  	defer db.Close(ctx) // cleanup resources
   140  	if err = retry.Retry(ctx, func(ctx context.Context) (err error) {
   141  		res, err := db.Scripting().Explain(
   142  			ctx,
   143  			"SELECT 1+1",
   144  			scripting.ExplainModeValidate,
   145  		)
   146  		if err != nil {
   147  			return err
   148  		}
   149  		if len(res.ParameterTypes) > 0 {
   150  			return retry.RetryableError(fmt.Errorf("unexpected parameter types"))
   151  		}
   152  
   153  		return nil
   154  	}, retry.WithIdempotent(true)); err != nil {
   155  		fmt.Printf("Explain failed: %v", err)
   156  	}
   157  }