Class: Innodb::RecordDescriber
- Inherits:
-
Object
- Object
- Innodb::RecordDescriber
- Defined in:
- lib/innodb/record_describer.rb
Overview
A class to describe record layouts for InnoDB indexes. Designed to be usable in two different ways: statically built and dynamically built. Note that in both cases, the order that statements are encountered is critical. Columns must be added to the key and row structures in the correct order.
STATIC USAGE
Static building is useful for building a custom describer for any index and looks like the following:
To describe the SQL syntax:
CREATE TABLE my_table (
id BIGINT NOT NULL,
name VARCHAR(100) NOT NULL,
age INT UNSIGNED,
PRIMARY KEY (id)
);
The clustered key would require a class like:
class MyTableClusteredDescriber < Innodb::RecordDescriber
type :clustered
key "id", :BIGINT, :UNSIGNED, :NOT_NULL
row "name", "VARCHAR(100)", :NOT_NULL
row "age", :INT, :UNSIGNED
end
It can then be instantiated as usual:
my_table_clustered = MyTableClusteredDescriber.new
All statically-defined type, key, and row information will be copied into the instance when it is initialized. Once initialized, the instance can be additionally used dynamically, as per below. (A dynamic class is just the same as a static class that is empty.)
Note that since InnoDB works in terms of indexes individually, a new class must be created for each index.
DYNAMIC USAGE
If a record describer needs to be built based on runtime information, such as index descriptions from a live data dictionary, instances can be built dynamically. For the same table above, this would require:
my_table_clustered = Innodb::RecordDescriber.new
my_table_clustered.type = :clustered
my_table_clustered.key "id", :BIGINT, :UNSIGNED, :NOT_NULL
my_table_clustered.row "name", "VARCHAR(100)", :NOT_NULL
my_table_clustered.row "age", :INT, :UNSIGNED
Direct Known Subclasses
DataDictionary::SYS_COLUMNS_PRIMARY, DataDictionary::SYS_FIELDS_PRIMARY, DataDictionary::SYS_INDEXES_PRIMARY, DataDictionary::SYS_TABLES_ID, DataDictionary::SYS_TABLES_PRIMARY
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
Class Method Summary collapse
-
.add_static_field(group, name, type) ⇒ Object
An internal method wrapped with ‘key’ and ‘row’ helpers.
-
.key(name, *type) ⇒ Object
A ‘key’ method to be used from the DSL.
-
.row(name, *type) ⇒ Object
A ‘row’ method to be used from the DSL.
-
.static_description ⇒ Object
Internal method to initialize the class’s instance variable on access.
-
.type(type) ⇒ Object
A ‘type’ method to be used from the DSL.
Instance Method Summary collapse
-
#add_field(group, name, type) ⇒ Object
An internal method wrapped with ‘key’ and ‘row’ helpers.
-
#initialize ⇒ RecordDescriber
constructor
A new instance of RecordDescriber.
-
#key(name, *type) ⇒ Object
Add a key column to the record description.
-
#row(name, *type) ⇒ Object
Add a row (non-key) column to the record description.
-
#type(type) ⇒ Object
Set the type of this record (:clustered or :secondary).
Constructor Details
#initialize ⇒ RecordDescriber
Returns a new instance of RecordDescriber.
89 90 91 |
# File 'lib/innodb/record_describer.rb', line 89 def initialize @description = self.class.static_description.dup end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
87 88 89 |
# File 'lib/innodb/record_describer.rb', line 87 def description @description end |
Class Method Details
.add_static_field(group, name, type) ⇒ Object
An internal method wrapped with ‘key’ and ‘row’ helpers.
73 74 75 |
# File 'lib/innodb/record_describer.rb', line 73 def self.add_static_field(group, name, type) static_description[group] << { :name => name, :type => type } end |
.key(name, *type) ⇒ Object
A ‘key’ method to be used from the DSL.
78 79 80 |
# File 'lib/innodb/record_describer.rb', line 78 def self.key(name, *type) add_static_field :key, name, type end |
.row(name, *type) ⇒ Object
A ‘row’ method to be used from the DSL.
83 84 85 |
# File 'lib/innodb/record_describer.rb', line 83 def self.row(name, *type) add_static_field :row, name, type end |
.static_description ⇒ Object
Internal method to initialize the class’s instance variable on access.
59 60 61 62 63 64 65 |
# File 'lib/innodb/record_describer.rb', line 59 def self.static_description @static_description ||= { :type => nil, :key => [], :row => [] } end |
.type(type) ⇒ Object
A ‘type’ method to be used from the DSL.
68 69 70 |
# File 'lib/innodb/record_describer.rb', line 68 def self.type(type) static_description[:type] = type end |
Instance Method Details
#add_field(group, name, type) ⇒ Object
An internal method wrapped with ‘key’ and ‘row’ helpers.
99 100 101 |
# File 'lib/innodb/record_describer.rb', line 99 def add_field(group, name, type) description[group] << { :name => name, :type => type } end |
#key(name, *type) ⇒ Object
Add a key column to the record description.
104 105 106 |
# File 'lib/innodb/record_describer.rb', line 104 def key(name, *type) add_field :key, name, type end |
#row(name, *type) ⇒ Object
Add a row (non-key) column to the record description.
109 110 111 |
# File 'lib/innodb/record_describer.rb', line 109 def row(name, *type) add_field :row, name, type end |
#type(type) ⇒ Object
Set the type of this record (:clustered or :secondary).
94 95 96 |
# File 'lib/innodb/record_describer.rb', line 94 def type(type) description[:type] = type end |