Skip to content

Commit 4b15965

Browse files
authored
Merge pull request #338 from ydb-platform/stmt
added test for Operation error ABORTED (Transaction locks invalidated)
2 parents 03cac2d + a4bdae2 commit 4b15965

File tree

6 files changed

+66
-39
lines changed

6 files changed

+66
-39
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ jobs:
116116
name: connection
117117
- name: Test database/sql
118118
run: go test -race -coverpkg=./... -coverprofile database_sql.txt -covermode atomic ./sql_e2e_test.go
119-
- name: Upload coverage to Codecov
119+
- name: Upload database/sql coverage report to Codecov
120120
uses: codecov/codecov-action@v2
121121
with:
122122
file: ./database_sql.txt

internal/xsql/conn.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
119119
}
120120
return &stmt{
121121
conn: c,
122+
tx: c.currentTx,
122123
params: internal.Params(s),
123124
query: query,
124125
}, nil
@@ -163,7 +164,12 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
163164
}
164165
switch m {
165166
case DataQueryMode:
166-
_, res, err := c.session.Execute(ctx, txControl(ctx, c.defaultTxControl), query, toQueryParams(args))
167+
_, res, err := c.session.Execute(ctx,
168+
txControl(ctx, c.defaultTxControl),
169+
query,
170+
toQueryParams(args),
171+
dataQueryOptions(ctx)...,
172+
)
167173
if err != nil {
168174
return nil, c.checkClosed(err)
169175
}
@@ -172,7 +178,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
172178
}
173179
return c, nil
174180
case SchemeQueryMode:
175-
err := c.session.ExecuteSchemeQuery(ctx, query, toSchemeOptions(args)...)
181+
err := c.session.ExecuteSchemeQuery(ctx, query)
176182
if err != nil {
177183
return nil, c.checkClosed(err)
178184
}
@@ -203,7 +209,12 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
203209
}
204210
switch m {
205211
case DataQueryMode:
206-
_, res, err := c.session.Execute(ctx, txControl(ctx, c.defaultTxControl), query, toQueryParams(args))
212+
_, res, err := c.session.Execute(ctx,
213+
txControl(ctx, c.defaultTxControl),
214+
query,
215+
toQueryParams(args),
216+
dataQueryOptions(ctx)...,
217+
)
207218
if err != nil {
208219
return nil, c.checkClosed(err)
209220
}
@@ -214,7 +225,11 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
214225
result: res,
215226
}, nil
216227
case ScanQueryMode:
217-
res, err := c.session.StreamExecuteScanQuery(ctx, query, toQueryParams(args), scanQueryOptions(ctx)...)
228+
res, err := c.session.StreamExecuteScanQuery(ctx,
229+
query,
230+
toQueryParams(args),
231+
scanQueryOptions(ctx)...,
232+
)
218233
if err != nil {
219234
return nil, c.checkClosed(err)
220235
}

internal/xsql/context.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ func txControl(ctx context.Context, defaultTxControl *table.TransactionControl)
3838
return defaultTxControl
3939
}
4040

41-
func WithScanQueryOptions(ctx context.Context, opts []options.ExecuteScanQueryOption) context.Context {
42-
return context.WithValue(ctx, ctxScanQueryOptionsKey{}, append(scanQueryOptions(ctx), opts...))
41+
func WithScanQueryOptions(ctx context.Context, opts ...options.ExecuteScanQueryOption) context.Context {
42+
return context.WithValue(ctx,
43+
ctxScanQueryOptionsKey{},
44+
append(
45+
append([]options.ExecuteScanQueryOption{}, scanQueryOptions(ctx)...),
46+
opts...,
47+
),
48+
)
4349
}
4450

4551
func scanQueryOptions(ctx context.Context) []options.ExecuteScanQueryOption {
@@ -49,8 +55,14 @@ func scanQueryOptions(ctx context.Context) []options.ExecuteScanQueryOption {
4955
return nil
5056
}
5157

52-
func WithDataQueryOptions(ctx context.Context, opts []options.ExecuteDataQueryOption) context.Context {
53-
return context.WithValue(ctx, ctxDataQueryOptionsKey{}, append(dataQueryOptions(ctx), opts...))
58+
func WithDataQueryOptions(ctx context.Context, opts ...options.ExecuteDataQueryOption) context.Context {
59+
return context.WithValue(ctx,
60+
ctxDataQueryOptionsKey{},
61+
append(
62+
append([]options.ExecuteDataQueryOption{}, dataQueryOptions(ctx)...),
63+
opts...,
64+
),
65+
)
5466
}
5567

5668
func dataQueryOptions(ctx context.Context) []options.ExecuteDataQueryOption {
@@ -59,3 +71,7 @@ func dataQueryOptions(ctx context.Context) []options.ExecuteDataQueryOption {
5971
}
6072
return nil
6173
}
74+
75+
func withKeepInCache(ctx context.Context) context.Context {
76+
return WithDataQueryOptions(ctx, options.WithKeepInCache(true))
77+
}

internal/xsql/stmt.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77

88
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
99

10-
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
10+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1111
)
1212

1313
type stmt struct {
1414
nopResult
1515
namedValueChecker
1616

1717
conn *conn
18+
tx *tx
1819
params map[string]*Ydb.Type
1920
query string
2021
}
@@ -31,19 +32,22 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv
3132
if s.conn.isClosed() {
3233
return nil, errClosedConn
3334
}
34-
switch m := queryModeFromContext(ctx, s.conn.defaultQueryMode); m {
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 {
3545
case DataQueryMode:
3646
_, res, err := s.conn.session.Execute(ctx,
3747
txControl(ctx, s.conn.defaultTxControl),
3848
s.query,
3949
toQueryParams(args),
40-
append(
41-
append(
42-
[]options.ExecuteDataQueryOption{},
43-
dataQueryOptions(ctx)...,
44-
),
45-
options.WithKeepInCache(true),
46-
)...,
50+
dataQueryOptions(withKeepInCache(ctx))...,
4751
)
4852
if err != nil {
4953
return nil, s.conn.checkClosed(err)
@@ -63,19 +67,22 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
6367
if s.conn.isClosed() {
6468
return nil, errClosedConn
6569
}
66-
switch m := queryModeFromContext(ctx, s.conn.defaultQueryMode); m {
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 {
6780
case DataQueryMode:
6881
_, res, err := s.conn.session.Execute(ctx,
6982
txControl(ctx, s.conn.defaultTxControl),
7083
s.query,
7184
toQueryParams(args),
72-
append(
73-
append(
74-
[]options.ExecuteDataQueryOption{},
75-
dataQueryOptions(ctx)...,
76-
),
77-
options.WithKeepInCache(true),
78-
)...,
85+
dataQueryOptions(withKeepInCache(ctx))...,
7986
)
8087
if err != nil {
8188
return nil, s.conn.checkClosed(err)

internal/xsql/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (tx *tx) QueryContext(ctx context.Context, query string, args []driver.Name
2929
if tx.conn.isClosed() {
3030
return nil, errClosedConn
3131
}
32-
res, err := tx.transaction.Execute(ctx, query, toQueryParams(args))
32+
res, err := tx.transaction.Execute(ctx, query, toQueryParams(args), dataQueryOptions(ctx)...)
3333
if err != nil {
3434
return nil, tx.conn.checkClosed(err)
3535
}
@@ -45,7 +45,7 @@ func (tx *tx) ExecContext(ctx context.Context, query string, args []driver.Named
4545
if tx.conn.isClosed() {
4646
return nil, errClosedConn
4747
}
48-
res, err := tx.transaction.Execute(ctx, query, toQueryParams(args))
48+
res, err := tx.transaction.Execute(ctx, query, toQueryParams(args), dataQueryOptions(ctx)...)
4949
if err != nil {
5050
return nil, tx.conn.checkClosed(err)
5151
}

internal/xsql/xsql.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"database/sql/driver"
55

66
"github.com/ydb-platform/ydb-go-sdk/v3/table"
7-
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
87
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
98
)
109

@@ -21,13 +20,3 @@ func toQueryParams(values []driver.NamedValue) *table.QueryParameters {
2120
}
2221
return table.NewQueryParameters(opts...)
2322
}
24-
25-
func toSchemeOptions(values []driver.NamedValue) (opts []options.ExecuteSchemeQueryOption) {
26-
if len(values) == 0 {
27-
return nil
28-
}
29-
for _, arg := range values {
30-
opts = append(opts, arg.Value.(options.ExecuteSchemeQueryOption))
31-
}
32-
return opts
33-
}

0 commit comments

Comments
 (0)