Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对model进行扩展 #66

Open
UbunGit opened this issue Apr 23, 2021 · 0 comments
Open

对model进行扩展 #66

UbunGit opened this issue Apr 23, 2021 · 0 comments

Comments

@UbunGit
Copy link

UbunGit commented Apr 23, 2021

朋友们:你们好

在使用Perfect-CRUD的时候我想对model进行扩展一下功能,但是我遇到的问题是无法传递args,比如 //MARK :1 无法传递rest到对应的位置,所以来求助你们,希望能得到你们的帮助

public protocol SqlitInsterProtocol{
  
    func insert() throws -> Int?

    func insert<Z: Decodable>( setKeys: KeyPath<Self, Z>, _ rest: PartialKeyPath<Self>...) throws -> Int?

    func insert<Z: Decodable>( ignoreKeys: KeyPath<Self, Z>, _ rest: PartialKeyPath<Self>...) throws -> Int?
}

extension SqlitInsterProtocol{
    
    func insert() throws -> Int?{
        do {
            let db = Database(configuration: try! SQLiteDatabaseConfiguration(Self.dbfile))
            return try db.table(Self.self)
                .insert(self)
                .lastInsertId()
        }catch{
            throw APIError(code: -1, msg: "error")
        }
    }
    
    func insert<Z: Decodable>( setKeys: KeyPath<Self, Z>, _ rest: PartialKeyPath<Self>...) throws -> Int?{
        do {
            let db = Database(configuration: try! SQLiteDatabaseConfiguration(Self.dbfile))
            // MARK :1  ignoreKeys +rest
            guard let id = try db.table(Self.self)
                    .insert(self,setKeys:setKeys)
                    .lastInsertId() else{
                throw APIError(code: -1, msg: "error")
            }
            return id
        }catch{
            throw APIError(code: -1, msg: "error")
        }
    }
    func insert<Z: Decodable>( ignoreKeys: KeyPath<Self, Z> , _ rest: PartialKeyPath<Self>...) throws -> Int{
        do {
            let db = Database(configuration: try! SQLiteDatabaseConfiguration(Self.dbfile))
            // MARK :1  ignoreKeys +rest
            guard let id = try db.table(Self.self)
                    .insert(self,ignoreKeys:ignoreKeys)
                    .lastInsertId() else{
                throw APIError(code: -1, msg: "error")
            }
            return id
        }catch{
            throw APIError(code: -1, msg: "error")
        }
    }
}

struct Cat: Codable,SqliteProtocol{
    var id:Int = 0
    var name:String=""
}

let cat = Cat(id:1,name:"kiter")
try cat.inster(ignoreKeys: \Cat.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant