Skip to content

Commit

Permalink
Replace Dataset#virtual_row_block_call with Sequel.virtual_row
Browse files Browse the repository at this point in the history
This removes the private virtual_row_block_call Dataset instance
method, replacing with the Sequel.virtual_row module method.
The API is slightly different, with virtual_row_block_call taking
the block as a regular argument, and Sequel.virtual_row taking
it as a block.  This allows the easier use of virtual rows
outside of the select, order, and filter calls.  For example:

  net_benefit = Sequel.virtual_row{revenue > cost}
  good_employee = Sequel.virtual_row{num_commendations > 0}
  fire = ~net_benefit & ~good_employee
  demote = ~net_benefit & good_employee
  promote = net_benefit & good_employee
  DB[:employees].filter(fire).update(:employed=>false)
  DB[:employees].filter(demote).update(:rank=>:rank-1)
  DB[:employees].filter(promote).update(:rank=>:rank+1)

There wasn't an easy way to do the above before, without
creating the Sequel::SQL::VirtualRow instance manually.
  • Loading branch information
jeremyevans committed Sep 14, 2009
1 parent d0a98fa commit f62c1e1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== HEAD

* Replace Dataset#virtual_row_block_call with Sequel.virtual_row (jeremyevans)

* Allow Dataset #delete, #update, and #insert to respect existing WITH clauses on MSSQL (dlee, jeremyevans)

* Add touch plugin, which adds Model#touch for updating an instance's timestamp, as well as touching associations when an instance is updated or destroyed (jeremyevans)
Expand Down
14 changes: 14 additions & 0 deletions lib/sequel/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ def self.string_to_time(s)
def self.typecast_to_application_timestamp(v)
convert_timestamp(v, Sequel.typecast_timezone)
end

# If the supplied block takes a single argument,
# yield a new SQL::VirtualRow instance to the block
# argument. Otherwise, evaluate the block in the context of a new
# SQL::VirtualRow instance.
def self.virtual_row(&block)
vr = SQL::VirtualRow.new
case block.arity
when -1, 0
vr.instance_eval(&block)
else
block.call(vr)
end
end

### Private Class Methods ###

Expand Down
14 changes: 0 additions & 14 deletions lib/sequel/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,5 @@ def output_identifier(v)
# default, added to make the model eager loading code simpler.
def post_load(all_records)
end

# If a block argument is passed to a method that uses a VirtualRow,
# yield a new VirtualRow instance to the block if it accepts a single
# argument. Otherwise, evaluate the block in the context of a new
# VirtualRow instance.
def virtual_row_block_call(block)
return unless block
case block.arity
when -1, 0
SQL::VirtualRow.new.instance_eval(&block)
else
block.call(SQL::VirtualRow.new)
end
end
end
end
6 changes: 3 additions & 3 deletions lib/sequel/dataset/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def or(*cond, &block)
# ds.order{|o| o.sum(:name)}.sql #=> 'SELECT * FROM items ORDER BY sum(name)'
# ds.order(nil).sql #=> 'SELECT * FROM items'
def order(*columns, &block)
columns += Array(virtual_row_block_call(block)) if block
columns += Array(Sequel.virtual_row(&block)) if block
clone(:order => (columns.compact.empty?) ? nil : columns)
end
alias_method :order_by, :order
Expand Down Expand Up @@ -741,7 +741,7 @@ def schema_and_table(table_name)
# dataset.select(:a, :b) # SELECT a, b FROM items
# dataset.select{|o| o.a, o.sum(:b)} # SELECT a, sum(b) FROM items
def select(*columns, &block)
columns += Array(virtual_row_block_call(block)) if block
columns += Array(Sequel.virtual_row(&block)) if block
m = []
columns.map do |i|
i.is_a?(Hash) ? m.concat(i.map{|k, v| SQL::AliasedExpression.new(k,v)}) : m << i
Expand Down Expand Up @@ -1026,7 +1026,7 @@ def filter_expr(expr = nil, &block)
SQL::BooleanExpression.new(:AND, *expr.map{|x| filter_expr(x)})
end
when Proc
filter_expr(virtual_row_block_call(expr))
filter_expr(Sequel.virtual_row(&expr))
when SQL::NumericExpression, SQL::StringExpression
raise(Error, "Invalid SQL Expression type: #{expr.inspect}")
when Symbol, SQL::Expression
Expand Down

0 comments on commit f62c1e1

Please sign in to comment.