11package exported
22
33import (
4- "cloud.google.com/go/spanner/apiv1/spannerpb"
54 "fmt"
5+ "testing"
6+
7+ "cloud.google.com/go/spanner/apiv1/spannerpb"
68 "google.golang.org/protobuf/proto"
79 "google.golang.org/protobuf/types/known/structpb"
8- "testing"
910)
1011
1112func TestExecute (t * testing.T ) {
@@ -19,6 +20,57 @@ func TestExecute(t *testing.T) {
1920 }
2021 stmtBytes , _ := proto .Marshal (& stmt )
2122 rows := Execute (pool .ObjectId , conn .ObjectId , stmtBytes )
23+ metadata := Metadata (pool .ObjectId , conn .ObjectId , rows .ObjectId )
24+ metadataValue := spannerpb.ResultSetMetadata {}
25+ _ = proto .Unmarshal (metadata .Res , & metadataValue )
26+ fmt .Printf ("Row type: %v\n " , metadataValue .RowType )
27+ for {
28+ row := Next (pool .ObjectId , conn .ObjectId , rows .ObjectId )
29+ rowValue := structpb.ListValue {}
30+ _ = proto .Unmarshal (row .Res , & rowValue )
31+ if row .Length () == 0 {
32+ break
33+ }
34+ fmt .Printf ("row: %v\n " , rowValue .Values )
35+ }
36+ CloseRows (pool .ObjectId , conn .ObjectId , rows .ObjectId )
37+ CloseConnection (pool .ObjectId , conn .ObjectId )
38+ ClosePool (pool .ObjectId )
39+ }
40+
41+ func TestExecuteDml (t * testing.T ) {
42+ pool := CreatePool ()
43+ conn := CreateConnection (pool .ObjectId , "appdev-soda-spanner-staging" , "knut-test-ycsb" , "knut-test-db" )
44+ txOpts := & spannerpb.TransactionOptions {
45+ Mode : & spannerpb.TransactionOptions_ReadOnly_ {
46+ ReadOnly : & spannerpb.TransactionOptions_ReadOnly {},
47+ },
48+ }
49+ txOptsBytes , _ := proto .Marshal (txOpts )
50+ BeginTransaction (pool .ObjectId , conn .ObjectId , txOptsBytes )
51+ stmt := spannerpb.ExecuteBatchDmlRequest_Statement {
52+ Sql : "update all_types set col_float8=$1 where col_varchar=$2" ,
53+ Params : & structpb.Struct {
54+ Fields : map [string ]* structpb.Value {
55+ "p1" : {Kind : & structpb.Value_NumberValue {NumberValue : 3.14 }},
56+ "p2" : {Kind : & structpb.Value_StringValue {StringValue : "61763b0e7feb3ea8fc9e734a6700f6a4" }},
57+ },
58+ },
59+ }
60+ stmtBytes , _ := proto .Marshal (& stmt )
61+ rows := Execute (pool .ObjectId , conn .ObjectId , stmtBytes )
62+ if rows .Code != 0 {
63+ t .Fatalf ("failed to execute statement: %s" , string (rows .Res ))
64+ }
65+ metadata := Metadata (pool .ObjectId , conn .ObjectId , rows .ObjectId )
66+ metadataValue := spannerpb.ResultSetMetadata {}
67+ _ = proto .Unmarshal (metadata .Res , & metadataValue )
68+ if len (metadataValue .RowType .Fields ) > 0 {
69+ fmt .Printf ("Row type: %v\n " , metadataValue .RowType )
70+ } else {
71+ rowCount := UpdateCount (pool .ObjectId , conn .ObjectId , rows .ObjectId )
72+ fmt .Printf ("Update count: %v\n " , string (rowCount .Res ))
73+ }
2274 for {
2375 row := Next (pool .ObjectId , conn .ObjectId , rows .ObjectId )
2476 rowValue := structpb.ListValue {}
0 commit comments