DatabaseFunction

public final class DatabaseFunction : Hashable

An SQL function or aggregate.

  • The name of the SQL function

    Declaration

    Swift

    public var name: String { get }
  • Creates an SQL function.

    For example:

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

    Declaration

    Swift

    public init(
        _ name: String,
        argumentCount: Int32? = nil,
        pure: Bool = false,
        function: @escaping ([DatabaseValue]) throws -> 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, NSDate, etc. The array is guaranteed to have exactly argumentCount elements, provided argumentCount is not nil.

  • Creates an SQL aggregate function.

    For example:

    struct MySum: DatabaseAggregate {
        var sum: Int = 0
    
        mutating func step(_ dbValues: [DatabaseValue]) {
            if let int = Int.fromDatabaseValue(dbValues[0]) {
                sum += int
            }
        }
    
        func finalize() -> DatabaseValueConvertible? {
            return sum
        }
    }
    
    let dbQueue = DatabaseQueue()
    let fn = DatabaseFunction("mysum", argumentCount: 1, aggregate: MySum.self)
    try dbQueue.write { db in
        db.add(function: fn)
        try db.execute(sql: "CREATE TABLE test(i)")
        try db.execute(sql: "INSERT INTO test(i) VALUES (1)")
        try db.execute(sql: "INSERT INTO test(i) VALUES (2)")
        try Int.fetchOne(db, sql: "SELECT mysum(i) FROM test")! // 3
    }
    

    Declaration

    Swift

    public init<Aggregate: DatabaseAggregate>(
        _ name: String,
        argumentCount: Int32? = nil,
        pure: Bool = false,
        aggregate: Aggregate.Type)

    Parameters

    name

    The function name.

    argumentCount

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

    pure

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

    aggregate

    A type that implements the DatabaseAggregate protocol. For each step of the aggregation, its step method is called with an array of DatabaseValue arguments. The array is guaranteed to have exactly argumentCount elements, provided argumentCount is not nil.

  • Returns an SQL expression that applies the function.

    See https://github.com/groue/GRDB.swift/#sql-functions

    Declaration

    Swift

    public func callAsFunction(_ arguments: SQLExpressible...) -> SQLExpression

SQL Functions

  • An SQL function that returns the Swift built-in capitalized String property.

    The function returns NULL for non-strings values.

    This function is automatically added by GRDB to your database connections. It is the function used by the query interface’s capitalized:

    let nameColumn = Column("name")
    let request = Player.select(nameColumn.capitalized)
    let names = try String.fetchAll(dbQueue, request)   // [String]
    

    Declaration

    Swift

    public static let capitalize: DatabaseFunction
  • An SQL function that returns the Swift built-in lowercased String property.

    The function returns NULL for non-strings values.

    This function is automatically added by GRDB to your database connections. It is the function used by the query interface’s lowercased:

    let nameColumn = Column("name")
    let request = Player.select(nameColumn.lowercased())
    let names = try String.fetchAll(dbQueue, request)   // [String]
    

    Declaration

    Swift

    public static let lowercase: DatabaseFunction
  • An SQL function that returns the Swift built-in uppercased String property.

    The function returns NULL for non-strings values.

    This function is automatically added by GRDB to your database connections. It is the function used by the query interface’s uppercased:

    let nameColumn = Column("name")
    let request = Player.select(nameColumn.uppercased())
    let names = try String.fetchAll(dbQueue, request)   // [String]
    

    Declaration

    Swift

    public static let uppercase: DatabaseFunction
  • An SQL function that returns the Swift built-in localizedCapitalized String property.

    The function returns NULL for non-strings values.

    This function is automatically added by GRDB to your database connections. It is the function used by the query interface’s localizedCapitalized:

    let nameColumn = Column("name")
    let request = Player.select(nameColumn.localizedCapitalized)
    let names = try String.fetchAll(dbQueue, request)   // [String]
    

    Declaration

    Swift

    @available(macOS 10.11, watchOS 3.0, *)
    public static let localizedCapitalize: DatabaseFunction
  • An SQL function that returns the Swift built-in localizedLowercased String property.

    The function returns NULL for non-strings values.

    This function is automatically added by GRDB to your database connections. It is the function used by the query interface’s localizedLowercased:

    let nameColumn = Column("name")
    let request = Player.select(nameColumn.localizedLowercased)
    let names = try String.fetchAll(dbQueue, request)   // [String]
    

    Declaration

    Swift

    @available(macOS 10.11, watchOS 3.0, *)
    public static let localizedLowercase: DatabaseFunction
  • An SQL function that returns the Swift built-in localizedUppercased String property.

    The function returns NULL for non-strings values.

    This function is automatically added by GRDB to your database connections. It is the function used by the query interface’s localizedUppercased:

    let nameColumn = Column("name")
    let request = Player.select(nameColumn.localizedUppercased)
    let names = try String.fetchAll(dbQueue, request)   // [String]
    

    Declaration

    Swift

    @available(macOS 10.11, watchOS 3.0, *)
    public static let localizedUppercase: DatabaseFunction