Method: Cql::Model::Query.cql_value

Defined in:
lib/cql/model/query.rb

.cql_value(value, context = nil) ⇒ String

Transform a Ruby object into its CQL literal value representation. A literal value is anything that can appear in a CQL statement as a key or column value (but not column NAME; see #cql_identifier to convert values to column names).

When used as a key or column value, CQL supports the following kinds of literal value:

* unquoted identifier (treated as a string value)
* string literal
* integer number
* UUID
* floating-point number
* boolean true/false

When used as a column name, any value that is not a valid identifier MUST BE ENCLOSED IN DOUBLE QUOTES. This method does not handle the double-quote escaping; see #cql_identifier for that.

Parameters:

  • value (String, Numeric, Boolean, Array, Set, Array, #map)

Returns:

  • (String)

    the CQL equivalent of value

See Also:



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
95
96
97
# File 'lib/cql/model/query.rb', line 69

def cql_value(value, context=nil)
  # TODO UUID, Time, ...
  case value
  when String
    "#{SQ}#{value.gsub(SQ, SQSQ)}#{SQ}"
  when Numeric, TrueClass, FalseClass
    value.to_s
  when Set
    raise SyntaxError, "Set notation is not allowed outside UPDATE statements" unless (context == :update)
    '{' + value.map { |v| cql_value(v) }.join(', ') + '}'
  else
    if value.respond_to?(:map)
      if value.respond_to?(:each_pair)
        # Pairwise map -- CQL map literal
        '{' + value.map { |k, v| "#{cql_value(k)}: #{cql_value(v)}" }.join(', ') + '}'
      else
        # Single map -- CQL list (for UPDATE) or set (for WHERE...IN) literal
        case context
        when :update
          '[' + value.map { |v| cql_value(v) }.join(', ') + ']'
        else
          '(' + value.map { |v| cql_value(v) }.join(', ') + ')'
        end
      end
    else
      raise Cql::Model::SyntaxError, "Cannot convert #{value.class} to a CQL value"
    end
  end
end