Class: Atomically::QueryService
- Inherits:
-
Object
- Object
- Atomically::QueryService
- Defined in:
- lib/atomically/query_service.rb
Instance Method Summary collapse
- #create_or_plus(columns, data, update_columns) ⇒ Object
-
#initialize(klass, relation: nil, model: nil) ⇒ QueryService
constructor
A new instance of QueryService.
-
#pay_all(hash, update_columns, primary_key: :id) ⇒ Object
{ id => pay_count }.
- #update(attrs, from: :not_set) ⇒ Object
Constructor Details
#initialize(klass, relation: nil, model: nil) ⇒ QueryService
Returns a new instance of QueryService.
11 12 13 14 15 |
# File 'lib/atomically/query_service.rb', line 11 def initialize(klass, relation: nil, model: nil) @klass = klass @relation = relation || @klass @model = model end |
Instance Method Details
#create_or_plus(columns, data, update_columns) ⇒ Object
17 18 19 |
# File 'lib/atomically/query_service.rb', line 17 def create_or_plus(columns, data, update_columns) @klass.import(columns, data, on_duplicate_key_update: on_duplicate_key_plus_sql(update_columns)) end |
#pay_all(hash, update_columns, primary_key: :id) ⇒ Object
{ id => pay_count }
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/atomically/query_service.rb', line 21 def pay_all(hash, update_columns, primary_key: :id) # { id => pay_count } return 0 if hash.blank? update_columns = update_columns.map(&method(:quote_column)) query = hash.inject(@klass.none) do |relation, (id, pay_count)| condition = @relation.where(primary_key => id) update_columns.each{|s| condition = condition.where("#{s} >= ?", pay_count) } next relation.or(condition) end raw_when_sql = hash.map{|id, pay_count| "WHEN #{sanitize(id)} THEN #{sanitize(-pay_count)}" }.join("\n") update_sqls = update_columns.map.with_index do |column, idx| value = idx == 0 ? "(@change := \nCASE #{quote_column(primary_key)}\n#{raw_when_sql}\nEND)" : '@change' next "#{column} = #{column} + #{value}" end return query.where("(#{@klass.from(query).select('COUNT(*)').to_sql}) = ?", hash.size) .update_all(update_sqls.join(', ')) end |
#update(attrs, from: :not_set) ⇒ Object
42 43 44 45 46 |
# File 'lib/atomically/query_service.rb', line 42 def update(attrs, from: :not_set) success = update_and_return_number_of_updated_rows(attrs, from) == 1 assign_without_changes(attrs) if success return success end |