Module: ArPerfToolkit::Base::ClassMethods

Defined in:
lib/base.rb

Instance Method Summary collapse

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(options)
  #add piggy back option if plugin is installed
  add_piggy_back!(options) if self.respond_to? :add_piggy_back!
  scope = scope(:find)
  sql = "#{options[:pre_sql]} SELECT".strip
  sql << " #{options[:keywords]}" if options[:keywords]
  sql << " #{(scope && scope[:select]) || options[:select] || '*'} "
  sql << "FROM #{(scope && scope[:from]) || options[:from] || table_name} "
  
  add_joins!(sql, options, scope)
  add_conditions!(sql, options[:conditions], scope)

  sql << " GROUP BY #{options[:group]} " if options[:group]
  sql << " ORDER BY #{options[:order]} " if options[:order]
  
  add_limit!(sql, options, scope)
  sql << "#{options[:post_sql]}" if options[:post_sql]
  sql
end

#construct_sql(options = {}, valid_options = []) {|sql, options| ... } ⇒ Object

Construct keywords around sql

Yields:

  • (sql, options)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/base.rb', line 33

def construct_sql(options={}, valid_options = [], &block)
  options.assert_valid_keys(:keywords, :command, :duplicate, :post_sql, :pre_sql, :select, *valid_options)
  sql = ''
  sql << (options[:pre_sql] + ' ') if options[:pre_sql]
  sql << "#{options[:command]} "
  add_keywords!(sql, options[:keywords]) if options[:keywords]
  yield sql, options
  add_duplicate!(sql, options[:duplicate]) if options[:duplicate]
  sql << options[:post_sql] if options[: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 options={}
    
  query = options[:conditions] || "delete from " + table_name+" c1 using "+table_name+" c1, " +table_name+" c2 where (" +
          options[:fields].collect{|field| ('c1.'+field.to_s+'=c2.'+field.to_s) }.join(" and ") +
          (options[:additional_clause] ? 'and ('+options[:additional_clause]+') ' : '')+') and '+
          (options[:winner_clause] || ('c1.'+(qf=(options[: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(options)       
  select_sql = self.send(((scoped?(:find, :include) || options[:include]) ?
    :finder_sql_with_included_associations :
    :construct_finder_sql), options)
end