Skip to content

Instantly share code, notes, and snippets.

@sora0077
Created November 5, 2015 18:10
Show Gist options
  • Save sora0077/5a8a34bea75fe7466f40 to your computer and use it in GitHub Desktop.
Save sora0077/5a8a34bea75fe7466f40 to your computer and use it in GitHub Desktop.
// Playground - noun: a place where people can play
import UIKit
import XCPlayground
protocol ObjectProtocol {}
protocol QuerySetProtocol {
}
class QuerySetJoining<Q: QuerySetProtocol> {
let query: Q
init(query: Q) {
self.query = query
}
func on<Lhs: AttributeProtocol, Rhs: AttributeProtocol where Lhs.Element == Rhs.Owner>(lhs: Lhs, with rhs: Rhs) -> Q {
return query
}
func on<Lhs: AttributeProtocol, Rhs: AttributeProtocol where Lhs.Owner == Rhs.Element>(lhs: Lhs, with rhs: Rhs) -> Q {
return query
}
}
extension QuerySetProtocol {
func filter(p: NSPredicate) -> Self {
return self
}
func join<O: ObjectProtocol>(o: O.Type) -> QuerySetJoining<Self> {
return QuerySetJoining(query: self)
}
}
extension QuerySetProtocol where Self: Select {
func from<O: ObjectProtocol>(o: O.Type) -> Self {
return self
}
}
class Select: QuerySetProtocol {
let keys: [String]
init(_ keys: String...) {
self.keys = keys
}
}
class Update<O: ObjectProtocol>: QuerySetProtocol {
func set<Attribute: AttributeProtocol where Attribute.Owner == O>(attr: Attribute, _ val: Attribute.Element) -> Self {
return self
}
}
class User: ObjectProtocol {
var name: String = ""
static let id = Attribute<Int, User>("id")
static let name = Attribute<String, User>("name")
}
class Photo: ObjectProtocol {
static let id = Attribute<Int, Photo>("id")
static let user_id = Attribute<User, Photo>("user_id")
}
protocol AttributeProtocol {
typealias Element
typealias Owner
}
final class Attribute<E, O: ObjectProtocol>: AttributeProtocol {
typealias Element = E
typealias Owner = O
let key: String
init(_ key: String) {
self.key = key
}
func val(val: Element) -> Self {
return self
}
}
func == <Element, Owner>(lhs: Attribute<Element, Owner>, rhs: Element) -> NSPredicate {
return NSPredicate(value: true)
}
Select().from(User).join(Photo).on(Photo.user_id, with: User.id).filter(User.name == "test")
Select().from(User).join(Photo).on(User.id, with: Photo.user_id).filter(User.name == "test")
Update<User>()
.set(User.name, "")
.set(User.id, 2)
.filter(User.name == "test")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment