1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include "row.hpp"
18 
19 #include "external.hpp"
20 #include "result_metadata.hpp"
21 #include "result_response.hpp"
22 #include "serialization.hpp"
23 #include "string_ref.hpp"
24 
25 using namespace datastax;
26 using namespace datastax::internal::core;
27 
28 extern "C" {
29 
cass_row_get_column(const CassRow * row,size_t index)30 const CassValue* cass_row_get_column(const CassRow* row, size_t index) {
31   if (index >= row->values.size()) {
32     return NULL;
33   }
34   return CassValue::to(&row->values[index]);
35 }
36 
cass_row_get_column_by_name(const CassRow * row,const char * name)37 const CassValue* cass_row_get_column_by_name(const CassRow* row, const char* name) {
38 
39   return cass_row_get_column_by_name_n(row, name, SAFE_STRLEN(name));
40 }
41 
cass_row_get_column_by_name_n(const CassRow * row,const char * name,size_t name_length)42 const CassValue* cass_row_get_column_by_name_n(const CassRow* row, const char* name,
43                                                size_t name_length) {
44 
45   return CassValue::to(row->get_by_name(StringRef(name, name_length)));
46 }
47 
48 } // extern "C"
49 
50 namespace datastax { namespace internal { namespace core {
51 
decode_row(Decoder & decoder,const ResultResponse * result,OutputValueVec & output)52 bool decode_row(Decoder& decoder, const ResultResponse* result, OutputValueVec& output) {
53   output.clear();
54   output.reserve(result->column_count());
55   for (int i = 0; i < result->column_count(); ++i) {
56     Value value;
57     const ColumnDefinition& def = result->metadata()->get_column_definition(i);
58     CHECK_RESULT(decoder.decode_value(def.data_type, value));
59     output.push_back(value);
60   }
61 
62   return true;
63 }
64 
65 }}} // namespace datastax::internal::core
66 
get_by_name(const StringRef & name) const67 const Value* Row::get_by_name(const StringRef& name) const {
68   IndexVec indices;
69   if (result_->metadata()->get_indices(name, &indices) == 0) {
70     return NULL;
71   }
72   return &values[indices[0]];
73 }
74 
get_string_by_name(const StringRef & name,String * out) const75 bool Row::get_string_by_name(const StringRef& name, String* out) const {
76   const Value* value = get_by_name(name);
77   if (value == NULL || value->is_null()) {
78     return false;
79   }
80   *out = value->decoder().as_string();
81   return true;
82 }
83 
get_uuid_by_name(const StringRef & name,CassUuid * out) const84 bool Row::get_uuid_by_name(const StringRef& name, CassUuid* out) const {
85   const Value* value = get_by_name(name);
86   if (value == NULL || value->is_null() || value->value_type() != CASS_VALUE_TYPE_UUID ||
87       value->value_type() == CASS_VALUE_TYPE_TIMEUUID) {
88     return false;
89   }
90   *out = value->as_uuid();
91   return true;
92 }
93