init(_:argumentCount:pure:function:)
Creates an SQL function.
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 optionalDatabaseValueConvertible
such asInt
,String
,Date
, etc. The array is guaranteed to have exactlyargumentCount
elements, providedargumentCount
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
func add(function: DatabaseFunction
) Adds or redefines a custom SQL function.