Class: Cassandra::Statements::Simple

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cassandra::Statement

#idempotent?

Constructor Details

#initialize(cql, params = nil, type_hints = nil, idempotent = false) ⇒ Simple

Note:

Positional arguments for simple statements are only supported starting with Apache Cassandra 2.0 and above.

Note:

Named arguments for simple statements are only supported starting with Apache Cassandra 2.1 and above.

Returns a new instance of Simple.

Parameters:

  • cql (String)

    a cql statement

  • params (Array, Hash) (defaults to: nil)

    (nil) positional or named arguments for the query

  • type_hints (Array, Hash) (defaults to: nil)

    (nil) positional or named types to override type guessing for the query

  • idempotent (Boolean) (defaults to: false)

    (false) whether this statement can be safely retries on timeouts

Raises:

  • (ArgumentError)

    if cql statement given is not a String



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cassandra/statements/simple.rb', line 50

def initialize(cql, params = nil, type_hints = nil, idempotent = false)
  Util.assert_instance_of(::String, cql) do
    "cql must be a string, #{cql.inspect} given"
  end

  params ||= EMPTY_LIST

  if params.is_a?(::Hash)
    params_names = []
    params = params.each_with_object([]) do |(name, value), collector|
      params_names << name
      collector    << value
    end
    if type_hints && !type_hints.empty?
      Util.assert_instance_of(::Hash, type_hints) do
        'type_hints must be a Hash when using named params'
      end
    end
  else
    Util.assert_instance_of(::Array, params) do
      "params must be an Array or a Hash, #{params.inspect} given"
    end
    params_names = EMPTY_LIST
  end

  type_hints ||= EMPTY_LIST

  if type_hints.is_a?(::Hash)
    type_hints = params_names.map {|name| type_hints[name] }
  else
    Util.assert_instance_of(::Array, type_hints) do
      "type_hints must be an Array or a Hash, #{type_hints.inspect} given"
    end
  end

  @cql          = cql
  @params       = params
  @params_types = params.each_with_index.map do |value, index|
    (!type_hints.empty? && type_hints[index] && type_hints[index].is_a?(Type)) ?
        type_hints[index] :
        Util.guess_type(value)
  end
  @params_names = params_names
  @idempotent   = idempotent
end

Instance Attribute Details

#cqlString (readonly)

Returns original cql used to prepare this statement.

Returns:

  • (String)

    original cql used to prepare this statement



25
26
27
# File 'lib/cassandra/statements/simple.rb', line 25

def cql
  @cql
end

#paramsArray<Object> (readonly)

Returns a list of positional parameters for the cql.

Returns:

  • (Array<Object>)

    a list of positional parameters for the cql



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

def params
  @params
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns whether the statements are equal.

Parameters:

Returns:

  • (Boolean)

    whether the statements are equal



109
110
111
112
113
# File 'lib/cassandra/statements/simple.rb', line 109

def eql?(other)
  other.is_a?(Simple) &&
    @cql == other.cql &&
    @params == other.params
end

#inspectString

Returns a CLI-friendly simple statement representation.

Returns:

  • (String)

    a CLI-friendly simple statement representation



102
103
104
105
# File 'lib/cassandra/statements/simple.rb', line 102

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