mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
More Associations and inflections
This commit is contained in:
1 parent
a1b0baaca9
commit
8e2a8fe9d8
17 files changed
+236
-203
No files matched your search
@@ -196,9 +196,15 @@ extension Association {
|
||||
/// let team: Team = row["custom"]
|
||||
/// }
|
||||
public func forKey(_ key: String) -> Self {
|
||||
// TODO: consider instead Player.including(required: Player.team, forKey: "custom")
|
||||
let key = SQLAssociationKey(name: key, isInflectable: false)
|
||||
return Self.init(sqlAssociation: sqlAssociation.forDestinationKey(key))
|
||||
let associationKey: SQLAssociationKey
|
||||
if sqlAssociation.destinationIsSingular {
|
||||
assert(key.singularized == key)
|
||||
associationKey = .inflectableSingular(key)
|
||||
} else {
|
||||
assert(key.pluralized == key)
|
||||
associationKey = .inflectablePlural(key)
|
||||
}
|
||||
return Self.init(sqlAssociation: sqlAssociation.forDestinationKey(associationKey))
|
||||
}
|
||||
|
||||
/// Creates an association with the given key.
|
||||
@@ -459,22 +465,36 @@ public protocol AssociationToOne: Association { }
|
||||
|
||||
// MARK: - SQLAssociationKey
|
||||
|
||||
struct SQLAssociationKey {
|
||||
var name: String
|
||||
var isInflectable: Bool
|
||||
enum SQLAssociationKey {
|
||||
case inflectableSingular(String)
|
||||
case inflectablePlural(String)
|
||||
case inflectable(String)
|
||||
case fixed(String)
|
||||
|
||||
var pluralizedName: String {
|
||||
guard isInflectable else {
|
||||
switch self {
|
||||
case .inflectableSingular(let name):
|
||||
return name.pluralized
|
||||
case .inflectablePlural(let name):
|
||||
return name
|
||||
case .inflectable(let name):
|
||||
return name.pluralized
|
||||
case .fixed(let name):
|
||||
return name
|
||||
}
|
||||
return name.pluralized
|
||||
}
|
||||
|
||||
var singularizedName: String {
|
||||
guard isInflectable else {
|
||||
switch self {
|
||||
case .inflectableSingular(let name):
|
||||
return name
|
||||
case .inflectablePlural(let name):
|
||||
return name.singularized
|
||||
case .inflectable(let name):
|
||||
return name.singularized
|
||||
case .fixed(let name):
|
||||
return name
|
||||
}
|
||||
return name.singularized
|
||||
}
|
||||
}
|
||||
|
||||
@@ -558,6 +578,7 @@ public /* TODO: internal */ struct SQLAssociation {
|
||||
set { steps[steps.count - 1] = newValue }
|
||||
}
|
||||
var destinationKey: SQLAssociationKey { return destination.key }
|
||||
var destinationIsSingular: Bool { return destination.isSingular }
|
||||
|
||||
private var pivot: AssociationStep {
|
||||
get { return steps[0] }
|
||||
@@ -751,7 +772,7 @@ public /* TODO: internal */ struct SQLAssociation {
|
||||
// key, and prevent merging of intermediate steps of
|
||||
// indirect associations:
|
||||
return reducedAssociation
|
||||
.forPivotKey(SQLAssociationKey(name: "grdb_\(UUID().uuidString)", isInflectable: false))
|
||||
.forPivotKey(SQLAssociationKey.fixed("grdb_\(UUID().uuidString)"))
|
||||
.extendedRelation(origin, kind: .allNotPrefetched)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ extension TableRecord {
|
||||
/// foreign keys to the destination table.
|
||||
public static func belongsTo<Destination>(
|
||||
_ destination: Destination.Type,
|
||||
name: String? = nil, // TODD: fixit because `name` has replaced `key`
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> BelongsToAssociation<Self, Destination>
|
||||
where Destination: TableRecord
|
||||
@@ -150,8 +150,16 @@ extension TableRecord {
|
||||
foreignKeyRequest: foreignKeyRequest,
|
||||
originIsLeft: true)
|
||||
|
||||
let associationKey: SQLAssociationKey
|
||||
if let key = key {
|
||||
assert(key.singularized == key)
|
||||
associationKey = .inflectableSingular(key)
|
||||
} else {
|
||||
associationKey = .inflectable(Destination.databaseTableName)
|
||||
}
|
||||
|
||||
return BelongsToAssociation(sqlAssociation: SQLAssociation(
|
||||
key: SQLAssociationKey(name: name ?? Destination.databaseTableName, isInflectable: true),
|
||||
key: associationKey,
|
||||
condition: condition,
|
||||
relation: Destination.all().relation,
|
||||
isSingular: true))
|
||||
|
||||
@@ -136,7 +136,7 @@ extension TableRecord {
|
||||
/// foreign keys from the destination table.
|
||||
public static func hasMany<Destination>(
|
||||
_ destination: Destination.Type,
|
||||
name: String? = nil, // TODD: fixit because `name` has replaced `key`
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasManyAssociation<Self, Destination>
|
||||
where Destination: TableRecord
|
||||
@@ -150,8 +150,16 @@ extension TableRecord {
|
||||
foreignKeyRequest: foreignKeyRequest,
|
||||
originIsLeft: false)
|
||||
|
||||
let associationKey: SQLAssociationKey
|
||||
if let key = key {
|
||||
assert(key.pluralized == key)
|
||||
associationKey = .inflectablePlural(key)
|
||||
} else {
|
||||
associationKey = .inflectable(Destination.databaseTableName)
|
||||
}
|
||||
|
||||
return HasManyAssociation(sqlAssociation: SQLAssociation(
|
||||
key: SQLAssociationKey(name: name ?? Destination.databaseTableName, isInflectable: true),
|
||||
key: associationKey,
|
||||
condition: condition,
|
||||
relation: Destination.all().relation,
|
||||
isSingular: false))
|
||||
|
||||
@@ -138,7 +138,7 @@ extension TableRecord {
|
||||
/// foreign keys from the destination table.
|
||||
public static func hasOne<Destination>(
|
||||
_ destination: Destination.Type,
|
||||
name: String? = nil, // TODD: fixit because `name` has replaced `key`
|
||||
key: String? = nil,
|
||||
using foreignKey: ForeignKey? = nil)
|
||||
-> HasOneAssociation<Self, Destination>
|
||||
where Destination: TableRecord
|
||||
@@ -152,8 +152,16 @@ extension TableRecord {
|
||||
foreignKeyRequest: foreignKeyRequest,
|
||||
originIsLeft: false)
|
||||
|
||||
let associationKey: SQLAssociationKey
|
||||
if let key = key {
|
||||
assert(key.singularized == key)
|
||||
associationKey = .inflectableSingular(key)
|
||||
} else {
|
||||
associationKey = .inflectable(Destination.databaseTableName)
|
||||
}
|
||||
|
||||
return HasOneAssociation(sqlAssociation: SQLAssociation(
|
||||
key: SQLAssociationKey(name: name ?? Destination.databaseTableName, isInflectable: true),
|
||||
key: associationKey,
|
||||
condition: condition,
|
||||
relation: Destination.all().relation,
|
||||
isSingular: true))
|
||||
|
||||
@@ -321,7 +321,7 @@ extension Array where Element == TableAlias {
|
||||
var index = 1
|
||||
for alias in group {
|
||||
if alias.hasUserName { continue }
|
||||
let radical = alias.identityName.databaseQualifierRadical
|
||||
let radical = alias.identityName.digitlessRadical
|
||||
var resolvedName: String
|
||||
repeat {
|
||||
resolvedName = "\(radical)\(index)"
|
||||
@@ -334,17 +334,3 @@ extension Array where Element == TableAlias {
|
||||
return resolvedNames
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
/// "bar" => "bar"
|
||||
/// "foo12" => "foo"
|
||||
fileprivate var databaseQualifierRadical: String {
|
||||
let digits: ClosedRange<Character> = "0"..."9"
|
||||
let radicalEndIndex = self // "foo12"
|
||||
.reversed() // "21oof"
|
||||
.prefix(while: { digits.contains($0) }) // "21"
|
||||
.endIndex // reversed(foo^12)
|
||||
.base // foo^12
|
||||
return String(prefix(upTo: radicalEndIndex))
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,7 @@ struct SQLRelation {
|
||||
}
|
||||
|
||||
fileprivate func makeAssociationForKey(_ key: String) -> SQLAssociation {
|
||||
let key = SQLAssociationKey(name: key, isInflectable: false)
|
||||
let key = SQLAssociationKey.fixed(key)
|
||||
return SQLAssociation(key: key, condition: condition, relation: relation, isSingular: kind.isSingular)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user