Class: PerconaAr::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/percona_ar/query_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(conn = ActiveRecord::Base.connection) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



2
3
4
5
# File 'lib/percona_ar/query_builder.rb', line 2

def initialize(conn = ActiveRecord::Base.connection)
  @tables = Hash.new {|h, k| h[k] = [] }
  @conn = conn
end

Instance Method Details

#add(sql) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/percona_ar/query_builder.rb', line 13

def add(sql)
  if sql =~ /^ALTER TABLE `?([^ `]*)`? (.*)/i
    @tables[$1.to_s] << get_sql_for($2)
  elsif sql =~ /DROP INDEX/i
    drop_clause, table = sql.split(/ ON /i)
    @tables[table.delete('`')] << get_sql_for(drop_clause)
  elsif sql =~ /CREATE  INDEX (`?[^ ]*)  ON `?([^ `]*)`? (.*)/i
    @tables[$2] << get_sql_for("ADD INDEX #{$1} #{$3}")
  end
  self
end

#executeObject



7
8
9
10
11
# File 'lib/percona_ar/query_builder.rb', line 7

def execute
  @tables.each do |table, snippets|
    PerconaAr::PtOnlineSchemaChangeExecutor.new(table, snippets.join(", "), @conn).call
  end
end