Class: StructuredDataToSql::MysqlDumpXml::TableStructure
- Inherits:
-
Object
- Object
- StructuredDataToSql::MysqlDumpXml::TableStructure
- Defined in:
- lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#indexes ⇒ Object
readonly
Returns the value of attribute indexes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#primary_keys ⇒ Object
readonly
Returns the value of attribute primary_keys.
-
#unique_keys ⇒ Object
readonly
Returns the value of attribute unique_keys.
Instance Method Summary collapse
- #add_column(attrs) ⇒ Object
- #column_names ⇒ Object
-
#initialize(name) ⇒ TableStructure
constructor
A new instance of TableStructure.
- #to_create_table_sql ⇒ Object
Constructor Details
#initialize(name) ⇒ TableStructure
Returns a new instance of TableStructure.
10 11 12 13 14 15 16 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 10 def initialize(name) @name = name @columns = [] @primary_keys = [] @indexes = {} @unique_keys = {} end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
8 9 10 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 8 def columns @columns end |
#indexes ⇒ Object (readonly)
Returns the value of attribute indexes.
8 9 10 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 8 def indexes @indexes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 8 def name @name end |
#primary_keys ⇒ Object (readonly)
Returns the value of attribute primary_keys.
8 9 10 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 8 def primary_keys @primary_keys end |
#unique_keys ⇒ Object (readonly)
Returns the value of attribute unique_keys.
8 9 10 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 8 def unique_keys @unique_keys end |
Instance Method Details
#add_column(attrs) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 18 def add_column(attrs) col = { name: attrs["Field"].to_s, type: attrs["Type"] || "text", null: attrs["Null"] == "YES", key: attrs["Key"].to_s, default: attrs["Default"], extra: attrs["Extra"].to_s } @columns << col case col[:key] when "PRI" @primary_keys << col[:name] when "UNI" @unique_keys["uk_#{col[:name]}"] = [col[:name]] when "MUL" @indexes["idx_#{col[:name]}"] = [col[:name]] end end |
#column_names ⇒ Object
38 39 40 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 38 def column_names @columns.map { |col| col[:name] } end |
#to_create_table_sql ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/structured_data_to_sql/mysql_dump_xml/table_structure.rb', line 42 def to_create_table_sql lines = @columns.map do |col| definition = " #{SqlText.escape_identifier(col[:name])} #{col[:type]}" definition += " NOT NULL" unless col[:null] if col[:default] && col[:default] != "null" default = col[:default] definition += if default.start_with?("CURRENT_") || default == "NULL" || numeric_type?(col[:type]) " DEFAULT #{default}" else " DEFAULT #{SqlText.escape_sql_string(default)}" end elsif col[:null] && col[:default] == "null" definition += " DEFAULT NULL" end if col[:extra].downcase.include?("auto_increment") definition += " AUTO_INCREMENT" elsif col[:extra].downcase.include?( "on update current_timestamp" ) || col[:extra].include?("DEFAULT_GENERATED") definition += " ON UPDATE CURRENT_TIMESTAMP" unless definition.upcase.include?( "ON UPDATE CURRENT_TIMESTAMP" ) end definition end if @primary_keys.any? lines << " PRIMARY KEY (#{@primary_keys.map { |key| SqlText.escape_identifier(key) }.join(", ")})" end @unique_keys.each do |name, cols| lines << " UNIQUE KEY #{SqlText.escape_identifier(name)} (#{cols.map { |col| SqlText.escape_identifier(col) }.join(", ")})" end @indexes.each do |name, cols| lines << " KEY #{SqlText.escape_identifier(name)} (#{cols.map { |col| SqlText.escape_identifier(col) }.join(", ")})" end "DROP TABLE IF EXISTS #{SqlText.escape_identifier(@name)};\n" \ "CREATE TABLE #{SqlText.escape_identifier(@name)} (\n" \ "#{lines.join(",\n")}\n" \ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n" end |