-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathSettingsFields.h
More file actions
726 lines (586 loc) · 25.3 KB
/
SettingsFields.h
File metadata and controls
726 lines (586 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#pragma once
#include <Core/Field.h>
#include <Core/MultiEnum.h>
#include <base/types.h>
#include <Poco/Timespan.h>
#include <Poco/URI.h>
#include <string_view>
namespace DB
{
class ReadBuffer;
class WriteBuffer;
/** One setting for any type.
* Stores a value within itself, as well as a flag - whether the value was changed.
* This is done so that you can send to the remote servers only changed settings (or explicitly specified in the config) values.
* That is, if the configuration was not specified in the config and was not dynamically changed, it is not sent to the remote server,
* and the remote server will use its default value.
*/
struct SettingFieldBase
{
virtual ~SettingFieldBase() = default;
virtual SettingFieldBase & operator=(const Field & f) = 0;
virtual bool isChanged() const = 0;
virtual void setChanged(bool changed_) = 0;
virtual explicit operator Field() const = 0;
virtual String toString() const = 0;
virtual void parseFromString(const String & str) = 0;
virtual void writeBinary(WriteBuffer & out) const = 0;
virtual void readBinary(ReadBuffer & in) = 0;
};
template <typename T>
struct SettingFieldNumber : SettingFieldBase
{
using Type = T;
using ValueType = T;
Type value;
bool changed = false;
explicit SettingFieldNumber(Type x = 0);
explicit SettingFieldNumber(const Field & f);
SettingFieldNumber(const SettingFieldNumber & o)
: value(o.value), changed(o.changed)
{}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
virtual SettingFieldNumber & operator=(Type x);
SettingFieldNumber & operator=(const Field & f) override;
SettingFieldNumber & operator=(const SettingFieldNumber & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
operator Type() const { return value; } /// NOLINT
explicit operator Field() const override { return value; }
String toString() const override;
void parseFromString(const String & str) override;
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
using SettingFieldUInt64 = SettingFieldNumber<UInt64>;
using SettingFieldInt64 = SettingFieldNumber<Int64>;
using SettingFieldUInt32 = SettingFieldNumber<UInt32>;
using SettingFieldInt32 = SettingFieldNumber<Int32>;
using SettingFieldFloat = SettingFieldNumber<float>;
using SettingFieldDouble = SettingFieldNumber<double>;
using SettingFieldBool = SettingFieldNumber<bool>;
/** Wraps any SettingField to support special value 'auto' that can be checked with `is_auto` flag.
* Note about serialization:
* The new versions with `SettingsWriteFormat::STRINGS_WITH_FLAGS` serialize values as a string.
* In legacy SettingsWriteFormat mode, functions `read/writeBinary` would serialize values as a binary, and 'is_auto' would be ignored.
* It's possible to upgrade settings from regular type to wrapped ones and keep compatibility with old versions,
* but when serializing 'auto' old version will see binary representation of the default value.
*/
template <typename Base>
struct SettingAutoWrapper final : SettingFieldBase
{
constexpr static auto keyword = "auto";
static bool isAuto(const Field & f) { return f.getType() == Field::Types::String && f.safeGet<String>() == keyword; }
static bool isAuto(const String & str) { return str == keyword; }
using Type = Base::Type;
Base base;
bool is_auto = false;
bool changed = false;
explicit SettingAutoWrapper() : is_auto(true) {}
explicit SettingAutoWrapper(Type val) : is_auto(false) { base = Base(val); }
SettingAutoWrapper(const SettingAutoWrapper & o)
: base(o.base), is_auto(o.is_auto), changed(o.changed)
{}
explicit SettingAutoWrapper(const Field & f)
: is_auto(isAuto(f))
{
if (!is_auto)
base = Base(f);
}
SettingAutoWrapper & operator=(const Field & f) override
{
changed = true;
if (is_auto = isAuto(f); !is_auto)
base = f;
return *this;
}
SettingAutoWrapper & operator=(const SettingAutoWrapper & o)
{
if (this != &o)
{
base = o.base;
is_auto = o.is_auto;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
explicit operator Field() const override { return is_auto ? Field(keyword) : Field(base); }
String toString() const override { return is_auto ? keyword : base.toString(); }
void parseFromString(const String & str) override
{
changed = true;
if (is_auto = isAuto(str); !is_auto)
base.parseFromString(str);
}
void writeBinary(WriteBuffer & out) const override
{
if (is_auto)
Base().writeBinary(out); /// serialize default value
else
base.writeBinary(out);
}
/*
* That it is fine to reset `is_auto` here and to use default value in case `is_auto`
* because settings will be serialized only if changed.
* If they were changed they were requested to use explicit value instead of `auto`.
* And so interactions between client-server, and server-server (distributed queries), should be OK.
*/
void readBinary(ReadBuffer & in) override { changed = true; is_auto = false; base.readBinary(in); }
Type valueOr(Type default_value) const { return is_auto ? default_value : base.value; }
std::optional<Type> valueOrNullopt() const { return is_auto ? std::optional<Type>(std::nullopt) : base.value; }
};
using SettingFieldBoolAuto = SettingAutoWrapper<SettingFieldBool>;
using SettingFieldUInt64Auto = SettingAutoWrapper<SettingFieldUInt64>;
using SettingFieldInt64Auto = SettingAutoWrapper<SettingFieldInt64>;
using SettingFieldFloatAuto = SettingAutoWrapper<SettingFieldFloat>;
using SettingFieldUInt32Auto = SettingAutoWrapper<SettingFieldUInt32>;
using SettingFieldInt32Auto = SettingAutoWrapper<SettingFieldInt32>;
using SettingFieldDoubleAuto = SettingAutoWrapper<SettingFieldDouble>;
/* Similar to SettingFieldUInt64Auto with small differences to behave like regular UInt64, supported to compatibility.
* When setting to 'auto' it becomes equal to the number of processor cores without taking into account SMT.
* A value of 0 is also treated as 'auto', so 'auto' is parsed and serialized in the same way as 0.
*/
struct SettingFieldMaxThreads final : SettingFieldBase
{
bool is_auto;
UInt64 value;
bool changed = false;
using ValueType = UInt64;
explicit SettingFieldMaxThreads(UInt64 x = 0) : is_auto(!x), value(is_auto ? getAuto() : x) {}
explicit SettingFieldMaxThreads(const Field & f);
SettingFieldMaxThreads(const SettingFieldMaxThreads & o)
: is_auto(o.is_auto), value(o.value), changed(o.changed)
{}
SettingFieldMaxThreads & operator=(UInt64 x) { is_auto = !x; value = is_auto ? getAuto() : x; changed = true; return *this; }
SettingFieldMaxThreads & operator=(const Field & f) override;
SettingFieldMaxThreads & operator=(const SettingFieldMaxThreads & o)
{
if (this != &o)
{
is_auto = o.is_auto;
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator UInt64() const { return value; } /// NOLINT
explicit operator Field() const override { return value; }
/// Writes "auto(<number>)" instead of simple "<number>" if `is_auto == true`.
String toString() const override;
void parseFromString(const String & str) override;
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
private:
static UInt64 getAuto();
};
enum class SettingFieldTimespanUnit : uint8_t
{
Millisecond,
Second
};
template <SettingFieldTimespanUnit unit_>
struct SettingFieldTimespan final : SettingFieldBase
{
using Unit = SettingFieldTimespanUnit;
static constexpr Unit unit = unit_;
static constexpr UInt64 microseconds_per_unit = (unit == SettingFieldTimespanUnit::Millisecond) ? 1000 : 1000000;
Poco::Timespan value;
bool changed = false;
explicit SettingFieldTimespan() : value({}) {}
explicit SettingFieldTimespan(const Poco::Timespan & x) : value(x) {}
SettingFieldTimespan(const SettingFieldTimespan & o)
: value(o.value), changed(o.changed)
{}
explicit SettingFieldTimespan(UInt64 x) : SettingFieldTimespan(Poco::Timespan{static_cast<Poco::Timespan::TimeDiff>(x * microseconds_per_unit)}) {}
explicit SettingFieldTimespan(const Field & f);
SettingFieldTimespan & operator =(const Poco::Timespan & x) { value = x; changed = true; return *this; }
SettingFieldTimespan & operator =(UInt64 x) { *this = Poco::Timespan{static_cast<Poco::Timespan::TimeDiff>(x * microseconds_per_unit)}; return *this; }
SettingFieldTimespan & operator =(const Field & f) override;
SettingFieldTimespan & operator =(const SettingFieldTimespan & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator Poco::Timespan() const { return value; } /// NOLINT
explicit operator UInt64() const { return value.totalMicroseconds() / microseconds_per_unit; }
explicit operator Field() const override;
Poco::Timespan::TimeDiff totalMicroseconds() const { return value.totalMicroseconds(); }
Poco::Timespan::TimeDiff totalMilliseconds() const { return value.totalMilliseconds(); }
Poco::Timespan::TimeDiff totalSeconds() const { return value.totalSeconds(); }
String toString() const override;
void parseFromString(const String & str) override;
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
using SettingFieldSeconds = SettingFieldTimespan<SettingFieldTimespanUnit::Second>;
using SettingFieldMilliseconds = SettingFieldTimespan<SettingFieldTimespanUnit::Millisecond>;
struct SettingFieldString final : SettingFieldBase
{
String value;
bool changed = false;
using ValueType = String;
explicit SettingFieldString(std::string_view str = {}) : value(str) {}
explicit SettingFieldString(const String & str) : SettingFieldString(std::string_view{str}) {}
explicit SettingFieldString(String && str) : value(std::move(str)) {}
explicit SettingFieldString(const char * str) : SettingFieldString(std::string_view{str}) {}
explicit SettingFieldString(const Field & f) : SettingFieldString(f.safeGet<String>()) {}
SettingFieldString(const SettingFieldString & o)
: value(o.value), changed(o.changed)
{}
SettingFieldString & operator =(std::string_view str) { value = str; changed = true; return *this; }
SettingFieldString & operator =(const String & str) { *this = std::string_view{str}; return *this; }
SettingFieldString & operator =(String && str) { value = std::move(str); changed = true; return *this; }
SettingFieldString & operator =(const char * str) { *this = std::string_view{str}; return *this; }
SettingFieldString & operator =(const Field & f) override { *this = f.safeGet<String>(); return *this; }
SettingFieldString & operator =(const SettingFieldString & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator const String &() const { return value; } /// NOLINT
explicit operator Field() const override { return value; }
String toString() const override { return value; }
void parseFromString(const String & str) override { *this = str; }
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
struct SettingFieldMap final : SettingFieldBase
{
public:
Map value;
bool changed = false;
explicit SettingFieldMap(const Map & map = {}) : value(map) {}
explicit SettingFieldMap(Map && map) : value(std::move(map)) {}
explicit SettingFieldMap(const Field & f);
SettingFieldMap(const SettingFieldMap & o)
: value(o.value), changed(o.changed)
{}
SettingFieldMap & operator =(const Map & map) { value = map; changed = true; return *this; }
SettingFieldMap & operator =(const Field & f) override;
SettingFieldMap & operator =(const SettingFieldMap & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator const Map &() const { return value; } /// NOLINT
explicit operator Field() const override { return value; }
String toString() const override;
void parseFromString(const String & str) override;
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
struct SettingFieldChar final : SettingFieldBase
{
public:
char value;
bool changed = false;
explicit SettingFieldChar(char c = '\0') : value(c) {}
explicit SettingFieldChar(const Field & f);
SettingFieldChar(const SettingFieldChar & o)
: value(o.value), changed(o.changed)
{}
SettingFieldChar & operator =(char c) { value = c; changed = true; return *this; }
SettingFieldChar & operator =(const Field & f) override;
SettingFieldChar & operator =(const SettingFieldChar & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator char() const { return value; } /// NOLINT
explicit operator Field() const override { return toString(); }
String toString() const override { return String(&value, 1); }
void parseFromString(const String & str) override;
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
struct SettingFieldURI final : SettingFieldBase
{
Poco::URI value;
bool changed = false;
explicit SettingFieldURI(const Poco::URI & uri = {}) : value(uri) {}
explicit SettingFieldURI(const String & str) : SettingFieldURI(Poco::URI{str}) {}
explicit SettingFieldURI(const char * str) : SettingFieldURI(Poco::URI{str}) {}
explicit SettingFieldURI(const Field & f) : SettingFieldURI(f.safeGet<String>()) {}
SettingFieldURI(const SettingFieldURI & o)
: value(o.value), changed(o.changed)
{}
SettingFieldURI & operator =(const Poco::URI & x) { value = x; changed = true; return *this; }
SettingFieldURI & operator =(const String & str) { *this = Poco::URI{str}; return *this; }
SettingFieldURI & operator =(const char * str) { *this = Poco::URI{str}; return *this; }
SettingFieldURI & operator =(const Field & f) override { *this = f.safeGet<String>(); return *this; }
SettingFieldURI & operator =(const SettingFieldURI & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator const Poco::URI &() const { return value; } /// NOLINT
explicit operator String() const { return toString(); }
explicit operator Field() const override { return toString(); }
String toString() const override { return value.toString(); }
void parseFromString(const String & str) override { *this = str; }
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
/** Template class to define enum-based settings.
* Example of usage:
*
* mysettings.h:
* enum Gender { Male, Female };
* DECLARE_SETTING_ENUM(SettingFieldGender, Gender)
*
* mysettings.cpp:
* IMPLEMENT_SETTING_ENUM(SettingFieldGender, ExceptionType,
* {{"Male", Gender::Male}, {"Female", Gender::Female}})
*/
template <typename EnumT, typename Traits>
struct SettingFieldEnum final : SettingFieldBase
{
using EnumType = EnumT;
using ValueType = EnumT;
EnumType value;
bool changed = false;
explicit SettingFieldEnum(EnumType x = EnumType{}) : value(x) {}
explicit SettingFieldEnum(const Field & f) : SettingFieldEnum(Traits::fromString(f.safeGet<String>())) {}
SettingFieldEnum(const SettingFieldEnum & o)
: value(o.value), changed(o.changed)
{}
SettingFieldEnum & operator =(EnumType x) { value = x; changed = true; return *this; }
SettingFieldEnum & operator =(const Field & f) override { *this = Traits::fromString(f.safeGet<String>()); return *this; }
SettingFieldEnum & operator =(const SettingFieldEnum & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator EnumType() const { return value; } /// NOLINT
explicit operator Field() const override { return toString(); }
String toString() const override { return Traits::toString(value); }
void parseFromString(const String & str) override { *this = Traits::fromString(str); }
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
struct SettingFieldEnumHelpers
{
static void writeBinary(std::string_view str, WriteBuffer & out);
static String readBinary(ReadBuffer & in);
};
template <typename EnumT, typename Traits>
void SettingFieldEnum<EnumT, Traits>::writeBinary(WriteBuffer & out) const
{
SettingFieldEnumHelpers::writeBinary(toString(), out);
}
template <typename EnumT, typename Traits>
void SettingFieldEnum<EnumT, Traits>::readBinary(ReadBuffer & in)
{
*this = Traits::fromString(SettingFieldEnumHelpers::readBinary(in));
}
// Mostly like SettingFieldEnum, but can have multiple enum values (or none) set at once.
template <typename Enum, typename Traits>
struct SettingFieldMultiEnum final : SettingFieldBase
{
using EnumType = Enum;
using ValueType = std::vector<Enum>;
ValueType value;
bool changed = false;
explicit SettingFieldMultiEnum(ValueType v = ValueType{}) : value{v} {}
explicit SettingFieldMultiEnum(EnumType e) : value{e} {}
explicit SettingFieldMultiEnum(const Field & f) : value(parseValueFromString(f.safeGet<String>())) {}
SettingFieldMultiEnum(const SettingFieldMultiEnum & o)
: value(o.value), changed(o.changed)
{}
SettingFieldMultiEnum & operator= (ValueType x) { changed = true; value = x; return *this; }
SettingFieldMultiEnum & operator= (const Field & x) override { parseFromString(x.safeGet<String>()); return *this; }
SettingFieldMultiEnum & operator= (const SettingFieldMultiEnum & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator ValueType() const { return value; } /// NOLINT
explicit operator Field() const override { return toString(); }
operator MultiEnum<EnumType>() const /// NOLINT
{
MultiEnum<EnumType> res;
for (const auto & v : value)
res.set(v);
return res;
}
String toString() const override
{
constexpr String separator = ",";
String result;
for (const auto & v : value)
{
result += Traits::toString(v);
result += separator;
}
if (!result.empty())
result.erase(result.size() - separator.size());
return result;
}
void parseFromString(const String & str) override { *this = parseValueFromString(str); }
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
private:
static ValueType parseValueFromString(const std::string_view str)
{
constexpr String separators=", ";
ValueType result;
std::unordered_set<EnumType> values_set;
//to avoid allocating memory on substr()
const std::string_view str_view{str};
auto value_start = str_view.find_first_not_of(separators);
while (value_start != std::string::npos)
{
auto value_end = str_view.find_first_of(separators, value_start + 1);
if (value_end == std::string::npos)
value_end = str_view.size();
auto value = Traits::fromString(str_view.substr(value_start, value_end - value_start));
/// Deduplicate values
auto [_, inserted] = values_set.emplace(value);
if (inserted)
result.push_back(value);
value_start = str_view.find_first_not_of(separators, value_end);
}
return result;
}
};
template <typename EnumT, typename Traits>
void SettingFieldMultiEnum<EnumT, Traits>::writeBinary(WriteBuffer & out) const
{
SettingFieldEnumHelpers::writeBinary(toString(), out);
}
template <typename EnumT, typename Traits>
void SettingFieldMultiEnum<EnumT, Traits>::readBinary(ReadBuffer & in)
{
parseFromString(SettingFieldEnumHelpers::readBinary(in));
}
/// Setting field for specifying user-defined timezone. It is basically a string, but it needs validation.
struct SettingFieldTimezone final : SettingFieldBase
{
String value;
bool changed = false;
explicit SettingFieldTimezone(std::string_view str = {}) { validateTimezone(std::string(str)); value = str; }
explicit SettingFieldTimezone(const String & str) { validateTimezone(str); value = str; }
explicit SettingFieldTimezone(String && str) { validateTimezone(str); value = std::move(str); }
explicit SettingFieldTimezone(const char * str) { validateTimezone(str); value = str; }
explicit SettingFieldTimezone(const Field & f) { const String & str = f.safeGet<String>(); validateTimezone(str); value = str; }
SettingFieldTimezone(const SettingFieldTimezone & o)
: value(o.value), changed(o.changed)
{}
SettingFieldTimezone & operator =(std::string_view str) { validateTimezone(std::string(str)); value = str; changed = true; return *this; }
SettingFieldTimezone & operator =(const String & str) { *this = std::string_view{str}; return *this; }
SettingFieldTimezone & operator =(String && str) { validateTimezone(str); value = std::move(str); changed = true; return *this; }
SettingFieldTimezone & operator =(const char * str) { *this = std::string_view{str}; return *this; }
SettingFieldTimezone & operator =(const Field & f) override { *this = f.safeGet<String>(); return *this; }
SettingFieldTimezone & operator =(const SettingFieldTimezone & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
operator const String &() const { return value; } /// NOLINT
explicit operator Field() const override { return value; }
String toString() const override { return value; }
void parseFromString(const String & str) override { *this = str; }
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
private:
void validateTimezone(const std::string & tz_str);
};
/// Can keep a value of any type. Used for user-defined settings.
struct SettingFieldCustom final : SettingFieldBase
{
Field value;
bool changed = false;
explicit SettingFieldCustom(const Field & f = {}) : value(f) {}
SettingFieldCustom(const SettingFieldCustom & o)
: value(o.value), changed(o.changed)
{}
SettingFieldCustom & operator =(const Field & f) override { value = f; changed = true; return *this; }
SettingFieldCustom & operator =(const SettingFieldCustom & o)
{
if (this != &o)
{
value = o.value;
changed = o.changed;
}
return *this;
}
bool isChanged() const override { return changed; }
void setChanged(bool changed_) override { changed = changed_; }
explicit operator Field() const override { return value; }
String toString() const override;
void parseFromString(const String & str) override;
void writeBinary(WriteBuffer & out) const override;
void readBinary(ReadBuffer & in) override;
};
struct SettingFieldNonZeroUInt64 : public SettingFieldUInt64
{
public:
explicit SettingFieldNonZeroUInt64(UInt64 x = 1);
explicit SettingFieldNonZeroUInt64(const Field & f);
SettingFieldNonZeroUInt64 & operator=(UInt64 x) override;
SettingFieldNonZeroUInt64 & operator=(const Field & f) override;
void parseFromString(const String & str) override;
private:
void checkValueNonZero() const;
};
bool stringToBool(const String & str);
}