Class: Innodb::Field

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

Defined Under Namespace

Classes: ExternReference

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.



26
27
28
29
30
31
32
# File 'lib/innodb/field.rb', line 26

def initialize(position, name, type_definition, *properties)
  @position = position
  @name = name
  @nullable = !properties.delete(:NOT_NULL)
  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.



20
21
22
# File 'lib/innodb/field.rb', line 20

def data_type
  @data_type
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/innodb/field.rb', line 19

def name
  @name
end

#nullableObject (readonly)

Returns the value of attribute nullable.



21
22
23
# File 'lib/innodb/field.rb', line 21

def nullable
  @nullable
end

#positionObject (readonly)

Returns the value of attribute position.



18
19
20
# File 'lib/innodb/field.rb', line 18

def position
  @position
end

Instance Method Details

#blob?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/innodb/field.rb', line 57

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

#extern(cursor, record) ⇒ Object

Read an InnoDB external pointer field.



95
96
97
98
99
# File 'lib/innodb/field.rb', line 95

def extern(cursor, record)
  return unless 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)


45
46
47
# File 'lib/innodb/field.rb', line 45

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

#length(record) ⇒ Object

Return the actual length of this variable-length field.



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

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)


40
41
42
# File 'lib/innodb/field.rb', line 40

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

#nullable?Boolean

Return whether this field can be NULL.

Returns:

  • (Boolean)


35
36
37
# File 'lib/innodb/field.rb', line 35

def nullable?
  @nullable
end

#read(cursor, field_length) ⇒ Object

Read an InnoDB encoded data field.



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

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

#value(cursor, record) ⇒ Object

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



88
89
90
91
92
# File 'lib/innodb/field.rb', line 88

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

  value_by_length(cursor, length(record))
end

#value_by_length(cursor, field_length) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/innodb/field.rb', line 77

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)


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

def variable?
  [
    Innodb::DataType::BlobType,
    Innodb::DataType::VariableBinaryType,
    Innodb::DataType::VariableCharacterType,
  ].any? { |c| @data_type.is_a?(c) }
end