Class: Innodb::Field
- Inherits:
-
Object
- Object
- Innodb::Field
- 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
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nullable ⇒ Object
readonly
Returns the value of attribute nullable.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Instance Method Summary collapse
- #blob? ⇒ Boolean
-
#extern(record, cursor) ⇒ Object
Read an InnoDB external pointer field.
-
#extern?(record) ⇒ Boolean
Return whether a part of this field is stored externally (off-page).
-
#initialize(position, name, type_definition, *properties) ⇒ Field
constructor
A new instance of Field.
-
#length(record) ⇒ Object
Return the actual length of this variable-length field.
-
#null?(record) ⇒ Boolean
Return whether this field is NULL.
-
#nullable? ⇒ Boolean
Return whether this field can be NULL.
-
#read(record, cursor) ⇒ Object
Read an InnoDB encoded data field.
-
#value(record, cursor) ⇒ Object
Read the data value (e.g. encoded in the data).
- #variable? ⇒ Boolean
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_type ⇒ Object (readonly)
Returns the value of attribute data_type.
9 10 11 |
# File 'lib/innodb/field.rb', line 9 def data_type @data_type end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/innodb/field.rb', line 9 def name @name end |
#nullable ⇒ Object (readonly)
Returns the value of attribute nullable.
9 10 11 |
# File 'lib/innodb/field.rb', line 9 def nullable @nullable end |
#position ⇒ Object (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
43 44 45 |
# File 'lib/innodb/field.rb', line 43 def blob? @data_type.is_a? Innodb::DataType::BlobType end |
#extern(record, cursor) ⇒ Object
Read an InnoDB external pointer field.
70 71 72 73 |
# File 'lib/innodb/field.rb', line 70 def extern(record, cursor) 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).
33 34 35 |
# File 'lib/innodb/field.rb', line 33 def extern?(record) record[:header][:field_externs][position] end |
#length(record) ⇒ Object
Return the actual length of this variable-length field.
48 49 50 51 52 53 54 55 |
# File 'lib/innodb/field.rb', line 48 def length(record) if variable? len = record[:header][:field_lengths][position] else len = @data_type.width end extern?(record) ? len - EXTERN_FIELD_SIZE : len end |
#null?(record) ⇒ Boolean
Return whether this field is NULL.
28 29 30 |
# File 'lib/innodb/field.rb', line 28 def null?(record) nullable? && record[:header][:field_nulls][position] end |
#nullable? ⇒ Boolean
Return whether this field can be NULL.
23 24 25 |
# File 'lib/innodb/field.rb', line 23 def nullable? @nullable end |
#read(record, cursor) ⇒ Object
Read an InnoDB encoded data field.
58 59 60 |
# File 'lib/innodb/field.rb', line 58 def read(record, cursor) cursor.name(@data_type.name) { cursor.get_bytes(length(record)) } end |
#value(record, cursor) ⇒ Object
Read the data value (e.g. encoded in the data).
63 64 65 66 67 |
# File 'lib/innodb/field.rb', line 63 def value(record, cursor) return :NULL if null?(record) data = read(record, cursor) @data_type.respond_to?(:value) ? @data_type.value(data) : data end |
#variable? ⇒ 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 |