    # File lib/sqlite3/database.rb, line 496
        def create_aggregate_handler( handler )
          arity = -1
          text_rep = Constants::TextRep::ANY

          arity = handler.arity if handler.respond_to?(:arity)
          text_rep = handler.text_rep if handler.respond_to?(:text_rep)
          name = handler.name

          step = proc do |func,*args|
            ctx = @driver.aggregate_context( func )
            unless ctx[ :__error ]
              ctx[ :handler ] ||= handler.new
              begin
                ctx[ :handler ].step( FunctionProxy.new( @driver, func, ctx ),
                  *args.map{|v| Value.new(self,v)} )
              rescue Exception, StandardError => e
                ctx[ :__error ] = e
              end
            end
          end

          finalize = proc do |func|
            ctx = @driver.aggregate_context( func )
            unless ctx[ :__error ]
              ctx[ :handler ] ||= handler.new
              begin
                ctx[ :handler ].finalize( FunctionProxy.new( @driver, func, ctx ) )
              rescue Exception => e
                ctx[ :__error ] = e
              end
            end

            if ctx[ :__error ]
              e = ctx[ :__error ]
              @driver.sqlite3_result_error( func, "#{e.message} (#{e.class})", -1 )
            end
          end

          result = @driver.create_function( @handle, name, arity, text_rep, nil,
            nil, step, finalize )
          Error.check( result, self )

          self
        end