Initializergrdb 7.3.0GRDB

init(_:argumentCount:pure:function:)

Creates an SQL function.

DatabaseFunction.swift:85
init(_ name: String, argumentCount: Int? = nil, pure: Bool = false, function: @escaping ([DatabaseValue]) throws -> (any DatabaseValueConvertible)?)

Parameters

name

The function name.

argumentCount

The number of arguments of the function. If omitted, or nil, the function accepts any number of arguments.

pure

Whether the function is “pure”, which means that its results only depends on its inputs. When a function is pure, SQLite has the opportunity to perform additional optimizations. Default value is false.

function

A function that takes an array of DatabaseValue arguments, and returns an optional DatabaseValueConvertible such as Int, String, Date, etc. The array is guaranteed to have exactly argumentCount elements, provided argumentCount is not nil.

For example:

let succ = DatabaseFunction("succ", argumentCount: 1) { dbValues in
    guard let int = Int.fromDatabaseValue(dbValues[0]) else {
        return nil
    }
    return int + 1
}
let dbQueue = try DatabaseQueue()
try dbQueue.read { db in
    db.add(function: succ)
    try Int.fetchOne(db, sql: "SELECT succ(1)")! // 2
}

Related APIs