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: the length of the fixed width portion of the field
Instance Attribute Summary collapse
-
#fixed_len ⇒ Object
readonly
Returns the value of attribute fixed_len.
-
#nullable ⇒ Object
readonly
Returns the value of attribute nullable.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#variable_len ⇒ Object
readonly
Returns the value of attribute variable_len.
Instance Method Summary collapse
-
#fixed_len_map ⇒ Object
Maps data type to fixed storage length.
-
#get_variable_len(record) ⇒ Object
Return the length of this variable-length field.
-
#initialize(position, type, *properties) ⇒ Field
constructor
A new instance of Field.
-
#null?(record) ⇒ Boolean
Return whether this field is NULL.
-
#parse_data_type(data_type) ⇒ Object
Parse the data type description string of a field.
-
#read(record, cursor) ⇒ Object
Read an InnoDB encoded data field.
Constructor Details
#initialize(position, type, *properties) ⇒ Field
Returns a new instance of Field.
7 8 9 10 11 12 |
# File 'lib/innodb/field.rb', line 7 def initialize(position, type, *properties) @position = position @type, @fixed_len, @variable_len = parse_data_type(type.to_s) @nullable = (not properties.include?(:NOT_NULL)) @unsigned = properties.include?(:UNSIGNED) end |
Instance Attribute Details
#fixed_len ⇒ Object (readonly)
Returns the value of attribute fixed_len.
5 6 7 |
# File 'lib/innodb/field.rb', line 5 def fixed_len @fixed_len end |
#nullable ⇒ Object (readonly)
Returns the value of attribute nullable.
5 6 7 |
# File 'lib/innodb/field.rb', line 5 def nullable @nullable end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
5 6 7 |
# File 'lib/innodb/field.rb', line 5 def position @position end |
#variable_len ⇒ Object (readonly)
Returns the value of attribute variable_len.
5 6 7 |
# File 'lib/innodb/field.rb', line 5 def variable_len @variable_len end |
Instance Method Details
#fixed_len_map ⇒ Object
Maps data type to fixed storage length.
31 32 33 34 35 36 37 38 39 |
# File 'lib/innodb/field.rb', line 31 def fixed_len_map { :TINYINT => 1, :SMALLINT => 2, :MEDIUMINT => 3, :INT => 4, :BIGINT => 8, } end |
#get_variable_len(record) ⇒ Object
Return the length of this variable-length field.
51 52 53 54 55 56 57 |
# File 'lib/innodb/field.rb', line 51 def get_variable_len(record) case record[:format] when :compact header = record[:header] header[:variable_length][@position] end end |
#null?(record) ⇒ Boolean
Return whether this field is NULL.
42 43 44 45 46 47 48 |
# File 'lib/innodb/field.rb', line 42 def null?(record) case record[:format] when :compact header = record[:header] header[:null_bitmap][@position] end end |
#parse_data_type(data_type) ⇒ Object
Parse the data type description string of a field.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/innodb/field.rb', line 15 def parse_data_type(data_type) case data_type when /^(tinyint|smallint|mediumint|int|bigint)$/i type = data_type.upcase.to_sym fixed_len = fixed_len_map[type] [type, fixed_len, 0] when /^varchar\((\d+)\)$/i [:VARCHAR, 0, $1.to_i] when /^char\((\d+)\)$/i [:CHAR, $1.to_i, 0] else raise "Data type '#{data_type}' is not supported" end end |
#read(record, cursor) ⇒ Object
Read an InnoDB encoded data field.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/innodb/field.rb', line 60 def read(record, cursor) return :NULL if @nullable and null?(record) case @type when :TINYINT, :SMALLINT, :MEDIUMINT, :INT, :BIGINT symbol = @unsigned ? :get_uint_by_size : :get_i_sint_by_size cursor.send(symbol, @fixed_len) when :VARCHAR cursor.get_bytes(get_variable_len(record)) when :CHAR # Fixed-width character fields will be space-padded up to their length, # so SQL defines that trailing spaces should be removed. cursor.get_bytes(fixed_len).sub(/[ ]+$/, "") end end |