class SQLite3::Database
def create_aggregate_handler( handler )
db.create_aggregate_handler( LengthsAggregateHandler )
end
end
ctx.result = @total
def finalize( ctx )
end
@total += ( name ? name.length : 0 )
def step( ctx, name )
end
@total = 0
def initialize
def self.name; 'lengths'; end
def self.arity; 1; end
class LengthsAggregateHandler
Example:
#create_aggregate.
same signature as the +finalize+ callback for
aggregate function's evaluation. It should implement the
+finalize+:: this is the method that will be called to finalize the
signature as the +step+ callback for #create_aggregate.
aggregate function's evaluation. It should implement the same
+step+:: this is the method that will be called for each step of the
above), must respond to the following messages:
The handler instance (the object returned by the +new+ message, described
the function.
instance of the object that will handle a specific invocation of
+new+:: this must be implemented by the handler. It should return a new
this message.
+name+:: this is the name of the function. The handler _must_ implement
the function will have an arity of -1.
message is optional, and if the handler does not respond to it,
+arity+:: corresponds to the +arity+ parameter of #create_aggregate. This
handler should respond to the following messages:
(the "handler") that knows how to obtain all of that information. The
callbacks, arity, and type, you specify a factory object
#create_aggregate). Instead of explicitly specifying the name,
This is another approach to creating an aggregate function (see
def create_aggregate_handler( handler ) # This is a compatiblity shim so the (basically pointless) FunctionProxy # "ctx" object is passed as first argument to both step() and finalize(). # Now its up to the library user whether he prefers to store his # temporaries as instance varibales or fields in the FunctionProxy. # The library user still must set the result value with # FunctionProxy.result= as there is no backwards compatible way to # change this. proxy = Class.new(handler) do def initialize super @fp = FunctionProxy.new end def step( *args ) super(@fp, *args) end def finalize super(@fp) @fp.result end end define_aggregator2(proxy, proxy.name) self end