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.
- #field_names ⇒ Object
- #generate_class(name = "Describer_#{object_id}") ⇒ Object
-
#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 92 93 |
# File 'lib/innodb/record_describer.rb', line 89 def initialize @description = self.class.static_description.dup @description[:key] = @description[:key].dup @description[:row] = @description[:row].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.
101 102 103 |
# File 'lib/innodb/record_describer.rb', line 101 def add_field(group, name, type) description[group] << { :name => name, :type => type } end |
#field_names ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/innodb/record_describer.rb', line 115 def field_names names = [] [:key, :row].each do |group| names += description[group].map { |n| n[:name] } end names end |
#generate_class(name = "Describer_#{object_id}") ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/innodb/record_describer.rb', line 123 def generate_class(name="Describer_#{object_id}") str = "class #{name}\n" str << " type %s\n" % [ description[:type].inspect ] [:key, :row].each do |group| description[group].each do |item| str << " %s %s, %s\n" % [ group, item[:name].inspect, item[:type].map { |s| s.inspect }.join(", "), ] end end str << "end\n" str end |
#key(name, *type) ⇒ Object
Add a key column to the record description.
106 107 108 |
# File 'lib/innodb/record_describer.rb', line 106 def key(name, *type) add_field :key, name, type end |
#row(name, *type) ⇒ Object
Add a row (non-key) column to the record description.
111 112 113 |
# File 'lib/innodb/record_describer.rb', line 111 def row(name, *type) add_field :row, name, type end |
#type(type) ⇒ Object
Set the type of this record (:clustered or :secondary).
96 97 98 |
# File 'lib/innodb/record_describer.rb', line 96 def type(type) description[:type] = type end |