Skip to content

Commit

Permalink
Merge pull request #176 from vapor/138
Browse files Browse the repository at this point in the history
138
  • Loading branch information
loganwright authored Feb 13, 2017
2 parents 8063e1b + f8a64ed commit ca14e1b
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 54 deletions.
4 changes: 4 additions & 0 deletions Sources/Fluent/Database/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public protocol Driver {
The `idKey` will be used when
`Model.find(_:)` or other find
by identifier methods are used.
This value is overriden by
entities that implement the
`Entity.idKey` static property.
*/
var idKey: String { get }

Expand Down
23 changes: 17 additions & 6 deletions Sources/Fluent/Entity/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ public protocol Entity: Preparation, NodeConvertible {
like pivots.
*/
static var name: String { get }


/**
The name of the column that corresponds
to this entity's key.
The default return is 'database.driver.idKey',
and if no database is set, 'id' is returned,
instead.
*/
static var idKey: String { get }

/**
The entity's primary identifier.
This is the same value used for
Expand Down Expand Up @@ -82,6 +92,10 @@ extension Entity {
public static var name: String {
return String(describing: self).lowercased()
}

public static var idKey: String {
return database?.driver.idKey ?? "id"
}

// FIXME: Remove in 2.0. Also, make exists optional.
@available(*, deprecated: 1.0, message: "This 'exists' property is not stored. Add `var exists: Bool = false` to the model. This default implementation will be removed in a future update.")
Expand Down Expand Up @@ -138,11 +152,8 @@ extension Entity {
Finds the entity with the given `id`.
*/
public static func find(_ id: NodeRepresentable) throws -> Self? {
guard let idKey = database?.driver.idKey else {
return nil
}

return try Self.query().filter(idKey, .equals, id).first()
guard let _ = database else { return nil }
return try Self.query().filter(Self.idKey, .equals, id).first()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Sources/Fluent/Memory/Memory+Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension MemoryDriver {
return []
}

if count > dataToReturn.count {
if count >= dataToReturn.count {
count = dataToReturn.count - 1
}

Expand Down
3 changes: 1 addition & 2 deletions Sources/Fluent/Memory/MemoryDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public final class MemoryDriver: Driver {
guard let data = query.data else {
throw Error.dataRequired
}
let i = group.create(data, idKey: idKey)

let i = group.create(data, idKey: T.idKey)
return Node.number(.int(i))
case .delete:
group.delete(query.filters)
Expand Down
29 changes: 14 additions & 15 deletions Sources/Fluent/Query/Join.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public struct Union {
) {
self.local = local
self.foreign = foreign
self.localKey = localKey ?? "\(foreign.name)_\(idKey)"
self.foreignKey = foreignKey ?? idKey
self.localKey = localKey ?? "\(foreign.name)_\(foreign.idKey)"
self.foreignKey = foreignKey ?? foreign.idKey
}
}

Expand Down Expand Up @@ -64,11 +64,11 @@ public final class Pivot<
}

public init(node: Node, in context: Context) throws {
let idKey = try First.query().idKey
let idKey = First.idKey
id = try node.extract(idKey)

let firstKey = "\(First.name)_\(idKey)"
let secondKey = "\(Second.name)_\(idKey)"
let firstKey = "\(First.name)_\(First.idKey)"
let secondKey = "\(Second.name)_\(Second.idKey)"

if First.self == type(of: self).left {
leftId = try node.extract(firstKey)
Expand All @@ -80,19 +80,18 @@ public final class Pivot<
}

public func makeNode(context: Context = EmptyNode) throws -> Node {
let idKey = try First.query().idKey
return try Node(node: [
"\(idKey)": id,
"\(type(of: self).left.name)_\(idKey)": leftId,
"\(type(of: self).right.name)_\(idKey)": rightId,
"\(type(of: self).idKey)": id,
"\(type(of: self).left.name)_\(type(of: self).left.idKey)": leftId,
"\(type(of: self).right.name)_\(type(of: self).right.idKey)": rightId,
])
}

public static func prepare(_ database: Database) throws {
try database.create(entity) { builder in
builder.id()
builder.int("\(left.name)_\(database.driver.idKey)")
builder.int("\(right.name)_\(database.driver.idKey)")
builder.int("\(left.name)_\(left.idKey)")
builder.int("\(right.name)_\(right.idKey)")
}
}

Expand All @@ -111,7 +110,7 @@ extension QueryRepresentable {
let union = Union(
local: T.self,
foreign: sibling,
idKey: query.database.driver.idKey,
idKey: query.idKey,
localKey: nil,
foreignKey: nil
)
Expand All @@ -130,7 +129,7 @@ extension QueryRepresentable {
let union = Union(
local: T.self,
foreign: sibling,
idKey: query.database.driver.idKey,
idKey: query.idKey,
localKey: nil,
foreignKey: foreignKey
)
Expand All @@ -149,7 +148,7 @@ extension QueryRepresentable {
let union = Union(
local: T.self,
foreign: sibling,
idKey: query.database.driver.idKey,
idKey: query.idKey,
localKey: localKey,
foreignKey: nil
)
Expand All @@ -169,7 +168,7 @@ extension QueryRepresentable {
let union = Union(
local: T.self,
foreign: sibling,
idKey: query.database.driver.idKey,
idKey: query.idKey,
localKey: localKey,
foreignKey: foreignKey
)
Expand Down
6 changes: 3 additions & 3 deletions Sources/Fluent/Query/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class Query<T: Entity>: QueryRepresentable {
do {
var model = try T(node: result, in: _context)
if case .object(let dict) = result {
model.id = dict[database.driver.idKey]
model.id = dict[T.idKey]
}
models.append(model)
} catch {
Expand Down Expand Up @@ -252,7 +252,7 @@ extension QueryRepresentable {
let filter = Filter(
T.self,
.compare(
query.database.driver.idKey,
T.idKey,
.equals,
id
)
Expand Down Expand Up @@ -280,7 +280,7 @@ extension QueryRepresentable {
query.action = .modify
query.data = serialized

let idKey = query.database.driver.idKey
let idKey = T.idKey
if let id = serialized?[idKey] {
_ = try filter(idKey, id)
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/Fluent/Relations/Children.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ extension Children: QueryRepresentable {
guard let ident = parent.id else {
throw RelationError.noIdentifier
}

let query = try T.query()

let foreignId = foreignKey ?? "\(type(of: parent).name)_\(query.idKey)"

let foreignId = foreignKey ?? "\(T.name)_\(T.idKey)"
return try T.query().filter(foreignId, ident)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Fluent/Relations/Parent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public final class Parent<T: Entity> {
extension Parent: QueryRepresentable {
public func makeQuery() throws -> Query<T> {
let query = try T.query()
return try query.filter(foreignKey ?? query.idKey, parentId)
return try query.filter(foreignKey ?? T.idKey, parentId)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Fluent/Relations/Siblings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public final class Siblings<T: Entity> {

let query = try T.query()

let localKey = localKey ?? query.idKey
let foreignKey = foreignKey ?? "\(T.name)_\(query.idKey)"
let localKey = localKey ?? T.idKey
let foreignKey = foreignKey ?? "\(T.name)_\(T.idKey)"

self.localKey = localKey
self.foreignKey = foreignKey
Expand All @@ -28,7 +28,7 @@ public final class Siblings<T: Entity> {
foreignKey: foreignKey
)

try query.filter(pivot, "\(E.name)_\(query.idKey)", ident)
try query.filter(pivot, "\(E.name)_\(E.idKey)", ident)

_query = query
}
Expand Down
11 changes: 8 additions & 3 deletions Sources/FluentTester/Atom.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Fluent

public final class Atom: Entity {

public static var idKey: String {
return "atom_id"
}

public var id: Node?

public var name: String
Expand All @@ -17,15 +22,15 @@ public final class Atom: Entity {
}

public init(node: Node, in context: Context) throws {
id = try node.extract("id")
id = try node.extract(type(of: self).idKey)
name = try node.extract("name")
protons = try node.extract("protons")
weight = try node.extract("weight")
}

public func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
type(of: self).idKey: id,
"name": name,
"protons": protons,
"weight": weight
Expand All @@ -38,7 +43,7 @@ public final class Atom: Entity {

public static func prepare(_ database: Database) throws {
try database.create(entity) { atoms in
atoms.id()
atoms.id(idKey)
atoms.string("name")
atoms.int("protons")
atoms.double("weight")
Expand Down
Loading

0 comments on commit ca14e1b

Please sign in to comment.