mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Extract TableAlias
This commit is contained in:
1 parent
430141344d
commit
55edcff97f
4 files changed
+314
-309
No files matched your search
@@ -68,6 +68,7 @@
|
||||
562206071E420EA4005860AC /* DatabasePoolConcurrencyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560A37AA1C90085D00949E71 /* DatabasePoolConcurrencyTests.swift */; };
|
||||
562206081E420EA4005860AC /* DatabasePoolReadOnlyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EA869D1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift */; };
|
||||
5622060C1E420EB3005860AC /* DatabaseQueueConcurrencyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363BC1C93FD5E000BE133 /* DatabaseQueueConcurrencyTests.swift */; };
|
||||
56231A442DCF3A19007676E4 /* TableAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56231A432DCF3A19007676E4 /* TableAlias.swift */; };
|
||||
562393181DECC02000A6B01F /* RowFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393171DECC02000A6B01F /* RowFetchTests.swift */; };
|
||||
562393301DEDFC5700A6B01F /* AnyCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623932F1DEDFC5700A6B01F /* AnyCursorTests.swift */; };
|
||||
5623934E1DEDFEFB00A6B01F /* EnumeratedCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623934D1DEDFEFB00A6B01F /* EnumeratedCursorTests.swift */; };
|
||||
@@ -487,6 +488,7 @@
|
||||
561F38D72AC88A550051EEE9 /* JSONColumnTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONColumnTests.swift; sourceTree = "<group>"; };
|
||||
561F38EE2AC9CE130051EEE9 /* DatabaseDataEncodingStrategyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDataEncodingStrategyTests.swift; sourceTree = "<group>"; };
|
||||
561F38F32AC9CE510051EEE9 /* DatabaseDataDecodingStrategyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDataDecodingStrategyTests.swift; sourceTree = "<group>"; };
|
||||
56231A432DCF3A19007676E4 /* TableAlias.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableAlias.swift; sourceTree = "<group>"; };
|
||||
562393171DECC02000A6B01F /* RowFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowFetchTests.swift; sourceTree = "<group>"; };
|
||||
5623932F1DEDFC5700A6B01F /* AnyCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyCursorTests.swift; sourceTree = "<group>"; };
|
||||
5623934D1DEDFEFB00A6B01F /* EnumeratedCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EnumeratedCursorTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1284,6 +1286,7 @@
|
||||
568D13182207213E00674B58 /* SQLQueryGenerator.swift */,
|
||||
56F89E142A585C0B002FE2AA /* SQLTableAlterationGenerator.swift */,
|
||||
56F89E0B2A57EC16002FE2AA /* SQLTableGenerator.swift */,
|
||||
56231A432DCF3A19007676E4 /* TableAlias.swift */,
|
||||
);
|
||||
path = SQLGeneration;
|
||||
sourceTree = "<group>";
|
||||
@@ -2168,6 +2171,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
568ECA9F25D7E53E00B71526 /* SQLOrdering.swift in Sources */,
|
||||
56231A442DCF3A19007676E4 /* TableAlias.swift in Sources */,
|
||||
56713FDD2691F409006153C3 /* JSONRequiredEncoder.swift in Sources */,
|
||||
5685C1932AD52EE600DA4B7A /* ListDumpFormat.swift in Sources */,
|
||||
56158780288D875E00A67323 /* Optional.swift in Sources */,
|
||||
|
||||
@@ -244,315 +244,6 @@ class StatementArgumentsSink {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - TableAlias
|
||||
|
||||
/// A TableAlias identifies a table in a request.
|
||||
///
|
||||
/// See ``TableRequest/aliased(_:)`` for more information and examples.
|
||||
///
|
||||
/// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features)
|
||||
public class TableAlias: @unchecked Sendable {
|
||||
// This Sendable conformance is transient. TableAlias IS NOT really Sendable.
|
||||
// TODO: GRDB7 Make TableAlias really Sendable
|
||||
private enum Impl {
|
||||
/// A TableAlias is undefined when it is created by the GRDB user:
|
||||
///
|
||||
/// let alias = TableAlias()
|
||||
/// let alias = TableAlias(name: "custom")
|
||||
case undefined(userName: String?)
|
||||
|
||||
/// A TableAlias is a table when explicitly specified:
|
||||
///
|
||||
/// let alias = TableAlias(tableName: "player")
|
||||
///
|
||||
/// Or when it qualifies a request that wasn't qualified yet (in which
|
||||
/// case it turns from undefined to a table):
|
||||
///
|
||||
/// // SELECT custom.* FROM player custom
|
||||
/// let alias = TableAlias(name: "custom")
|
||||
/// let request = Player.all().aliased(alias)
|
||||
case table(tableName: String, userName: String?)
|
||||
|
||||
/// A TableAlias can be a proxy for another table alias. Two different
|
||||
/// instances for the same table identifier:
|
||||
///
|
||||
/// // Pointless example: make alias2 a proxy for alias1
|
||||
/// let alias1 = TableAlias()
|
||||
/// let alias2 = TableAlias()
|
||||
/// Player.all()
|
||||
/// .aliased(alias1)
|
||||
/// .aliased(alias2)
|
||||
///
|
||||
/// Proxies are useful because queries get implicit aliases as soon
|
||||
/// as they are joined with associations. In the example below,
|
||||
/// customAlias becomes a proxy for the request's implicit alias, which
|
||||
/// gets a custom name. This allows implicit and user aliases to merge
|
||||
/// into a single "table identifier" that matches the user's expectations:
|
||||
///
|
||||
/// // SELECT custom.*, team.*
|
||||
/// // FROM player custom
|
||||
/// // JOIN team ON taem.id = custom.teamId
|
||||
/// // WHERE custom.name = 'Arthur'
|
||||
/// let customAlias = TableAlias(name: "custom")
|
||||
/// let request = Player
|
||||
/// .including(required: Player.team)
|
||||
/// .filter(sql: "custom.name = 'Arthur'")
|
||||
/// .aliased(customAlias)
|
||||
case proxy(TableAlias)
|
||||
}
|
||||
|
||||
private var impl: Impl
|
||||
|
||||
/// Resolve all proxies
|
||||
private var root: TableAlias {
|
||||
if case .proxy(let base) = impl {
|
||||
return base.root
|
||||
} else {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
// exposed to SQLGenerationContext
|
||||
fileprivate var identityName: String {
|
||||
userName ?? tableName
|
||||
}
|
||||
|
||||
// exposed to SQLGenerationContext
|
||||
fileprivate var hasUserName: Bool {
|
||||
userName != nil
|
||||
}
|
||||
|
||||
var tableName: String {
|
||||
switch impl {
|
||||
case .undefined:
|
||||
// Likely a GRDB bug
|
||||
fatalError("Undefined alias has no table name")
|
||||
case .table(tableName: let tableName, userName: _):
|
||||
return tableName
|
||||
case .proxy(let base):
|
||||
return base.tableName
|
||||
}
|
||||
}
|
||||
|
||||
private var userName: String? {
|
||||
switch impl {
|
||||
case .undefined(let userName):
|
||||
return userName
|
||||
case .table(tableName: _, userName: let userName):
|
||||
return userName
|
||||
case .proxy(let base):
|
||||
return base.userName
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a TableAlias.
|
||||
///
|
||||
/// When the alias is given a name, this name is guaranteed to be used as
|
||||
/// the table alias in the SQL query:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT p.* FROM player p
|
||||
/// let alias = TableAlias(name: "p")
|
||||
/// let request = Player.all().aliased(alias)
|
||||
/// ```
|
||||
public init(name: String? = nil) {
|
||||
self.impl = .undefined(userName: name)
|
||||
}
|
||||
|
||||
init(tableName: String, userName: String? = nil) {
|
||||
self.impl = .table(tableName: tableName, userName: userName)
|
||||
}
|
||||
|
||||
func becomeProxy(of base: TableAlias) {
|
||||
if self === base {
|
||||
return
|
||||
}
|
||||
|
||||
switch impl {
|
||||
case let .undefined(userName):
|
||||
if let userName {
|
||||
// rename
|
||||
assert(base.userName == nil || base.userName == userName)
|
||||
base.setUserName(userName)
|
||||
}
|
||||
self.impl = .proxy(base)
|
||||
case let .table(tableName: tableName, userName: userName):
|
||||
assert(tableName == base.tableName)
|
||||
if let userName {
|
||||
// rename
|
||||
assert(base.userName == nil || base.userName == userName)
|
||||
base.setUserName(userName)
|
||||
}
|
||||
self.impl = .proxy(base)
|
||||
case let .proxy(selfBase):
|
||||
selfBase.becomeProxy(of: base)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns nil if aliases can't be merged (conflict in tables, aliases...)
|
||||
func merged(with other: TableAlias) -> TableAlias? {
|
||||
if self === other {
|
||||
return self
|
||||
}
|
||||
|
||||
let root = self.root
|
||||
let otherRoot = other.root
|
||||
switch (root.impl, otherRoot.impl) {
|
||||
case let (.table(tableName: tableName, userName: userName),
|
||||
.table(tableName: otherTableName, userName: otherUserName)):
|
||||
guard tableName == otherTableName else {
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
if let userName, let otherUserName, userName != otherUserName {
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
root.becomeProxy(of: otherRoot)
|
||||
return otherRoot
|
||||
default:
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func setUserName(_ userName: String) {
|
||||
switch impl {
|
||||
case .undefined:
|
||||
self.impl = .undefined(userName: userName)
|
||||
case .table(tableName: let tableName, userName: _):
|
||||
self.impl = .table(tableName: tableName, userName: userName)
|
||||
case .proxy(let base):
|
||||
base.setUserName(userName)
|
||||
}
|
||||
}
|
||||
|
||||
func setTableName(_ tableName: String) {
|
||||
switch impl {
|
||||
case .undefined(let userName):
|
||||
self.impl = .table(tableName: tableName, userName: userName)
|
||||
case .table(tableName: let initialTableName, userName: _):
|
||||
// It is a programmer error to reuse the same TableAlias for
|
||||
// multiple tables.
|
||||
//
|
||||
// // Don't do that
|
||||
// let alias = TableAlias()
|
||||
// let books = Book.aliased(alias)...
|
||||
// let authors = Author.aliased(alias)...
|
||||
GRDBPrecondition(
|
||||
tableName.lowercased() == initialTableName.lowercased(),
|
||||
"A TableAlias most not be used to refer to multiple tables")
|
||||
case .proxy(let base):
|
||||
base.setTableName(tableName)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a result column that refers to the aliased table.
|
||||
public subscript(_ selectable: some SQLSelectable) -> SQLSelection {
|
||||
// TODO: test
|
||||
selectable.sqlSelection.qualified(with: self)
|
||||
}
|
||||
|
||||
/// Returns an SQL expression that refers to the aliased table.
|
||||
///
|
||||
/// For example, let's sort books by author name first, and then by title:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT book.*
|
||||
/// // FROM book
|
||||
/// // JOIN author ON author.id = book.authorId
|
||||
/// // ORDER BY author.name, book.title
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(required: Book.author.aliased(authorAlias))
|
||||
/// .order(authorAlias[Column("name")], Column("title"))
|
||||
/// ```
|
||||
public subscript(_ expression: some SQLSpecificExpressible & SQLSelectable & SQLOrderingTerm) -> SQLExpression {
|
||||
expression.sqlExpression.qualified(with: self)
|
||||
}
|
||||
|
||||
public subscript(_ expression: some SQLJSONExpressible &
|
||||
SQLSpecificExpressible &
|
||||
SQLSelectable &
|
||||
SQLOrderingTerm)
|
||||
-> AnySQLJSONExpressible
|
||||
{
|
||||
AnySQLJSONExpressible(sqlExpression: expression.sqlExpression.qualified(with: self))
|
||||
}
|
||||
|
||||
/// Returns an SQL ordering term that refers to the aliased table.
|
||||
///
|
||||
/// For example, let's sort books by author name first, and then by title:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT book.*
|
||||
/// // FROM book
|
||||
/// // JOIN author ON author.id = book.authorId
|
||||
/// // ORDER BY author.name ASC, book.title ASC
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(required: Book.author.aliased(authorAlias))
|
||||
/// .order(authorAlias[Column("name").asc], Column("title").asc)
|
||||
/// ```
|
||||
public subscript(_ ordering: some SQLOrderingTerm) -> SQLOrdering {
|
||||
ordering.sqlOrdering.qualified(with: self)
|
||||
}
|
||||
|
||||
/// Returns an SQL column that refers to the aliased table.
|
||||
///
|
||||
/// For example, let's sort books by author name first, and then by title:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT book.*
|
||||
/// // FROM book
|
||||
/// // JOIN author ON author.id = book.authorId
|
||||
/// // ORDER BY author.name, book.title
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(required: Book.author.aliased(authorAlias))
|
||||
/// .order(authorAlias["name"], Column("title"))
|
||||
/// ```
|
||||
public subscript(_ column: String) -> SQLExpression {
|
||||
.qualifiedColumn(column, self)
|
||||
}
|
||||
|
||||
/// A boolean SQL expression indicating whether this alias refers to some
|
||||
/// rows, or not.
|
||||
///
|
||||
/// In the example below, we only fetch books that are not associated to
|
||||
/// any author:
|
||||
///
|
||||
/// ```swift
|
||||
/// struct Author: TableRecord, FetchableRecord { }
|
||||
/// struct Book: TableRecord, FetchableRecord {
|
||||
/// static let author = belongsTo(Author.self)
|
||||
/// }
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(optional: Book.author.aliased(authorAlias))
|
||||
/// .filter(!authorAlias.exists)
|
||||
/// let books = try request.fetchAll(db)
|
||||
/// }
|
||||
/// ```
|
||||
public var exists: SQLExpression {
|
||||
SQLExpression.qualifiedExists(self)
|
||||
}
|
||||
}
|
||||
|
||||
extension TableAlias: Equatable {
|
||||
public static func == (lhs: TableAlias, rhs: TableAlias) -> Bool {
|
||||
ObjectIdentifier(lhs.root) == ObjectIdentifier(rhs.root)
|
||||
}
|
||||
}
|
||||
|
||||
extension TableAlias: Hashable {
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(ObjectIdentifier(root))
|
||||
}
|
||||
}
|
||||
|
||||
extension [TableAlias] {
|
||||
/// Resolve ambiguities in aliases' names.
|
||||
fileprivate var resolvedNames: [TableAlias: String] {
|
||||
|
||||
@@ -0,0 +1,306 @@
|
||||
/// A TableAlias identifies a table in a request.
|
||||
///
|
||||
/// See ``TableRequest/aliased(_:)`` for more information and examples.
|
||||
///
|
||||
/// - note: [**🔥 EXPERIMENTAL**](https://github.com/groue/GRDB.swift/blob/master/README.md#what-are-experimental-features)
|
||||
public class TableAlias: @unchecked Sendable {
|
||||
// This Sendable conformance is transient. TableAlias IS NOT really Sendable.
|
||||
// TODO: Make TableAlias really Sendable
|
||||
private enum Impl {
|
||||
/// A TableAlias is undefined when it is created by the GRDB user:
|
||||
///
|
||||
/// let alias = TableAlias()
|
||||
/// let alias = TableAlias(name: "custom")
|
||||
case undefined(userName: String?)
|
||||
|
||||
/// A TableAlias is a table when explicitly specified:
|
||||
///
|
||||
/// let alias = TableAlias(tableName: "player")
|
||||
///
|
||||
/// Or when it qualifies a request that wasn't qualified yet (in which
|
||||
/// case it turns from undefined to a table):
|
||||
///
|
||||
/// // SELECT custom.* FROM player custom
|
||||
/// let alias = TableAlias(name: "custom")
|
||||
/// let request = Player.all().aliased(alias)
|
||||
case table(tableName: String, userName: String?)
|
||||
|
||||
/// A TableAlias can be a proxy for another table alias. Two different
|
||||
/// instances for the same table identifier:
|
||||
///
|
||||
/// // Pointless example: make alias2 a proxy for alias1
|
||||
/// let alias1 = TableAlias()
|
||||
/// let alias2 = TableAlias()
|
||||
/// Player.all()
|
||||
/// .aliased(alias1)
|
||||
/// .aliased(alias2)
|
||||
///
|
||||
/// Proxies are useful because queries get implicit aliases as soon
|
||||
/// as they are joined with associations. In the example below,
|
||||
/// customAlias becomes a proxy for the request's implicit alias, which
|
||||
/// gets a custom name. This allows implicit and user aliases to merge
|
||||
/// into a single "table identifier" that matches the user's expectations:
|
||||
///
|
||||
/// // SELECT custom.*, team.*
|
||||
/// // FROM player custom
|
||||
/// // JOIN team ON taem.id = custom.teamId
|
||||
/// // WHERE custom.name = 'Arthur'
|
||||
/// let customAlias = TableAlias(name: "custom")
|
||||
/// let request = Player
|
||||
/// .including(required: Player.team)
|
||||
/// .filter(sql: "custom.name = 'Arthur'")
|
||||
/// .aliased(customAlias)
|
||||
case proxy(TableAlias)
|
||||
}
|
||||
|
||||
private var impl: Impl
|
||||
|
||||
/// Resolve all proxies
|
||||
private var root: TableAlias {
|
||||
if case .proxy(let base) = impl {
|
||||
return base.root
|
||||
} else {
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
// exposed to SQLGenerationContext
|
||||
var identityName: String {
|
||||
userName ?? tableName
|
||||
}
|
||||
|
||||
// exposed to SQLGenerationContext
|
||||
var hasUserName: Bool {
|
||||
userName != nil
|
||||
}
|
||||
|
||||
var tableName: String {
|
||||
switch impl {
|
||||
case .undefined:
|
||||
// Likely a GRDB bug
|
||||
fatalError("Undefined alias has no table name")
|
||||
case .table(tableName: let tableName, userName: _):
|
||||
return tableName
|
||||
case .proxy(let base):
|
||||
return base.tableName
|
||||
}
|
||||
}
|
||||
|
||||
private var userName: String? {
|
||||
switch impl {
|
||||
case .undefined(let userName):
|
||||
return userName
|
||||
case .table(tableName: _, userName: let userName):
|
||||
return userName
|
||||
case .proxy(let base):
|
||||
return base.userName
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a TableAlias.
|
||||
///
|
||||
/// When the alias is given a name, this name is guaranteed to be used as
|
||||
/// the table alias in the SQL query:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT p.* FROM player p
|
||||
/// let alias = TableAlias(name: "p")
|
||||
/// let request = Player.all().aliased(alias)
|
||||
/// ```
|
||||
public init(name: String? = nil) {
|
||||
self.impl = .undefined(userName: name)
|
||||
}
|
||||
|
||||
init(tableName: String, userName: String? = nil) {
|
||||
self.impl = .table(tableName: tableName, userName: userName)
|
||||
}
|
||||
|
||||
func becomeProxy(of base: TableAlias) {
|
||||
if self === base {
|
||||
return
|
||||
}
|
||||
|
||||
switch impl {
|
||||
case let .undefined(userName):
|
||||
if let userName {
|
||||
// rename
|
||||
assert(base.userName == nil || base.userName == userName)
|
||||
base.setUserName(userName)
|
||||
}
|
||||
self.impl = .proxy(base)
|
||||
case let .table(tableName: tableName, userName: userName):
|
||||
assert(tableName == base.tableName)
|
||||
if let userName {
|
||||
// rename
|
||||
assert(base.userName == nil || base.userName == userName)
|
||||
base.setUserName(userName)
|
||||
}
|
||||
self.impl = .proxy(base)
|
||||
case let .proxy(selfBase):
|
||||
selfBase.becomeProxy(of: base)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns nil if aliases can't be merged (conflict in tables, aliases...)
|
||||
func merged(with other: TableAlias) -> TableAlias? {
|
||||
if self === other {
|
||||
return self
|
||||
}
|
||||
|
||||
let root = self.root
|
||||
let otherRoot = other.root
|
||||
switch (root.impl, otherRoot.impl) {
|
||||
case let (.table(tableName: tableName, userName: userName),
|
||||
.table(tableName: otherTableName, userName: otherUserName)):
|
||||
guard tableName == otherTableName else {
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
if let userName, let otherUserName, userName != otherUserName {
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
root.becomeProxy(of: otherRoot)
|
||||
return otherRoot
|
||||
default:
|
||||
// can't merge
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func setUserName(_ userName: String) {
|
||||
switch impl {
|
||||
case .undefined:
|
||||
self.impl = .undefined(userName: userName)
|
||||
case .table(tableName: let tableName, userName: _):
|
||||
self.impl = .table(tableName: tableName, userName: userName)
|
||||
case .proxy(let base):
|
||||
base.setUserName(userName)
|
||||
}
|
||||
}
|
||||
|
||||
func setTableName(_ tableName: String) {
|
||||
switch impl {
|
||||
case .undefined(let userName):
|
||||
self.impl = .table(tableName: tableName, userName: userName)
|
||||
case .table(tableName: let initialTableName, userName: _):
|
||||
// It is a programmer error to reuse the same TableAlias for
|
||||
// multiple tables.
|
||||
//
|
||||
// // Don't do that
|
||||
// let alias = TableAlias()
|
||||
// let books = Book.aliased(alias)...
|
||||
// let authors = Author.aliased(alias)...
|
||||
GRDBPrecondition(
|
||||
tableName.lowercased() == initialTableName.lowercased(),
|
||||
"A TableAlias most not be used to refer to multiple tables")
|
||||
case .proxy(let base):
|
||||
base.setTableName(tableName)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a result column that refers to the aliased table.
|
||||
public subscript(_ selectable: some SQLSelectable) -> SQLSelection {
|
||||
// TODO: test
|
||||
selectable.sqlSelection.qualified(with: self)
|
||||
}
|
||||
|
||||
/// Returns an SQL expression that refers to the aliased table.
|
||||
///
|
||||
/// For example, let's sort books by author name first, and then by title:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT book.*
|
||||
/// // FROM book
|
||||
/// // JOIN author ON author.id = book.authorId
|
||||
/// // ORDER BY author.name, book.title
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(required: Book.author.aliased(authorAlias))
|
||||
/// .order(authorAlias[Column("name")], Column("title"))
|
||||
/// ```
|
||||
public subscript(_ expression: some SQLSpecificExpressible & SQLSelectable & SQLOrderingTerm) -> SQLExpression {
|
||||
expression.sqlExpression.qualified(with: self)
|
||||
}
|
||||
|
||||
public subscript(_ expression: some SQLJSONExpressible &
|
||||
SQLSpecificExpressible &
|
||||
SQLSelectable &
|
||||
SQLOrderingTerm)
|
||||
-> AnySQLJSONExpressible
|
||||
{
|
||||
AnySQLJSONExpressible(sqlExpression: expression.sqlExpression.qualified(with: self))
|
||||
}
|
||||
|
||||
/// Returns an SQL ordering term that refers to the aliased table.
|
||||
///
|
||||
/// For example, let's sort books by author name first, and then by title:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT book.*
|
||||
/// // FROM book
|
||||
/// // JOIN author ON author.id = book.authorId
|
||||
/// // ORDER BY author.name ASC, book.title ASC
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(required: Book.author.aliased(authorAlias))
|
||||
/// .order(authorAlias[Column("name").asc], Column("title").asc)
|
||||
/// ```
|
||||
public subscript(_ ordering: some SQLOrderingTerm) -> SQLOrdering {
|
||||
ordering.sqlOrdering.qualified(with: self)
|
||||
}
|
||||
|
||||
/// Returns an SQL column that refers to the aliased table.
|
||||
///
|
||||
/// For example, let's sort books by author name first, and then by title:
|
||||
///
|
||||
/// ```swift
|
||||
/// // SELECT book.*
|
||||
/// // FROM book
|
||||
/// // JOIN author ON author.id = book.authorId
|
||||
/// // ORDER BY author.name, book.title
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(required: Book.author.aliased(authorAlias))
|
||||
/// .order(authorAlias["name"], Column("title"))
|
||||
/// ```
|
||||
public subscript(_ column: String) -> SQLExpression {
|
||||
.qualifiedColumn(column, self)
|
||||
}
|
||||
|
||||
/// A boolean SQL expression indicating whether this alias refers to some
|
||||
/// rows, or not.
|
||||
///
|
||||
/// In the example below, we only fetch books that are not associated to
|
||||
/// any author:
|
||||
///
|
||||
/// ```swift
|
||||
/// struct Author: TableRecord, FetchableRecord { }
|
||||
/// struct Book: TableRecord, FetchableRecord {
|
||||
/// static let author = belongsTo(Author.self)
|
||||
/// }
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// let authorAlias = TableAlias()
|
||||
/// let request = Book
|
||||
/// .joining(optional: Book.author.aliased(authorAlias))
|
||||
/// .filter(!authorAlias.exists)
|
||||
/// let books = try request.fetchAll(db)
|
||||
/// }
|
||||
/// ```
|
||||
public var exists: SQLExpression {
|
||||
SQLExpression.qualifiedExists(self)
|
||||
}
|
||||
}
|
||||
|
||||
extension TableAlias: Equatable {
|
||||
public static func == (lhs: TableAlias, rhs: TableAlias) -> Bool {
|
||||
ObjectIdentifier(lhs.root) == ObjectIdentifier(rhs.root)
|
||||
}
|
||||
}
|
||||
|
||||
extension TableAlias: Hashable {
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(ObjectIdentifier(root))
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@
|
||||
562205FE1E420EA2005860AC /* DatabasePoolConcurrencyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 560A37AA1C90085D00949E71 /* DatabasePoolConcurrencyTests.swift */; };
|
||||
562205FF1E420EA2005860AC /* DatabasePoolReadOnlyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56EA869D1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift */; };
|
||||
562206091E420EB2005860AC /* DatabaseQueueConcurrencyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363BC1C93FD5E000BE133 /* DatabaseQueueConcurrencyTests.swift */; };
|
||||
56231A462DCF3A28007676E4 /* TableAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56231A452DCF3A28007676E4 /* TableAlias.swift */; };
|
||||
56231E6325CEBF08001DFD2F /* RowDecodingError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56231E6025CEBF06001DFD2F /* RowDecodingError.swift */; };
|
||||
5623931B1DECC02000A6B01F /* RowFetchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393171DECC02000A6B01F /* RowFetchTests.swift */; };
|
||||
562393331DEDFC5700A6B01F /* AnyCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5623932F1DEDFC5700A6B01F /* AnyCursorTests.swift */; };
|
||||
@@ -500,6 +501,7 @@
|
||||
561F38DA2AC8914D0051EEE9 /* JSONColumnTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONColumnTests.swift; sourceTree = "<group>"; };
|
||||
561F38F02AC9CE220051EEE9 /* DatabaseDataEncodingStrategyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDataEncodingStrategyTests.swift; sourceTree = "<group>"; };
|
||||
561F38F52AC9CE5A0051EEE9 /* DatabaseDataDecodingStrategyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseDataDecodingStrategyTests.swift; sourceTree = "<group>"; };
|
||||
56231A452DCF3A28007676E4 /* TableAlias.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableAlias.swift; sourceTree = "<group>"; };
|
||||
56231E6025CEBF06001DFD2F /* RowDecodingError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RowDecodingError.swift; sourceTree = "<group>"; };
|
||||
562393171DECC02000A6B01F /* RowFetchTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowFetchTests.swift; sourceTree = "<group>"; };
|
||||
5623932F1DEDFC5700A6B01F /* AnyCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyCursorTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1275,6 +1277,7 @@
|
||||
5656A82A2295BD56001FF3FF /* SQLQueryGenerator.swift */,
|
||||
56F89E182A585E0D002FE2AA /* SQLTableAlterationGenerator.swift */,
|
||||
56F89E192A585E0D002FE2AA /* SQLTableGenerator.swift */,
|
||||
56231A452DCF3A28007676E4 /* TableAlias.swift */,
|
||||
);
|
||||
path = SQLGeneration;
|
||||
sourceTree = "<group>";
|
||||
@@ -1989,6 +1992,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
5656A8712295BD56001FF3FF /* HasOneThroughAssociation.swift in Sources */,
|
||||
56231A462DCF3A28007676E4 /* TableAlias.swift in Sources */,
|
||||
56713FE32691F487006153C3 /* JSONRequiredEncoder.swift in Sources */,
|
||||
5685C1962AD52EF200DA4B7A /* ListDumpFormat.swift in Sources */,
|
||||
566BD7332927AFD600595649 /* ValueConcurrentObserver.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user