Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expressions are no longer PATs #330

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions GRDB/Core/DatabaseValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,6 @@ extension DatabaseValue {
}
}

// MARK: - SQLSelectable

extension DatabaseValue {
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func qualified(by qualifier: SQLTableQualifier) -> DatabaseValue {
return self
}
}

// MARK: - Lossless conversions

extension DatabaseValue {
Expand Down Expand Up @@ -360,6 +349,12 @@ extension DatabaseValue {
return SQLExpressionNot(self)
}
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
/// :nodoc:
public func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return self
}
}

// CustomStringConvertible
Expand Down
4 changes: 1 addition & 3 deletions GRDB/QueryInterface/Column.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public struct Column : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func expressionSQL(_ arguments: inout StatementArguments?) -> String {
if let qualifierName = qualifier?.name {
Expand All @@ -26,9 +25,8 @@ public struct Column : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func qualified(by qualifier: SQLTableQualifier) -> Column {
public func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
if self.qualifier != nil {
// Never requalify
return self
Expand Down
52 changes: 25 additions & 27 deletions GRDB/QueryInterface/SQLExpression+QueryInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public struct SQLExpressionLiteral : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func expressionSQL(_ arguments: inout StatementArguments?) -> String {
if let literalArguments = self.arguments {
Expand All @@ -84,9 +83,8 @@ public struct SQLExpressionLiteral : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionLiteral {
public func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return self
}
}
Expand Down Expand Up @@ -166,17 +164,15 @@ public struct SQLExpressionUnary : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func expressionSQL(_ arguments: inout StatementArguments?) -> String {
return op.sql + (op.needsRightSpace ? " " : "") + expression.expressionSQL(&arguments)
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionUnary {
return SQLExpressionUnary(op, expression.qualified(by: qualifier))
public func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionUnary(op, expression.qualifiedExpression(with: qualifier))
}
}

Expand Down Expand Up @@ -285,11 +281,13 @@ public struct SQLExpressionBinary : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
/// :nodoc:
public func expressionSQL(_ arguments: inout StatementArguments?) -> String {
return "(" + lhs.expressionSQL(&arguments) + " " + op.sql + " " + rhs.expressionSQL(&arguments) + ")"
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
/// :nodoc:
public var negated: SQLExpression {
if let negatedOp = op.negated {
return SQLExpressionBinary(negatedOp, lhs, rhs)
Expand All @@ -299,6 +297,7 @@ public struct SQLExpressionBinary : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
/// :nodoc:
public func matchedRowIds(rowIdName: String?) -> Set<Int64>? {
switch op {
case .equal, .is:
Expand Down Expand Up @@ -349,10 +348,9 @@ public struct SQLExpressionBinary : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionBinary {
return SQLExpressionBinary(op, lhs.qualified(by: qualifier), rhs.qualified(by: qualifier))
public func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionBinary(op, lhs.qualifiedExpression(with: qualifier), rhs.qualifiedExpression(with: qualifier))
}
}

Expand All @@ -375,10 +373,10 @@ struct SQLExpressionBinaryOperatorChain : SQLExpression {
return "(" + expressionSQLs.joined(separator: joiner) + ")"
}

func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionBinaryOperatorChain {
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionBinaryOperatorChain(
op: op,
expressions: expressions.map { $0.qualified(by: qualifier) })
expressions: expressions.map { $0.qualifiedExpression(with: qualifier) })
}
}

Expand Down Expand Up @@ -440,8 +438,8 @@ struct SQLExpressionContains : SQLExpression {
return rowIDs
}

func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionContains {
return SQLExpressionContains(expression.qualified(by: qualifier), collection, negated: isNegated)
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionContains(expression.qualifiedExpression(with: qualifier), collection, negated: isNegated)
}
}

Expand Down Expand Up @@ -479,11 +477,11 @@ struct SQLExpressionBetween : SQLExpression {
return SQLExpressionBetween(expression, lowerBound, upperBound, negated: !isNegated)
}

func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionBetween {
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionBetween(
expression.qualified(by: qualifier),
lowerBound.qualified(by: qualifier),
upperBound.qualified(by: qualifier),
expression.qualifiedExpression(with: qualifier),
lowerBound.qualifiedExpression(with: qualifier),
upperBound.qualifiedExpression(with: qualifier),
negated: isNegated)
}
}
Expand Down Expand Up @@ -570,15 +568,15 @@ public struct SQLExpressionFunction : SQLExpression {
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
/// :nodoc:
public func expressionSQL(_ arguments: inout StatementArguments?) -> String {
return functionName.sql + "(" + (self.arguments.map { $0.expressionSQL(&arguments) } as [String]).joined(separator: ", ") + ")"
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionFunction {
return SQLExpressionFunction(functionName, arguments: arguments.map { $0.qualified(by: qualifier) })
public func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionFunction(functionName, arguments: arguments.map { $0.qualifiedExpression(with: qualifier) })
}
}

Expand All @@ -600,8 +598,8 @@ struct SQLExpressionCount : SQLExpression {
return "COUNT(" + counted.countedSQL(&arguments) + ")"
}

func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionCount {
return SQLExpressionCount(counted.qualified(by: qualifier))
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionCount(counted.qualifiedSelectable(with: qualifier))
}
}

Expand All @@ -622,8 +620,8 @@ struct SQLExpressionCountDistinct : SQLExpression {
return "COUNT(DISTINCT " + counted.expressionSQL(&arguments) + ")"
}

func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionCountDistinct {
return SQLExpressionCountDistinct(counted.qualified(by: qualifier))
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionCountDistinct(counted.qualifiedExpression(with: qualifier))
}
}

Expand Down Expand Up @@ -651,7 +649,7 @@ struct SQLExpressionCollate : SQLExpression {
}
}

func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionCollate {
return SQLExpressionCollate(expression.qualified(by: qualifier), collationName: collationName)
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionCollate(expression.qualifiedExpression(with: qualifier), collationName: collationName)
}
}
16 changes: 14 additions & 2 deletions GRDB/QueryInterface/SQLExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public protocol SQLExpression : SQLSpecificExpressible, SQLSelectable, SQLOrderi
///
/// Returns the rowIds matched by the expression.
func matchedRowIds(rowIdName: String?) -> Set<Int64>? // TODO: this method should take SQLTableQualifier in account

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression
}

extension SQLExpression {
Expand All @@ -84,6 +87,15 @@ extension SQLExpression {
public func matchedRowIds(rowIdName: String?) -> Set<Int64>? {
return nil
}

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// The default implementation returns qualifiedExpression(with:)
///
/// :nodoc:
public func qualifiedSelectable(with qualifier: SQLTableQualifier) -> SQLSelectable {
return qualifiedExpression(with: qualifier)
}
}

// SQLExpression: SQLExpressible
Expand Down Expand Up @@ -137,7 +149,7 @@ struct SQLExpressionNot : SQLExpression {
return expression
}

func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionNot {
return SQLExpressionNot(expression.qualified(by: qualifier))
func qualifiedExpression(with qualifier: SQLTableQualifier) -> SQLExpression {
return SQLExpressionNot(expression.qualifiedExpression(with: qualifier))
}
}
6 changes: 3 additions & 3 deletions GRDB/QueryInterface/SQLSelectable+QueryInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension AllColumns : SQLSelectable {
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
///
/// :nodoc:
public func qualified(by qualifier: SQLTableQualifier) -> AllColumns {
public func qualifiedSelectable(with qualifier: SQLTableQualifier) -> SQLSelectable {
if self.qualifier != nil {
// Never requalify
return self
Expand Down Expand Up @@ -110,8 +110,8 @@ struct SQLAliasedExpression : SQLSelectable {
return expression.count(distinct: distinct)
}

func qualified(by qualifier: SQLTableQualifier) -> SQLAliasedExpression {
return SQLAliasedExpression(expression.qualified(by: qualifier), alias: alias)
func qualifiedSelectable(with qualifier: SQLTableQualifier) -> SQLSelectable {
return SQLAliasedExpression(expression.qualifiedExpression(with: qualifier), alias: alias)
}

func columnCount(_ db: Database) throws -> Int {
Expand Down
4 changes: 2 additions & 2 deletions GRDB/QueryInterface/SQLSelectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public protocol SQLSelectable {
func columnCount(_ db: Database) throws -> Int

/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
func qualified(by qualifier: SQLTableQualifier) -> Self
func qualifiedSelectable(with qualifier: SQLTableQualifier) -> SQLSelectable
}

// MARK: - SQLSelectionLiteral
Expand Down Expand Up @@ -58,7 +58,7 @@ struct SQLSelectionLiteral : SQLSelectable {
fatalError("Selection literals don't known how many columns they contain. To resolve this error, select one or several SQLExpressionLiteral instead.")
}

func qualified(by qualifier: SQLTableQualifier) -> SQLSelectionLiteral {
func qualifiedSelectable(with qualifier: SQLTableQualifier) -> SQLSelectable {
return self
}
}
Expand Down
4 changes: 2 additions & 2 deletions GRDB/Record/TableRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension TableRecord {
/// let sql = "SELECT \(Player.selectionSQL(alias: "p")) FROM players AS p"
public static func selectionSQL(alias: String? = nil) -> String {
let qualifier = SQLTableQualifier(tableName: databaseTableName, alias: alias ?? databaseTableName)
let selection = databaseSelection.map { $0.qualified(by: qualifier) }
let selection = databaseSelection.map { $0.qualifiedSelectable(with: qualifier) }
var arguments: StatementArguments? = nil
return selection
.map { $0.resultColumnSQL(&arguments) }
Expand Down Expand Up @@ -115,7 +115,7 @@ extension TableRecord {
public static func numberOfSelectedColumns(_ db: Database) throws -> Int {
let qualifier = SQLTableQualifier(tableName: databaseTableName, alias: nil)
return try databaseSelection
.map { try $0.qualified(by: qualifier).columnCount(db) }
.map { try $0.qualifiedSelectable(with: qualifier).columnCount(db) }
.reduce(0, +)
}
}
Expand Down