class Cassandra::Statements::Prepared
A prepared statement is created by calling {Cassandra::Session#prepare} or {Cassandra::Session#prepare_async}.
Attributes
cql[R]
@return [String] original cql used to prepare this statement
id[R]
@private prepared-statement id
result_metadata[R]
@private
Public Class Methods
new(id, payload, warnings, cql, params_metadata, result_metadata, partition_key, trace_id, keyspace, statement, options, hosts, consistency, retries, client, connection_options)
click to toggle source
@private
# File lib/cassandra/statements/prepared.rb 34 def initialize(id, 35 payload, 36 warnings, 37 cql, 38 params_metadata, 39 result_metadata, 40 partition_key, 41 trace_id, 42 keyspace, 43 statement, 44 options, 45 hosts, 46 consistency, 47 retries, 48 client, 49 connection_options) 50 @id = id 51 @payload = payload 52 @warnings = warnings 53 @cql = cql 54 @params_metadata = params_metadata 55 @result_metadata = result_metadata 56 @partition_key = partition_key 57 @trace_id = trace_id 58 @keyspace = keyspace 59 @statement = statement 60 @options = options 61 @hosts = hosts 62 @consistency = consistency 63 @retries = retries 64 @client = client 65 @connection_options = connection_options 66 @idempotent = options.idempotent? 67 @payload = payload 68 end
Public Instance Methods
accept(client, options)
click to toggle source
@private
# File lib/cassandra/statements/prepared.rb 164 def accept(client, options) 165 client.execute(bind(options.arguments), options) 166 end
bind(args = nil)
click to toggle source
Creates a statement bound with specific arguments
@param args [Array, Hash] (nil) positional or named arguments to bind,
must contain the same number of parameters as the number of positional (`?`) or named (`:name`) markers in the original CQL passed to {Cassandra::Session#prepare}
@return [Cassandra::Statements::Bound] bound statement
# File lib/cassandra/statements/prepared.rb 78 def bind(args = nil) 79 if args 80 Util.assert_instance_of_one_of([::Array, ::Hash], args) do 81 "args must be an Array or a Hash, #{args.inspect} given" 82 end 83 else 84 args = EMPTY_LIST 85 end 86 87 params = [] 88 param_types = [] 89 90 if args.is_a?(::Hash) 91 @params_metadata.each do |(_, _, name, type)| 92 name = name.to_sym unless args.key?(name) 93 value = args.fetch(name, NOT_SET) 94 95 if NOT_SET.eql?(value) 96 if @connection_options.protocol_version < 4 97 raise ::ArgumentError, 98 "argument #{name.inspect} it not present in #{args.inspect}" 99 end 100 else 101 Util.assert_type(type, value) do 102 "argument for #{name.inspect} must be #{type}, #{value} given" 103 end 104 end 105 106 params << value 107 param_types << type 108 end 109 else 110 Util.assert_equal(@params_metadata.size, args.size) do 111 "expecting exactly #{@params_metadata.size} bind parameters, " \ 112 "#{args.size} given" 113 end 114 @params_metadata.zip(args) do |(_, _, name, type), value| 115 if NOT_SET.eql?(value) 116 if @connection_options.protocol_version < 4 117 raise ::ArgumentError, 118 "argument #{name.inspect} it not present in #{args.inspect}" 119 end 120 else 121 Util.assert_type(type, value) do 122 "argument for #{name.inspect} must be #{type}, #{value} given" 123 end 124 end 125 126 params << value 127 param_types << type 128 end 129 end 130 131 # params_metadata is an array of column-specs; each column-spec is an array 132 # of keyspace, tablename, other stuff. We only care about the keyspace name. 133 # See read_prepared_metadata_v4 in coder.rb for more details. 134 keyspace_name = @params_metadata.first.first unless @params_metadata.empty? 135 136 partition_key = create_partition_key(params) 137 138 Bound.new(@id, 139 @cql, 140 param_types, 141 @result_metadata, 142 params, 143 keyspace_name, 144 partition_key, 145 @idempotent) 146 end
execution_info()
click to toggle source
@return [Cassandra::Execution::Info] execution info for PREPARE request
# File lib/cassandra/statements/prepared.rb 149 def execution_info 150 @info ||= Execution::Info.new(@payload, 151 @warnings, 152 @keyspace, 153 @statement, 154 @options, 155 @hosts, 156 @consistency, 157 @retries, 158 @trace_id ? 159 Execution::Trace.new(@trace_id, @client, @options.load_balancing_policy) : 160 nil) 161 end
inspect()
click to toggle source
@return [String] a CLI-friendly prepared statement representation
# File lib/cassandra/statements/prepared.rb 169 def inspect 170 "#<#{self.class.name}:0x#{object_id.to_s(16)} @cql=#{@cql.inspect}>" 171 end
Private Instance Methods
create_partition_key(values)
click to toggle source
# File lib/cassandra/statements/prepared.rb 175 def create_partition_key(values) 176 partition_key = @partition_key 177 return nil if partition_key.empty? || partition_key.size > values.size 178 params_metadata = @params_metadata 179 180 buffer = Protocol::CqlByteBuffer.new 181 if partition_key.one? 182 i = partition_key.first 183 value = values[i] 184 metadata = params_metadata[i] 185 name = metadata[2] 186 type = metadata[3] 187 188 if NOT_SET.eql?(value) 189 raise ::ArgumentError, "argument #{name.inspect} is a part of " \ 190 'the partition key and must be present.' 191 end 192 193 if @connection_options.protocol_version >= 4 194 Protocol::Coder.write_value_v4(buffer, value, type) 195 elsif @connection_options.protocol_version >= 3 196 Protocol::Coder.write_value_v3(buffer, value, type) 197 else 198 Protocol::Coder.write_value_v1(buffer, value, type) 199 end 200 201 buffer.discard(4) # discard size 202 else 203 buf = Protocol::CqlByteBuffer.new 204 partition_key.each do |ind| 205 value = values[ind] 206 metadata = params_metadata[ind] 207 name = metadata[2] 208 type = metadata[3] 209 210 if NOT_SET.eql?(value) 211 raise ::ArgumentError, "argument #{name.inspect} is a part of " \ 212 'the partition key and must be present.' 213 end 214 215 if @connection_options.protocol_version >= 4 216 Protocol::Coder.write_value_v4(buf, value, type) 217 elsif @connection_options.protocol_version >= 3 218 Protocol::Coder.write_value_v3(buf, value, type) 219 else 220 Protocol::Coder.write_value_v1(buf, value, type) 221 end 222 223 buf.discard(4) # discard size 224 225 size = buf.length 226 buffer.append_short(size) 227 buffer << buf.read(size) << NULL_BYTE 228 end 229 end 230 231 buffer.to_str 232 end