module Cequel::Type

The Type module encapsulates information about the CQL3 type system. Each type has a `cql_name`, which is the name of the type as defined in CQL, and an `internal_name`, which is the name of the type in the lower-level interface that is exposed when introspecting table information in the database.

As well as knowing their respective names, types also know how to cast Ruby objects to the correct canonical class corresponding to the type. These implicit types are used by the underlying `cassandra-cql` library to determine how to represent values when passing them to Cassandra.

@since 1.0.0

Constants

BY_CQL_NAME
BY_INTERNAL_NAME
UnknownType

Raised if an unknown type is looked up

Public Class Methods

[](cql_name) click to toggle source

Return a type corresponding to the given input

@param cql_name [Symbol,Base] CQL name of a type, or a type @return [Base] type with the given CQL name

# File lib/cequel/type.rb, line 45
def self.[](cql_name)
  cql_name.is_a?(Base) ? cql_name : lookup_cql(cql_name)
end
lookup_cql(cql_name) click to toggle source

Look up a type by CQL name

@param cql_name [Symbol] CQL name of a type @return [Base] type with the given CQL name @raise [UnknownType] if no type by that name is registered

# File lib/cequel/type.rb, line 56
def self.lookup_cql(cql_name)
  BY_CQL_NAME.fetch(cql_name.to_sym)
rescue KeyError
  raise UnknownType, "Unrecognized CQL type #{cql_name.inspect}"
end
lookup_internal(internal_name) click to toggle source

Look up a type by internal name

@param internal_name [String] internal name of a type @return [Base] type with the given internal name @raise [UnknownType] if no type by that name is registered

# File lib/cequel/type.rb, line 69
def self.lookup_internal(internal_name)
  BY_INTERNAL_NAME.fetch(internal_name)
rescue KeyError
  raise UnknownType, "Unrecognized internal type #{internal_name.inspect}"
end
quote(value) click to toggle source

Quote an arbitrary value for use in a CQL statement by inferring the equivalent CQL type to the value's Ruby type

@return [String] quoted value

# File lib/cequel/type.rb, line 81
def self.quote(value)
  if value.is_a?(Array)
    return value.map { |element| quote(element) }.join(',')
  end
  case value
  when Time, ActiveSupport::TimeWithZone
    (value.to_r * 1000).round.to_s
  when DateTime
    quote(value.utc.to_time)
  when ::Date
    quote(Time.gm(value.year, value.month, value.day))
  when Numeric, true, false, Cassandra::Uuid
    value.to_s
  else
    quote_string(value.to_s)
  end
end
register(type) click to toggle source

Register a type for lookup

@param type [Type] a new type @return [void]

# File lib/cequel/type.rb, line 31
def self.register(type)
  BY_CQL_NAME[type.cql_name] = type
  type.cql_aliases.each { |aliaz| BY_CQL_NAME[aliaz] = type }
  type.internal_names.each do |internal_name|
    BY_INTERNAL_NAME[internal_name] = type
  end
end

Private Class Methods

quote_string(string) click to toggle source
# File lib/cequel/type.rb, line 99
def self.quote_string(string)
  if string.encoding == Encoding::ASCII_8BIT && string =~ /^[[:xdigit:]]+$/
    "0x#{string}"
  else
    "'#{string.gsub("'", "''")}'"
  end
end