-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathvalue.hpp
More file actions
144 lines (111 loc) · 3.94 KB
/
value.hpp
File metadata and controls
144 lines (111 loc) · 3.94 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
/*
Copyright (c) DataStax, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef DATASTAX_INTERNAL_VALUE_HPP
#define DATASTAX_INTERNAL_VALUE_HPP
#include "cassandra.h"
#include "decoder.hpp"
#include "external.hpp"
#include "result_metadata.hpp"
#include "string_ref.hpp"
namespace datastax { namespace internal { namespace core {
class Value {
public:
Value()
: count_(0)
, is_null_(false) {}
// Used for "null" values
Value(const DataType::ConstPtr& data_type)
: data_type_(data_type)
, count_(0)
, is_null_(true) {}
// Used for regular values, tuples, and UDTs
Value(const DataType::ConstPtr& data_type, Decoder decoder);
// Used for collections and schema metadata collections (converted from JSON)
Value(const DataType::ConstPtr& data_type, int32_t count, Decoder decoder)
: data_type_(data_type)
, count_(count)
, decoder_(decoder)
, is_null_(false) {}
Decoder decoder() const { return decoder_; }
ProtocolVersion protocol_version() const { return decoder_.protocol_version(); }
int64_t size() const { return (is_null_ ? -1 : decoder_.remaining()); }
bool is_valid() const { return !!data_type_; }
CassValueType value_type() const {
if (!data_type_) {
return CASS_VALUE_TYPE_UNKNOWN;
}
return data_type_->value_type();
}
const DataType::ConstPtr& data_type() const { return data_type_; }
CassValueType primary_value_type() const {
const DataType::ConstPtr& primary(primary_data_type());
if (!primary) {
return CASS_VALUE_TYPE_UNKNOWN;
}
return primary->value_type();
}
const DataType::ConstPtr& primary_data_type() const {
if (!data_type_ || !data_type_->is_collection()) {
return DataType::NIL;
}
const CollectionType::ConstPtr& collection_type(data_type_);
if (collection_type->types().size() < 1) {
return DataType::NIL;
}
return collection_type->types()[0];
}
CassValueType secondary_value_type() const {
const DataType::ConstPtr& secondary(secondary_data_type());
if (!secondary) {
return CASS_VALUE_TYPE_UNKNOWN;
}
return secondary->value_type();
}
const DataType::ConstPtr& secondary_data_type() const {
if (!data_type_ || !data_type_->is_map()) {
return DataType::NIL;
}
const CollectionType::ConstPtr& collection_type(data_type_);
if (collection_type->types().size() < 2) {
return DataType::NIL;
}
return collection_type->types()[1];
}
bool is_null() const { return is_null_; }
bool is_collection() const { return data_type_ && data_type_->is_collection(); }
bool is_map() const { return data_type_ && data_type_->is_map(); }
bool is_tuple() const { return data_type_ && data_type_->is_tuple(); }
bool is_user_type() const { return data_type_ && data_type_->is_user_type(); }
int32_t count() const { return count_ * !is_null_; }
StringRef to_string_ref() const {
if (is_null()) return StringRef();
return decoder_.as_string_ref();
}
String to_string() const { return to_string_ref().to_string(); }
bool as_bool() const;
int32_t as_int32() const;
CassUuid as_uuid() const;
StringVec as_stringlist() const;
private:
friend class Decoder;
bool update(const Decoder& decoder);
private:
DataType::ConstPtr data_type_;
int32_t count_;
Decoder decoder_;
bool is_null_;
};
typedef Vector<Value> OutputValueVec;
}}} // namespace datastax::internal::core
EXTERNAL_TYPE(datastax::internal::core::Value, CassValue)
#endif