github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/query/example_test.go (about) 1 package query_test 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "io" 8 9 "github.com/ydb-platform/ydb-go-sdk/v3" 10 "github.com/ydb-platform/ydb-go-sdk/v3/query" 11 ) 12 13 func Example_selectWithoutParameters() { 14 ctx := context.TODO() 15 db, err := ydb.Open(ctx, "grpc://localhost:2136/local") 16 if err != nil { 17 fmt.Printf("failed connect: %v", err) 18 19 return 20 } 21 defer db.Close(ctx) // cleanup resources 22 var ( 23 id int32 // required value 24 myStr string // optional value 25 ) 26 // Do retry operation on errors with best effort 27 err = db.Query().Do(ctx, // context manage exiting from Do 28 func(ctx context.Context, s query.Session) (err error) { // retry operation 29 _, res, err := s.Execute(ctx, 30 `SELECT 42 as id, "my string" as myStr`, 31 ) 32 if err != nil { 33 return err // for auto-retry with driver 34 } 35 defer func() { _ = res.Close(ctx) }() // cleanup resources 36 for { // iterate over result sets 37 rs, err := res.NextResultSet(ctx) 38 if err != nil { 39 if errors.Is(err, io.EOF) { 40 break 41 } 42 43 return err 44 } 45 for { // iterate over rows 46 row, err := rs.NextRow(ctx) 47 if err != nil { 48 if errors.Is(err, io.EOF) { 49 break 50 } 51 52 return err 53 } 54 if err = row.Scan(&id, &myStr); err != nil { 55 return err // generally scan error not retryable, return it for driver check error 56 } 57 } 58 } 59 60 return res.Err() // return finally result error for auto-retry with driver 61 }, 62 query.WithIdempotent(), 63 ) 64 if err != nil { 65 fmt.Printf("unexpected error: %v", err) 66 } 67 fmt.Printf("id=%v, myStr='%s'\n", id, myStr) 68 } 69 70 func Example_selectWithParameters() { 71 ctx := context.TODO() 72 db, err := ydb.Open(ctx, "grpc://localhost:2136/local") 73 if err != nil { 74 fmt.Printf("failed connect: %v", err) 75 76 return 77 } 78 defer db.Close(ctx) // cleanup resources 79 var ( 80 id int32 // required value 81 myStr string // optional value 82 ) 83 // Do retry operation on errors with best effort 84 err = db.Query().Do(ctx, // context manage exiting from Do 85 func(ctx context.Context, s query.Session) (err error) { // retry operation 86 _, res, err := s.Execute(ctx, 87 `SELECT CAST($id AS Uint64) AS id, CAST($myStr AS Text) AS myStr`, 88 query.WithParameters( 89 ydb.ParamsBuilder(). 90 Param("$id").Uint64(123). 91 Param("$myStr").Text("123"). 92 Build(), 93 ), 94 ) 95 if err != nil { 96 return err // for auto-retry with driver 97 } 98 defer func() { _ = res.Close(ctx) }() // cleanup resources 99 for { // iterate over result sets 100 rs, err := res.NextResultSet(ctx) 101 if err != nil { 102 if errors.Is(err, io.EOF) { 103 break 104 } 105 106 return err 107 } 108 for { // iterate over rows 109 row, err := rs.NextRow(ctx) 110 if err != nil { 111 if errors.Is(err, io.EOF) { 112 break 113 } 114 115 return err 116 } 117 if err = row.ScanNamed( 118 query.Named("id", &id), 119 query.Named("myStr", &myStr), 120 ); err != nil { 121 return err // generally scan error not retryable, return it for driver check error 122 } 123 } 124 } 125 126 return res.Err() // return finally result error for auto-retry with driver 127 }, 128 query.WithIdempotent(), 129 ) 130 if err != nil { 131 fmt.Printf("unexpected error: %v", err) 132 } 133 fmt.Printf("id=%v, myStr='%s'\n", id, myStr) 134 } 135 136 func Example_txSelect() { 137 ctx := context.TODO() 138 db, err := ydb.Open(ctx, "grpc://localhost:2136/local") 139 if err != nil { 140 fmt.Printf("failed connect: %v", err) 141 142 return 143 } 144 defer db.Close(ctx) // cleanup resources 145 var ( 146 id int32 // required value 147 myStr string // optional value 148 ) 149 // Do retry operation on errors with best effort 150 err = db.Query().DoTx(ctx, // context manage exiting from Do 151 func(ctx context.Context, tx query.TxActor) (err error) { // retry operation 152 res, err := tx.Execute(ctx, 153 `SELECT 42 as id, "my string" as myStr`, 154 ) 155 if err != nil { 156 return err // for auto-retry with driver 157 } 158 defer func() { _ = res.Close(ctx) }() // cleanup resources 159 for { // iterate over result sets 160 rs, err := res.NextResultSet(ctx) 161 if err != nil { 162 if errors.Is(err, io.EOF) { 163 break 164 } 165 166 return err 167 } 168 for { // iterate over rows 169 row, err := rs.NextRow(ctx) 170 if err != nil { 171 if errors.Is(err, io.EOF) { 172 break 173 } 174 175 return err 176 } 177 if err = row.ScanNamed( 178 query.Named("id", &id), 179 query.Named("myStr", &myStr), 180 ); err != nil { 181 return err // generally scan error not retryable, return it for driver check error 182 } 183 } 184 } 185 186 return res.Err() // return finally result error for auto-retry with driver 187 }, 188 query.WithIdempotent(), 189 query.WithTxSettings(query.TxSettings( 190 query.WithSnapshotReadOnly(), 191 )), 192 ) 193 if err != nil { 194 fmt.Printf("unexpected error: %v", err) 195 } 196 fmt.Printf("id=%v, myStr='%s'\n", id, myStr) 197 }