Skip to content

Protocol

SQLiteCustomAggregate

The protocol for custom SQLite aggregates.
protocol SQLiteCustomAggregate : Sendable

Overview

For example:

struct MySum: DatabaseAggregate {
    var sum: Int = 0

    mutating func step(_ dbValues: [SQLiteData]) {
        if let int = dbValues[0].integer {
            sum += int
        }
    }

    func finalize() -> (any SQLiteDataConvertible)? {
        sum
    }
}

let connection: SQLiteConnection = ...
let fn = SQLiteCustomFunction("mysum", argumentCount: 1, aggregate: MySum.self)
try await connection.install(customFunction: fn).get()
try await connection.query("CREATE TABLE test(i)").get()
try await connection.query("INSERT INTO test(i) VALUES (1)").get()
try await connection.query("INSERT INTO test(i) VALUES (2)").get()
let sum = (try await connection.query("SELECT mysum(i) FROM test").get().first?.columns.first?.integer)!

Topics

Initializers

Instance Methods

  • finalize()
    Return the final result. Called only once, at the end of the aggregation.
  • step(_:)
    This method is called at each step of the aggregation.

Relationships

Inherits From

  • Swift.Sendable
  • Swift.SendableMetatype