DatabaseFunction

An SQL function or aggregate.

  • The name of the SQL function

  • 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
    
  • 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() -> (any DatabaseValueConvertible)? {
            return sum
        }
    }
    
    let dbQueue = try 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
    }
    
  • Returns an SQL expression that applies the function.

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

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]
    
  • 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]
    
  • 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]
    
  • 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]
    
  • 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]
    
  • 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]