Kitura / Swift-Kuery-ORM

An ORM for Swift, built on Codable
Apache License 2.0
212 stars 30 forks source link

confusing about 'executeQuery' is inaccessible due to 'internal' protection level #53

Closed diuming closed 5 years ago

diuming commented 6 years ago

Context and Description

as title

struct Student: Model {
    var name: String
    static func students(withName name: String) {
        //  error: 'executeQuery' is inaccessible due to 'internal' protection level
        Student.executeQuery(query: select) { (users: [Student]?, error: RequestError?) in
        }
    }
}

Environment Details

macOS 10.13.4 Xcode 9.4

EnriqueL8 commented 6 years ago

Hey @diuming, did you mean to close this?

jmmaloney4 commented 6 years ago

I am having the same issue, could someone explain how this was resolved? @diuming @EnriqueL8

kilnerm commented 6 years ago

@jmmaloney4, Can you tell me a bit more about what you are trying to achieve with the executeQuery method? You should be able to do the same thing with the public api.

I believe the reason the methods are internal is that they are not intended to be executed directly against the model, perhaps @EnriqueL8 can confirm this?

EnriqueL8 commented 6 years ago

The idea of these methods is to preform your own queries within the Model. It can be that since they are internal, they cannot be used within a type conforming to Model . I consider making them public or updating the documentation.

jmmaloney4 commented 6 years ago

@kilnerm @EnriqueL8 I actually figured it out I believe. The problem was that using findAll with query parameters was poorly documented. I saw it and thought that I could only retrieve every instance, so I was trying to implement my own query to do the SELECT for me. But now I seem to have figured it out. I am a database noob so its probably assumed that of course you would never retrieve every object from the database, but if there could be some documentation on how to use QueryParameters to filter the select query that would be fantastic (I'd offer to do it but I really don't think I understand it well enough to write documentation on it). While unrelated, the other thing I'm having a hard time finding is the behavior with incomplete objects/objects with optionals, if someone could give me some direction on that that would be great too.

kenlaws commented 6 years ago

I am admittedly new to Kitura, but I'm in the same boat, and the docs and source code are not helping. For example, the docs at Swift-Kuery-ORM tell me (near the bottom) specifically to use executeQuery, which is impossible.

I'll back up a moment and state my goal: Given a class B that is properly mirrored in the db, I want to run a query against it, ideally something complex like fieldA == 'me' || fieldB == 'you', and have the results come back as they normally do in a findAll call, all nice and organized as instances of class B.

kilnerm commented 6 years ago

Hi @kenlaws, have you see the following blog post that gives a good example of using QueryParameters to filter results from findAll?

https://developer.ibm.com/swift/2018/06/08/filtering-swift-kuery-orm/

I am torn on exposing the raw query capability through the model, my gut says that the currently public API should be updated to support use cases that you could currently only achieve through the protected API, there probably needs to be some discussion around the direction this is eventually taken in.

Regarding your specific example there is no current API that would allow you to return the results from a single call. You would need to make two separate filter calls and merge the results.

kenlaws commented 6 years ago

Aha! Thank you, that blog post has some info that isn't in the current docs.

Regarding the specific "or" example I gave, I've written a view and stored procedure in my PostgreSQL db that produce what I want, and I may just try to call it via Swift-Kuery, if possible, and interpret the results. Of course, ideally, I'd like to be able to have something higher level...

kilnerm commented 6 years ago

@kenlaws, Glad the blog was useful.

If you have any other use cases that the ORM does not currently support please raise an issue.

I'll discuss the current privacy levels of the executeQuery calls with some of the other folks here and see how they feel about exposing them vs updating the API.

kilnerm commented 5 years ago

The ORM documentation needs a better example of how to use the internal methods. I will write some sample code and update the README.

kilnerm commented 5 years ago

I was just writing an example for the question regarding the ORM's executeQuery methods..... turns out that internal methods defined on a protocol extension are not accessible within a further extension of that protocol. eg......

extension Info: Model {

    public func getOverTwenties() -> [Info]? {
        var table: Table
        do {
            table = try Info.getTable()
        } catch {
            return nil
        }
        let query = Select(from: table).where("age >= 20")

        executeQuery // <------ Cannot be called here
        return nil
    }
}

Will see how we can make this work as intended.

kilnerm commented 5 years ago

We have just merged a PR that exposes the executeQuery methods as public meaning they can now be used as intended. The README has been updated with an example of their use: https://github.com/IBM-Swift/Swift-Kuery-ORM#customizing-your-model

I am going to mark this as closed, please reopen if there are any follow up questions.