dotConnect for SQLite Documentation
Devart.Data.SQLite Namespace / SQLiteAggregateFunction Class
Members Example

SQLiteAggregateFunction Class
Base class for user-defined aggregate functions.
Syntax
'Declaration
 
Public MustInherit Class SQLiteAggregateFunction 
   Inherits SQLiteFunction
   Implements System.IDisposable 
 
Remarks

Use this class to derive classes for user-defined aggregate functions from it.

The result of an aggregate function is a scalar object, but its value is accumulated when processing query result records. It is executed for each record, and its result can be accumulated. Average, Summ, or Count are the examples of such functions.

To register the user-defined aggregate funtion, create the class, derived from SQLiteAggregateFunction and pass its name and number of arguments to the base class constructor.

You should also override Step and Complete methods. Step method should implement the way of acumulating and storing result, and Complete method should process the accumulated value after all records were processed.

Example
This example illustrates creating and using user-defined function for calculating Average value. Step method is used to accumulate sum of values and iteration count, and Complete method is used to divide sum by iteration count.
public class MyFunction : SQLiteAggregateFunction {

        private long count;

        public MyFunction() : base("Average", 1) {

                count = 0;
        }

        protected override void Step(object[] args, SQLiteConnection connection, ref object contextData) {

                if (contextData == null)
                        contextData = (Int64)0;
                contextData = (Int64)contextData + (Int64)args[0];
                count++;
        }

        protected override object Complete(SQLiteConnection connection, object contextData) {
      
                return (double)((long)contextData / count);
        }
}

... 

SQLiteConnection sqLiteConnection = new SQLiteConnection(@"Data Source=D:\SQLite\test.db");
sqLiteConnection.Open();
MyFunction function = new MyFunction();
sqLiteConnection.RegisterFunction(function);
SQLiteCommand command = new SQLiteCommand("select Average(deptno) from Dept", sqLiteConnection);
double result = (double)command.ExecuteScalar();
sqLiteConnection.UnRegisterFunction(function);
sqLiteConnection.Close();
Inheritance Hierarchy
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also