mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Introduce Table
This commit is contained in:
1 parent
220d221ea6
commit
d7971d76e7
5 files changed
+473
No files matched your search
@@ -129,6 +129,10 @@
|
||||
5623936D1DEE0CD200A6B01F /* FlattenCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */; };
|
||||
562393721DEE104400A6B01F /* MapCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393711DEE104400A6B01F /* MapCursorTests.swift */; };
|
||||
562393761DEE104400A6B01F /* MapCursorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 562393711DEE104400A6B01F /* MapCursorTests.swift */; };
|
||||
56256ED025D1ACD0008C2BDD /* Table.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ECF25D1ACD0008C2BDD /* Table.swift */; };
|
||||
56256ED125D1ACD0008C2BDD /* Table.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ECF25D1ACD0008C2BDD /* Table.swift */; };
|
||||
56256ED225D1ACD0008C2BDD /* Table.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ECF25D1ACD0008C2BDD /* Table.swift */; };
|
||||
56256ED325D1ACD0008C2BDD /* Table.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ECF25D1ACD0008C2BDD /* Table.swift */; };
|
||||
56256ED925D1B316008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ED825D1B316008C2BDD /* ForeignKey.swift */; };
|
||||
56256EDA25D1B316008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ED825D1B316008C2BDD /* ForeignKey.swift */; };
|
||||
56256EDB25D1B316008C2BDD /* ForeignKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56256ED825D1B316008C2BDD /* ForeignKey.swift */; };
|
||||
@@ -1290,6 +1294,7 @@
|
||||
562393681DEE0CD200A6B01F /* FlattenCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlattenCursorTests.swift; sourceTree = "<group>"; };
|
||||
562393711DEE104400A6B01F /* MapCursorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapCursorTests.swift; sourceTree = "<group>"; };
|
||||
5623E0901B4AFACC00B20B7F /* GRDBTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTestCase.swift; sourceTree = "<group>"; };
|
||||
56256ECF25D1ACD0008C2BDD /* Table.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Table.swift; sourceTree = "<group>"; };
|
||||
56256ED825D1B316008C2BDD /* ForeignKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForeignKey.swift; sourceTree = "<group>"; };
|
||||
562756421E963AAC0035B653 /* DatabaseWriterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseWriterTests.swift; sourceTree = "<group>"; };
|
||||
562EA8251F17B2AC00FA528C /* CompilationProtocolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompilationProtocolTests.swift; sourceTree = "<group>"; };
|
||||
@@ -2060,6 +2065,7 @@
|
||||
56D91AA12205E03700770D8D /* SQLRelation.swift */,
|
||||
568ECA8925D7013000B71526 /* SQLSelection.swift */,
|
||||
568ECB1725D9161500B71526 /* SQLSubquery.swift */,
|
||||
56256ECF25D1ACD0008C2BDD /* Table.swift */,
|
||||
);
|
||||
path = SQL;
|
||||
sourceTree = "<group>";
|
||||
@@ -2867,6 +2873,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
5616AAF3207CD45E00AC3664 /* RequestProtocols.swift in Sources */,
|
||||
56256ED225D1ACD0008C2BDD /* Table.swift in Sources */,
|
||||
56B964BF1DA51D0A0002DA19 /* FTS5Pattern.swift in Sources */,
|
||||
56D51D061EA789FA0074638A /* FetchableRecord+TableRecord.swift in Sources */,
|
||||
565490CE1D5AE252005622CB /* NSNull.swift in Sources */,
|
||||
@@ -3030,6 +3037,7 @@
|
||||
566475D61D981D5E00FF74B8 /* SQLOperators.swift in Sources */,
|
||||
5698AD381DABAF4A0056AF8C /* FTS5CustomTokenizer.swift in Sources */,
|
||||
56012BA8257404E000B4925B /* CommonTableExpression.swift in Sources */,
|
||||
56256ED125D1ACD0008C2BDD /* Table.swift in Sources */,
|
||||
5653EB0A20944C7C00F46237 /* Association.swift in Sources */,
|
||||
56A2387E1B9C75030082EB20 /* Database.swift in Sources */,
|
||||
56CEB4FD1EAA2F4D00BFAF62 /* FTS3.swift in Sources */,
|
||||
@@ -3621,6 +3629,7 @@
|
||||
AAA4DC7F230F1E0600C74B15 /* SQLOperators.swift in Sources */,
|
||||
AAA4DC80230F1E0600C74B15 /* FTS5CustomTokenizer.swift in Sources */,
|
||||
56012BB2257404E200B4925B /* CommonTableExpression.swift in Sources */,
|
||||
56256ED325D1ACD0008C2BDD /* Table.swift in Sources */,
|
||||
AAA4DC81230F1E0600C74B15 /* Association.swift in Sources */,
|
||||
AAA4DC82230F1E0600C74B15 /* Database.swift in Sources */,
|
||||
AAA4DC83230F1E0600C74B15 /* FTS3.swift in Sources */,
|
||||
@@ -3982,6 +3991,7 @@
|
||||
566B91091FA4C3970012D5B0 /* Database+Statements.swift in Sources */,
|
||||
566B91231FA4CF810012D5B0 /* Database+Schema.swift in Sources */,
|
||||
56012B9F257404DF00B4925B /* CommonTableExpression.swift in Sources */,
|
||||
56256ED025D1ACD0008C2BDD /* Table.swift in Sources */,
|
||||
563B06AB217EF0CC00B38F35 /* ValueObservation.swift in Sources */,
|
||||
56CEB5111EAA324B00BFAF62 /* FTS3+QueryInterface.swift in Sources */,
|
||||
5659F4901EA8D964004A4992 /* ReadWriteBox.swift in Sources */,
|
||||
|
||||
@@ -408,4 +408,38 @@ extension CommonTableExpression {
|
||||
{
|
||||
JoinAssociation(to: Destination.relationForAll, condition: .none)
|
||||
}
|
||||
|
||||
/// Creates an association to a table that you can join
|
||||
/// or include in another request.
|
||||
///
|
||||
/// The key of the returned association is the table name of `Destination`.
|
||||
///
|
||||
/// - parameter cte: A common table expression.
|
||||
/// - parameter condition: A function that returns the joining clause.
|
||||
/// - parameter left: A `TableAlias` for the left table.
|
||||
/// - parameter right: A `TableAlias` for the right table.
|
||||
/// - returns: An association to the common table expression.
|
||||
public func association<Destination>(
|
||||
to destination: Table<Destination>,
|
||||
on condition: @escaping (_ left: TableAlias, _ right: TableAlias) -> SQLExpressible)
|
||||
-> JoinAssociation<RowDecoder, Destination>
|
||||
{
|
||||
JoinAssociation(
|
||||
to: destination.relationForAll,
|
||||
condition: .expression { condition($0, $1).sqlExpression })
|
||||
}
|
||||
|
||||
/// Creates an association to a table that you can join
|
||||
/// or include in another request.
|
||||
///
|
||||
/// The key of the returned association is the table name of `Destination`.
|
||||
///
|
||||
/// - parameter cte: A common table expression.
|
||||
/// - returns: An association to the common table expression.
|
||||
public func association<Destination>(
|
||||
to destination: Table<Destination>)
|
||||
-> JoinAssociation<RowDecoder, Destination>
|
||||
{
|
||||
JoinAssociation(to: destination.relationForAll, condition: .none)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
/// `Table` can build query interface requests.
|
||||
///
|
||||
/// // SELECT * FROM player WHERE score >= 1000
|
||||
/// let table = Table("player")
|
||||
/// let rows: [Row] = try dbQueue.read { db in
|
||||
/// table.all()
|
||||
/// .filter(Column("score") >= 1000)
|
||||
/// .fetchAll(db)
|
||||
/// }
|
||||
public struct Table<RowDecoder> {
|
||||
/// The table name
|
||||
public var tableName: String
|
||||
|
||||
private init(_ tableName: String, _ type: RowDecoder.Type) {
|
||||
self.tableName = tableName
|
||||
}
|
||||
|
||||
/// Create a `Table`
|
||||
///
|
||||
/// let table = Table<Row>("player")
|
||||
/// let table = Table<Player>("player")
|
||||
public init(_ tableName: String) {
|
||||
self.init(tableName, RowDecoder.self)
|
||||
}
|
||||
}
|
||||
|
||||
extension Table where RowDecoder == Row {
|
||||
/// Create a `Table` of `Row`.
|
||||
///
|
||||
/// let table = Table("player") // Table<Row>
|
||||
public init(_ tableName: String) {
|
||||
self.init(tableName, Row.self)
|
||||
}
|
||||
}
|
||||
|
||||
extension Table {
|
||||
var relationForAll: SQLRelation {
|
||||
.all(fromTable: tableName)
|
||||
}
|
||||
|
||||
/// Creates a request for all rows of the table.
|
||||
///
|
||||
/// You can, for example, fetch from this request:
|
||||
///
|
||||
/// let table = Table<Row>("player")
|
||||
/// let rows: [Row] = try table.all().fetchAll(db)
|
||||
public func all() -> QueryInterfaceRequest<RowDecoder> {
|
||||
QueryInterfaceRequest(relation: relationForAll)
|
||||
}
|
||||
}
|
||||
|
||||
extension Table {
|
||||
/// Returns a request which embeds the common table expression.
|
||||
///
|
||||
/// For more information, see `TableRecord.with(_:)`.
|
||||
///
|
||||
/// - parameter cte: A common table expression.
|
||||
/// - returns: A request.
|
||||
public func with<CTERowDecoder>(_ cte: CommonTableExpression<CTERowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
|
||||
all().with(cte)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Associations to TableRecord
|
||||
|
||||
extension Table {
|
||||
/// Creates a "Belongs To" association between Self and the destination
|
||||
/// type, based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.belongsTo(_:key:using:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The record type at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.databaseTableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key to the destination table, or when the schema defines several
|
||||
/// foreign keys to the destination table.
|
||||
public func belongsTo<Destination>(
|
||||
_ destination: Destination.Type,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> BelongsToAssociation<RowDecoder, Destination>
|
||||
where Destination: TableRecord
|
||||
{
|
||||
BelongsToAssociation(
|
||||
to: Destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
|
||||
/// Creates a "Has many" association between Self and the destination type,
|
||||
/// based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasMany(_:key:using:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The record type at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.databaseTableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key from the destination table, or when the schema defines several
|
||||
/// foreign keys from the destination table.
|
||||
public func hasMany<Destination>(
|
||||
_ destination: Destination.Type,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasManyAssociation<RowDecoder, Destination>
|
||||
where Destination: TableRecord
|
||||
{
|
||||
HasManyAssociation(
|
||||
to: Destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
|
||||
/// Creates a "Has one" association between Self and the destination type,
|
||||
/// based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasOne(_:key:using:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The record type at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.databaseTableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key from the destination table, or when the schema defines several
|
||||
/// foreign keys from the destination table.
|
||||
public func hasOne<Destination>(
|
||||
_ destination: Destination.Type,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasOneAssociation<Self, Destination>
|
||||
where Destination: TableRecord
|
||||
{
|
||||
HasOneAssociation(
|
||||
to: Destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Associations to Table
|
||||
|
||||
extension Table {
|
||||
/// Creates a "Belongs To" association between Self and the destination
|
||||
/// table, based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.belongsTo(_:key:using:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The table at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.tableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key to the destination table, or when the schema defines several
|
||||
/// foreign keys to the destination table.
|
||||
public func belongsTo<Destination>(
|
||||
_ destination: Table<Destination>,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> BelongsToAssociation<RowDecoder, Destination>
|
||||
{
|
||||
BelongsToAssociation(
|
||||
to: destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
|
||||
/// Creates a "Has many" association between Self and the destination table,
|
||||
/// based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasMany(_:key:using:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The table at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.tableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key from the destination table, or when the schema defines several
|
||||
/// foreign keys from the destination table.
|
||||
public func hasMany<Destination>(
|
||||
_ destination: Table<Destination>,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasManyAssociation<RowDecoder, Destination>
|
||||
{
|
||||
HasManyAssociation(
|
||||
to: destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
|
||||
/// Creates a "Has one" association between Self and the destination table,
|
||||
/// based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasOne(_:key:using:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The table at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.databaseTableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key from the destination table, or when the schema defines several
|
||||
/// foreign keys from the destination table.
|
||||
public func hasOne<Destination>(
|
||||
_ destination: Table<Destination>,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasOneAssociation<RowDecoder, Destination>
|
||||
{
|
||||
HasOneAssociation(
|
||||
to: destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Associations to CommonTableExpression
|
||||
|
||||
extension Table {
|
||||
/// Creates an association to a common table expression that you can join
|
||||
/// or include in another request.
|
||||
///
|
||||
/// For more information, see `TableRecord.association(to:on:)`.
|
||||
///
|
||||
/// - parameter cte: A common table expression.
|
||||
/// - parameter condition: A function that returns the joining clause.
|
||||
/// - parameter left: A `TableAlias` for the left table.
|
||||
/// - parameter right: A `TableAlias` for the right table.
|
||||
/// - returns: An association to the common table expression.
|
||||
public func association<Destination>(
|
||||
to cte: CommonTableExpression<Destination>,
|
||||
on condition: @escaping (_ left: TableAlias, _ right: TableAlias) -> SQLExpressible)
|
||||
-> JoinAssociation<RowDecoder, Destination>
|
||||
{
|
||||
JoinAssociation(
|
||||
to: cte.relationForAll,
|
||||
condition: .expression { condition($0, $1).sqlExpression })
|
||||
}
|
||||
|
||||
/// Creates an association to a common table expression that you can join
|
||||
/// or include in another request.
|
||||
///
|
||||
/// The key of the returned association is the table name of the common
|
||||
/// table expression.
|
||||
///
|
||||
/// - parameter cte: A common table expression.
|
||||
/// - returns: An association to the common table expression.
|
||||
public func association<Destination>(
|
||||
to cte: CommonTableExpression<Destination>)
|
||||
-> JoinAssociation<RowDecoder, Destination>
|
||||
{
|
||||
JoinAssociation(to: cte.relationForAll, condition: .none)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - "Through" Associations
|
||||
|
||||
extension Table {
|
||||
/// Creates a "Has Many Through" association between Self and the
|
||||
/// destination type.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasMany(_:through:using:key:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The record type at the other side of the association.
|
||||
/// - pivot: An association from Self to the intermediate type.
|
||||
/// - target: A target association from the intermediate type to the
|
||||
/// destination type.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is the same key as the target.
|
||||
public func hasMany<Pivot, Target>(
|
||||
_ destination: Target.RowDecoder.Type,
|
||||
through pivot: Pivot,
|
||||
using target: Target,
|
||||
key: String? = nil)
|
||||
-> HasManyThroughAssociation<RowDecoder, Target.RowDecoder>
|
||||
where Pivot: Association,
|
||||
Target: Association,
|
||||
Pivot.OriginRowDecoder == Self,
|
||||
Pivot.RowDecoder == Target.OriginRowDecoder
|
||||
{
|
||||
let association = HasManyThroughAssociation<RowDecoder, Target.RowDecoder>(
|
||||
sqlAssociation: target._sqlAssociation.through(pivot._sqlAssociation))
|
||||
|
||||
if let key = key {
|
||||
return association.forKey(key)
|
||||
} else {
|
||||
return association
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a "Has One Through" association between Self and the
|
||||
/// destination type.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasOne(_:through:using:key:)`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The record type at the other side of the association.
|
||||
/// - pivot: An association from Self to the intermediate type.
|
||||
/// - target: A target association from the intermediate type to the
|
||||
/// destination type.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is the same key as the target.
|
||||
public func hasOne<Pivot, Target>(
|
||||
_ destination: Target.RowDecoder.Type,
|
||||
through pivot: Pivot,
|
||||
using target: Target,
|
||||
key: String? = nil)
|
||||
-> HasOneThroughAssociation<RowDecoder, Target.RowDecoder>
|
||||
where Pivot: AssociationToOne,
|
||||
Target: AssociationToOne,
|
||||
Pivot.OriginRowDecoder == Self,
|
||||
Pivot.RowDecoder == Target.OriginRowDecoder
|
||||
{
|
||||
let association = HasOneThroughAssociation<RowDecoder, Target.RowDecoder>(
|
||||
sqlAssociation: target._sqlAssociation.through(pivot._sqlAssociation))
|
||||
|
||||
if let key = key {
|
||||
return association.forKey(key)
|
||||
} else {
|
||||
return association
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,6 +209,91 @@ extension TableRecord {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Associations to Table
|
||||
|
||||
extension TableRecord {
|
||||
/// Creates a "Belongs To" association between Self and the destination
|
||||
/// table, based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.belongsTo(_:key:using:)` where
|
||||
/// the first argument is `TableRecord`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The table at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.tableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key to the destination table, or when the schema defines several
|
||||
/// foreign keys to the destination table.
|
||||
public static func belongsTo<Destination>(
|
||||
_ destination: Table<Destination>,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> BelongsToAssociation<Self, Destination>
|
||||
{
|
||||
BelongsToAssociation(
|
||||
to: destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
|
||||
/// Creates a "Has many" association between Self and the destination table,
|
||||
/// based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasMany(_:key:using:)` where
|
||||
/// the first argument is `TableRecord`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The table at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.tableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key from the destination table, or when the schema defines several
|
||||
/// foreign keys from the destination table.
|
||||
public static func hasMany<Destination>(
|
||||
_ destination: Table<Destination>,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasManyAssociation<Self, Destination>
|
||||
{
|
||||
HasManyAssociation(
|
||||
to: destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
|
||||
/// Creates a "Has one" association between Self and the destination table,
|
||||
/// based on a database foreign key.
|
||||
///
|
||||
/// For more information, see `TableRecord.hasOne(_:key:using:)` where
|
||||
/// the first argument is `TableRecord`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - destination: The table at the other side of the association.
|
||||
/// - key: An eventual decoding key for the association. By default, it
|
||||
/// is `destination.databaseTableName`.
|
||||
/// - foreignKey: An eventual foreign key. You need to provide an
|
||||
/// explicit foreign key when GRDB can't infer one from the database
|
||||
/// schema. This happens when the schema does not define any foreign
|
||||
/// key from the destination table, or when the schema defines several
|
||||
/// foreign keys from the destination table.
|
||||
public static func hasOne<Destination>(
|
||||
_ destination: Table<Destination>,
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasOneAssociation<Self, Destination>
|
||||
{
|
||||
HasOneAssociation(
|
||||
to: destination.relationForAll,
|
||||
key: key,
|
||||
using: foreignKey)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Associations to CommonTableExpression
|
||||
|
||||
extension TableRecord {
|
||||
|
||||
@@ -362,6 +362,8 @@
|
||||
56894FE8260658A500268F4D /* Decimal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FA0260657F600268F4D /* Decimal.swift */; };
|
||||
56894FF2260658E600268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FF1260658E600268F4D /* FoundationDecimalTests.swift */; };
|
||||
56894FF3260658E600268F4D /* FoundationDecimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894FF1260658E600268F4D /* FoundationDecimalTests.swift */; };
|
||||
56894F332604FC1E00268F4D /* Table.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F322604FC1E00268F4D /* Table.swift */; };
|
||||
56894F342604FC1E00268F4D /* Table.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56894F322604FC1E00268F4D /* Table.swift */; };
|
||||
568ECB0D25D904CA00B71526 /* SQLSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB0C25D904CA00B71526 /* SQLSelection.swift */; };
|
||||
568ECB0E25D904CA00B71526 /* SQLSelection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB0C25D904CA00B71526 /* SQLSelection.swift */; };
|
||||
568ECB3525D91CEF00B71526 /* SQLSubquery.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568ECB3425D91CEE00B71526 /* SQLSubquery.swift */; };
|
||||
@@ -976,6 +978,7 @@
|
||||
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
|
||||
56894FA0260657F600268F4D /* Decimal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Decimal.swift; sourceTree = "<group>"; };
|
||||
56894FF1260658E600268F4D /* FoundationDecimalTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDecimalTests.swift; sourceTree = "<group>"; };
|
||||
56894F322604FC1E00268F4D /* Table.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Table.swift; sourceTree = "<group>"; };
|
||||
568ECB0C25D904CA00B71526 /* SQLSelection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLSelection.swift; sourceTree = "<group>"; };
|
||||
568ECB3425D91CEE00B71526 /* SQLSubquery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLSubquery.swift; sourceTree = "<group>"; };
|
||||
5690C3251D23E6D800E59934 /* FoundationDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationDateComponentsTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1562,6 +1565,7 @@
|
||||
5656A84E2295BD56001FF3FF /* SQLRelation.swift */,
|
||||
568ECB0C25D904CA00B71526 /* SQLSelection.swift */,
|
||||
568ECB3425D91CEE00B71526 /* SQLSubquery.swift */,
|
||||
56894F322604FC1E00268F4D /* Table.swift */,
|
||||
);
|
||||
path = SQL;
|
||||
sourceTree = "<group>";
|
||||
@@ -2280,6 +2284,7 @@
|
||||
56CEB4F61EAA2EFA00BFAF62 /* FetchableRecord.swift in Sources */,
|
||||
5656A8522295BD56001FF3FF /* SQLInterpolation+QueryInterface.swift in Sources */,
|
||||
56BB6EAE1D3009B100A1CA52 /* SchedulingWatchdog.swift in Sources */,
|
||||
56894F342604FC1E00268F4D /* Table.swift in Sources */,
|
||||
F3BA801A1CFB2876003DC1BA /* StatementColumnConvertible.swift in Sources */,
|
||||
566B91381FA4D3810012D5B0 /* TransactionObserver.swift in Sources */,
|
||||
56CEB4FF1EAA2F4D00BFAF62 /* FTS3.swift in Sources */,
|
||||
@@ -2641,6 +2646,7 @@
|
||||
56CEB4F31EAA2EFA00BFAF62 /* FetchableRecord.swift in Sources */,
|
||||
5656A8512295BD56001FF3FF /* SQLInterpolation+QueryInterface.swift in Sources */,
|
||||
56BB6EAB1D3009B100A1CA52 /* SchedulingWatchdog.swift in Sources */,
|
||||
56894F332604FC1E00268F4D /* Table.swift in Sources */,
|
||||
F3BA80761CFB2E55003DC1BA /* StatementColumnConvertible.swift in Sources */,
|
||||
566B91351FA4D3810012D5B0 /* TransactionObserver.swift in Sources */,
|
||||
56CEB4FC1EAA2F4D00BFAF62 /* FTS3.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user