class SQLite3::Database
def create_aggregate( name, arity, step=nil, finalize=nil,
See also #create_aggregate_handler for a more object-oriented approach to
puts db.get_first_value( "select lengths(name) from table" )
end
end
func.result = func[ :total ] || 0
finalize do |func|
end
func[ :total ] += ( value ? value.length : 0 )
func[ :total ] ||= 0
step do |func, value|
db.create_aggregate( "lengths", 1 ) do
Example:
store the result of the function.
function invocation. It should invoke FunctionProxy#result= to
single parameter, the FunctionProxy instance representing the current
The +finalize+ parameter must be a +proc+ object that accepts only a
The +step+ callback will be invoked once for each row of the result set.
invocation), with any subsequent parameters (up to the function's arity).
parameter a FunctionProxy instance (representing the function
The +step+ parameter must be a proc object that accepts as its first
variable arity functions, use -1 for the arity.)
The new function will be added as +name+, with the given +arity+. (For
a query.)
is the "count" function, for determining the number of rows that match
instead of over just a single row. (A very common aggregate function
functions are functions that apply over every row in the result set,
Creates a new aggregate function for use in SQL statements. Aggregate
def create_aggregate( name, arity, step=nil, finalize=nil, text_rep=Constants::TextRep::ANY, &block ) proxy = Class.new do def self.step( &block ) define_method(:step_with_ctx, &block) end def self.finalize( &block ) define_method(:finalize_with_ctx, &block) end end if block_given? proxy.instance_eval(&block) else proxy.class_eval do define_method(:step_with_ctx, step) define_method(:finalize_with_ctx, finalize) end end proxy.class_eval do # class instance variables @name = name @arity = arity def self.name @name end def self.arity @arity end def initialize @ctx = FunctionProxy.new end def step( *args ) step_with_ctx(@ctx, *args) end def finalize finalize_with_ctx(@ctx) @ctx.result end end define_aggregator2(proxy, name) end