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 "iterator.hpp"
18 
19 #include "collection_iterator.hpp"
20 #include "external.hpp"
21 #include "map_iterator.hpp"
22 #include "result_iterator.hpp"
23 #include "row_iterator.hpp"
24 #include "user_type_field_iterator.hpp"
25 
26 using namespace datastax;
27 using namespace datastax::internal::core;
28 
29 extern "C" {
30 
cass_iterator_free(CassIterator * iterator)31 void cass_iterator_free(CassIterator* iterator) { delete iterator->from(); }
32 
cass_iterator_next(CassIterator * iterator)33 cass_bool_t cass_iterator_next(CassIterator* iterator) {
34   return static_cast<cass_bool_t>(iterator->from()->next());
35 }
36 
cass_iterator_type(CassIterator * iterator)37 CassIteratorType cass_iterator_type(CassIterator* iterator) { return iterator->type(); }
38 
cass_iterator_from_result(const CassResult * result)39 CassIterator* cass_iterator_from_result(const CassResult* result) {
40   return CassIterator::to(new ResultIterator(result));
41 }
42 
cass_iterator_from_row(const CassRow * row)43 CassIterator* cass_iterator_from_row(const CassRow* row) {
44   return CassIterator::to(new RowIterator(row));
45 }
46 
cass_iterator_from_collection(const CassValue * value)47 CassIterator* cass_iterator_from_collection(const CassValue* value) {
48   if (value->is_null() || !value->is_collection()) {
49     return NULL;
50   }
51   return CassIterator::to(new CollectionIterator(value));
52 }
53 
cass_iterator_from_tuple(const CassValue * value)54 CassIterator* cass_iterator_from_tuple(const CassValue* value) {
55   if (value->is_null() || !value->is_tuple()) {
56     return NULL;
57   }
58   return CassIterator::to(new TupleIterator(value));
59 }
60 
cass_iterator_from_map(const CassValue * value)61 CassIterator* cass_iterator_from_map(const CassValue* value) {
62   if (value->is_null() || !value->is_map()) {
63     return NULL;
64   }
65   return CassIterator::to(new MapIterator(value));
66 }
67 
cass_iterator_fields_from_user_type(const CassValue * value)68 CassIterator* cass_iterator_fields_from_user_type(const CassValue* value) {
69   if (value->is_null() || !value->is_user_type()) {
70     return NULL;
71   }
72   return CassIterator::to(new UserTypeFieldIterator(value));
73 }
74 
cass_iterator_get_user_type_field_name(const CassIterator * iterator,const char ** name,size_t * name_length)75 CassError cass_iterator_get_user_type_field_name(const CassIterator* iterator, const char** name,
76                                                  size_t* name_length) {
77   if (iterator->type() != CASS_ITERATOR_TYPE_USER_TYPE_FIELD) {
78     return CASS_ERROR_LIB_BAD_PARAMS;
79   }
80   StringRef field_name = static_cast<const UserTypeFieldIterator*>(iterator->from())->field_name();
81   *name = field_name.data();
82   *name_length = field_name.size();
83   return CASS_OK;
84 }
85 
cass_iterator_get_user_type_field_value(const CassIterator * iterator)86 const CassValue* cass_iterator_get_user_type_field_value(const CassIterator* iterator) {
87   if (iterator->type() != CASS_ITERATOR_TYPE_USER_TYPE_FIELD) {
88     return NULL;
89   }
90   return CassValue::to(static_cast<const UserTypeFieldIterator*>(iterator->from())->field_value());
91 }
92 
cass_iterator_get_row(const CassIterator * iterator)93 const CassRow* cass_iterator_get_row(const CassIterator* iterator) {
94   if (iterator->type() != CASS_ITERATOR_TYPE_RESULT) {
95     return NULL;
96   }
97   return CassRow::to(static_cast<const ResultIterator*>(iterator->from())->row());
98 }
99 
cass_iterator_get_column(const CassIterator * iterator)100 const CassValue* cass_iterator_get_column(const CassIterator* iterator) {
101   if (iterator->type() != CASS_ITERATOR_TYPE_ROW) {
102     return NULL;
103   }
104   return CassValue::to(static_cast<const RowIterator*>(iterator->from())->column());
105 }
106 
cass_iterator_get_value(const CassIterator * iterator)107 const CassValue* cass_iterator_get_value(const CassIterator* iterator) {
108   if (iterator->type() != CASS_ITERATOR_TYPE_COLLECTION &&
109       iterator->type() != CASS_ITERATOR_TYPE_TUPLE) {
110     return NULL;
111   }
112   return CassValue::to(static_cast<const ValueIterator*>(iterator->from())->value());
113 }
114 
cass_iterator_get_map_key(const CassIterator * iterator)115 const CassValue* cass_iterator_get_map_key(const CassIterator* iterator) {
116   if (iterator->type() != CASS_ITERATOR_TYPE_MAP) {
117     return NULL;
118   }
119   return CassValue::to(static_cast<const MapIterator*>(iterator->from())->key());
120 }
121 
cass_iterator_get_map_value(const CassIterator * iterator)122 const CassValue* cass_iterator_get_map_value(const CassIterator* iterator) {
123   if (iterator->type() != CASS_ITERATOR_TYPE_MAP) {
124     return NULL;
125   }
126   return CassValue::to(static_cast<const MapIterator*>(iterator->from())->value());
127 }
128 
129 } // extern "C"
130