Module: ArPerfToolkit::Base::ClassMethods
- Defined in:
- lib/base.rb
Instance Method Summary collapse
- #construct_finder_sql_x(options) ⇒ Object
-
#construct_sql(options = {}, valid_options = []) {|sql, options| ... } ⇒ Object
Construct keywords around sql.
-
#delete_duplicates(options = {}) ⇒ Object
add a util method here until I find a better home for it.
-
#finder_sql_to_string(options) ⇒ Object
expose the finder sql to a string with the same options that the find method takes.
Instance Method Details
#construct_finder_sql_x(options) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/base.rb', line 45 def construct_finder_sql_x() #add piggy back option if plugin is installed add_piggy_back!() if self.respond_to? :add_piggy_back! scope = scope(:find) sql = "#{[:pre_sql]} SELECT".strip sql << " #{[:keywords]}" if [:keywords] sql << " #{(scope && scope[:select]) || [:select] || '*'} " sql << "FROM #{(scope && scope[:from]) || [:from] || table_name} " add_joins!(sql, , scope) add_conditions!(sql, [:conditions], scope) sql << " GROUP BY #{[:group]} " if [:group] sql << " ORDER BY #{[:order]} " if [:order] add_limit!(sql, , scope) sql << "#{[:post_sql]}" if [:post_sql] sql end |
#construct_sql(options = {}, valid_options = []) {|sql, options| ... } ⇒ Object
Construct keywords around sql
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/base.rb', line 33 def construct_sql(={}, = [], &block) .assert_valid_keys(:keywords, :command, :duplicate, :post_sql, :pre_sql, :select, *) sql = '' sql << ([:pre_sql] + ' ') if [:pre_sql] sql << "#{[:command]} " add_keywords!(sql, [:keywords]) if [:keywords] yield sql, add_duplicate!(sql, [:duplicate]) if [:duplicate] sql << [:post_sql] if [:post_sql] sql end |
#delete_duplicates(options = {}) ⇒ Object
add a util method here until I find a better home for it. delete all the dups
ex.
Make all the phone numbers of contacts unique by deleting the
>>Contacts.delete_duplicates(:fields=>)
:fields -> the fields to match on :conditions-> specify this to do it yourself. :additional_clause -> append something to the query, tho you could just use scope :winner_clause-> the part of the query specifying what wins. Default is that with the greatest id. :query_field -> if you dont like id as the winner, specify another field
80 81 82 83 84 85 86 87 88 |
# File 'lib/base.rb', line 80 def delete_duplicates ={} query = [:conditions] || "delete from " + table_name+" c1 using "+table_name+" c1, " +table_name+" c2 where (" + [:fields].collect{|field| ('c1.'+field.to_s+'=c2.'+field.to_s) }.join(" and ") + ([:additional_clause] ? 'and ('+[:additional_clause]+') ' : '')+') and '+ ([:winner_clause] || ('c1.'+(qf=([:query_field] || 'id').to_s) + '> c2.'+qf)) self.connection.execute(self.sanitize_sql(query)) end |
#finder_sql_to_string(options) ⇒ Object
expose the finder sql to a string with the same options that the find method takes.
sql = Contact.finder_sql_to_string(:include => :primary_email_address)
Contact.find_by_sql(sql + 'USE_INDEX(blah)')
25 26 27 28 29 |
# File 'lib/base.rb', line 25 def finder_sql_to_string() select_sql = self.send(((scoped?(:find, :include) || [:include]) ? :finder_sql_with_included_associations : :construct_finder_sql), ) end |