Sha256: 5458d6ece1735460c2a956eed98179084f25bc0a4646284bf8c8cf1be08883ab

Contents?: true

Size: 1.52 KB

Versions: 8

Compression:

Stored size: 1.52 KB

Contents

class Baza::Driver::Mysql::Sql::CreateIndexes
  def initialize(args)
    @create = args[:create]
    @indexes = args.fetch(:indexes)
    @on_table = args[:on_table]
    @table_name = args.fetch(:table_name)
  end

  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::SEPARATOR_INDEX}#{Baza::Driver::Mysql.escape_index(index_data.fetch(:name))}#{Baza::Driver::Mysql::SEPARATOR_INDEX}"

      if @on_table || @on_table.nil?
        sql << " ON #{Baza::Driver::Mysql::SEPARATOR_TABLE}#{Baza::Driver::Mysql.escape_table(@table_name)}#{Baza::Driver::Mysql::SEPARATOR_TABLE}"
      end

      sql << " ("

      first = true
      index_data[:columns].each do |col_name|
        sql << ", " unless first
        first = false if first

        sql << "#{Baza::Driver::Mysql::SEPARATOR_COLUMN}#{Baza::Driver::Mysql.escape_column(col_name)}#{Baza::Driver::Mysql::SEPARATOR_COLUMN}"
      end

      sql << ")"
    end

    [sql]
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
baza-0.0.35 lib/baza/driver/mysql/sql/create_indexes.rb
baza-0.0.34 lib/baza/driver/mysql/sql/create_indexes.rb
baza-0.0.33 lib/baza/driver/mysql/sql/create_indexes.rb
baza-0.0.32 lib/baza/driver/mysql/sql/create_indexes.rb
baza-0.0.31 lib/baza/driver/mysql/sql/create_indexes.rb
baza-0.0.30 lib/baza/driver/mysql/sql/create_indexes.rb
baza-0.0.29 lib/baza/driver/mysql/sql/create_indexes.rb
baza-0.0.28 lib/baza/driver/mysql/sql/create_indexes.rb