DatabaseFunction

public final class DatabaseFunction : Hashable

An SQL function or aggregate.

  • Undocumented

    Declaration

    Swift

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

    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.

  • Returns an SQL aggregate function.

    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)
    dbQueue.add(function: fn)
    try dbQueue.write { db in
        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.

  • 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(iOS 9.0, OSX 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(iOS 9.0, OSX 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(iOS 9.0, OSX 10.11, watchOS 3.0, *)
    public static let localizedUppercase: DatabaseFunction