Class: MysqlInspector::Table
- Inherits:
-
Object
- Object
- MysqlInspector::Table
- Defined in:
- lib/mysql_inspector/table.rb
Constant Summary collapse
- BACKTICK_WORD =
/`([^`]+)`/
- BACKTICK_CSV =
/\(([^\)]+)\)/
- REFERENCE_OPTION =
/RESTRICT|CASCADE|SET NULL|NO ACTION/
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#columns ⇒ Object
Public: Get all of the columns defined in the table.
-
#constraints ⇒ Object
Public: Get all of the constraints defined in the table.
- #eql?(other) ⇒ Boolean (also: #==)
-
#indices ⇒ Object
Public: Get all of the indices defined in the table.
-
#initialize(schema) ⇒ Table
constructor
A new instance of Table.
- #options ⇒ Object
-
#table_name ⇒ Object
Public: Get then name of the table.
- #to_simple_schema ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize(schema) ⇒ Table
Returns a new instance of Table.
8 9 10 11 12 |
# File 'lib/mysql_inspector/table.rb', line 8 def initialize(schema) @schema = schema @lines = schema.split("\n") @lines.delete_if { |line| line =~ /(\/\*|--)/ or line.strip.empty? } end |
Instance Method Details
#<=>(other) ⇒ Object
103 104 105 |
# File 'lib/mysql_inspector/table.rb', line 103 def <=>(other) table_name <=> other.table_name end |
#columns ⇒ Object
Public: Get all of the columns defined in the table.
Returns an Array of MysqlInspector::Column.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mysql_inspector/table.rb', line 27 def columns @columns ||= @lines.map { |line| if line.strip =~ /^#{BACKTICK_WORD} ([\w\(\)\d]+)/ name = $1 sql_type = $2 nullable = !!(line !~ /NOT NULL/) default = line[/DEFAULT ('?[^']+'?)/, 1] default = nil if default =~ /NULL/ auto_increment = !!(line =~ /AUTO_INCREMENT/) table_part line, MysqlInspector::Column.new(name, sql_type, nullable, default, auto_increment) end }.compact.sort end |
#constraints ⇒ Object
Public: Get all of the constraints defined in the table
Returns an Array of MysqlInspector::Constraint.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mysql_inspector/table.rb', line 63 def constraints @constraints ||= @lines.map { |line| if line.strip =~ /^CONSTRAINT #{BACKTICK_WORD} FOREIGN KEY #{BACKTICK_CSV} REFERENCES #{BACKTICK_WORD} #{BACKTICK_CSV} ON DELETE (#{REFERENCE_OPTION}) ON UPDATE (#{REFERENCE_OPTION})$/ name = $1 column_names = backtick_names_in_csv($2) foreign_name = $3 foreign_column_names = backtick_names_in_csv($4) on_delete = $5 on_update = $6 table_part line, MysqlInspector::Constraint.new(name, column_names, foreign_name, foreign_column_names, on_update, on_delete) end }.compact.sort end |
#eql?(other) ⇒ Boolean Also known as: ==
93 94 95 96 97 98 99 |
# File 'lib/mysql_inspector/table.rb', line 93 def eql?(other) table_name == other.table_name && columns == other.columns && indices == other.indices && constraints == other.constraints && = other. end |
#indices ⇒ Object
Public: Get all of the indices defined in the table
Returns an Array of MysqlInspector::Index.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mysql_inspector/table.rb', line 44 def indices @indices ||= @lines.map { |line| if line.strip =~ /^(UNIQUE )?KEY #{BACKTICK_WORD} #{BACKTICK_CSV}/ unique = !!$1 name = $2 column_names = backtick_names_in_csv($3) table_part line, MysqlInspector::Index.new(name, column_names, unique) elsif line.strip =~ /^PRIMARY KEY #{BACKTICK_CSV}/ unique = true name = "PRIMARY KEY" column_names = backtick_names_in_csv($1) table_part line, MysqlInspector::Index.new(name, column_names, unique) end }.compact.sort end |
#options ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mysql_inspector/table.rb', line 77 def @options ||= begin if line = @lines.find { |line| line =~ /ENGINE=/} # AUTO_INCREMENT is not useful. line.sub!(/AUTO_INCREMENT=\d+/, '') # Compact multiple spaces. line.gsub!(/\s+/, ' ') # Remove paren at the beginning. line.sub!(/^\)\s*/, '') # Remove semicolon at the end. line.chomp!(';') line end end end |
#table_name ⇒ Object
Public: Get then name of the table.
Returns a String.
17 18 19 20 21 22 |
# File 'lib/mysql_inspector/table.rb', line 17 def table_name @table_name ||= begin line = @lines.find { |line| line =~ /CREATE TABLE #{BACKTICK_WORD}/} $1 if line end end |
#to_simple_schema ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mysql_inspector/table.rb', line 107 def to_simple_schema lines = [] lines << "CREATE TABLE `#{table_name}`" lines << nil simple_schema_items(lines, columns) simple_schema_items(lines, indices) simple_schema_items(lines, constraints) lines << lines.join("\n") end |
#to_sql ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/mysql_inspector/table.rb', line 120 def to_sql lines = [] lines << "CREATE TABLE `#{table_name}` (" lines << (columns + indices + constraints).map { |x| " #{x.to_sql}" }.join(",\n") lines << ") #{}" lines.join("\n") end |