Skip to content

Commit 70f3044

Browse files
committed
* Added query/ResultSet.{Columns,ColumnTypes} methods for get column names and types from query result set
1 parent e1c6de0 commit 70f3044

File tree

5 files changed

+113
-70
lines changed

5 files changed

+113
-70
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Added `query/ResultSet.{Columns,ColumnTypes}` methods for get column names and types from query result set
12
* Added experimental `retry.RetryWithResult` helper for retry lambda and return value from lambda
23

34
## v3.70.0

internal/query/result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func exactlyOneResultSetFromResult(ctx context.Context, r query.Result) (rs quer
315315
return nil, xerrors.WithStackTrace(err)
316316
}
317317

318-
return NewMaterializedResultSet(rows), nil
318+
return NewMaterializedResultSet(rs.Columns(), rs.ColumnTypes(), rows), nil
319319
}
320320

321321
func resultToMaterializedResult(ctx context.Context, r query.Result) (query.Result, error) {
@@ -345,7 +345,7 @@ func resultToMaterializedResult(ctx context.Context, r query.Result) (query.Resu
345345
rows = append(rows, row)
346346
}
347347

348-
resultSets = append(resultSets, NewMaterializedResultSet(rows))
348+
resultSets = append(resultSets, NewMaterializedResultSet(rs.Columns(), rs.ColumnTypes(), rows))
349349
}
350350

351351
return newMaterializedResult(resultSets), nil

internal/query/result_set.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
1010

1111
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
12+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
1213
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1314
"github.com/ydb-platform/ydb-go-sdk/v3/query"
1415
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
@@ -21,8 +22,10 @@ var (
2122

2223
type (
2324
materializedResultSet struct {
24-
rows []query.Row
25-
idx int
25+
columnNames []string
26+
columnTypes []types.Type
27+
rows []query.Row
28+
idx int
2629
}
2730
resultSet struct {
2831
index int64
@@ -35,6 +38,32 @@ type (
3538
}
3639
)
3740

41+
func (rs *materializedResultSet) Columns() (columnNames []string) {
42+
return rs.columnNames
43+
}
44+
45+
func (rs *materializedResultSet) ColumnTypes() []types.Type {
46+
return rs.columnTypes
47+
}
48+
49+
func (rs *resultSet) ColumnTypes() (columnTypes []types.Type) {
50+
columnTypes = make([]types.Type, len(rs.columns))
51+
for i := range rs.columns {
52+
columnTypes[i] = types.TypeFromYDB(rs.columns[i].GetType())
53+
}
54+
55+
return columnTypes
56+
}
57+
58+
func (rs *resultSet) Columns() (columnNames []string) {
59+
columnNames = make([]string, len(rs.columns))
60+
for i := range rs.columns {
61+
columnNames[i] = rs.columns[i].GetName()
62+
}
63+
64+
return columnNames
65+
}
66+
3867
func (rs *materializedResultSet) NextRow(ctx context.Context) (query.Row, error) {
3968
if rs.idx == len(rs.rows) {
4069
return nil, xerrors.WithStackTrace(io.EOF)
@@ -47,9 +76,15 @@ func (rs *materializedResultSet) NextRow(ctx context.Context) (query.Row, error)
4776
return rs.rows[rs.idx], nil
4877
}
4978

50-
func NewMaterializedResultSet(rows []query.Row) *materializedResultSet {
79+
func NewMaterializedResultSet(
80+
columnNames []string,
81+
columnTypes []types.Type,
82+
rows []query.Row,
83+
) *materializedResultSet {
5184
return &materializedResultSet{
52-
rows: rows,
85+
columnNames: columnNames,
86+
columnTypes: columnTypes,
87+
rows: rows,
5388
}
5489
}
5590

query/result.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
77
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/scanner"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
89
)
910

1011
type (
@@ -15,6 +16,8 @@ type (
1516
Err() error
1617
}
1718
ResultSet interface {
19+
Columns() []string
20+
ColumnTypes() []types.Type
1821
NextRow(ctx context.Context) (Row, error)
1922
}
2023
Row interface {

sugar/query_test.go

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -140,78 +140,82 @@ func TestUnmarshallResultSet(t *testing.T) {
140140
ID uint64 `sql:"id"`
141141
Str string `sql:"myStr"`
142142
}
143-
v, err := sugar.UnmarshallResultSet[myStruct](internalQuery.NewMaterializedResultSet([]query.Row{
144-
func() query.Row {
145-
row, err := internalQuery.NewRow(ctx, []*Ydb.Column{
146-
{
147-
Name: "id",
148-
Type: &Ydb.Type{
149-
Type: &Ydb.Type_TypeId{
150-
TypeId: Ydb.Type_UINT64,
143+
v, err := sugar.UnmarshallResultSet[myStruct](internalQuery.NewMaterializedResultSet(
144+
nil,
145+
nil,
146+
[]query.Row{
147+
func() query.Row {
148+
row, err := internalQuery.NewRow(ctx, []*Ydb.Column{
149+
{
150+
Name: "id",
151+
Type: &Ydb.Type{
152+
Type: &Ydb.Type_TypeId{
153+
TypeId: Ydb.Type_UINT64,
154+
},
151155
},
152156
},
153-
},
154-
{
155-
Name: "myStr",
156-
Type: &Ydb.Type{
157-
Type: &Ydb.Type_TypeId{
158-
TypeId: Ydb.Type_UTF8,
157+
{
158+
Name: "myStr",
159+
Type: &Ydb.Type{
160+
Type: &Ydb.Type_TypeId{
161+
TypeId: Ydb.Type_UTF8,
162+
},
159163
},
160164
},
161-
},
162-
}, &Ydb.Value{
163-
Items: []*Ydb.Value{{
164-
Value: &Ydb.Value_Uint64Value{
165-
Uint64Value: 123,
166-
},
167-
}, {
168-
Value: &Ydb.Value_TextValue{
169-
TextValue: "my string 1",
170-
},
171-
}},
172-
}, &trace.Query{})
173-
if err != nil {
174-
panic(err)
175-
}
176-
177-
return row
178-
}(),
179-
func() query.Row {
180-
row, err := internalQuery.NewRow(ctx, []*Ydb.Column{
181-
{
182-
Name: "id",
183-
Type: &Ydb.Type{
184-
Type: &Ydb.Type_TypeId{
185-
TypeId: Ydb.Type_UINT64,
165+
}, &Ydb.Value{
166+
Items: []*Ydb.Value{{
167+
Value: &Ydb.Value_Uint64Value{
168+
Uint64Value: 123,
186169
},
187-
},
188-
},
189-
{
190-
Name: "myStr",
191-
Type: &Ydb.Type{
192-
Type: &Ydb.Type_TypeId{
193-
TypeId: Ydb.Type_UTF8,
170+
}, {
171+
Value: &Ydb.Value_TextValue{
172+
TextValue: "my string 1",
173+
},
174+
}},
175+
}, &trace.Query{})
176+
if err != nil {
177+
panic(err)
178+
}
179+
180+
return row
181+
}(),
182+
func() query.Row {
183+
row, err := internalQuery.NewRow(ctx, []*Ydb.Column{
184+
{
185+
Name: "id",
186+
Type: &Ydb.Type{
187+
Type: &Ydb.Type_TypeId{
188+
TypeId: Ydb.Type_UINT64,
189+
},
194190
},
195191
},
196-
},
197-
}, &Ydb.Value{
198-
Items: []*Ydb.Value{{
199-
Value: &Ydb.Value_Uint64Value{
200-
Uint64Value: 456,
201-
},
202-
}, {
203-
Value: &Ydb.Value_TextValue{
204-
TextValue: "my string 2",
192+
{
193+
Name: "myStr",
194+
Type: &Ydb.Type{
195+
Type: &Ydb.Type_TypeId{
196+
TypeId: Ydb.Type_UTF8,
197+
},
198+
},
205199
},
206-
}},
207-
}, &trace.Query{})
208-
if err != nil {
209-
panic(err)
210-
}
211-
212-
return row
213-
}(),
214-
}))
200+
}, &Ydb.Value{
201+
Items: []*Ydb.Value{{
202+
Value: &Ydb.Value_Uint64Value{
203+
Uint64Value: 456,
204+
},
205+
}, {
206+
Value: &Ydb.Value_TextValue{
207+
TextValue: "my string 2",
208+
},
209+
}},
210+
}, &trace.Query{})
211+
if err != nil {
212+
panic(err)
213+
}
214+
215+
return row
216+
}(),
217+
},
218+
))
215219
require.NoError(t, err)
216220
require.Len(t, v, 2)
217221
require.EqualValues(t, 123, v[0].ID)

0 commit comments

Comments
 (0)