Class: MysqlInterface
- Inherits:
-
Object
- Object
- MysqlInterface
- Defined in:
- lib/db_helper/mysql_interface.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #admin ⇒ Object
- #csv_import(csv_file_full_path, columns, csv_options = {}) ⇒ Object
- #database ⇒ Object
- #drop_and_create ⇒ Object
- #import(file) ⇒ Object
-
#initialize(config) ⇒ MysqlInterface
constructor
A new instance of MysqlInterface.
Constructor Details
#initialize(config) ⇒ MysqlInterface
Returns a new instance of MysqlInterface.
7 8 9 10 11 12 13 14 |
# File 'lib/db_helper/mysql_interface.rb', line 7 def initialize(config) @config = config.symbolize_keys @config[:username] ||= 'root' @options = "--user=#{@config[:username]}" [:password,:host,:port,:socket].each do |opt| @options += " --#{opt}=#{@config[opt]}" unless @config[opt].blank? end end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/db_helper/mysql_interface.rb', line 6 def config @config end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/db_helper/mysql_interface.rb', line 6 def @options end |
Instance Method Details
#admin ⇒ Object
46 47 |
# File 'lib/db_helper/mysql_interface.rb', line 46 def admin end |
#csv_import(csv_file_full_path, columns, csv_options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/db_helper/mysql_interface.rb', line 34 def csv_import(csv_file_full_path, columns, = {}) ['fields-terminated-by'] ||= "','" ['fields-enclosed-by'] ||= "'\"'" ['lines-terminated-by'] ||= "'\n'" ['ignore-lines'] = '1' # Ignore first line so it can be used to show columns `mysqlimport #{@options} #{database} \ #{.map{|k,v| "--#{k}=#{v}"}.join(' ')} \ --columns='#{columns.join(',')}' \ --local \ #{csv_file_full_path}` end |
#database ⇒ Object
16 17 18 |
# File 'lib/db_helper/mysql_interface.rb', line 16 def database @config[:database] end |
#drop_and_create ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/db_helper/mysql_interface.rb', line 20 def drop_and_create puts "0. Dropping and creating database" begin command "mysqladmin #{@options} drop --force #{database}" rescue puts "database doesn't exist. Skipping drop" end command "mysqladmin #{@options} create #{database}" end |
#import(file) ⇒ Object
30 31 32 |
# File 'lib/db_helper/mysql_interface.rb', line 30 def import(file) command "mysql #{@options} #{database} < #{file.gsub('.gz', '')}" end |