jsuchal-activerecord-fast-import 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{activerecord-fast-import}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jan Suchal"]
12
- s.date = %q{2009-08-19}
12
+ s.date = %q{2009-08-21}
13
13
  s.description = %q{Native MySQL additions to ActiveRecord, like LOAD DATA INFILE, ENABLE/DISABLE KEYS, TRUNCATE TABLE.}
14
14
  s.email = %q{johno@jsmf.net}
15
15
  s.extra_rdoc_files = [
@@ -1,53 +1,54 @@
1
- module ActiveRecord #:nodoc:
2
- class Base
3
- # Deletes all rows in table very fast, but without calling +destroy+ method
4
- # nor any hooks.
5
- def self.truncate_table
6
- connection.execute("TRUNCATE TABLE #{quoted_table_name}")
7
- end
8
-
9
- # Disables key updates for model table
10
- def self.disable_keys
11
- connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
12
- end
13
-
14
- # Enables key updates for model table
15
- def self.enable_keys
16
- connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS")
17
- end
18
-
19
- # Loads data from file using MySQL native LOAD DATA INFILE query, disabling
20
- # key updates for even faster import speed
21
- #
22
- # ==== Parameters
23
- # * +file+ the file to load
24
- # * +options+ (see <tt>load_data_infile</tt>)
25
- def self.fast_import(file, options = {})
26
- disable_keys
27
- load_data_infile(file, options)
28
- enable_keys
29
- end
30
-
31
- # Loads data from file using MySQL native LOAD DATA INFILE query
32
- #
33
- # ==== Parameters
34
- # * +file+ the file to import
35
- # * +options+
36
- def self.load_data_infile(file, options = {})
37
- sql = "LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{quoted_table_name} "
38
- sql << "FIELDS TERMINATED BY '#{options[:fields_terminated_by]}' " if options[:fields_terminated_by]
39
- sql << "LINES TERMINATED BY '#{options[:lines_terminated_by]}' " if options[:lines_terminated_by]
40
- sql << "IGNORE #{options[:ignore_lines]} LINES " if options[:ignore_lines]
41
- sql << "(" + options[:columns].join(', ') + ") " if options[:columns]
42
- if options[:mapping]
43
- mappings = []
44
- options[:mapping].each_pair do |column, mapping|
45
- mappings << "#{column} = #{mapping}"
46
- end
47
- sql << "SET #{mappings.join(', ')} " if mappings.size > 0
48
- end
49
- sql << ";"
50
- connection.execute(sql)
51
- end
52
- end
1
+ module ActiveRecord #:nodoc:
2
+ class Base
3
+ # Deletes all rows in table very fast, but without calling +destroy+ method
4
+ # nor any hooks.
5
+ def self.truncate_table
6
+ connection.execute("TRUNCATE TABLE #{quoted_table_name}")
7
+ end
8
+
9
+ # Disables key updates for model table
10
+ def self.disable_keys
11
+ connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
12
+ end
13
+
14
+ # Enables key updates for model table
15
+ def self.enable_keys
16
+ connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS")
17
+ end
18
+
19
+ # Loads data from file using MySQL native LOAD DATA INFILE query, disabling
20
+ # key updates for even faster import speed
21
+ #
22
+ # ==== Parameters
23
+ # * +file+ file(s) to import
24
+ # * +options+ (see <tt>load_data_infile</tt>)
25
+ def self.fast_import(files, options = {})
26
+ files = [files] unless files.is_a? Array
27
+ enable_keys
28
+ files.each {|file| load_data_infile(file, options)}
29
+ disable_keys
30
+ end
31
+
32
+ # Loads data from file using MySQL native LOAD DATA INFILE query
33
+ #
34
+ # ==== Parameters
35
+ # * +file+ the file to import
36
+ # * +options+
37
+ def self.load_data_infile(file, options = {})
38
+ sql = "LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{quoted_table_name} "
39
+ sql << "FIELDS TERMINATED BY '#{options[:fields_terminated_by]}' " if options[:fields_terminated_by]
40
+ sql << "LINES TERMINATED BY '#{options[:lines_terminated_by]}' " if options[:lines_terminated_by]
41
+ sql << "IGNORE #{options[:ignore_lines]} LINES " if options[:ignore_lines]
42
+ sql << "(" + options[:columns].join(', ') + ") " if options[:columns]
43
+ if options[:mapping]
44
+ mappings = []
45
+ options[:mapping].each_pair do |column, mapping|
46
+ mappings << "#{column} = #{mapping}"
47
+ end
48
+ sql << "SET #{mappings.join(', ')} " if mappings.size > 0
49
+ end
50
+ sql << ";"
51
+ connection.execute(sql)
52
+ end
53
+ end
53
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsuchal-activerecord-fast-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Suchal
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-19 00:00:00 -07:00
12
+ date: 2009-08-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency