Class: Vertica::DataType

Inherits:
Object
  • Object
show all
Defined in:
lib/vertica/data_type.rb

Overview

Class that represents a data type of a column.

This gem is only able to handle registered types. Types are registered using DataType.register. If an unregistered type is encountered, the library will raise Error::UnknownTypeError.

Examples:

Handling an unknown OID:

Vertica::DataType.register 12345, 'fancy_type', lambda { |bytes| ... }

See Also:

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oid: nil, name: nil, size: nil, modifier: nil, format: 0, deserializer: nil) ⇒ DataType

Instantiates a new DataType.

Parameters:

  • oid (Integer) (defaults to: nil)

    The object ID of the type.

  • name (String) (defaults to: nil)

    The name of the type as it can be used in SQL.

  • size (Integer) (defaults to: nil)

    The size of the type.

  • modifier (Integer) (defaults to: nil)

    A modifier of the type.

  • format (Integer) (defaults to: 0)

    The serialization format of this type.

  • deserializer (Proc) (defaults to: nil)

    Proc that can deserialize values of this type coming from the database.

See Also:



67
68
69
# File 'lib/vertica/data_type.rb', line 67

def initialize(oid: nil, name: nil, size: nil, modifier: nil, format: 0, deserializer: nil)
  @oid, @name, @size, @modifier, @format, @deserializer = oid, name, size, modifier, format, deserializer
end

Class Attribute Details

.registered_typesHash<Integer, Hash>

Returns The Vertica types that are registered with this library, indexed by OID.

Returns:

  • (Hash<Integer, Hash>)

    The Vertica types that are registered with this library, indexed by OID.

See Also:



22
23
24
# File 'lib/vertica/data_type.rb', line 22

def registered_types
  @registered_types
end

Instance Attribute Details

#deserializerProc (readonly)

Proc that can deserialize values of this type coming from the database.

Returns:

  • (Proc)

    the current value of deserializer



17
18
19
# File 'lib/vertica/data_type.rb', line 17

def deserializer
  @deserializer
end

#formatInteger (readonly)

The serialization format of this type.

Returns:

  • (Integer)

    the current value of format



17
18
19
# File 'lib/vertica/data_type.rb', line 17

def format
  @format
end

#modifierInteger (readonly)

A modifier of the type.

Returns:

  • (Integer)

    the current value of modifier



17
18
19
# File 'lib/vertica/data_type.rb', line 17

def modifier
  @modifier
end

#nameString (readonly)

The name of the type as it can be used in SQL.

Returns:

  • (String)

    the current value of name



17
18
19
# File 'lib/vertica/data_type.rb', line 17

def name
  @name
end

#oidInteger (readonly)

The object ID of the type.

Returns:

  • (Integer)

    the current value of oid



17
18
19
# File 'lib/vertica/data_type.rb', line 17

def oid
  @oid
end

#sizeInteger (readonly)

The size of the type.

Returns:

  • (Integer)

    the current value of size



17
18
19
# File 'lib/vertica/data_type.rb', line 17

def size
  @size
end

Class Method Details

.build(oid: nil, **kwargs) ⇒ Vertica::DataType

Builds a new type instance based on an OID.

Parameters:

  • oid (Integer) (defaults to: nil)

    The object ID of the type.

  • name (String)

    The name of the type as it can be used in SQL.

  • size (Integer)

    The size of the type.

  • modifier (Integer)

    A modifier of the type.

  • format (Integer)

    The serialization format of this type.

  • deserializer (Proc)

    Proc that can deserialize values of this type coming from the database.

Returns:

Raises:



40
41
42
43
44
45
46
# File 'lib/vertica/data_type.rb', line 40

def build(oid: nil, **kwargs)
  args = registered_types.fetch(oid) do |unknown_oid|
    raise Vertica::Error::UnknownTypeError, "Unknown type OID: #{unknown_oid}"
  end

  new(args.merge(kwargs))
end

.default_deserializerSymbol

The name of the default deserializer proc.

Returns:

  • (Symbol)


50
51
52
# File 'lib/vertica/data_type.rb', line 50

def default_deserializer
  :generic
end

.register(oid, name, deserializer = self.default_deserializer)

This method returns an undefined value.

Registers a new type by OID.

Parameters:

  • oid (Integer)

    The object ID of the type.

  • name (String)

    The name of the type as it can be used in SQL.

  • deserializer (Proc) (defaults to: self.default_deserializer)

    Proc that can deserialize values of this type coming from the database.



31
32
33
34
# File 'lib/vertica/data_type.rb', line 31

def register(oid, name, deserializer = self.default_deserializer)
  self.registered_types ||= {}
  self.registered_types[oid] = { oid: oid, name: name, deserializer: TYPE_DESERIALIZERS.fetch(deserializer) }
end

Instance Method Details

#deserialize(bytes) ⇒ Object

Deserializes a value of this type as returned by the server.

Parameters:

  • bytes (String, nil)

    The representation of the value returned by the server.

Returns:

  • (Object)

    The Ruby-value taht repesents the value returned from the DB.

See Also:



88
89
90
91
# File 'lib/vertica/data_type.rb', line 88

def deserialize(bytes)
  return nil if bytes.nil?
  deserializer.call(bytes)
end

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

Returns true iff this record is equal to the other provided object

Returns:

  • (Boolean)

    Returns true iff this record is equal to the other provided object



77
78
79
80
# File 'lib/vertica/data_type.rb', line 77

def eql?(other)
  other.kind_of?(Vertica::DataType) && oid == other.oid && size == other.size &&
    modifier == other.modifier && other.format == format
end

#hashInteger

Returns a hash digtest of this object.

Returns:

  • (Integer)

    Returns a hash digtest of this object.



72
73
74
# File 'lib/vertica/data_type.rb', line 72

def hash
  [oid, size, modifier, format].hash
end

#inspectString

Returns a user-consumable string representation of this type.

Returns:

  • (String)

    Returns a user-consumable string representation of this type.



94
95
96
# File 'lib/vertica/data_type.rb', line 94

def inspect
  "#<#{self.class.name}:#{oid} #{sql.inspect}>"
end

#sqlString

TODO:

Take size and modifier into account.

Returns a SQL representation of this type.

Returns:

  • (String)


101
102
103
# File 'lib/vertica/data_type.rb', line 101

def sql
  name
end