Class: Baza::Driver::Mysql::Sql::CreateIndexes
- Inherits:
-
Object
- Object
- Baza::Driver::Mysql::Sql::CreateIndexes
- Defined in:
- lib/baza/driver/mysql/sql/create_indexes.rb
Instance Method Summary collapse
-
#initialize(args) ⇒ CreateIndexes
constructor
A new instance of CreateIndexes.
- #sql ⇒ Object
Constructor Details
#initialize(args) ⇒ CreateIndexes
Returns a new instance of CreateIndexes.
2 3 4 5 6 7 |
# File 'lib/baza/driver/mysql/sql/create_indexes.rb', line 2 def initialize(args) @create = args[:create] @indexes = args.fetch(:indexes) @on_table = args[:on_table] @table_name = args.fetch(:table_name) end |
Instance Method Details
#sql ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/baza/driver/mysql/sql/create_indexes.rb', line 9 def sql sql = "" first = true @indexes.each do |index_data| sql << "CREATE" if @create || @create.nil? if index_data.is_a?(String) || index_data.is_a?(Symbol) index_data = {name: index_data, columns: [index_data]} end raise "No name was given: '#{index_data}'." if !index_data.key?(:name) || index_data[:name].to_s.strip.empty? raise "No columns was given on index: '#{index_data.fetch(:name)}'." if !index_data[:columns] || index_data[:columns].empty? if first first = false else sql << ", " end sql << " UNIQUE" if index_data[:unique] sql << " INDEX #{Baza::Driver::Mysql.quote_index(index_data.fetch(:name))}" if @on_table || @on_table.nil? sql << " ON #{Baza::Driver::Mysql.quote_table(@table_name)}" end sql << " (" first = true index_data[:columns].each do |col_name| sql << ", " unless first first = false if first sql << Baza::Driver::Mysql.quote_column(col_name) end sql << ")" end [sql] end |