SQLExpression
public protocol SQLExpression : SQLSpecificExpressible, SQLSelectable, SQLOrderingTerm
SQLExpression is the protocol for types that represent an SQL expression, as described at https://www.sqlite.org/lang_expr.html
GRDB ships with a variety of types that already adopt this protocol, and allow to represent many SQLite expressions:
- Column
- DatabaseValue
- SQLExpressionLiteral
- SQLExpressionUnary
- SQLExpressionBinary
- SQLExpressionExists
- SQLExpressionFunction
- SQLExpressionCollate
-
Returns an SQL string that represents the expression.
When the arguments parameter is nil, any value must be written down as a literal in the returned SQL:
var arguments: StatementArguments? = nil let expression = "foo'bar".databaseValue expression.expressionSQL(&arguments) // "'foo''bar'"
When the arguments parameter is not nil, then values may be replaced by
?
or colon-prefixed tokens, and fed into arguments.var arguments = StatementArguments() let expression = "foo'bar".databaseValue expression.expressionSQL(&arguments) // "?" arguments // ["foo'bar"]
Declaration
Swift
func expressionSQL(_ arguments: inout StatementArguments?) -> String
-
negated
Default implementationReturns the expression, negated. This property fuels the
!
operator.The default implementation returns the expression prefixed by
NOT
.let column = Column("favorite") column.negated // NOT favorite
Some expressions may provide a custom implementation that returns a more natural SQL expression.
let expression = [1,2,3].contains(Column("id")) // id IN (1,2,3) expression.negated // id NOT IN (1,2,3)
Default Implementation
The default implementation returns the expression prefixed by
NOT
.let column = Column("favorite") column.negated // NOT favorite
Declaration
Swift
var negated: SQLExpression
-
matchedRowIds(rowIdName:)
Default implementationReturns the rowIds matched by the expression.
Default Implementation
The default implementation returns nil
Declaration
Swift
func matchedRowIds(rowIdName: String?) -> Set<Int64>?
-
literal
Extension methodConverts an expression to an SQLExpressionLiteral
Declaration
Swift
public var literal: SQLExpressionLiteral
-
sqlExpression
Extension methodDeclaration
Swift
public var sqlExpression: SQLExpression
-
count(distinct:)
Extension methodDeclaration
Swift
public func count(distinct: Bool) -> SQLCount?