mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
TableMapping.selectionSQL(_:)
This commit is contained in:
1 parent
22949813af
commit
3bdfdaa40a
12 files changed
+371
-7
No files matched your search
@@ -140,6 +140,8 @@
|
||||
564448831EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448871EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564A50C81BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */; };
|
||||
564E73DF203D50B9000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73DE203D50B9000C443C /* JoinSupportTests.swift */; };
|
||||
564E73E0203D50B9000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73DE203D50B9000C443C /* JoinSupportTests.swift */; };
|
||||
564F9C1E1F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
564F9C221F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
564F9C2D1F075DD200877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
@@ -787,6 +789,7 @@
|
||||
56439B501F4CA1DC0066043F /* GRDBOSXPerformanceComparisonTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBOSXPerformanceComparisonTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterNextTransactionCommitTests.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
|
||||
564E73DE203D50B9000C443C /* JoinSupportTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinSupportTests.swift; sourceTree = "<group>"; };
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseFunction.swift; sourceTree = "<group>"; };
|
||||
565490A01D5A4798005622CB /* GRDB.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GRDB.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -1468,6 +1471,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56B15D0A1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift */,
|
||||
564E73DE203D50B9000C443C /* JoinSupportTests.swift */,
|
||||
560B3FA41C19DFF800C58EC7 /* Persistable */,
|
||||
56176C9E1EACEDF9000F3F2B /* Record */,
|
||||
5680520F201320E600E87861 /* RecordBoxTests.swift */,
|
||||
@@ -2421,6 +2425,7 @@
|
||||
56176C6C1EACCCC9000F3F2B /* FTS5PatternTests.swift in Sources */,
|
||||
563363B21C933FF8000BE133 /* PersistableTests.swift in Sources */,
|
||||
56A238421B9C74A90082EB20 /* DatabaseValueConversionTests.swift in Sources */,
|
||||
564E73E0203D50B9000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A7121F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
5657AB521D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */,
|
||||
56A238441B9C74A90082EB20 /* RawRepresentable+DatabaseValueConvertibleTests.swift in Sources */,
|
||||
@@ -2565,6 +2570,7 @@
|
||||
56176C5A1EACCCC7000F3F2B /* FTS5PatternTests.swift in Sources */,
|
||||
56D496581D81304E008276D7 /* FoundationDateTests.swift in Sources */,
|
||||
562205F31E420E47005860AC /* DatabaseQueueReleaseMemoryTests.swift in Sources */,
|
||||
564E73DF203D50B9000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A70F1F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
56D496711D81309E008276D7 /* RecordPrimaryKeySingleTests.swift in Sources */,
|
||||
5698AC031D9B9FCF0056AF8C /* QueryInterfaceExtensibilityTests.swift in Sources */,
|
||||
|
||||
@@ -204,6 +204,17 @@ 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 {
|
||||
|
||||
+3
-1
@@ -132,7 +132,8 @@ extension Row {
|
||||
// MARK: - Extracting Values
|
||||
|
||||
/// Returns true if and only if one column contains a non-null value.
|
||||
public var hasNonNullValue: Bool {
|
||||
public var containsNonNullValue: Bool {
|
||||
// In case of trouble, see 0149a0265010ef7bb7b41815f91315c4b8dbd4fd
|
||||
if let sqliteStatement = sqliteStatement { // fast path
|
||||
for i in (0..<Int32(count)) {
|
||||
if sqlite3_column_type(sqliteStatement, Int32(i)) != SQLITE_NULL { return true }
|
||||
@@ -151,6 +152,7 @@ extension Row {
|
||||
/// righmost column.
|
||||
public func hasNull(atIndex index: Int) -> Bool {
|
||||
GRDBPrecondition(index >= 0 && index < count, "row index out of range")
|
||||
// In case of trouble, see 0149a0265010ef7bb7b41815f91315c4b8dbd4fd
|
||||
if let sqliteStatement = sqliteStatement { // fast path
|
||||
return sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL
|
||||
}
|
||||
|
||||
@@ -1,25 +1,40 @@
|
||||
/// A column in the database
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift#the-query-interface
|
||||
public struct Column {
|
||||
public struct Column : SQLExpression {
|
||||
/// The hidden rowID column
|
||||
public static let rowID = Column("rowid")
|
||||
|
||||
/// The name of the column
|
||||
public let name: String
|
||||
|
||||
private var qualifier: SQLTableQualifier?
|
||||
|
||||
/// Creates a column given its name.
|
||||
public init(_ name: String) {
|
||||
self.name = name
|
||||
}
|
||||
}
|
||||
|
||||
extension 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 {
|
||||
return qualifierName.quotedDatabaseIdentifier + "." + name.quotedDatabaseIdentifier
|
||||
}
|
||||
return name.quotedDatabaseIdentifier
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// :nodoc:
|
||||
public func qualified(by qualifier: SQLTableQualifier) -> Column {
|
||||
if self.qualifier != nil {
|
||||
// Never requalify
|
||||
return self
|
||||
}
|
||||
var column = self
|
||||
column.qualifier = qualifier
|
||||
return column
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,13 @@ public struct SQLExpressionLiteral : SQLExpression {
|
||||
}
|
||||
return sql
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// :nodoc:
|
||||
public func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionLiteral {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionUnary
|
||||
@@ -149,9 +156,18 @@ 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))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionBinary
|
||||
@@ -331,6 +347,13 @@ public struct SQLExpressionBinary : SQLExpression {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// [**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))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionContains
|
||||
@@ -390,6 +413,10 @@ struct SQLExpressionContains : SQLExpression {
|
||||
}
|
||||
return rowIDs
|
||||
}
|
||||
|
||||
func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionContains {
|
||||
return SQLExpressionContains(expression.qualified(by: qualifier), collection, negated: isNegated)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionBetween
|
||||
@@ -425,6 +452,14 @@ struct SQLExpressionBetween : SQLExpression {
|
||||
var negated: SQLExpression {
|
||||
return SQLExpressionBetween(expression, lowerBound, upperBound, negated: !isNegated)
|
||||
}
|
||||
|
||||
func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionBetween {
|
||||
return SQLExpressionBetween(
|
||||
expression.qualified(by: qualifier),
|
||||
lowerBound.qualified(by: qualifier),
|
||||
upperBound.qualified(by: qualifier),
|
||||
negated: isNegated)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionFunction
|
||||
@@ -510,6 +545,13 @@ public struct SQLExpressionFunction : SQLExpression {
|
||||
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) })
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionCount
|
||||
@@ -529,6 +571,10 @@ struct SQLExpressionCount : SQLExpression {
|
||||
func expressionSQL(_ arguments: inout StatementArguments?) -> String {
|
||||
return "COUNT(" + counted.countedSQL(&arguments) + ")"
|
||||
}
|
||||
|
||||
func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionCount {
|
||||
return SQLExpressionCount(counted.qualified(by: qualifier))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionCountDistinct
|
||||
@@ -547,6 +593,10 @@ struct SQLExpressionCountDistinct : SQLExpression {
|
||||
func expressionSQL(_ arguments: inout StatementArguments?) -> String {
|
||||
return "COUNT(DISTINCT " + counted.expressionSQL(&arguments) + ")"
|
||||
}
|
||||
|
||||
func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionCountDistinct {
|
||||
return SQLExpressionCountDistinct(counted.qualified(by: qualifier))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SQLExpressionCollate
|
||||
@@ -572,4 +622,8 @@ struct SQLExpressionCollate : SQLExpression {
|
||||
return sql + " COLLATE " + collationName.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionCollate {
|
||||
return SQLExpressionCollate(expression.qualified(by: qualifier), collationName: collationName)
|
||||
}
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public protocol SQLExpression : SQLSpecificExpressible, SQLSelectable, SQLOrderi
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// Returns the rowIds matched by the expression.
|
||||
func matchedRowIds(rowIdName: String?) -> Set<Int64>?
|
||||
func matchedRowIds(rowIdName: String?) -> Set<Int64>? // TODO: this method should take SQLTableQualifier in account
|
||||
}
|
||||
|
||||
extension SQLExpression {
|
||||
@@ -136,4 +136,8 @@ struct SQLExpressionNot : SQLExpression {
|
||||
var negated: SQLExpression {
|
||||
return expression
|
||||
}
|
||||
|
||||
func qualified(by qualifier: SQLTableQualifier) -> SQLExpressionNot {
|
||||
return SQLExpressionNot(expression.qualified(by: qualifier))
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@
|
||||
/// // SELECT *, rowid FROM players
|
||||
/// let request = Player.all()
|
||||
public struct AllColumns {
|
||||
private var qualifier: SQLTableQualifier?
|
||||
|
||||
///
|
||||
public init() { }
|
||||
}
|
||||
@@ -24,6 +26,9 @@ extension AllColumns : SQLSelectable {
|
||||
///
|
||||
/// :nodoc:
|
||||
public func resultColumnSQL(_ arguments: inout StatementArguments?) -> String {
|
||||
if let qualifierName = qualifier?.name {
|
||||
return qualifierName.quotedDatabaseIdentifier + ".*"
|
||||
}
|
||||
return "*"
|
||||
}
|
||||
|
||||
@@ -31,6 +36,10 @@ extension AllColumns : SQLSelectable {
|
||||
///
|
||||
/// :nodoc:
|
||||
public func countedSQL(_ arguments: inout StatementArguments?) -> String {
|
||||
guard qualifier == nil else {
|
||||
// SELECT COUNT(t.*) is invalid SQL
|
||||
fatalError("Not implemented, or invalid query")
|
||||
}
|
||||
return "*"
|
||||
}
|
||||
|
||||
@@ -43,11 +52,28 @@ extension AllColumns : SQLSelectable {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard qualifier == nil else {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SELECT * FROM tableName ...
|
||||
// ->
|
||||
// SELECT COUNT(*) FROM tableName ...
|
||||
return .all
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// :nodoc:
|
||||
public func qualified(by qualifier: SQLTableQualifier) -> AllColumns {
|
||||
if self.qualifier != nil {
|
||||
// Never requalify
|
||||
return self
|
||||
}
|
||||
var allColumns = AllColumns()
|
||||
allColumns.qualifier = qualifier
|
||||
return allColumns
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,4 +99,8 @@ struct SQLAliasedExpression : SQLSelectable {
|
||||
func count(distinct: Bool) -> SQLCount? {
|
||||
return expression.count(distinct: distinct)
|
||||
}
|
||||
|
||||
func qualified(by qualifier: SQLTableQualifier) -> SQLAliasedExpression {
|
||||
return SQLAliasedExpression(expression.qualified(by: qualifier), alias: alias)
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,26 @@ public protocol SQLSelectable {
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
func count(distinct: Bool) -> SQLCount?
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
func qualified(by qualifier: SQLTableQualifier) -> Self
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// :nodoc:
|
||||
public class SQLTableQualifier {
|
||||
private var tableName: String
|
||||
private var alias: String?
|
||||
|
||||
init(tableName: String, alias: String?) {
|
||||
self.tableName = tableName
|
||||
self.alias = alias
|
||||
}
|
||||
|
||||
var name: String? {
|
||||
return alias
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Counting
|
||||
|
||||
@@ -58,7 +58,7 @@ extension TableMapping {
|
||||
|
||||
extension TableMapping {
|
||||
|
||||
// MARK: Counting All
|
||||
// MARK: - Counting All
|
||||
|
||||
/// The number of records.
|
||||
///
|
||||
@@ -68,3 +68,30 @@ extension TableMapping {
|
||||
}
|
||||
}
|
||||
|
||||
extension TableMapping {
|
||||
|
||||
// MARK: - SQL Generation
|
||||
|
||||
/// The selection as an SQL String.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// struct Player: TableMapping {
|
||||
/// static let databaseTableName = "players"
|
||||
/// }
|
||||
///
|
||||
/// // SELECT "players".* FROM players
|
||||
/// let sql = "SELECT \(Player.selectionSQL()) FROM players"
|
||||
///
|
||||
/// // SELECT "p".* FROM players AS p
|
||||
/// 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) }
|
||||
var arguments: StatementArguments? = nil
|
||||
return selection
|
||||
.map { $0.resultColumnSQL(&arguments) }
|
||||
.joined(separator: ", ")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +187,10 @@
|
||||
564448851EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448881EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564448891EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564E73EF203DA2A2000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73EB203DA29B000C443C /* JoinSupportTests.swift */; };
|
||||
564E73F0203DA2A3000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73EB203DA29B000C443C /* JoinSupportTests.swift */; };
|
||||
564E73F1203DA2A3000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73EB203DA29B000C443C /* JoinSupportTests.swift */; };
|
||||
564E73F2203DA2A4000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73EB203DA29B000C443C /* JoinSupportTests.swift */; };
|
||||
564F9C1F1F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
564F9C201F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
564F9C231F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
@@ -862,6 +866,7 @@
|
||||
5636E9BB1D22574100B9B05F /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterNextTransactionCommitTests.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
|
||||
564E73EB203DA29B000C443C /* JoinSupportTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinSupportTests.swift; sourceTree = "<group>"; };
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseFunction.swift; sourceTree = "<group>"; };
|
||||
5657AAB81D107001006283EF /* NSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSData.swift; sourceTree = "<group>"; };
|
||||
@@ -1502,6 +1507,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56B15D0A1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift */,
|
||||
564E73EB203DA29B000C443C /* JoinSupportTests.swift */,
|
||||
560B3FA41C19DFF800C58EC7 /* Persistable */,
|
||||
56176C9E1EACEDF9000F3F2B /* Record */,
|
||||
5680521C2013296800E87861 /* RecordBoxTests.swift */,
|
||||
@@ -2119,6 +2125,7 @@
|
||||
5698ACB71DA6285E0056AF8C /* FTS3TokenizerTests.swift in Sources */,
|
||||
5672DE5A1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */,
|
||||
56176C601EACCCC7000F3F2B /* FTS5PatternTests.swift in Sources */,
|
||||
564E73EF203DA2A2000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A7111F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
5657AB471D108BA9006283EF /* FoundationNSNullTests.swift in Sources */,
|
||||
560FC5811CB00B880014AA8E /* RecordCopyTests.swift in Sources */,
|
||||
@@ -2263,6 +2270,7 @@
|
||||
5698ACB81DA6285E0056AF8C /* FTS3TokenizerTests.swift in Sources */,
|
||||
567156401CB16729007DC145 /* RawRepresentable+DatabaseValueConvertibleTests.swift in Sources */,
|
||||
56176C661EACCCC8000F3F2B /* FTS5PatternTests.swift in Sources */,
|
||||
564E73F0203DA2A3000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A7131F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
567156411CB16729007DC145 /* PersistableTests.swift in Sources */,
|
||||
5657AB501D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */,
|
||||
@@ -2504,6 +2512,7 @@
|
||||
5657AB531D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */,
|
||||
5698AC9B1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */,
|
||||
56176C721EACCCCA000F3F2B /* FTS5PatternTests.swift in Sources */,
|
||||
564E73F1203DA2A3000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A7101F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
56AFCA551CB1AA9900F48B96 /* RawRepresentable+DatabaseValueConvertibleTests.swift in Sources */,
|
||||
5698ACBB1DA6285E0056AF8C /* FTS3TokenizerTests.swift in Sources */,
|
||||
@@ -2648,6 +2657,7 @@
|
||||
56FF45461D2C23BA00F21EF9 /* MutablePersistableDeleteTests.swift in Sources */,
|
||||
5698AC9C1DA4B0430056AF8C /* FTS4RecordTests.swift in Sources */,
|
||||
56176C781EACCCCB000F3F2B /* FTS5PatternTests.swift in Sources */,
|
||||
564E73F2203DA2A4000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A70D1F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
56AFCAAC1CB1ABC800F48B96 /* DatabaseValueConversionTests.swift in Sources */,
|
||||
5698ACBC1DA6285E0056AF8C /* FTS3TokenizerTests.swift in Sources */,
|
||||
|
||||
@@ -45,6 +45,8 @@
|
||||
5636E9C11D22574100B9B05F /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5636E9BB1D22574100B9B05F /* Request.swift */; };
|
||||
564448861EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
5644488A1EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */; };
|
||||
564E73F3203DA2AC000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73E7203DA278000C443C /* JoinSupportTests.swift */; };
|
||||
564E73F4203DA2AD000C443C /* JoinSupportTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564E73E7203DA278000C443C /* JoinSupportTests.swift */; };
|
||||
564F9C211F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
564F9C251F069B4E00877A00 /* DatabaseAggregateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */; };
|
||||
564F9C301F07611500877A00 /* DatabaseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */; };
|
||||
@@ -569,6 +571,7 @@
|
||||
5636E9BB1D22574100B9B05F /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
|
||||
564448821EF56B1B00DD2861 /* DatabaseAfterNextTransactionCommitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAfterNextTransactionCommitTests.swift; sourceTree = "<group>"; };
|
||||
564A50C61BFF4B7F00B3A3A2 /* DatabaseCollationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseCollationTests.swift; sourceTree = "<group>"; };
|
||||
564E73E7203DA278000C443C /* JoinSupportTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoinSupportTests.swift; sourceTree = "<group>"; };
|
||||
564F9C1D1F069B4E00877A00 /* DatabaseAggregateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseAggregateTests.swift; sourceTree = "<group>"; };
|
||||
564F9C2C1F075DD200877A00 /* DatabaseFunction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseFunction.swift; sourceTree = "<group>"; };
|
||||
5657AAB81D107001006283EF /* NSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSData.swift; sourceTree = "<group>"; };
|
||||
@@ -1182,6 +1185,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
56B15D0A1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift */,
|
||||
564E73E7203DA278000C443C /* JoinSupportTests.swift */,
|
||||
560B3FA41C19DFF800C58EC7 /* Persistable */,
|
||||
56176C9E1EACEDF9000F3F2B /* Record */,
|
||||
568052182013295100E87861 /* RecordBoxTests.swift */,
|
||||
@@ -1828,6 +1832,7 @@
|
||||
F3BA80DC1CFB300E003DC1BA /* DatabaseValueConvertibleFetchTests.swift in Sources */,
|
||||
5698AD021DAA8ACB0056AF8C /* FTS5CustomTokenizerTests.swift in Sources */,
|
||||
56B964CA1DA521450002DA19 /* FTS5PatternTests.swift in Sources */,
|
||||
564E73F4203DA2AD000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A70E1F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
5657AB551D108BA9006283EF /* FoundationNSNumberTests.swift in Sources */,
|
||||
F3BA81291CFB3063003DC1BA /* RecordInitializersTests.swift in Sources */,
|
||||
@@ -2069,6 +2074,7 @@
|
||||
5698ACD11DA8C2620056AF8C /* RecordPrimaryKeyHiddenRowIDTests.swift in Sources */,
|
||||
562205FB1E420E49005860AC /* DatabasePoolSchemaCacheTests.swift in Sources */,
|
||||
5657AB611D108BA9006283EF /* FoundationNSURLTests.swift in Sources */,
|
||||
564E73F3203DA2AC000C443C /* JoinSupportTests.swift in Sources */,
|
||||
5674A70C1F3087710095F066 /* DatabaseValueConvertibleEncodableTests.swift in Sources */,
|
||||
5698AC8C1DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */,
|
||||
F3BA81391CFB3064003DC1BA /* RecordWithColumnNameManglingTests.swift in Sources */,
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
import XCTest
|
||||
#if GRDBCIPHER
|
||||
import GRDBCipher
|
||||
#elseif GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
class JoinSupportTests: GRDBTestCase {
|
||||
|
||||
func testExample() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
|
||||
// Schema
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(table: "t1") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.create(table: "t2") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("t1id", .integer).notNull().references("t1", onDelete: .cascade)
|
||||
t.column("name", .text).notNull()
|
||||
t.uniqueKey(["t1id", "name"])
|
||||
}
|
||||
try db.create(table: "t3") { t in
|
||||
t.column("t1id", .integer).primaryKey().references("t1", onDelete: .cascade)
|
||||
t.column("name", .text).notNull()
|
||||
t.column("ignored", .integer)
|
||||
}
|
||||
try db.create(table: "t4") { t in
|
||||
t.column("t1id", .integer).primaryKey().references("t1", onDelete: .cascade)
|
||||
t.column("name", .text).notNull()
|
||||
}
|
||||
try db.create(table: "t5") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("t3id", .integer).references("t3", onDelete: .cascade)
|
||||
t.column("t4id", .integer).references("t4", onDelete: .cascade)
|
||||
t.column("name", .text).notNull()
|
||||
t.check(sql: "(t3id IS NOT NULL) + (t4id IS NOT NULL) = 1")
|
||||
}
|
||||
|
||||
// Sample data
|
||||
|
||||
try db.execute("""
|
||||
INSERT INTO t1 (id, name) VALUES (1, 'A1');
|
||||
INSERT INTO t1 (id, name) VALUES (2, 'A2');
|
||||
INSERT INTO t2 (id, t1id, name) VALUES (1, 1, 'left');
|
||||
INSERT INTO t2 (id, t1id, name) VALUES (2, 1, 'right');
|
||||
INSERT INTO t2 (id, t1id, name) VALUES (3, 2, 'left');
|
||||
INSERT INTO t3 (t1id, name) VALUES (1, 'A3');
|
||||
INSERT INTO t4 (t1id, name) VALUES (1, 'A4');
|
||||
INSERT INTO t4 (t1id, name) VALUES (2, 'B4');
|
||||
INSERT INTO t5 (id, t3id, t4id, name) VALUES (1, 1, NULL, 'A5');
|
||||
INSERT INTO t5 (id, t3id, t4id, name) VALUES (2, 1, NULL, 'B5');
|
||||
INSERT INTO t5 (id, t3id, t4id, name) VALUES (3, NULL, 1, 'C5');
|
||||
INSERT INTO t5 (id, t3id, t4id, name) VALUES (4, NULL, 1, 'D5');
|
||||
INSERT INTO t5 (id, t3id, t4id, name) VALUES (5, NULL, 1, 'E5');
|
||||
INSERT INTO t5 (id, t3id, t4id, name) VALUES (6, NULL, 2, 'F5');
|
||||
INSERT INTO t5 (id, t3id, t4id, name) VALUES (7, NULL, 2, 'G5');
|
||||
""")
|
||||
|
||||
// Check sample data with SQL
|
||||
|
||||
do {
|
||||
let sql = """
|
||||
SELECT
|
||||
t1.*,
|
||||
t2Left.*,
|
||||
t2Right.*,
|
||||
t3.t1id, t3.name,
|
||||
COUNT(DISTINCT t5.id) AS t5count
|
||||
FROM t1
|
||||
LEFT JOIN t2 t2Left ON t2Left.t1id = t1.id AND t2Left.name = 'left'
|
||||
LEFT JOIN t2 t2Right ON t2Right.t1id = t1.id AND t2Right.name = 'right'
|
||||
LEFT JOIN t3 ON t3.t1id = t1.id
|
||||
LEFT JOIN t4 ON t4.t1id = t1.id
|
||||
LEFT JOIN t5 ON t5.t3id = t3.t1id OR t5.t4id = t4.t1id
|
||||
GROUP BY t1.id
|
||||
ORDER BY t1.id
|
||||
"""
|
||||
let rows = try Row.fetchAll(db, sql)
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0], [
|
||||
// t1.*
|
||||
"id": 1, "name": "A1",
|
||||
// t2Left.*
|
||||
"id": 1, "t1id": 1, "name": "left",
|
||||
// t2Right.*
|
||||
"id": 2, "t1id": 1, "name": "right",
|
||||
// t3.*
|
||||
"t1id": 1, "name": "A3",
|
||||
// t5count
|
||||
"t5count": 5])
|
||||
XCTAssertEqual(rows[1], [
|
||||
// t1.*
|
||||
"id": 2, "name": "A2",
|
||||
// t2Left.*
|
||||
"id": 3, "t1id": 2, "name": "left",
|
||||
// t2Right.*
|
||||
"id": nil, "t1id": nil, "name": nil,
|
||||
// t3.*
|
||||
"t1id": nil, "name": nil,
|
||||
// t5count
|
||||
"t5count": 2])
|
||||
}
|
||||
|
||||
// Records
|
||||
|
||||
struct T1: Codable, RowConvertible, TableMapping {
|
||||
static let databaseTableName = "t1"
|
||||
var id: Int64
|
||||
var name: String
|
||||
}
|
||||
struct T2: Codable, RowConvertible, TableMapping {
|
||||
static let databaseTableName = "t2"
|
||||
var id: Int64
|
||||
var t1id: Int64
|
||||
var name: String
|
||||
}
|
||||
struct T3: Codable, RowConvertible, TableMapping {
|
||||
static let databaseTableName = "t3"
|
||||
static let databaseSelection: [SQLSelectable] = [Column("t1id"), Column("name")]
|
||||
var t1id: Int64
|
||||
var name: String
|
||||
}
|
||||
struct T4: Codable, RowConvertible, TableMapping {
|
||||
static let databaseTableName = "t4"
|
||||
var t1id: Int64
|
||||
var name: String
|
||||
}
|
||||
struct T5: Codable, RowConvertible, TableMapping {
|
||||
static let databaseTableName = "t5"
|
||||
var id: Int64
|
||||
var t3id: Int64?
|
||||
var t4id: Int64?
|
||||
var name: String
|
||||
}
|
||||
|
||||
// Generated SQL
|
||||
|
||||
let sql = """
|
||||
SELECT
|
||||
\(T1.selectionSQL()),
|
||||
\(T2.selectionSQL(alias: "t2Left"))
|
||||
\(T2.selectionSQL(alias: "t2Right"))
|
||||
\(T3.selectionSQL()),
|
||||
COUNT(DISTINCT t5.id) AS t5count
|
||||
FROM t1
|
||||
LEFT JOIN t2 t2Left ON t2Left.t1id = t1.id AND t2Left.name = 'left'
|
||||
LEFT JOIN t2 t2Right ON t2Right.t1id = t1.id AND t2Right.name = 'right'
|
||||
LEFT JOIN t3 ON t3.t1id = t1.id
|
||||
LEFT JOIN t4 ON t4.t1id = t1.id
|
||||
LEFT JOIN t5 ON t5.t3id = t3.t1id OR t5.t4id = t4.t1id
|
||||
GROUP BY t1.id
|
||||
ORDER BY t1.id
|
||||
"""
|
||||
let expectedSQL = """
|
||||
SELECT
|
||||
"t1".*,
|
||||
"t2Left".*
|
||||
"t2Right".*
|
||||
"t3"."t1id", "t3"."name",
|
||||
COUNT(DISTINCT t5.id) AS t5count
|
||||
FROM t1
|
||||
LEFT JOIN t2 t2Left ON t2Left.t1id = t1.id AND t2Left.name = 'left'
|
||||
LEFT JOIN t2 t2Right ON t2Right.t1id = t1.id AND t2Right.name = 'right'
|
||||
LEFT JOIN t3 ON t3.t1id = t1.id
|
||||
LEFT JOIN t4 ON t4.t1id = t1.id
|
||||
LEFT JOIN t5 ON t5.t3id = t3.t1id OR t5.t4id = t4.t1id
|
||||
GROUP BY t1.id
|
||||
ORDER BY t1.id
|
||||
"""
|
||||
XCTAssertEqual(sql, expectedSQL)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user