Rename SQLSource to SQLRelation.Source

This commit is contained in:
Gwendal Roué committed 2019-02-03 10:06:23 +01:00
1 parent 91b68ffa4f
commit 4a209fcbc4
2 files changed
+163 -159

No files matched your search

-70
View File
@@ -511,76 +511,6 @@ struct Join {
}
}
// MARK: - SQLSource
enum SQLSource {
case table(tableName: String, alias: TableAlias?)
indirect case query(SQLSelectQuery)
var alias: TableAlias? {
switch self {
case .table(_, let alias):
return alias
case .query(let query):
return query.alias
}
}
func sourceSQL(_ db: Database, _ context: inout SQLGenerationContext) throws -> String {
switch self {
case .table(let tableName, let alias):
if let alias = alias, let aliasName = context.aliasName(for: alias) {
return "\(tableName.quotedDatabaseIdentifier) \(aliasName.quotedDatabaseIdentifier)"
} else {
return "\(tableName.quotedDatabaseIdentifier)"
}
case .query(let query):
return try "(\(query.sql(db, &context)))"
}
}
func qualified(with alias: TableAlias) -> SQLSource {
switch self {
case .table(let tableName, let sourceAlias):
if let sourceAlias = sourceAlias {
alias.becomeProxy(of: sourceAlias)
return self
} else {
alias.setTableName(tableName)
return .table(tableName: tableName, alias: alias)
}
case .query(let query):
return .query(query.qualified(with: alias))
}
}
/// Returns nil if sources can't be merged (conflict in tables, aliases...)
func merged(with other: SQLSource) -> SQLSource? {
switch (self, other) {
case let (.table(tableName: tableName, alias: alias), .table(tableName: otherTableName, alias: otherAlias)):
guard tableName == otherTableName else {
// can't merge
return nil
}
switch (alias, otherAlias) {
case (nil, nil):
return .table(tableName: tableName, alias: nil)
case let (alias?, nil), let (nil, alias?):
return .table(tableName: tableName, alias: alias)
case let (alias?, otherAlias?):
guard let mergedAlias = alias.merge(with: otherAlias) else {
// can't merge
return nil
}
return .table(tableName: tableName, alias: mergedAlias)
}
default:
// can't merge
return nil
}
}
}
struct SQLLimit {
let limit: Int
let offset: Int?