@@ -976,79 +976,124 @@ func WithExecuteScanQueryStats(stats ExecuteScanQueryStatsType) ExecuteScanQuery
976976 })
977977}
978978
979- // Read table options
979+ var (
980+ _ ReadTableOption = readOrderedOption {}
981+ _ ReadTableOption = readKeyRangeOption {}
982+ _ ReadTableOption = readGreaterOrEqualOption {}
983+ _ ReadTableOption = readLessOrEqualOption {}
984+ _ ReadTableOption = readLessOption {}
985+ _ ReadTableOption = readGreaterOption {}
986+ _ ReadTableOption = readRowLimitOption (0 )
987+ )
988+
980989type (
981990 ReadTableDesc Ydb_Table.ReadTableRequest
982- ReadTableOption func (* ReadTableDesc , * allocator.Allocator )
991+ ReadTableOption interface {
992+ ApplyReadTableOption (* ReadTableDesc , * allocator.Allocator )
993+ }
994+
995+ readColumnsOption []string
996+ readOrderedOption struct {}
997+ readKeyRangeOption KeyRange
998+ readGreaterOrEqualOption struct { types.Value }
999+ readLessOrEqualOption struct { types.Value }
1000+ readLessOption struct { types.Value }
1001+ readGreaterOption struct { types.Value }
1002+ readRowLimitOption uint64
9831003)
9841004
985- func ReadColumn (name string ) ReadTableOption {
986- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
987- desc .Columns = append (desc .Columns , name )
1005+ func (n readRowLimitOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1006+ desc .RowLimit = uint64 (n )
1007+ }
1008+
1009+ func (x readGreaterOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1010+ desc .initKeyRange ()
1011+ desc .KeyRange .FromBound = & Ydb_Table.KeyRange_Greater {
1012+ Greater : value .ToYDB (x , a ),
1013+ }
1014+ }
1015+
1016+ func (x readLessOrEqualOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1017+ desc .initKeyRange ()
1018+ desc .KeyRange .ToBound = & Ydb_Table.KeyRange_LessOrEqual {
1019+ LessOrEqual : value .ToYDB (x , a ),
1020+ }
1021+ }
1022+
1023+ func (x readLessOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1024+ desc .initKeyRange ()
1025+ desc .KeyRange .ToBound = & Ydb_Table.KeyRange_Less {
1026+ Less : value .ToYDB (x , a ),
1027+ }
1028+ }
1029+
1030+ func (columns readColumnsOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1031+ desc .Columns = append (desc .Columns , columns ... )
1032+ }
1033+
1034+ func (r readOrderedOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1035+ desc .Ordered = true
1036+ }
1037+
1038+ func (x readKeyRangeOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1039+ desc .initKeyRange ()
1040+ if x .From != nil {
1041+ desc .KeyRange .FromBound = & Ydb_Table.KeyRange_GreaterOrEqual {
1042+ GreaterOrEqual : value .ToYDB (x .From , a ),
1043+ }
1044+ }
1045+ if x .To != nil {
1046+ desc .KeyRange .ToBound = & Ydb_Table.KeyRange_Less {
1047+ Less : value .ToYDB (x .To , a ),
1048+ }
9881049 }
9891050}
9901051
991- func ReadOrdered () ReadTableOption {
992- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
993- desc .Ordered = true
1052+ func (x readGreaterOrEqualOption ) ApplyReadTableOption (desc * ReadTableDesc , a * allocator.Allocator ) {
1053+ desc .initKeyRange ()
1054+ desc .KeyRange .FromBound = & Ydb_Table.KeyRange_GreaterOrEqual {
1055+ GreaterOrEqual : value .ToYDB (x , a ),
9941056 }
9951057}
9961058
1059+ func ReadColumn (name string ) readColumnsOption {
1060+ return []string {name }
1061+ }
1062+
1063+ func ReadColumns (names ... string ) readColumnsOption {
1064+ return names
1065+ }
1066+
1067+ func ReadOrdered () readOrderedOption {
1068+ return readOrderedOption {}
1069+ }
1070+
9971071// ReadKeyRange returns ReadTableOption which makes ReadTable read values
9981072// in range [x.From, x.To).
9991073//
10001074// Both x.From and x.To may be nil.
10011075func ReadKeyRange (x KeyRange ) ReadTableOption {
1002- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
1003- if x .From != nil {
1004- ReadGreaterOrEqual (x .From )(desc , a )
1005- }
1006- if x .To != nil {
1007- ReadLess (x .To )(desc , a )
1008- }
1009- }
1076+ return readKeyRangeOption (x )
10101077}
10111078
10121079func ReadGreater (x types.Value ) ReadTableOption {
1013- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
1014- desc .initKeyRange ()
1015- desc .KeyRange .FromBound = & Ydb_Table.KeyRange_Greater {
1016- Greater : value .ToYDB (x , a ),
1017- }
1018- }
1080+ return readGreaterOption {x }
10191081}
10201082
10211083func ReadGreaterOrEqual (x types.Value ) ReadTableOption {
1022- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
1023- desc .initKeyRange ()
1024- desc .KeyRange .FromBound = & Ydb_Table.KeyRange_GreaterOrEqual {
1025- GreaterOrEqual : value .ToYDB (x , a ),
1026- }
1027- }
1084+ return readGreaterOrEqualOption {x }
10281085}
10291086
10301087func ReadLess (x types.Value ) ReadTableOption {
1031- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
1032- desc .initKeyRange ()
1033- desc .KeyRange .ToBound = & Ydb_Table.KeyRange_Less {
1034- Less : value .ToYDB (x , a ),
1035- }
1036- }
1088+ return readLessOption {x }
10371089}
10381090
10391091func ReadLessOrEqual (x types.Value ) ReadTableOption {
1040- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
1041- desc .initKeyRange ()
1042- desc .KeyRange .ToBound = & Ydb_Table.KeyRange_LessOrEqual {
1043- LessOrEqual : value .ToYDB (x , a ),
1044- }
1045- }
1092+ return readLessOrEqualOption {x }
10461093}
10471094
10481095func ReadRowLimit (n uint64 ) ReadTableOption {
1049- return func (desc * ReadTableDesc , a * allocator.Allocator ) {
1050- desc .RowLimit = n
1051- }
1096+ return readRowLimitOption (n )
10521097}
10531098
10541099func (d * ReadTableDesc ) initKeyRange () {
0 commit comments