Class: Innodb::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/field.rb

Overview

A single field in an InnoDB record (within an INDEX page). This class provides essential information to parse records, including the length of the fixed-width and variable-width portion of the field.

Constant Summary collapse

EXTERN_FIELD_SIZE =

Size of a reference to data stored externally to the page.

20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, name, type_definition, *properties) ⇒ Field

Returns a new instance of Field.



14
15
16
17
18
19
20
# File 'lib/innodb/field.rb', line 14

def initialize(position, name, type_definition, *properties)
  @position = position
  @name = name
  @nullable = properties.delete(:NOT_NULL) ? false : true
  base_type, modifiers = parse_type_definition(type_definition.to_s)
  @data_type = Innodb::DataType.new(base_type, modifiers, properties)
end

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type.



9
10
11
# File 'lib/innodb/field.rb', line 9

def data_type
  @data_type
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/innodb/field.rb', line 9

def name
  @name
end

#nullableObject (readonly)

Returns the value of attribute nullable.



9
10
11
# File 'lib/innodb/field.rb', line 9

def nullable
  @nullable
end

#positionObject (readonly)

Returns the value of attribute position.



9
10
11
# File 'lib/innodb/field.rb', line 9

def position
  @position
end

Instance Method Details

#blob?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/innodb/field.rb', line 43

def blob?
  @data_type.is_a? Innodb::DataType::BlobType
end

#extern(cursor, record) ⇒ Object

Read an InnoDB external pointer field.



80
81
82
83
# File 'lib/innodb/field.rb', line 80

def extern(cursor, record)
  return nil if not extern?(record)
  cursor.name(@name) { read_extern(cursor) }
end

#extern?(record) ⇒ Boolean

Return whether a part of this field is stored externally (off-page).

Returns:

  • (Boolean)


33
34
35
# File 'lib/innodb/field.rb', line 33

def extern?(record)
  record[:header][:externs].include?(@name)
end

#length(record) ⇒ Object

Return the actual length of this variable-length field.



48
49
50
51
52
53
54
55
56
# File 'lib/innodb/field.rb', line 48

def length(record)
  if record[:header][:lengths].include?(@name)
    len = record[:header][:lengths][@name]
    raise "Fixed-length mismatch" unless variable? || len == @data_type.width
  else
    len = @data_type.width
  end
  extern?(record) ? len - EXTERN_FIELD_SIZE : len
end

#null?(record) ⇒ Boolean

Return whether this field is NULL.

Returns:

  • (Boolean)


28
29
30
# File 'lib/innodb/field.rb', line 28

def null?(record)
  nullable? && record[:header][:nulls].include?(@name)
end

#nullable?Boolean

Return whether this field can be NULL.

Returns:

  • (Boolean)


23
24
25
# File 'lib/innodb/field.rb', line 23

def nullable?
  @nullable
end

#read(cursor, field_length) ⇒ Object

Read an InnoDB encoded data field.



59
60
61
# File 'lib/innodb/field.rb', line 59

def read(cursor, field_length)
  cursor.name(@data_type.name) { cursor.get_bytes(field_length) }
end

#value(cursor, record) ⇒ Object

Read the data value (e.g. encoded in the data).



74
75
76
77
# File 'lib/innodb/field.rb', line 74

def value(cursor, record)
  return :NULL if null?(record)
  value_by_length(cursor, length(record))
end

#value_by_length(cursor, field_length) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/innodb/field.rb', line 63

def value_by_length(cursor, field_length)
  if @data_type.respond_to?(:read)
    cursor.name(@data_type.name) { @data_type.read(cursor) }
  elsif @data_type.respond_to?(:value)
    @data_type.value(read(cursor, field_length))
  else
    read(cursor, field_length)
  end
end

#variable?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/innodb/field.rb', line 37

def variable?
  @data_type.is_a? Innodb::DataType::BlobType or
  @data_type.is_a? Innodb::DataType::VariableBinaryType or
  @data_type.is_a? Innodb::DataType::VariableCharacterType
end