ar-extensions 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ module ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter # :nodoc:
2
+
3
+ include ActiveRecord::Extensions::Import::ImportSupport
4
+ include ActiveRecord::Extensions::Import::OnDuplicateKeyUpdateSupport
5
+
6
+ # Returns an array of post SQL statements given the passed in options.
7
+ def post_sql_statements( table_name, options ) # :nodoc:
8
+ post_sql_statements = []
9
+ if options[:on_duplicate_key_update]
10
+ post_sql_statements << sql_for_on_duplicate_key_update( table_name, options[:on_duplicate_key_update] )
11
+ end
12
+ post_sql_statements
13
+ end
14
+
15
+ def multiple_value_sets_insert_sql( table_name, column_names, options ) # :nodoc:
16
+ "INSERT #{options[:ignore]?'IGNORE ':''}INTO #{table_name} (#{column_names.join(',')}) VALUES "
17
+ end
18
+
19
+ # Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
20
+ # in +args+.
21
+ def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
22
+ sql = ' ON DUPLICATE KEY UPDATE '
23
+ arg = args.first
24
+ if arg.is_a?( Array )
25
+ sql << sql_for_on_duplicate_key_update_as_array( table_name, arg )
26
+ elsif arg.is_a?( Hash )
27
+ sql << sql_for_on_duplicate_key_update_as_hash( table_name, arg )
28
+ else
29
+ raise ArgumentError.new( "Expected Array or Hash" )
30
+ end
31
+ sql
32
+ end
33
+
34
+ def sql_for_on_duplicate_key_update_as_array( table_name, arr ) # :nodoc:
35
+ qt = quote_column_name( table_name )
36
+ results = arr.map do |column|
37
+ qc = quote_column_name( column )
38
+ "#{qt}.#{qc}=VALUES(#{qc})"
39
+ end
40
+ results.join( ',' )
41
+ end
42
+
43
+ def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc:
44
+ sql = ' ON DUPLICATE KEY UPDATE '
45
+ qt = quote_column_name( table_name )
46
+ results = hsh.map do |column1, column2|
47
+ qc1 = quote_column_name( column1 )
48
+ qc2 = quote_column_name( column2 )
49
+ "#{qt}.#{qc1}=VALUES( #{qc2} )"
50
+ end
51
+ results.join( ',')
52
+ end
53
+
54
+ # Returns SQL the VALUES for an INSERT statement given the passed in +columns+
55
+ # and +array_of_attributes+.
56
+ def values_sql_for_column_names_and_attributes( columns, array_of_attributes ) # :nodoc:
57
+ values = []
58
+ array_of_attributes.each do |arr|
59
+ my_values = []
60
+ arr.each_with_index do |val,j|
61
+ my_values << quote( val, columns[j] )
62
+ end
63
+ values << my_values
64
+ end
65
+ values_arr = values.map{ |arr| '(' + arr.join( ',' ) + ')' }
66
+ end
67
+
68
+ end
69
+
70
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send( 'include', ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter )
File without changes
@@ -0,0 +1,124 @@
1
+ module ActiveRecord::Extensions::TemporaryTableSupport # :nodoc:
2
+ def supports_temporary_tables? #:nodoc:
3
+ true
4
+ end
5
+ end
6
+
7
+
8
+ class ActiveRecord::Base
9
+ @@temporary_table_hsh ||= {}
10
+
11
+ # Returns true if the underlying database connection supports temporary tables
12
+ def self.supports_temporary_tables?
13
+ connection.supports_temporary_tables?
14
+ rescue NoMethodError
15
+ false
16
+ end
17
+
18
+ ######################################################################
19
+ # Creates a temporary table given the passed in options hash. The
20
+ # temporary table is created based off from another table the
21
+ # current model class. This method returns the constant for the new
22
+ # new model. This can also be used with block form (see below).
23
+ #
24
+ # == Parameters
25
+ # * options - the options hash used to define the temporary table.
26
+ #
27
+ # ==== Options
28
+ # * :table_name - the desired name of the temporary table. If not supplied \
29
+ # then a name of "temp_" + the current table_name of the current model \
30
+ # will be used.
31
+ # * :like - the table model you want to base the temporary tables \
32
+ # structure off from. If this is not supplied then the table_name of the \
33
+ # current model will be used.
34
+ # * :model_name - the name of the model you want to use for the temporary \
35
+ # table. This must be compliant with Ruby's naming conventions for \
36
+ # constants. If this is not supplied a rails-generated table name will \
37
+ # be created which is based off from the table_name of the temporary table. \
38
+ # IE: Account.create_temporary_table creates the TempAccount model class
39
+ #
40
+ # ==== Example 1, using defaults
41
+ # class Project < ActiveRecord::Base ; end
42
+ #
43
+ # Project.create_temporary_table
44
+ #
45
+ # This creates a temporary table named 'temp_projects' and creates a constant
46
+ # name TempProject. The table structure is copied from the _projects_ table.
47
+ #
48
+ # ==== Example 2, using :table_name and :model options
49
+ # Project.create_temporary_table :table_name=>'my_projects', :model=>'MyProject'
50
+ #
51
+ # This creates a temporary table named 'my_projects' and creates a constant named
52
+ # MyProject. The table structure is copied from the _projects_ table.
53
+ #
54
+ # ==== Example 3, using :like
55
+ # ActiveRecord::Base.create_temporary_table :like=>Project
56
+ #
57
+ # This is the same as calling Project.create_temporary_table.
58
+ #
59
+ # ==== Example 4, using block form
60
+ # Project.create_temporary_table do |t|
61
+ # # ...
62
+ # end
63
+ #
64
+ # Using the block form will automatically drop the temporary table
65
+ # when the block exits. _t_ which is passed into the block is the temporary
66
+ # table class. In the above example _t_ equals TempProject. The block form
67
+ # can be used with all of the available options.
68
+ #
69
+ # === See
70
+ # * drop
71
+ ######################################################################
72
+ def self.create_temporary_table( options={} )
73
+ options[:table_name] = "temp_#{self.table_name}" unless options[:table_name]
74
+ options[:like] = self unless options[:like]
75
+ options[:temporary] = true if not options[:permanent] and not options.has_key?( :temporary )
76
+ table_name = options[:table_name]
77
+ model_name = options[:model_name] || Inflector.classify( table_name )
78
+ raise Exception.new( "Model #{model_name} already exists! \n" ) if Object.const_defined? model_name
79
+
80
+ like_table_name = options[:like].table_name || self.table_name
81
+ sql = "CREATE #{options[:temporary] ? 'TEMPORARY' : ''} TABLE #{table_name} LIKE #{like_table_name}"
82
+ connection.execute( sql )
83
+
84
+ eval "class ::#{model_name} < #{ActiveRecord::TemporaryTable.name}
85
+ set_table_name :#{table_name}
86
+ end"
87
+
88
+ @@temporary_table_hsh[ model = Object.const_get( model_name ) ] = true
89
+
90
+ if block_given?
91
+ yield model
92
+ model.drop
93
+ nil
94
+ else
95
+ model
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ class ActiveRecord::TemporaryTable < ActiveRecord::Base
102
+
103
+ # Drops a temporary table from the database and removes
104
+ # the temporary table constant.
105
+ #
106
+ # ==== Example
107
+ # Project.create_temporary_table
108
+ # Object.const_defined?( :TempProject ) # => true
109
+ # TempProject.drop
110
+ # Object.const_defined?( :TempProject ) # => false
111
+ #
112
+ def self.drop
113
+ if @@temporary_table_hsh[ self ]
114
+ sql = 'DROP TABLE ' + self.table_name + ';'
115
+ connection.execute( sql )
116
+ Object.send( :remove_const, self.name.to_sym )
117
+ @@temporary_table_hsh.delete( self )
118
+ else
119
+ raise StandardError.new "Trying to drop nonexistance temporary table: #{self.name}"
120
+ end
121
+ end
122
+
123
+ end
124
+
@@ -0,0 +1,3 @@
1
+ class ActiveRecord::ConnectionAdapters::MysqlAdapter # :nodoc:
2
+ include ActiveRecord::Extensions::TemporaryTableSupport
3
+ end
@@ -0,0 +1,8 @@
1
+ module ActiveRecord # :nodoc:
2
+ module Extensions # :nodoc:
3
+ module VERSION
4
+ MAJOR, MINOR, REVISION = %W( 0 4 0 )
5
+ STRING = [ MAJOR, MINOR, REVISION ].join( '.' )
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.1
3
+ specification_version: 1
4
+ name: ar-extensions
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.1
7
+ date: 2007-03-15 00:00:00 -04:00
8
+ summary: Extends ActiveRecord functionality.
9
+ require_paths:
10
+ - lib
11
+ email: zach.dennis@gmail.com
12
+ homepage: http://www.continuousthinking.com/tags/arext
13
+ rubyforge_project: arext
14
+ description: Extends ActiveRecord functionality by adding better finder/query support, as well as supporting mass data import, foreign key, CSV and temporary tables
15
+ autorequire: ar-extensions.rb
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Zach Dennis
31
+ - Mark Van Holstyn
32
+ files:
33
+ - init.rb
34
+ - db/migrate
35
+ - db/migrate/generic_schema.rb
36
+ - db/migrate/mysql_schema.rb
37
+ - db/migrate/version.rb
38
+ - Rakefile
39
+ - ChangeLog
40
+ - README
41
+ - config/database.yml
42
+ - config/database.yml.template
43
+ - config/mysql.schema
44
+ - config/postgresql.schema
45
+ - lib/ar-extensions.rb
46
+ - lib/ar-extensions/csv.rb
47
+ - lib/ar-extensions/extensions.rb
48
+ - lib/ar-extensions/finders.rb
49
+ - lib/ar-extensions/foreign_keys.rb
50
+ - lib/ar-extensions/fulltext.rb
51
+ - lib/ar-extensions/import.rb
52
+ - lib/ar-extensions/temporary_table.rb
53
+ - lib/ar-extensions/version.rb
54
+ - lib/ar-extensions/adapters/abstract_adapter.rb
55
+ - lib/ar-extensions/adapters/mysql_adapter.rb
56
+ - lib/ar-extensions/adapters/postgresql.rb
57
+ - lib/ar-extensions/fulltext/mysql.rb
58
+ - lib/ar-extensions/import/mysql.rb
59
+ - lib/ar-extensions/import/postgresql.rb
60
+ - lib/ar-extensions/temporary_table/mysql.rb
61
+ test_files: []
62
+
63
+ rdoc_options:
64
+ - --main
65
+ - README
66
+ extra_rdoc_files:
67
+ - README
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ requirements: []
73
+
74
+ dependencies:
75
+ - !ruby/object:Gem::Dependency
76
+ name: activerecord
77
+ version_requirement:
78
+ version_requirements: !ruby/object:Gem::Version::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.14.1
83
+ version: