Skip to content

Commit adacfb2

Browse files
authored
Merge pull request #339 from ydb-platform/rm-tx
make conn as tx
2 parents 4b15965 + 040e59a commit adacfb2

File tree

3 files changed

+56
-151
lines changed

3 files changed

+56
-151
lines changed

internal/xsql/conn.go

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,35 @@ type conn struct {
5555
dataOpts []options.ExecuteDataQueryOption
5656
scanOpts []options.ExecuteScanQueryOption
5757

58-
currentTx *tx
58+
transaction table.Transaction
59+
}
60+
61+
func (c *conn) Commit() error {
62+
if c.isClosed() {
63+
return errClosedConn
64+
}
65+
defer func() {
66+
c.transaction = nil
67+
}()
68+
_, err := c.transaction.CommitTx(context.Background())
69+
if err != nil {
70+
return c.checkClosed(err)
71+
}
72+
return nil
73+
}
74+
75+
func (c *conn) Rollback() error {
76+
if c.isClosed() {
77+
return errClosedConn
78+
}
79+
defer func() {
80+
c.transaction = nil
81+
}()
82+
err := c.transaction.Rollback(context.Background())
83+
if err != nil {
84+
return c.checkClosed(err)
85+
}
86+
return err
5987
}
6088

6189
var (
@@ -119,7 +147,6 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
119147
}
120148
return &stmt{
121149
conn: c,
122-
tx: c.currentTx,
123150
params: internal.Params(s),
124151
query: query,
125152
}, nil
@@ -129,38 +156,38 @@ func (c *conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (driver.
129156
if c.isClosed() {
130157
return nil, errClosedConn
131158
}
132-
if c.currentTx != nil {
159+
if c.transaction != nil {
133160
return nil, xerrors.WithStackTrace(
134-
fmt.Errorf("conn already have an opened tx: %s", c.currentTx.transaction.ID()),
161+
fmt.Errorf("conn already have an opened tx: %s", c.transaction.ID()),
135162
)
136163
}
137164
txSettings, err := isolation.ToYDB(txOptions)
138165
if err != nil {
139166
return nil, xerrors.WithStackTrace(err)
140167
}
141-
t, err := c.session.BeginTransaction(ctx, txSettings)
168+
c.transaction, err = c.session.BeginTransaction(ctx, txSettings)
142169
if err != nil {
143170
return nil, c.checkClosed(err)
144171
}
145-
c.currentTx = &tx{
146-
conn: c,
147-
transaction: t,
148-
}
149-
return c.currentTx, nil
172+
return c, nil
150173
}
151174

152175
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
153176
if c.isClosed() {
154177
return nil, errClosedConn
155178
}
156179
m := queryModeFromContext(ctx, c.defaultQueryMode)
157-
if c.currentTx != nil {
180+
if c.transaction != nil {
158181
if m != DataQueryMode {
159182
return nil, xerrors.WithStackTrace(
160183
fmt.Errorf("query mode `%s` not supported with transaction", m.String()),
161184
)
162185
}
163-
return c.currentTx.ExecContext(ctx, query, args)
186+
_, err := c.transaction.Execute(ctx, query, toQueryParams(args), dataQueryOptions(ctx)...)
187+
if err != nil {
188+
return nil, c.checkClosed(err)
189+
}
190+
return c, nil
164191
}
165192
switch m {
166193
case DataQueryMode:
@@ -199,13 +226,22 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
199226
return nil, errClosedConn
200227
}
201228
m := queryModeFromContext(ctx, c.defaultQueryMode)
202-
if c.currentTx != nil {
229+
if c.transaction != nil {
203230
if m != DataQueryMode {
204231
return nil, xerrors.WithStackTrace(
205232
fmt.Errorf("query mode `%s` not supported with transaction", m.String()),
206233
)
207234
}
208-
return c.currentTx.QueryContext(ctx, query, args)
235+
res, err := c.transaction.Execute(ctx, query, toQueryParams(args), dataQueryOptions(ctx)...)
236+
if err != nil {
237+
return nil, c.checkClosed(err)
238+
}
239+
if err = res.Err(); err != nil {
240+
return nil, c.checkClosed(err)
241+
}
242+
return &rows{
243+
result: res,
244+
}, nil
209245
}
210246
switch m {
211247
case DataQueryMode:

internal/xsql/stmt.go

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import (
66
"fmt"
77

88
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
9-
10-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
119
)
1210

1311
type stmt struct {
1412
nopResult
1513
namedValueChecker
1614

1715
conn *conn
18-
tx *tx
1916
params map[string]*Ydb.Type
2017
query string
2118
}
@@ -32,67 +29,23 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv
3229
if s.conn.isClosed() {
3330
return nil, errClosedConn
3431
}
35-
m := queryModeFromContext(ctx, s.conn.defaultQueryMode)
36-
if s.tx != nil {
37-
if m != DataQueryMode {
38-
return nil, xerrors.WithStackTrace(
39-
fmt.Errorf("query mode `%s` not supported with prepared statement", m.String()),
40-
)
41-
}
42-
return s.tx.QueryContext(withKeepInCache(ctx), s.query, args)
43-
}
44-
switch m {
32+
switch m := queryModeFromContext(withKeepInCache(ctx), s.conn.defaultQueryMode); m {
4533
case DataQueryMode:
46-
_, res, err := s.conn.session.Execute(ctx,
47-
txControl(ctx, s.conn.defaultTxControl),
48-
s.query,
49-
toQueryParams(args),
50-
dataQueryOptions(withKeepInCache(ctx))...,
51-
)
52-
if err != nil {
53-
return nil, s.conn.checkClosed(err)
54-
}
55-
if err = res.Err(); err != nil {
56-
return nil, s.conn.checkClosed(res.Err())
57-
}
58-
return &rows{
59-
result: res,
60-
}, nil
34+
return s.conn.QueryContext(ctx, s.query, args)
6135
default:
62-
return nil, fmt.Errorf("unsupported query mode '%s' for execute statement query", m)
36+
return nil, fmt.Errorf("unsupported query mode '%s' for execute query on prepared statement", m)
6337
}
6438
}
6539

6640
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
6741
if s.conn.isClosed() {
6842
return nil, errClosedConn
6943
}
70-
m := queryModeFromContext(ctx, s.conn.defaultQueryMode)
71-
if s.tx != nil {
72-
if m != DataQueryMode {
73-
return nil, xerrors.WithStackTrace(
74-
fmt.Errorf("query mode `%s` not supported with prepared statement", m.String()),
75-
)
76-
}
77-
return s.tx.ExecContext(withKeepInCache(ctx), s.query, args)
78-
}
79-
switch m {
44+
switch m := queryModeFromContext(withKeepInCache(ctx), s.conn.defaultQueryMode); m {
8045
case DataQueryMode:
81-
_, res, err := s.conn.session.Execute(ctx,
82-
txControl(ctx, s.conn.defaultTxControl),
83-
s.query,
84-
toQueryParams(args),
85-
dataQueryOptions(withKeepInCache(ctx))...,
86-
)
87-
if err != nil {
88-
return nil, s.conn.checkClosed(err)
89-
}
90-
if err = res.Err(); err != nil {
91-
return nil, s.conn.checkClosed(res.Err())
92-
}
93-
return s, nil
46+
return s.conn.ExecContext(ctx, s.query, args)
9447
default:
95-
return nil, fmt.Errorf("unsupported query mode '%s' for execute query", m)
48+
return nil, fmt.Errorf("unsupported query mode '%s' for execute query on prepared statement", m)
9649
}
9750
}
9851

internal/xsql/tx.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)