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 "prepared.hpp"
18 
19 #include "execute_request.hpp"
20 #include "external.hpp"
21 #include "logger.hpp"
22 
23 using namespace datastax;
24 using namespace datastax::internal;
25 using namespace datastax::internal::core;
26 
27 extern "C" {
28 
cass_prepared_free(const CassPrepared * prepared)29 void cass_prepared_free(const CassPrepared* prepared) { prepared->dec_ref(); }
30 
cass_prepared_bind(const CassPrepared * prepared)31 CassStatement* cass_prepared_bind(const CassPrepared* prepared) {
32   ExecuteRequest* execute = new ExecuteRequest(prepared);
33   execute->inc_ref();
34   return CassStatement::to(execute);
35 }
36 
cass_prepared_parameter_name(const CassPrepared * prepared,size_t index,const char ** name,size_t * name_length)37 CassError cass_prepared_parameter_name(const CassPrepared* prepared, size_t index,
38                                        const char** name, size_t* name_length) {
39   const SharedRefPtr<ResultMetadata>& metadata(prepared->result()->metadata());
40   if (index >= metadata->column_count()) {
41     return CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS;
42   }
43   const ColumnDefinition def = metadata->get_column_definition(index);
44   *name = def.name.data();
45   *name_length = def.name.size();
46   return CASS_OK;
47 }
48 
cass_prepared_parameter_data_type(const CassPrepared * prepared,size_t index)49 const CassDataType* cass_prepared_parameter_data_type(const CassPrepared* prepared, size_t index) {
50   const SharedRefPtr<ResultMetadata>& metadata(prepared->result()->metadata());
51   if (index >= metadata->column_count()) {
52     return NULL;
53   }
54   return CassDataType::to(metadata->get_column_definition(index).data_type.get());
55 }
56 
cass_prepared_parameter_data_type_by_name(const CassPrepared * prepared,const char * name)57 const CassDataType* cass_prepared_parameter_data_type_by_name(const CassPrepared* prepared,
58                                                               const char* name) {
59   return cass_prepared_parameter_data_type_by_name_n(prepared, name, SAFE_STRLEN(name));
60 }
61 
cass_prepared_parameter_data_type_by_name_n(const CassPrepared * prepared,const char * name,size_t name_length)62 const CassDataType* cass_prepared_parameter_data_type_by_name_n(const CassPrepared* prepared,
63                                                                 const char* name,
64                                                                 size_t name_length) {
65 
66   const SharedRefPtr<ResultMetadata>& metadata(prepared->result()->metadata());
67 
68   IndexVec indices;
69   if (metadata->get_indices(StringRef(name, name_length), &indices) == 0) {
70     return NULL;
71   }
72   return CassDataType::to(metadata->get_column_definition(indices[0]).data_type.get());
73 }
74 
75 } // extern "C"
76 
Prepared(const ResultResponse::Ptr & result,const PrepareRequest::ConstPtr & prepare_request,const Metadata::SchemaSnapshot & schema_metadata)77 Prepared::Prepared(const ResultResponse::Ptr& result,
78                    const PrepareRequest::ConstPtr& prepare_request,
79                    const Metadata::SchemaSnapshot& schema_metadata)
80     : result_(result)
81     , id_(result->prepared_id().to_string())
82     , query_(prepare_request->query())
83     , keyspace_(prepare_request->keyspace())
84     , request_settings_(prepare_request->settings()) {
85   assert(result->protocol_version() > 0 && "The protocol version should be set");
86   if (result->protocol_version() >= CASS_PROTOCOL_VERSION_V4) {
87     key_indices_ = result->pk_indices();
88   } else {
89     const KeyspaceMetadata* keyspace = schema_metadata.get_keyspace(result->keyspace().to_string());
90     if (keyspace != NULL) {
91       const TableMetadata* table = keyspace->get_table(result->table().to_string());
92       if (table != NULL) {
93         const ColumnMetadata::Vec& partition_key = table->partition_key();
94         IndexVec indices;
95         for (ColumnMetadata::Vec::const_iterator i = partition_key.begin(),
96                                                  end = partition_key.end();
97              i != end; ++i) {
98           const ColumnMetadata::Ptr& column(*i);
99           if (column && result->metadata()->get_indices(column->name(), &indices) > 0) {
100             key_indices_.push_back(indices[0]);
101           } else {
102             LOG_WARN("Unable to find key column '%s' in prepared query",
103                      column ? column->name().c_str() : "<null>");
104             key_indices_.clear();
105             break;
106           }
107         }
108       }
109     }
110   }
111 }
112