Module: Cequel::Type

Defined in:
lib/cequel/type.rb

Overview

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

Defined Under Namespace

Classes: Ascii, Base, Bigint, Blob, Boolean, Counter, Decimal, Double, Float, Inet, Int, String, Text, Timestamp, Timeuuid, Uuid, Varint

Constant Summary collapse

UnknownType =

Raised if an unknown type is looked up

Since:

  • 1.0.0

Class.new(ArgumentError)
BY_CQL_NAME =

Since:

  • 1.0.0

{}
BY_INTERNAL_NAME =

Since:

  • 1.0.0

{}

Class Method Summary collapse

Class Method Details

.[](cql_name) ⇒ Base

Return a type corresponding to the given input

Parameters:

  • cql_name (Symbol, Base)

    CQL name of a type, or a type

Returns:

  • (Base)

    type with the given CQL name

Since:

  • 1.0.0



46
47
48
# File 'lib/cequel/type.rb', line 46

def self.[](cql_name)
  cql_name.is_a?(Base) ? cql_name : lookup_cql(cql_name)
end

.lookup_cql(cql_name) ⇒ Base

Look up a type by CQL name

Parameters:

  • cql_name (Symbol)

    CQL name of a type

Returns:

  • (Base)

    type with the given CQL name

Raises:

  • (UnknownType)

    if no type by that name is registered

Since:

  • 1.0.0



57
58
59
60
61
# File 'lib/cequel/type.rb', line 57

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) ⇒ Base

Look up a type by internal name

Parameters:

  • internal_name (String)

    internal name of a type

Returns:

  • (Base)

    type with the given internal name

Raises:

  • (UnknownType)

    if no type by that name is registered

Since:

  • 1.0.0



70
71
72
73
74
# File 'lib/cequel/type.rb', line 70

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) ⇒ String

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

Returns:

Since:

  • 1.0.0



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cequel/type.rb', line 82

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) ⇒ void

This method returns an undefined value.

Register a type for lookup

Parameters:

  • type (Type)

    a new type

Since:

  • 1.0.0



32
33
34
35
36
37
38
# File 'lib/cequel/type.rb', line 32

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