Code Monkey home page Code Monkey logo

Comments (4)

groue avatar groue commented on May 19, 2024

Hello @schveiguy

It's not documented in the README, but you can pass a dictionary to the key argument of fetchOne:

MyRecord.fetchOne(db, key: ["col1": val1, "col2": val2])

Documentation is missing, but the feature is there to stay: I want GRDB to support any primary key.

This variant of fetchOne actually supports any key, not only primary keys, because it does not introspect the database schema. It implicitly requires that the dictionary columns are actually a table key, and returns the first record that matches.

Why did I not add any generic function that eats a tuple of 2, 3, ... N items? Because these functions would depend on the ordering of columns in the primary key. And this ordering is not always obvious, or may even be fragile (in case of wild schema change):

// Not supported, on purpose:
MyRecord.fetchOne(db, key: (1, 2))  // what is 1? what is 2?

So now, I suggest you write your own extension, as below :-)

extension MyRecord {
    static func fetchOne(db: Database, key: (Int64, Int)) -> MyRecord? {
        return fetchOne(db, ["col1": key.0, "col2": key.1])
    }
}

from grdb.swift.

schveiguy avatar schveiguy commented on May 19, 2024

Hm... yes, that's much better than my SQL string forward that I had written, thanks :)

from grdb.swift.

groue avatar groue commented on May 19, 2024

You're welcome 😄

from grdb.swift.

groue avatar groue commented on May 19, 2024

I added a paragraph in README.md: https://github.com/groue/GRDB.swift#fetching-by-primary-key

from grdb.swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.