3 #ifndef STREAMBASE_TUPLE_H
4 #define STREAMBASE_TUPLE_H
6 #include "StreamBase.hpp"
8 #include "Timestamp.hpp"
11 #include "Exceptions.hpp"
13 #include <NMSTL/platform.hpp>
14 #include <streambase/impl/Memory.hpp>
15 #include "TuplePrivate.hpp"
16 #include "TupleExceptions.hpp"
17 #include "FieldValue.hpp"
20 #pragma warning( push )
21 #pragma warning(disable:4127)
24 SB_INTERNAL_FWD(TupleUtil);
25 SB_INTERNAL_FWD(Errors);
34 T Tuple_do_get(
const Tuple& tuple,
const Field &field);
36 void Tuple_do_set(Tuple& tuple,
const Field &field, T value);
62 bool operator==(
const Tuple& rhs)
const;
70 if(!getSchema()) {
return; }
71 for(
size_t i = 0; i < getNumFields(); ++i) {
77 const Schema &getSchema()
const;
79 void setSchema(
const Schema &);
83 const Schema& s = getSchema();
88 size_t getSize()
const;
109 template <
typename T> T
get(
const Field &f)
const {
110 return Tuple_do_get<T>(*
this, f);
112 template <
typename T> T
get(
size_t i)
const {
113 return get<T>(getSchema().getField(i));
115 template <
typename T> T
get(
const std::string &field_name)
const {
116 return get<T>(getSchema().getField(field_name));
129 template <
typename T>
void set(
const Field &field, T value) {
130 return Tuple_do_set<T>(*
this, field, value);
132 template <
typename T>
void set(
int fieldNum, T value) {
133 set(getSchema().getField(fieldNum), value);
135 template <
typename T>
void set(
const std::string &field_name, T value) {
136 set(getSchema().getField(field_name), value);
148 bool isNull(
const Field &f)
const;
150 return isNull(getSchema().getField(i));
152 bool isNull(
const std::string& n)
const {
153 return isNull(getSchema().getField(n));
164 void setNull(
const Field &f);
178 bool getBool(
const Field &f)
const;
180 return getBool(getSchema().getField(i));
183 return getBool(getSchema().getField(n));
194 void setBool(
const Field &f,
bool v);
196 setBool(getSchema().getField(i), v);
199 setBool(getSchema().getField(n), v);
212 int getInt(
const Field &f)
const;
214 return getInt(getSchema().getField(i));
217 return getInt(getSchema().getField(n));
228 void setInt(
const Field &f,
int v);
230 setInt(getSchema().getField(i), v);
232 void setInt(
const std::string& n,
int v) {
233 setInt(getSchema().getField(n), v);
246 long long getLong(
const Field &f)
const;
248 return getLong(getSchema().getField(i));
250 long long getLong(
const std::string& n)
const {
251 return getLong(getSchema().getField(n));
262 void setLong(
const Field &f,
long long v);
264 setLong(getSchema().getField(i), v);
266 void setLong(
const std::string& n,
long long v) {
267 setLong(getSchema().getField(n), v);
280 double getDouble(
const Field &f)
const;
282 return getDouble(getSchema().getField(i));
285 return getDouble(getSchema().getField(n));
296 void setDouble(
const Field &f,
double v);
298 setDouble(getSchema().getField(i), v);
301 setDouble(getSchema().getField(n), v);
316 return getTimestamp(getSchema().getField(i));
319 return getTimestamp(getSchema().getField(n));
332 setTimestamp(getSchema().getField(i), v);
335 setTimestamp(getSchema().getField(n), v);
349 const std::string& getBlobBuffer(
const Field &f)
const;
351 return getBlobBuffer(getSchema().getField(i));
354 return getBlobBuffer(getSchema().getField(n));
365 void setBlobBuffer(
const Field &f,
const std::string& v);
367 setBlobBuffer(getSchema().getField(i), v);
370 setBlobBuffer(getSchema().getField(n), v);
383 const std::vector<FieldValue>& getList(
const Schema::Field &f)
const;
384 const std::vector<FieldValue>&
getList(
size_t i)
const {
385 return getList(getSchema().getField(i));
387 const std::vector<FieldValue>&
getList(
const std::string& n)
const {
388 return getList(getSchema().getField(n));
399 void setList(
const Schema::Field &f,
const std::vector<FieldValue>& v);
400 void setList(
size_t i,
const std::vector<FieldValue>& v) {
401 setList(getSchema().getField(i), v);
403 void setList(
const std::string& n,
const std::vector<FieldValue>& v) {
404 setList(getSchema().getField(n), v);
416 const std::string& getString(
const Field &f)
const;
418 return getString(getSchema().getField(i));
420 const std::string&
getString(
const std::string& n)
const {
421 return getString(getSchema().getField(n));
433 void setString(
const Field &field,
434 const std::string &value, Flags flags = NONE);
435 void setString(
size_t field,
436 const char* data,
size_t len,
Flags flags = NONE) {
437 setString(field, std::string(data, len), flags);
439 void setString(
const std::string& field,
440 const char* data,
size_t len,
Flags flags = NONE) {
441 setString(field, std::string(data, len), flags);
444 const char* data,
size_t len,
Flags flags = NONE) {
445 setString(field, std::string(data, len), flags);
447 void setString(
size_t field_index,
448 const std::string &value,
Flags flags = NONE) {
449 setString(getSchema().getField(field_index), value, flags);
451 void setString(
const std::string& field_name,
452 const std::string& value,
Flags flags = NONE) {
453 setString(getSchema().getField(field_name), value, flags);
468 return getTuple(getSchema().getField(i));
471 return getTuple(getSchema().getField(n));
484 const Function& getFunction(
const Field& f)
const;
486 return getFunction(getSchema().getField(i));
489 return getFunction(getSchema().getField(n));
505 void setTuple(
const Field &f,
const Tuple& v);
507 setTuple(getSchema().getField(i), v);
510 setTuple(getSchema().getField(n), v);
522 void setFunction(
const Field& f,
const Function& v);
524 setFunction(getSchema().getField(i), v);
527 setFunction(getSchema().getField(n), v);
541 const FieldValue& getFieldValue(
size_t field_num)
const;
545 FieldValue& getFieldValue(
const std::string& field_name);
548 const FieldValue& getFieldValue(
const std::string& field_name)
const;
555 void setFieldValue(
size_t field_num,
const FieldValue& fv);
558 void setFieldValue(
const std::string& field_name,
const FieldValue& fv);
562 bool hasHeader()
const {
return getSchema().hasHeader(); }
566 unsigned int getId()
const;
568 void setId(
unsigned int tid);
573 std::string as_string()
const;
581 std::string as_string_external(
char delimiter =
',',
582 const std::string& null_string = getNullString())
const;
595 std::string toDelimitedString(
const std::string &delimiter,
596 const std::string& null_string = getNullString(),
597 bool include_names =
false,
bool quote_all_strings =
false)
const;
599 #ifndef DOXYGEN_INTERNAL_ONLY
602 void writeCSVString(std::ostream& out,
char delim,
char quote,
603 const std::string& null_string = getNullString())
const;
608 static std::string NULL_STRING =
"null";
612 #ifndef DOXYGEN_INTERNAL_ONLY
625 size_t copyIntoBuffer(
void* buf,
size_t len,
bool byteswap)
const;
636 size_t setFromPackedBuffer(
637 const void* buf,
size_t buf_len,
bool byteswap,
const Schema& s);
639 static Tuple createFromPackedBuffer(
size_t* size_used,
640 const void* buf,
size_t buf_len,
bool byteswap,
const Schema& s);
644 std::shared_ptr<TuplePrivateImpl> _;
649 void ensureAllocated()
const;
652 const Tuple& resolveField(
const Field& f)
const;
656 #define DO_ACCESSOR(type, getter) \
657 template <> inline type Tuple_do_get<type>(const Tuple& t, const Field& f) { return t.get ## getter(f); } \
658 template <> inline void Tuple_do_set<type>(Tuple& t, const Field& f, type v) { return t.set ## getter(f, v); }
660 DO_ACCESSOR(
bool, Bool)
661 DO_ACCESSOR(
int, Int)
662 DO_ACCESSOR(
long long, Long)
663 DO_ACCESSOR(
double, Double)
664 DO_ACCESSOR(std::
string, String)
665 DO_ACCESSOR(Timestamp, Timestamp)
671 #pragma warning( pop )
676 inline std::ostream& operator << (std::ostream& os,
const sb::Tuple& tuple) {