/
transaction.go
122 lines (100 loc) · 3.3 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2022 Codenotary Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"github.com/codenotary/immudb/pkg/api/schema"
"github.com/codenotary/immudb/pkg/client/errors"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/metadata"
)
// Tx represents an open transaction
//
// Note: Currently this object only supports SQL transactions
type Tx interface {
// Commit commits a transaction.
Commit(ctx context.Context) (*schema.CommittedSQLTx, error)
// Rollback rollbacks a transaction.
Rollback(ctx context.Context) error
// SQLExec performs a modifying SQL query within the transaction.
// Such query does not return SQL result.
SQLExec(ctx context.Context, sql string, params map[string]interface{}) error
// SQLQuery performs a query (read-only) operation.
SQLQuery(ctx context.Context, sql string, params map[string]interface{}) (*schema.SQLQueryResult, error)
}
type tx struct {
ic *immuClient
transactionID string
}
func (c *tx) Commit(ctx context.Context) (*schema.CommittedSQLTx, error) {
cmtx, err := c.ic.ServiceClient.Commit(c.populateCtx(ctx), new(empty.Empty))
return cmtx, errors.FromError(err)
}
func (c *tx) Rollback(ctx context.Context) error {
_, err := c.ic.ServiceClient.Rollback(c.populateCtx(ctx), new(empty.Empty))
return errors.FromError(err)
}
func (c *immuClient) NewTx(ctx context.Context, opts ...TxOption) (Tx, error) {
if !c.IsConnected() {
return nil, errors.FromError(ErrNotConnected)
}
req := &schema.NewTxRequest{
Mode: schema.TxMode_ReadWrite,
}
for _, opt := range opts {
err := opt(req)
if err != nil {
return nil, err
}
}
r, err := c.ServiceClient.NewTx(ctx, req)
if err != nil {
return nil, errors.FromError(err)
}
tx := &tx{
ic: c,
transactionID: r.TransactionID,
}
return tx, nil
}
func (c *tx) SQLExec(ctx context.Context, sql string, params map[string]interface{}) error {
namedParams, err := schema.EncodeParams(params)
if err != nil {
return errors.FromError(err)
}
_, err = c.ic.ServiceClient.TxSQLExec(c.populateCtx(ctx), &schema.SQLExecRequest{
Sql: sql,
Params: namedParams,
})
return errors.FromError(err)
}
func (c *tx) SQLQuery(ctx context.Context, sql string, params map[string]interface{}) (*schema.SQLQueryResult, error) {
namedParams, err := schema.EncodeParams(params)
if err != nil {
return nil, errors.FromError(err)
}
res, err := c.ic.ServiceClient.TxSQLQuery(c.populateCtx(ctx), &schema.SQLQueryRequest{
Sql: sql,
Params: namedParams,
})
return res, errors.FromError(err)
}
func (c *tx) GetTransactionID() string {
return c.transactionID
}
func (tx *tx) populateCtx(ctx context.Context) context.Context {
if tx.GetTransactionID() != "" {
ctx = metadata.AppendToOutgoingContext(ctx, "transactionid", tx.GetTransactionID())
}
return ctx
}