|
| 1 | +package exported |
| 2 | + |
| 3 | +import "C" |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "database/sql" |
| 7 | + "fmt" |
| 8 | + "sync" |
| 9 | + "sync/atomic" |
| 10 | + |
| 11 | + "cloud.google.com/go/spanner" |
| 12 | + "cloud.google.com/go/spanner/apiv1/spannerpb" |
| 13 | + spannerdriver "github.com/googleapis/go-sql-spanner" |
| 14 | + "google.golang.org/protobuf/proto" |
| 15 | + "spannerlib/backend" |
| 16 | +) |
| 17 | + |
| 18 | +func CloseConnection(poolId, connId int64) *Message { |
| 19 | + conn, err := findConnection(poolId, connId) |
| 20 | + if err != nil { |
| 21 | + return errMessage(err) |
| 22 | + } |
| 23 | + return conn.close() |
| 24 | +} |
| 25 | + |
| 26 | +func Execute(poolId, connId int64, statementBytes []byte) *Message { |
| 27 | + statement := spannerpb.ExecuteBatchDmlRequest_Statement{} |
| 28 | + if err := proto.Unmarshal(statementBytes, &statement); err != nil { |
| 29 | + return errMessage(err) |
| 30 | + } |
| 31 | + fmt.Printf("Statement: %v\n", statement.Sql) |
| 32 | + fmt.Printf("Params: %v\n", statement.Params) |
| 33 | + conn, err := findConnection(poolId, connId) |
| 34 | + if err != nil { |
| 35 | + return errMessage(err) |
| 36 | + } |
| 37 | + return conn.Execute(&statement) |
| 38 | +} |
| 39 | + |
| 40 | +type Connection struct { |
| 41 | + results *sync.Map |
| 42 | + resultsIdx atomic.Int64 |
| 43 | + |
| 44 | + backend *backend.SpannerConnection |
| 45 | +} |
| 46 | + |
| 47 | +func (conn *Connection) close() *Message { |
| 48 | + conn.results.Range(func(key, value interface{}) bool { |
| 49 | + res := value.(*rows) |
| 50 | + res.Close() |
| 51 | + return true |
| 52 | + }) |
| 53 | + err := conn.backend.Close() |
| 54 | + if err != nil { |
| 55 | + return errMessage(err) |
| 56 | + } |
| 57 | + return &Message{} |
| 58 | +} |
| 59 | + |
| 60 | +func (conn *Connection) Execute(statement *spannerpb.ExecuteBatchDmlRequest_Statement) *Message { |
| 61 | + paramsLen := 1 |
| 62 | + if statement.Params != nil { |
| 63 | + paramsLen = 1 + len(statement.Params.Fields) |
| 64 | + } |
| 65 | + params := make([]any, paramsLen) |
| 66 | + params = append(params, spannerdriver.ExecOptions{DecodeOption: spannerdriver.DecodeOptionProto}) |
| 67 | + if statement.Params != nil { |
| 68 | + if statement.ParamTypes == nil { |
| 69 | + statement.ParamTypes = make(map[string]*spannerpb.Type) |
| 70 | + } |
| 71 | + for param, value := range statement.Params.Fields { |
| 72 | + genericValue := spanner.GenericColumnValue{ |
| 73 | + Value: value, |
| 74 | + Type: statement.ParamTypes[param], |
| 75 | + } |
| 76 | + params = append(params, sql.Named(param, genericValue)) |
| 77 | + } |
| 78 | + } |
| 79 | + it, err := conn.backend.Conn.QueryContext(context.Background(), statement.Sql, params...) |
| 80 | + if err != nil { |
| 81 | + return errMessage(err) |
| 82 | + } |
| 83 | + id := conn.resultsIdx.Add(1) |
| 84 | + res := &rows{ |
| 85 | + backend: it, |
| 86 | + } |
| 87 | + conn.results.Store(id, res) |
| 88 | + return idMessage(id) |
| 89 | +} |
0 commit comments