Fattie
Fattie

Reputation: 12651

SQLite.Swift Expression() initialization for "not null" form?

Please note that this question ONLY relates to the popular SQLite.swift library, stephencelis/SQLite.swift

With SQLite.swift you can

    let a = Expression<String>("a")
    let b = Expression<String>("b")

and so on. But how do you

select a.x, a.y, ifnull(b.q, 'default text'), a.z
from a
left join b on blah

how do you make an expression for an inline sql ifnull clause?

(The doco mentions that Expression has an init(literal:) initializer - maybe it's relevant - but it's undocumented and has unusual binding arguments.)

Please note, I'm completely aware that you could make the value optional

  let q = Expression<String?>("q")

and then just put in the default later;

I am asking how to express "ifnull(b.q, 'default text')" as an Expression (or, learn it is impossible) so that value will actually be used in the SQL expression.

Once again, this question relates only to the library /stephencelis/SQLite.swift

Upvotes: 2

Views: 754

Answers (2)

GuessWho
GuessWho

Reputation: 672

Maybe this is too late, but anyway. I've checked the source code, there is function

public func ??<V : Value>(optional: Expression<V?>, defaultValue: V) -> Expression<V> {
    return "ifnull".wrap([optional, defaultValue])
}

So you have to make the value optional

row.get(Expression<Double?>("q")) ?? 0 //this will be equals ifnull(q, 0)

Upvotes: 2

Fattie
Fattie

Reputation: 12651

I dislike answering my own question,

but honestly the realistic answer here is:

nowadays you have to use GRDB like everyone else, github.com/groue/GRDB.swift

The older SQL-wrapper libraries (as magnificent, awesome, incredible as they were at the time) are honestly just

  • totally out of date now, technology-wise
  • not honestly maintained realistically any more

As of late 2017 GRDB is the only real possibility.

Upvotes: 1

Related Questions