Class: Cassandra::Statements::Prepared

Inherits:
Object
  • Object
show all
Includes:
Cassandra::Statement
Defined in:
lib/cassandra/statements/prepared.rb

Overview

A prepared statement is created by calling Cassandra::Session#prepare or Cassandra::Session#prepare_async.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cassandra::Statement

#idempotent?

Instance Attribute Details

#cqlString (readonly)



27
28
29
# File 'lib/cassandra/statements/prepared.rb', line 27

def cql
  @cql
end

Instance Method Details

#bind(args = nil) ⇒ Cassandra::Statements::Bound

Creates a statement bound with specific arguments



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cassandra/statements/prepared.rb', line 78

def bind(args = nil)
  if args
    Util.assert_instance_of_one_of([::Array, ::Hash], args) do
      "args must be an Array or a Hash, #{args.inspect} given"
    end
  else
    args = EMPTY_LIST
  end

  params = []
  param_types = []

  if args.is_a?(::Hash)
    @params_metadata.each do |(_, _, name, type)|
      name = name.to_sym unless args.key?(name)
      value = args.fetch(name, NOT_SET)

      if NOT_SET.eql?(value)
        if @connection_options.protocol_version < 4
          raise ::ArgumentError,
                "argument #{name.inspect} it not present in #{args.inspect}"
        end
      else
        Util.assert_type(type, value) do
          "argument for #{name.inspect} must be #{type}, #{value} given"
        end
      end

      params << value
      param_types << type
    end
  else
    Util.assert_equal(@params_metadata.size, args.size) do
      "expecting exactly #{@params_metadata.size} bind parameters, " \
          "#{args.size} given"
    end
    @params_metadata.zip(args) do |(_, _, name, type), value|
      if NOT_SET.eql?(value)
        if @connection_options.protocol_version < 4
          raise ::ArgumentError,
                "argument #{name.inspect} it not present in #{args.inspect}"
        end
      else
        Util.assert_type(type, value) do
          "argument for #{name.inspect} must be #{type}, #{value} given"
        end
      end

      params << value
      param_types << type
    end
  end

  # params_metadata is an array of column-specs; each column-spec is an array
  # of keyspace, tablename, other stuff. We only care about the keyspace name.
  # See read_prepared_metadata_v4 in coder.rb for more details.
  keyspace_name = @params_metadata.first.first unless @params_metadata.empty?

  partition_key = create_partition_key(params)

  Bound.new(@id,
            @cql,
            param_types,
            @result_metadata,
            params,
            keyspace_name,
            partition_key,
            @idempotent)
end

#execution_infoCassandra::Execution::Info



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cassandra/statements/prepared.rb', line 149

def execution_info
  @info ||= Execution::Info.new(@payload,
                                @warnings,
                                @keyspace,
                                @statement,
                                @options,
                                @hosts,
                                @consistency,
                                @retries,
                                @trace_id ?
                                    Execution::Trace.new(@trace_id, @client, @options.load_balancing_policy) :
                                    nil)
end

#inspectString



169
170
171
# File 'lib/cassandra/statements/prepared.rb', line 169

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} @cql=#{@cql.inspect}>"
end