Skip to content

Commit

Permalink
Merge pull request #211 from vapor/raw
Browse files Browse the repository at this point in the history
raw + sql cleanup
  • Loading branch information
tanner0101 committed Mar 22, 2017
2 parents f37c221 + 3365a70 commit 5d5861a
Show file tree
Hide file tree
Showing 58 changed files with 1,523 additions and 1,262 deletions.
8 changes: 1 addition & 7 deletions Sources/Fluent/Database/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ extension Database {
return try threadConnectionPool.connection().query(query)
}

/// Seeee Executor protocol.
public func schema(_ schema: Schema) throws {
log?(Log(schema))
try threadConnectionPool.connection().schema(schema)
}

/// Seeee Executor protocol.
/// See Executor protocol.
@discardableResult
public func raw(_ raw: String, _ values: [Node]) throws -> Node {
log?(Log(raw: raw))
Expand Down
7 changes: 1 addition & 6 deletions Sources/Fluent/Database/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ public protocol Driver: Executor {
extension Driver {
/// See Executor protocol.
@discardableResult
public func query<T: Entity>(_ query: Query<T>) throws -> Node {
public func query<E: Entity>(_ query: Query<E>) throws -> Node {
return try makeConnection().query(query)
}

/// See Executor protocol.
public func schema(_ schema: Schema) throws {
return try makeConnection().schema(schema)
}

/// See Executor protocol.
@discardableResult
public func raw(_ raw: String, _ values: [Node]) throws -> Node {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Fluent/Database/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public protocol Executor {
/// returns an array of results fetched,
/// created, or updated by the action.
@discardableResult
func query<T: Entity>(_ query: Query<T>) throws -> Node

/// Creates the `Schema` indicated
/// by the `Builder`.
func schema(_ schema: Schema) throws
func query<E: Entity>(_ query: Query<E>) throws -> Node

/// Drivers that support raw querying
/// accept string queries and parameterized values.
///
/// This allows Fluent extensions to be written that
/// can support custom querying behavior.
///
/// - note: Passing parameterized values as a `[Node]` array
/// instead of interpolating them into the raw string
/// can help prevent SQL injection.
@discardableResult
func raw(_ raw: String, _ values: [Node]) throws -> Node
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Fluent/Entity/KeyNamingConvention.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public enum KeyNamingConvention {
case camelCase
}

// MARK: Convert PascalCase to snake and camel

extension String {
internal func snake_case() -> String {
let characters = Array(self.characters)
Expand Down
9 changes: 3 additions & 6 deletions Sources/Fluent/Pagination/Query+Page.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ extension QueryRepresentable where E: Paginatable {
let total = try query.count()

// limit the query to the desired page
query.limit = Limit(
count: count,
offset: (page - 1) * count
)

try query.limit(count, offset: (page - 1) * count)

// add the sorts w/o replacing
query.sorts += sorts
_ = try sorts.map(query.sort)

// fetch the data
let data = try query.all()
Expand Down
2 changes: 2 additions & 0 deletions Sources/Fluent/Pivot/Pivot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public final class Pivot<
try row.set(Right.foreignIdKey, rightId)
return row
}
}

extension Pivot: Preparation {
public static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id(for: self)
Expand Down
1 change: 0 additions & 1 deletion Sources/Fluent/Preparation/Migration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ final class Migration: Entity {

func makeRow() throws -> Row {
var row = Row()
try row.set(idKey, id)
try row.set("name", name)
return row
}
Expand Down
39 changes: 39 additions & 0 deletions Sources/Fluent/Query/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,43 @@ public enum Action {
case delete
case create
case modify
case schema(Schema)
}

public enum Schema {
case create([RawOr<Field>])
case modify(add: [RawOr<Field>], remove: [RawOr<Field>])
case delete
}

extension Action: Equatable {
public static func ==(lhs: Action, rhs: Action) -> Bool {
switch (lhs, rhs) {
case (.fetch, .fetch),
(.count, .count),
(.delete, .delete),
(.create, .create),
(.modify, .modify):
return true
case (.schema(let a), .schema(let b)):
return a == b
default:
return false
}
}
}

extension Schema: Equatable {
public static func ==(lhs: Schema, rhs: Schema) -> Bool {
switch (lhs, rhs) {
case (.create(let a), .create(let b)):
return a == b
case (.modify(let addA, let removeA), .modify(let addB, let removeB)):
return addA == addB && removeA == removeB
case (.delete, .delete):
return true
default:
return false
}
}
}
163 changes: 0 additions & 163 deletions Sources/Fluent/Query/Filter.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extension Filter {
case hasSuffix
case hasPrefix
case contains
case custom(String)
}

}
Expand Down Expand Up @@ -44,3 +45,24 @@ func != (lhs: String, rhs: NodeRepresentable) throws -> Filter.Method {
let node = try rhs.makeNode(in: rowContext)
return .compare(lhs, .notEquals, node)
}

extension Filter.Comparison: Equatable {
public static func ==(lhs: Filter.Comparison, rhs: Filter.Comparison) -> Bool {
switch (lhs, rhs) {
case (.equals, .equals),
(.greaterThan, .greaterThan),
(.lessThan, .lessThan),
(.greaterThanOrEquals, .greaterThanOrEquals),
(.lessThanOrEquals, .lessThanOrEquals),
(.notEquals, .notEquals),
(.hasSuffix, .hasSuffix),
(.hasPrefix, .hasPrefix),
(.contains, .contains):
return true
case (.custom(let a), .custom(let b)):
return a == b
default:
return false
}
}
}
37 changes: 37 additions & 0 deletions Sources/Fluent/Query/Filter/Filter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// Defines a `Filter` that can be
/// added on fetch, delete, and update
/// operations to limit the set of
/// data affected.
public struct Filter {
public enum Relation {
case and, or
}

public enum Method {
case compare(String, Comparison, Node)
case subset(String, Scope, [Node])
case group(Relation, [RawOr<Filter>])
}

public init(_ entity: Entity.Type, _ method: Method) {
self.entity = entity
self.method = method
}

public var entity: Entity.Type
public var method: Method
}

extension Filter: CustomStringConvertible {
public var description: String {
switch method {
case .compare(let field, let comparison, let value):
return "(\(entity)) \(field) \(comparison) \(value)"
case .subset(let field, let scope, let values):
let valueDescriptions = values.map { $0.string ?? "" }
return "(\(entity)) \(field) \(scope) \(valueDescriptions)"
case .group(let relation, let filters):
return filters.map { $0.description }.joined(separator: "\(relation)")
}
}
}
Loading

0 comments on commit 5d5861a

Please sign in to comment.