Module: Cql::Model::Query

Defined in:
lib/cql/model/query/comparison_expression.rb,
lib/cql/model/query/mutation_statement.rb,
lib/cql/model/query/update_expression.rb,
lib/cql/model/query/update_statement.rb,
lib/cql/model/query/select_statement.rb,
lib/cql/model/query/insert_statement.rb,
lib/cql/model/query/expression.rb,
lib/cql/model/query/statement.rb,
lib/cql/model/query.rb

Defined Under Namespace

Classes: ComparisonExpression, Expression, InsertStatement, MutationStatement, SelectStatement, Statement, UpdateExpression, UpdateStatement

Constant Summary collapse

SQ =

CQL single quote character.

"'"
SQSQ =

CQL single-quote escape sequence.

"''"
DQ =

CQL double-quote character.

'"'
DQDQ =

CQL double-quote escape.

'""'
IDENTIFIER =

Valid CQL identifier (can be used as a column name without double-quoting)

/[a-z][a-z0-9_]*/i

Class Method Summary collapse

Class Method Details

.cql_column_names(list) ⇒ Object

Transform a list of symbols or strings into CQL column names. Performs no safety checks!!



20
21
22
23
24
25
26
# File 'lib/cql/model/query.rb', line 20

def cql_column_names(list)
  if list.empty?
    '*'
  else
    list.join(', ')
  end
end

.cql_identifier(value) ⇒ Object

Transform a Ruby object into its CQL identifier representation.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cql/model/query.rb', line 32

def cql_identifier(value)
  # TODO UUID, Time, ...
  case value
  when Symbol, String
    if value =~ IDENTIFIER
      value.to_s
    else
      "#{DQ}#{value.gsub(DQ, DQDQ)}#{DQ}"
    end
  when Numeric, TrueClass, FalseClass
    "#{DQ}#{cql_value(value)}#{DQ}"
  else
    raise Cql::Model::SyntaxError, "Cannot convert #{value.class} to a CQL identifier"
  end
end

.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