Functions

The following functions are available globally.

  • Returns an array of row adapters that split a row according to the input number of columns.

    For example:

    let sql = "SELECT 1, 2,3,4, 5,6, 7,8"
    //               <.><. . .><. .><. .>
    let adapters = splittingRowAdapters([1, 3, 2])
    let adapter = ScopeAdapter([
        "a": adapters[0],
        "b": adapters[1],
        "c": adapters[2],
        "d": adapters[3]])
    let row = try Row.fetchOne(db, sql: sql, adapter: adapter)
    row.scopes["a"] // [1]
    row.scopes["b"] // [2, 3, 4]
    row.scopes["c"] // [5, 6]
    row.scopes["d"] // [7, 8]
    

Logical Operators (AND, OR, NOT)

  • Returns a logically negated aggregate.

    For example:

    Author.having(!Author.books.isEmpty)
    
  • Groups two aggregates with the AND SQL operator.

    For example:

    Author.having(Author.books.isEmpty && Author.paintings.isEmpty)
    
  • Groups two aggregates with the OR SQL operator.

    For example:

    Author.having(!Author.books.isEmpty || !Author.paintings.isEmpty)
    

Egality and Identity Operators (=, <>, IS, IS NOT)

  • Returns an aggregate that compares two aggregates with the = SQL operator.

    For example:

    Author.having(Author.books.count == Author.paintings.count)
    
  • Returns an aggregate that compares two aggregates with the <> SQL operator.

    For example:

    Author.having(Author.books.count != Author.paintings.count)
    
  • Returns an aggregate that compares two aggregates with the IS SQL operator.

    For example:

    Author.having(Author.books.count === Author.paintings.count)
    
  • Returns an aggregate that compares two aggregates with the IS NOT SQL operator.

    For example:

    Author.having(Author.books.count !== Author.paintings.count)
    

Comparison Operators (<, >, <=, >=)

  • Returns an aggregate that compares two aggregates with the <= SQL operator.

    For example:

    Author.having(Author.books.count <= Author.paintings.count)
    
  • Returns an aggregate that compares two aggregates with the < SQL operator.

    For example:

    Author.having(Author.books.count < Author.paintings.count)
    
  • Returns an aggregate that compares two aggregates with the > SQL operator.

    For example:

    Author.having(Author.books.count > Author.paintings.count)
    
  • Returns an aggregate that compares two aggregates with the >= SQL operator.

    For example:

    Author.having(Author.books.count >= Author.paintings.count)
    

Arithmetic Operators (+, -, *, /)

  • Returns an arithmetically negated aggregate.

    For example:

    Author.annotated(with: -Author.books.count)
    
  • Returns an aggregate that sums two aggregates with the + SQL operator.

    For example:

    Author.annotated(with: Author.books.count + Author.paintings.count)
    
  • Returns an aggregate that substracts two aggregates with the - SQL operator.

    For example:

    Author.annotated(with: Author.books.count - Author.paintings.count)
    
  • Returns an aggregate that multiplies two aggregates with the * SQL operator.

    For example:

    Author.annotated(with: Author.books.count * Author.paintings.count)
    
  • Returns an aggregate that multiplies two aggregates with the / SQL operator.

    For example:

    Author.annotated(with: Author.books.count / Author.paintings.count)
    

IFNULL(…)

  • Returns an aggregate that evaluates the IFNULL SQL function.

    Team.annotated(with: Team.players.min(Column("score")) ?? 0)
    

ABS(…)

  • Returns an aggregate that evaluates to the absolute value of the input aggregate.

LENGTH(…)

  • Returns an aggregate that evaluates the LENGTH SQL function on the input aggregate.

ColumnAssignment

  • Creates an assignment that adds a value

    Column("score") += 1
    Column("score") += Column("bonus")
    
    try dbQueue.write { db in
        // UPDATE player SET score = score + 1
        try Player.updateAll(db, Column("score") += 1)
    }
    
  • Creates an assignment that subtracts a value

    Column("score") -= 1
    Column("score") -= Column("bonus")
    
    try dbQueue.write { db in
        // UPDATE player SET score = score - 1
        try Player.updateAll(db, Column("score") -= 1)
    }
    
  • Creates an assignment that multiplies by a value

    Column("score") *= 2
    Column("score") *= Column("factor")
    
    try dbQueue.write { db in
        // UPDATE player SET score = score * 2
        try Player.updateAll(db, Column("score") *= 2)
    }
    
  • Creates an assignment that divides by a value

    Column("score") /= 2
    Column("score") /= Column("factor")
    
    try dbQueue.write { db in
        // UPDATE player SET score = score / 2
        try Player.updateAll(db, Column("score") /= 2)
    }
    

AVG(…)

  • Returns an expression that evaluates the AVG SQL function.

    // AVG(length)
    average(Column("length"))
    

COUNT(…)

  • Returns an expression that evaluates the COUNT SQL function.

    // COUNT(email)
    count(Column("email"))
    

COUNT(DISTINCT …)

  • Returns an expression that evaluates the COUNT(DISTINCT) SQL function.

    // COUNT(DISTINCT email)
    count(distinct: Column("email"))
    

MAX(…)

  • Returns an expression that evaluates the MAX SQL function.

    // MAX(score)
    max(Column("score"))
    

MIN(…)

  • Returns an expression that evaluates the MIN SQL function.

    // MIN(score)
    min(Column("score"))
    

SUM(…)

TOTAL(…)

JULIANDAY(…)

DATETIME(…)

Public

  • Return as many question marks separated with commas as the count argument.

    databaseQuestionMarks(count: 3) // "?,?,?"