Module: Sunrise::Utils::Mysql::ClassMethods
- Defined in:
- lib/sunrise/utils/mysql.rb
Instance Method Summary collapse
-
#database_exists? ⇒ Boolean
Check if database exists.
-
#disable_keys ⇒ Object
Disables key updates for model table.
-
#enable_keys ⇒ Object
Enables key updates for model table.
-
#fast_import(files, options = {}) ⇒ Object
Loads data from file(s) using MySQL native LOAD DATA INFILE query, disabling key updates for even faster import speed.
-
#load_data_infile(file, options = {}) ⇒ Object
Loads data from file using MySQL native LOAD DATA INFILE query.
-
#load_data_infile_multiple(files, options = {}) ⇒ Object
Loads data from multiple files using MySQL native LOAD DATA INFILE query.
-
#truncate_table ⇒ Object
nor any hooks.
- #values(column = 'id') ⇒ Object
-
#with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
Instance Method Details
#database_exists? ⇒ Boolean
Check if database exists
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sunrise/utils/mysql.rb', line 17 def database_exists? = configurations[Rails.env].dup .symbolize_keys! begin connection return true rescue Exception => e return false end end |
#disable_keys ⇒ Object
Disables key updates for model table
36 37 38 |
# File 'lib/sunrise/utils/mysql.rb', line 36 def disable_keys connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS") end |
#enable_keys ⇒ Object
Enables key updates for model table
41 42 43 |
# File 'lib/sunrise/utils/mysql.rb', line 41 def enable_keys connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS") end |
#fast_import(files, options = {}) ⇒ Object
Loads data from file(s) using MySQL native LOAD DATA INFILE query, disabling key updates for even faster import speed
Parameters
filesfile(s) to importoptions(see load_data_infile)
58 59 60 61 62 63 |
# File 'lib/sunrise/utils/mysql.rb', line 58 def fast_import(files, = {}) files = [files] unless files.is_a? Array with_keys_disabled do load_data_infile_multiple(files, ) end end |
#load_data_infile(file, options = {}) ⇒ Object
Loads data from file using MySQL native LOAD DATA INFILE query
Parameters
filethe file to importoptions
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sunrise/utils/mysql.rb', line 77 def load_data_infile(file, = {}) sql = "LOAD DATA LOCAL INFILE '#{file}' " sql << "#{[:insert_method]} " if [:insert_method] sql << "INTO TABLE #{quoted_table_name} " sql << "CHARACTER SET #{[:charset_name]} " if [:charset_name] fields = "" fields << "TERMINATED BY '#{[:fields_terminated_by]}' " if [:fields_terminated_by] fields << "OPTIONALLY ENCLOSED BY '#{[:fields_optionally_enclosed_by]}' " if [:fields_optionally_enclosed_by] fields << "ESCAPED BY '#{[:fields_escaped_by]}' " if [:fields_escaped_by] sql << "FIELDS #{fields} " unless fields.empty? sql << "LINES TERMINATED BY '#{[:lines_terminated_by]}' " if [:lines_terminated_by] sql << "IGNORE #{[:ignore_lines]} LINES " if [:ignore_lines] sql << "(" + [:columns].join(', ') + ") " if [:columns] if [:mapping] mappings = [] [:mapping].each_pair do |column, mapping| mappings << "#{column} = #{mapping}" end sql << "SET #{mappings.join(', ')} " if mappings.size > 0 end sql << ";" connection.execute(sql) end |
#load_data_infile_multiple(files, options = {}) ⇒ Object
Loads data from multiple files using MySQL native LOAD DATA INFILE query
66 67 68 69 70 |
# File 'lib/sunrise/utils/mysql.rb', line 66 def load_data_infile_multiple(files, = {}) files.each do |file| load_data_infile(file, ) end end |
#truncate_table ⇒ Object
nor any hooks.
31 32 33 |
# File 'lib/sunrise/utils/mysql.rb', line 31 def truncate_table transaction { connection.execute("TRUNCATE TABLE #{quoted_table_name};") } end |
#values(column = 'id') ⇒ Object
11 12 13 14 |
# File 'lib/sunrise/utils/mysql.rb', line 11 def values(column = 'id') query = scoped.select(column) connection.select_values(query.to_sql).map(&:to_i).uniq end |
#with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
46 47 48 49 50 |
# File 'lib/sunrise/utils/mysql.rb', line 46 def with_keys_disabled disable_keys yield enable_keys end |