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 #ifndef DATASTAX_INTERNAL_STATEMENT_HPP
18 #define DATASTAX_INTERNAL_STATEMENT_HPP
19 
20 #include "abstract_data.hpp"
21 #include "constants.hpp"
22 #include "external.hpp"
23 #include "macros.hpp"
24 #include "prepared.hpp"
25 #include "request.hpp"
26 #include "result_metadata.hpp"
27 #include "result_response.hpp"
28 #include "retry_policy.hpp"
29 #include "scoped_ptr.hpp"
30 #include "string.hpp"
31 #include "vector.hpp"
32 
33 namespace datastax { namespace internal { namespace core {
34 
35 class RequestCallback;
36 
37 class Statement
38     : public RoutableRequest
39     , public AbstractData {
40 public:
41   typedef SharedRefPtr<Statement> Ptr;
42 
43   Statement(const char* query, size_t query_length, size_t values_count);
44 
45   Statement(const Prepared* prepared);
46 
~Statement()47   virtual ~Statement() {}
48 
49   // Used to get the original query string from a simple statement. To get the
50   // query from a execute request (bound statement) cast it and get it from the
51   // prepared object.
52   String query() const;
53 
set_has_names_for_values(bool has_names_for_values)54   void set_has_names_for_values(bool has_names_for_values) {
55     if (has_names_for_values) {
56       flags_ |= CASS_QUERY_FLAG_NAMES_FOR_VALUES;
57     } else {
58       flags_ &= ~CASS_QUERY_FLAG_NAMES_FOR_VALUES;
59     }
60   }
61 
has_names_for_values() const62   bool has_names_for_values() const { return (flags_ & CASS_QUERY_FLAG_NAMES_FOR_VALUES) != 0; }
63 
page_size() const64   int32_t page_size() const { return page_size_; }
65 
set_page_size(int32_t page_size)66   void set_page_size(int32_t page_size) { page_size_ = page_size; }
67 
paging_state() const68   const String& paging_state() const { return paging_state_; }
69 
set_paging_state(const String & paging_state)70   void set_paging_state(const String& paging_state) { paging_state_ = paging_state; }
71 
kind() const72   uint8_t kind() const {
73     return opcode() == CQL_OPCODE_QUERY ? CASS_BATCH_KIND_QUERY : CASS_BATCH_KIND_PREPARED;
74   }
75 
add_key_index(size_t index)76   void add_key_index(size_t index) { key_indices_.push_back(index); }
77 
get_routing_key(String * routing_key) const78   virtual bool get_routing_key(String* routing_key) const {
79     return calculate_routing_key(key_indices_, routing_key);
80   }
81 
82   int32_t encode_batch(ProtocolVersion version, RequestCallback* callback, BufferVec* bufs) const;
83 
84 protected:
85   bool with_keyspace(ProtocolVersion version) const;
86 
87   int32_t encode_query_or_id(BufferVec* bufs) const;
88   int32_t encode_begin(ProtocolVersion version, uint16_t element_count, RequestCallback* callback,
89                        BufferVec* bufs) const;
90   int32_t encode_values(ProtocolVersion version, RequestCallback* callback, BufferVec* bufs) const;
91   int32_t encode_end(ProtocolVersion version, RequestCallback* callback, BufferVec* bufs) const;
92 
93   bool calculate_routing_key(const Vector<size_t>& key_indices, String* routing_key) const;
94 
95 private:
96   Buffer query_or_id_;
97   int32_t flags_;
98   int32_t page_size_;
99   String paging_state_;
100   Vector<size_t> key_indices_;
101 
102 private:
103   DISALLOW_COPY_AND_ASSIGN(Statement);
104 };
105 
106 }}} // namespace datastax::internal::core
107 
108 EXTERNAL_TYPE(datastax::internal::core::Statement, CassStatement)
109 
110 #endif
111