adapter_extensions 0.3.1 → 0.4.0

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/CHANGELOG CHANGED
@@ -16,4 +16,8 @@
16
16
  * bulk_load method now handles table missing and file missing as error cases
17
17
 
18
18
  0.3.1 - May 4, 2007
19
- * Added support for modifying SELECT statements to add an INSERT INTO.
19
+ * Added support for modifying SELECT statements to add an INSERT INTO.
20
+
21
+ 0.4 - September 17, 2007
22
+ * Added copy_table method that can copy the structure and data from one table to another. Currently implemented in MySQL (tested), PostgreSQL (tested) and SQL Server adapters (untested).
23
+ * Added support for SELECT..INTO for PostgreSQL.
data/README CHANGED
@@ -1 +1,3 @@
1
- This library provides extensions to Rails' ActiveRecord adapters.
1
+ This library provides extensions to Rails' ActiveRecord adapters.
2
+
3
+ To execute the unit tests you must first construct a adapter_extensions_unittest database.
data/Rakefile CHANGED
@@ -29,6 +29,17 @@ Rake::TestTask.new(:test) do |t|
29
29
  # TODO: reset the database
30
30
  end
31
31
 
32
+ namespace :rcov do
33
+ desc 'Measures test coverage'
34
+ task :test do
35
+ rm_f 'coverage.data'
36
+ mkdir 'coverage' unless File.exist?('coverage')
37
+ rcov = "rcov --aggregate coverage.data --text-summary -Ilib"
38
+ system("#{rcov} test/*_test.rb test/**/*_test.rb")
39
+ system("open coverage/index.html") if PLATFORM['darwin']
40
+ end
41
+ end
42
+
32
43
  desc 'Generate documentation for the AdapterExtensions library.'
33
44
  Rake::RDocTask.new(:rdoc) do |rdoc|
34
45
  rdoc.rdoc_dir = 'rdoc'
@@ -27,6 +27,8 @@ module ActiveRecord #:nodoc:
27
27
  false
28
28
  end
29
29
 
30
+ # Add a chunk of SQL to the given query that will create a new table and
31
+ # execute the select into that table.
30
32
  def add_select_into_table(new_table_name, sql_query)
31
33
  raise NotImplementedError, "add_select_into_table is an abstract method"
32
34
  end
@@ -12,6 +12,14 @@ module ActiveRecord #:nodoc:
12
12
  def add_select_into_table(new_table_name, sql_query)
13
13
  "CREATE TABLE #{new_table_name} " + sql_query
14
14
  end
15
+
16
+ # Copy the specified table.
17
+ def copy_table(old_table_name, new_table_name)
18
+ transaction do
19
+ execute "CREATE TABLE #{new_table_name} LIKE #{old_table_name}"
20
+ execute "INSERT INTO #{new_table_name} SELECT * FROM #{old_table_name}"
21
+ end
22
+ end
15
23
 
16
24
  protected
17
25
  # Call +bulk_load+, as that method wraps this method.
@@ -26,6 +34,7 @@ module ActiveRecord #:nodoc:
26
34
  # * <tt>:delimited_by</tt> -- The field delimiter
27
35
  # * <tt>:enclosed_by</tt> -- The field enclosure
28
36
  def do_bulk_load(file, table_name, options={})
37
+ return if File.size(file) == 0
29
38
  q = "LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{table_name}"
30
39
  if options[:fields]
31
40
  q << " FIELDS"
@@ -3,6 +3,19 @@ module ActiveRecord #:nodoc:
3
3
  module ConnectionAdapters #:nodoc:
4
4
  # Adds new functionality to ActiveRecord PostgreSQLAdapter.
5
5
  class PostgreSQLAdapter < AbstractAdapter
6
+ def support_select_into_table?
7
+ true
8
+ end
9
+
10
+ # Inserts an INTO table_name clause to the sql_query.
11
+ def add_select_into_table(new_table_name, sql_query)
12
+ sql_query.sub(/FROM/i, "INTO #{new_table_name} FROM")
13
+ end
14
+
15
+ # Copy the specified table.
16
+ def copy_table(old_table_name, new_table_name)
17
+ execute add_select_into_table(new_table_name, "SELECT * FROM #{old_table_name}")
18
+ end
6
19
 
7
20
  protected
8
21
  # Call +bulk_load+, as that method wraps this method.
@@ -25,7 +38,7 @@ module ActiveRecord #:nodoc:
25
38
  q << "DELIMITER '#{options[:fields][:delimited_by]}' " if options[:fields][:delimited_by]
26
39
  if options[:fields][:enclosed_by]
27
40
  q << "CSV "
28
- q << "HEADER " if options[:ignore] > 0
41
+ q << "HEADER " if options[:ignore] && options[:ignore] > 0
29
42
  q << "QUOTE '#{options[:fields][:enclosed_by]}' " if options[:fields][:enclosed_by]
30
43
  end
31
44
  end
@@ -11,6 +11,11 @@ module ActiveRecord #:nodoc:
11
11
  def add_select_into_table(new_table_name, sql_query)
12
12
  sql_query.sub(/FROM/i, "INTO #{new_table_name} FROM")
13
13
  end
14
+
15
+ # Copy the specified table.
16
+ def copy_table(old_table_name, new_table_name)
17
+ execute add_select_into_table(new_table_name, "SELECT * FROM #{old_table_name}")
18
+ end
14
19
 
15
20
  protected
16
21
  # Call +bulk_load+, as that method wraps this method.
@@ -2,8 +2,8 @@
2
2
  module AdapterExtensions#:nodoc:
3
3
  module VERSION #:nodoc:
4
4
  MAJOR = 0
5
- MINOR = 3
6
- TINY = 1
5
+ MINOR = 4
6
+ TINY = 0
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
9
9
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: adapter_extensions
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-05-04 00:00:00 -04:00
6
+ version: 0.4.0
7
+ date: 2007-09-17 00:00:00 +02:00
8
8
  summary: Extensions to Rails ActiveRecord adapters.
9
9
  require_paths:
10
10
  - lib