### Home Classes Methods

### In Files

- lib/amalgalite/aggregate.rb

### Parent

Object

### Methods

- #finalize
- #signature

### Files

- HISTORY.rdoc
- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Amalgalite
- Amalgalite::Aggregate
- Amalgalite::Blob
- Amalgalite::Blob::Error
- Amalgalite::Boolean
- Amalgalite::BusyHandler
- Amalgalite::BusyTimeout
- Amalgalite::CSVTableImporter
- Amalgalite::Column
- Amalgalite::Database
- Amalgalite::Database::AggregateError
- Amalgalite::Database::BusyHandlerError
- Amalgalite::Database::FunctionError
- Amalgalite::Database::InvalidModeError
- Amalgalite::Database::ProgressHandlerError
- Amalgalite::Database::TransactionBehavior
- Amalgalite::Error
- Amalgalite::Function
- Amalgalite::Index
- Amalgalite::MemoryDatabase
- Amalgalite::Packer
- Amalgalite::Paths
- Amalgalite::ProfileSampler
- Amalgalite::ProfileTap
- Amalgalite::ProgressHandler
- Amalgalite::Requires
- Amalgalite::SQLite3
- Amalgalite::SQLite3::Constants
- Amalgalite::SQLite3::Constants::Config
- Amalgalite::SQLite3::Constants::DBStatus
- Amalgalite::SQLite3::Constants::DataType
- Amalgalite::SQLite3::Constants::Helpers
- Amalgalite::SQLite3::Constants::Open
- Amalgalite::SQLite3::Constants::ResultCode
- Amalgalite::SQLite3::Constants::StatementStatus
- Amalgalite::SQLite3::Constants::Status
- Amalgalite::SQLite3::Database
- Amalgalite::SQLite3::Database::DBStatus
- Amalgalite::SQLite3::Database::Function
- Amalgalite::SQLite3::Database::Stat
- Amalgalite::SQLite3::Stat
- Amalgalite::SQLite3::Status
- Amalgalite::SQLite3::Version
- Amalgalite::Schema
- Amalgalite::Statement
- Amalgalite::Table
- Amalgalite::Taps
- Amalgalite::Taps::IO
- Amalgalite::Taps::Stderr
- Amalgalite::Taps::Stdout
- Amalgalite::Taps::StringIO
- Amalgalite::TraceTap
- Amalgalite::TypeMap
- Amalgalite::TypeMaps
- Amalgalite::TypeMaps::DefaultMap
- Amalgalite::TypeMaps::StorageMap
- Amalgalite::TypeMaps::TextMap
- Amalgalite::Version
- Amalgalite::View
- Kernel
- Object

No matching classes.

# Amalgalite::Aggregate

A Base class to inherit from for creating your own SQL aggregate functions in ruby.

These are SQL functions similar to _max(X)_, _count(X)_, _avg(X)_. The built in SQLite aggregate functions are:

- www.sqlite.org/lang_aggfunc.html

If you choose to use Aggregate as a parent class of your SQL scalar function implementation you must:

- implement initalize with 0 arguments

- set the @arity data member

- set the @name data member

- implement step with arity of +@arity+

- implement finalize with arity of 0

For instance to implement a unique_word_count(X) aggregate function you could implement it as:

    class UniqueWordCount < ::Amalgalite::Aggregate
      attr_accessor :words

      def initialize
        @name = 'unique_word_count'
        @arity = 1
        @words = Hash.new { |h,k| h[k] = 0 }
      end

      def step( str )
        str.split(/\W+/).each do |word|
          words[ word.downcase ] += 1
        end
        return nil
      end

      def finalize
        return words.size
      end
    end

### Attributes

arity[RW]

The arity of the SQL function

name[RW]

The name of the SQL function

### Public Instance Methods

finalize() click to toggle source

finalize should return the final value of the aggregate function

    # File lib/amalgalite/aggregate.rb, line 54
    def finalize
      raise NotImplementedError, "Aggregate#finalize must be implemented"
    end

signature() click to toggle source

Do Not Override

The function signature for use by the Amaglaite datase in tracking function creation.

    # File lib/amalgalite/aggregate.rb, line 63
    def signature
      @signature ||= ::Amalgalite::SQLite3::Database::Function.signature( self.name, self.arity )
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.