Module: Lhm::SqlHelper

Extended by:
SqlHelper
Included in:
Chunker, Entangler, LockedSwitcher, Migrator, SqlHelper, Table::Parser
Defined in:
lib/lhm/sql_helper.rb

Instance Method Summary collapse

Instance Method Details

#annotationObject



8
9
10
# File 'lib/lhm/sql_helper.rb', line 8

def annotation
  "/* large hadron migration */"
end

#idx_name(table_name, cols) ⇒ Object



12
13
14
15
# File 'lib/lhm/sql_helper.rb', line 12

def idx_name(table_name, cols)
  column_names = column_definition(cols).map(&:first)
  "index_#{ table_name }_on_#{ column_names.join("_and_") }"
end

#idx_spec(cols) ⇒ Object



17
18
19
20
21
# File 'lib/lhm/sql_helper.rb', line 17

def idx_spec(cols)
  column_definition(cols).map do |name, length|
    "`#{ name }`#{ length }"
  end.join(', ')
end

#sql(statements) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/lhm/sql_helper.rb', line 27

def sql(statements)
  [statements].flatten.each do |statement|
    connection.execute(tagged(statement))
  end
rescue ActiveRecord::StatementInvalid, Mysql::Error => e
  error e.message
end

#table?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/lhm/sql_helper.rb', line 23

def table?(table_name)
  connection.table_exists?(table_name)
end

#update(statements) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/lhm/sql_helper.rb', line 35

def update(statements)
  [statements].flatten.inject(0) do |memo, statement|
    memo += connection.update(tagged(statement))
  end
rescue ActiveRecord::StatementInvalid, Mysql::Error => e
  error e.message
end