Skip to content

Commit

Permalink
Testing, new filters and more
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Apr 4, 2018
1 parent b0d5619 commit b3f7486
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 518 deletions.
20 changes: 20 additions & 0 deletions Classes/Libs/Filter/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public func ==* (lhs: String, rhs: StringQueryDataRepresentable?) -> QueryFilter
return _compare(lhs, .equals, rhs, false)
}

/// field CONTAINS value
infix operator ~~
public func ~~ (lhs: String, rhs: StringQueryDataRepresentable) -> QueryFilter {
return _compare(lhs, .contains, rhs)
}

/// field CONTAINS value, case insensitive
infix operator ~~*
public func ~~* (lhs: String, rhs: StringQueryDataRepresentable?) -> QueryFilter {
return _compare(lhs, .contains, rhs, false)
}

/// field != value
public func != (lhs: String, rhs: QueryDataRepresentable) -> QueryFilter {
return _compare(lhs, .notEquals, rhs)
Expand Down Expand Up @@ -91,6 +103,14 @@ public func <= (lhs: String, rhs: QueryDataRepresentable?) -> QueryFilter {
return _compare(lhs, .lessThanOrEquals, rhs)
}

extension QueryFilter {

public static func custom(_ lhs: String, _ comparison: QueryFilterType, _ rhs: QueryDataRepresentable?, caseSensitive: Bool = true) -> QueryFilter {
return _compare(lhs, comparison, rhs, caseSensitive)
}

}

private func _compare(_ lhs: String, _ comparison: QueryFilterType, _ rhs: QueryDataRepresentable?, _ caseSensitive: Bool = true) -> QueryFilter {
return QueryFilter(field: QueryField(name: lhs), type: comparison, value: rhs ?? NULL(), caseSensitive: caseSensitive)
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/Libs/Filter/QueryFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension QueryFilter {

public func asPredicate() -> NSPredicate {
if let value = value as? String {
let predicate = NSPredicate(format: "\(field.name) \(type.interpretation)\(caseSensitive ? "[c]" : "") %@", value)
let predicate = NSPredicate(format: "\(field.name) \(type.interpretation)\(caseSensitive ? "" : "[c]") %@", value)
return predicate
} else if let value = value as? Bool {
let predicate = NSPredicate(format: "\(field.name) \(type.interpretation) %@", NSNumber(booleanLiteral: value))
Expand Down
15 changes: 15 additions & 0 deletions Classes/Libs/Filter/QueryFilterType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public struct QueryFilterType: Equatable {

enum Storage: Equatable {
case equals
case contains
case beginsWith
case endsWith
case notEquals
case greaterThan
case lessThan
Expand All @@ -25,6 +28,12 @@ public struct QueryFilterType: Equatable {
switch storage {
case .equals:
return "=="
case .contains:
return "CONTAINS"
case .beginsWith:
return "BEGINSWITH"
case .endsWith:
return "ENDSWITH"
case .notEquals:
return "!="
case .greaterThan:
Expand All @@ -45,6 +54,12 @@ public struct QueryFilterType: Equatable {

/// ==
public static var equals: QueryFilterType { return .init(storage: .equals) }
/// CONTAINS
public static var contains: QueryFilterType { return .init(storage: .contains) }
/// BEGINSWITH
public static var beginsWith: QueryFilterType { return .init(storage: .beginsWith) }
/// ENDSWITH
public static var endsWith: QueryFilterType { return .init(storage: .endsWith) }
/// !=
public static var notEquals: QueryFilterType { return .init(storage: .notEquals) }
/// >
Expand Down
2 changes: 1 addition & 1 deletion Classes/Libs/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation


/// Root query available for each NSManagedObject conforming to Entity
public class Query<QueryEntityType>: QueryExecutable where QueryEntityType: Entity {
public class Query<QueryEntityType>: QueryExecutable where QueryEntityType: Entity {

/// Name of the entity this query will be executed on
public var entity: QueryEntityType.Type
Expand Down
8 changes: 0 additions & 8 deletions Classes/Libs/QueryField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,3 @@ public struct QueryField: Hashable {
}

}

extension KeyPath where Root: Entity {

public func makeQueryField() throws -> QueryField {
return QueryField(name: String(format: "%K", Value.self as! CVarArg))
}

}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Classes/Protocols/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import CoreData
@_exported import CoreData

/// Type erased Entity
public protocol AnyEntity: class {
Expand Down
7 changes: 6 additions & 1 deletion Classes/Protocols/QueryDataRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation


public protocol QueryDataRepresentable {
// var value: String { get }
var isNull: Bool { get }
}

Expand Down Expand Up @@ -49,7 +48,13 @@ extension Bool: ExactQueryDataRepresentable {

}

extension Date: QueryDataRepresentable { }
extension String: StringQueryDataRepresentable { }
extension Decimal: NumericQueryDataRepresentable { }
extension Double: NumericQueryDataRepresentable { }
extension Float: NumericQueryDataRepresentable { }
extension Int: NumericQueryDataRepresentable { }
extension Int16: NumericQueryDataRepresentable { }
extension Int32: NumericQueryDataRepresentable { }
extension Int64: NumericQueryDataRepresentable { }
extension Bool: NumericQueryDataRepresentable { }
23 changes: 17 additions & 6 deletions Classes/Protocols/QueryExecutable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation
import CoreData
@_exported import CoreData


public protocol QueryExecutable {
Expand Down Expand Up @@ -38,6 +38,10 @@ extension QueryExecutable {
}
return fetch
}

}

extension QueryExecutable where EntityType: NSManagedObject {

public func all(on context: NSManagedObjectContext = CoreData.managedContext) throws -> [EntityType] {
guard let data = try context.fetch(fetchRequest()) as? [EntityType] else {
Expand All @@ -47,12 +51,19 @@ extension QueryExecutable {
}

public func delete(on context: NSManagedObjectContext = CoreData.managedContext) throws {
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest())

guard let persistentStoreCoordinator = context.persistentStoreCoordinator else {
throw CoreData.Problem.invalidPersistentStoreCoordinator
for object in try all(on: context) {
try object.delete(on: context)
}
try persistentStoreCoordinator.execute(deleteRequest, with: context)
try context.save()

// let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest())
// do {
// let batchDeleteResult = try context.execute(deleteRequest) as! NSBatchDeleteResult
// print("The batch delete request has deleted \(batchDeleteResult.result!) records.")
// } catch {
// let updateError = error as NSError
// print("\(updateError), \(updateError.userInfo)")
// }
}

public func count(on context: NSManagedObjectContext = CoreData.managedContext) throws -> Int {
Expand Down
2 changes: 1 addition & 1 deletion Demo-iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ViewController: UIViewController {
hasChimney = !hasChimney
}

let all = try! Locomotive.query.filter("hasChimney" == true).filter(.or, "color" == "green", "color" == "black").sort(by: "color", direction: .orderedDescending).all()
let all = try! Locomotive.query.filter("hasChimney" == true).filter(.or, "color" == "green", "color" == "black").sort(by: "color", direction: .orderedAscending).all()
all.forEach { loco in
print("Color: \(loco.color ?? "Unknown"); Chimney: \(Bool(loco.hasChimney))")
}
Expand Down
Loading

0 comments on commit b3f7486

Please sign in to comment.