@@ -9,26 +9,25 @@ namespace NYT::NQueryClient {
9
9
10
10
// //////////////////////////////////////////////////////////////////////////////
11
11
12
- static std::vector<TString> Parenthesize (std::vector<TString> strings )
12
+ static void Parenthesize (TStringBuilderBase* builder, const std::string& str )
13
13
{
14
- for (auto & string : strings) {
15
- string.prepend (' (' ).append (' )' );
16
- }
17
- return strings;
14
+ builder->AppendChar (' (' );
15
+ builder->AppendString (str);
16
+ builder->AppendChar (' )' );
18
17
}
19
18
20
- void TQueryBuilder::SetSource (TString source)
19
+ void TQueryBuilder::SetSource (std::string source)
21
20
{
22
21
Source_ = std::move (source);
23
22
}
24
23
25
- void TQueryBuilder::SetSource (TString source, TString alias)
24
+ void TQueryBuilder::SetSource (std::string source, std::string alias)
26
25
{
27
26
Source_ = std::move (source);
28
27
SourceAlias_ = std::move (alias);
29
28
}
30
29
31
- int TQueryBuilder::AddSelectExpression (TString expression)
30
+ int TQueryBuilder::AddSelectExpression (std::string expression)
32
31
{
33
32
SelectEntries_.push_back (TEntryWithAlias{
34
33
std::move (expression),
@@ -37,7 +36,7 @@ int TQueryBuilder::AddSelectExpression(TString expression)
37
36
return SelectEntries_.size () - 1 ;
38
37
}
39
38
40
- int TQueryBuilder::AddSelectExpression (TString expression, TString alias)
39
+ int TQueryBuilder::AddSelectExpression (std::string expression, std::string alias)
41
40
{
42
41
SelectEntries_.push_back (TEntryWithAlias{
43
42
std::move (expression),
@@ -46,20 +45,20 @@ int TQueryBuilder::AddSelectExpression(TString expression, TString alias)
46
45
return SelectEntries_.size () - 1 ;
47
46
}
48
47
49
- void TQueryBuilder::AddWhereConjunct (TString expression)
48
+ void TQueryBuilder::AddWhereConjunct (std::string expression)
50
49
{
51
50
WhereConjuncts_.push_back (std::move (expression));
52
51
}
53
52
54
- void TQueryBuilder::AddGroupByExpression (TString expression)
53
+ void TQueryBuilder::AddGroupByExpression (std::string expression)
55
54
{
56
55
GroupByEntries_.push_back (TEntryWithAlias{
57
56
std::move (expression),
58
57
std::nullopt
59
58
});
60
59
}
61
60
62
- void TQueryBuilder::AddGroupByExpression (TString expression, TString alias)
61
+ void TQueryBuilder::AddGroupByExpression (std::string expression, std::string alias)
63
62
{
64
63
GroupByEntries_.push_back (TEntryWithAlias{
65
64
std::move (expression),
@@ -72,33 +71,33 @@ void TQueryBuilder::SetWithTotals(EWithTotalsMode withTotalsMode)
72
71
WithTotalsMode_ = withTotalsMode;
73
72
}
74
73
75
- void TQueryBuilder::AddHavingConjunct (TString expression)
74
+ void TQueryBuilder::AddHavingConjunct (std::string expression)
76
75
{
77
76
HavingConjuncts_.push_back (std::move (expression));
78
77
}
79
78
80
- void TQueryBuilder::AddOrderByExpression (TString expression)
79
+ void TQueryBuilder::AddOrderByExpression (std::string expression)
81
80
{
82
81
OrderByEntries_.push_back (TOrderByEntry{
83
82
std::move (expression),
84
83
std::nullopt,
85
84
});
86
85
}
87
86
88
- void TQueryBuilder::AddOrderByExpression (TString expression, std::optional<EOrderByDirection> direction)
87
+ void TQueryBuilder::AddOrderByExpression (std::string expression, std::optional<EOrderByDirection> direction)
89
88
{
90
89
OrderByEntries_.push_back (TOrderByEntry{
91
90
std::move (expression),
92
91
direction,
93
92
});
94
93
}
95
94
96
- void TQueryBuilder::AddOrderByAscendingExpression (TString expression)
95
+ void TQueryBuilder::AddOrderByAscendingExpression (std::string expression)
97
96
{
98
97
AddOrderByExpression (std::move (expression), EOrderByDirection::Ascending);
99
98
}
100
99
101
- void TQueryBuilder::AddOrderByDescendingExpression (TString expression)
100
+ void TQueryBuilder::AddOrderByDescendingExpression (std::string expression)
102
101
{
103
102
AddOrderByExpression (std::move (expression), EOrderByDirection::Descending);
104
103
}
@@ -114,9 +113,9 @@ void TQueryBuilder::SetLimit(i64 limit)
114
113
}
115
114
116
115
void TQueryBuilder::AddJoinExpression (
117
- TString table,
118
- TString alias,
119
- TString onExpression,
116
+ std::string table,
117
+ std::string alias,
118
+ std::string onExpression,
120
119
ETableJoinType type)
121
120
{
122
121
JoinEntries_.push_back (TJoinEntry{
@@ -127,94 +126,98 @@ void TQueryBuilder::AddJoinExpression(
127
126
});
128
127
}
129
128
130
- TString TQueryBuilder::Build ()
129
+ std::string TQueryBuilder::Build ()
131
130
{
132
- std::vector<TString> parts ;
133
- parts. reserve ( 8 );
131
+ TStringBuilder builder ;
132
+ TDelimitedStringBuilderWrapper wrapper (&builder, " " );
134
133
135
134
if (SelectEntries_.empty ()) {
136
135
THROW_ERROR_EXCEPTION (" Query must have at least one SELECT expression" );
137
136
}
138
- parts. push_back ( JoinSeq ( " , " , SelectEntries_) );
137
+ JoinToString (&wrapper, SelectEntries_. begin () , SelectEntries_. end (), &FormatEntryWithAlias );
139
138
140
139
if (!Source_) {
141
140
THROW_ERROR_EXCEPTION (" Source must be specified in query" );
142
141
}
143
142
if (!SourceAlias_) {
144
- parts. push_back ( Format ( " FROM [%v]" , *Source_) );
143
+ wrapper-> AppendFormat ( " FROM [%v]" , *Source_);
145
144
} else {
146
- parts. push_back ( Format ( " FROM [%v] AS %v" , *Source_, *SourceAlias_) );
145
+ wrapper-> AppendFormat ( " FROM [%v] AS %v" , *Source_, *SourceAlias_);
147
146
}
148
147
149
148
for (const auto & join : JoinEntries_) {
150
149
TStringBuf joinType = join.Type == ETableJoinType::Inner ? " JOIN" : " LEFT JOIN" ;
151
- parts. push_back ( Format ( " %v [%v] AS [%v] ON %v" , joinType, join.Table , join.Alias , join.OnExpression ) );
150
+ wrapper-> AppendFormat ( " %v [%v] AS [%v] ON %v" , joinType, join.Table , join.Alias , join.OnExpression );
152
151
}
153
152
154
153
if (!WhereConjuncts_.empty ()) {
155
- parts. push_back (" WHERE" );
156
- parts. push_back ( JoinSeq ( " AND " , Parenthesize (WhereConjuncts_)) );
154
+ wrapper-> AppendFormat (" WHERE" );
155
+ JoinToString (&wrapper, WhereConjuncts_. begin (), WhereConjuncts_. end (), &Parenthesize, " AND " );
157
156
}
158
157
159
158
if (!GroupByEntries_.empty ()) {
160
- parts. push_back (" GROUP BY" );
161
- parts. push_back ( JoinSeq ( " , " , GroupByEntries_) );
159
+ wrapper-> AppendString (" GROUP BY" );
160
+ JoinToString (&wrapper, GroupByEntries_. begin () , GroupByEntries_. end (), &FormatEntryWithAlias );
162
161
}
163
162
164
163
if (WithTotalsMode_ == EWithTotalsMode::BeforeHaving) {
165
- parts. push_back (" WITH TOTALS" );
164
+ wrapper-> AppendString (" WITH TOTALS" );
166
165
}
167
166
168
167
if (!HavingConjuncts_.empty ()) {
169
168
if (GroupByEntries_.empty ()) {
170
169
THROW_ERROR_EXCEPTION (" Having without group by is not valid" );
171
170
}
172
- parts. push_back (" HAVING" );
173
- parts. push_back ( JoinSeq ( " AND " , Parenthesize (HavingConjuncts_)) );
171
+ wrapper-> AppendString (" HAVING" );
172
+ JoinToString (&wrapper, HavingConjuncts_. begin (), HavingConjuncts_. end (), &Parenthesize, " AND " );
174
173
}
175
174
176
175
if (WithTotalsMode_ == EWithTotalsMode::AfterHaving) {
177
- parts. push_back (" WITH TOTALS" );
176
+ wrapper-> AppendString (" WITH TOTALS" );
178
177
}
179
178
180
179
if (!OrderByEntries_.empty ()) {
181
- parts. push_back (" ORDER BY" );
182
- parts. push_back ( JoinSeq ( " , " , OrderByEntries_) );
180
+ wrapper-> AppendString (" ORDER BY" );
181
+ JoinToString (&wrapper, OrderByEntries_. begin () , OrderByEntries_. end (), &FormatOrderByEntry );
183
182
}
184
183
185
184
if (Offset_) {
186
- parts. push_back ( Format ( " OFFSET %v" , *Offset_) );
185
+ wrapper-> AppendFormat ( " OFFSET %v" , *Offset_);
187
186
}
188
187
189
188
if (Limit_) {
190
- parts. push_back ( Format ( " LIMIT %v" , *Limit_) );
189
+ wrapper-> AppendFormat ( " LIMIT %v" , *Limit_);
191
190
}
192
191
193
- return JoinSeq ( " " , parts );
192
+ return builder. Flush ( );
194
193
}
195
194
196
- void AppendToString (TString& dst , const TQueryBuilder::TEntryWithAlias& entry)
195
+ void TQueryBuilder::FormatEntryWithAlias (TStringBuilderBase* builder , const TQueryBuilder::TEntryWithAlias& entry)
197
196
{
198
- TStringOutput output (dst);
199
197
if (entry.Expression == " *" ) {
200
- output << " * " ;
198
+ builder-> AppendChar ( ' * ' ) ;
201
199
return ;
202
200
}
203
- output << ' (' << entry.Expression << ' )' ;
201
+ builder->AppendChar (' (' );
202
+ builder->AppendString (entry.Expression );
203
+ builder->AppendChar (' )' );
204
204
if (entry.Alias ) {
205
- output << " AS " << *entry.Alias ;
205
+ builder->AppendString (" AS " );
206
+ builder->AppendString (*entry.Alias );
206
207
}
207
208
}
208
209
209
- void AppendToString (TString& dst , const TQueryBuilder::TOrderByEntry& entry)
210
+ void TQueryBuilder::FormatOrderByEntry (TStringBuilderBase* builder , const TQueryBuilder::TOrderByEntry& entry)
210
211
{
211
- TStringOutput output (dst);
212
- output << ' (' << entry.Expression << ' )' ;
212
+ builder->AppendChar (' (' );
213
+ builder->AppendString (entry.Expression );
214
+ builder->AppendChar (' )' );
213
215
if (entry.Direction ) {
214
216
TStringBuf directionString = (*entry.Direction == EOrderByDirection::Ascending)
215
217
? " ASC"
216
218
: " DESC" ;
217
- output << ' ' << directionString;
219
+ builder->AppendChar (' ' );
220
+ builder->AppendString (directionString);
218
221
}
219
222
}
220
223
0 commit comments