GithubHelp home page GithubHelp logo

Insert or Update about requery HOT 3 CLOSED

requery avatar requery commented on May 13, 2024
Insert or Update

from requery.

Comments (3)

npurushe avatar npurushe commented on May 13, 2024

Hi yes upsert is a good thing. Unfortunately the databases that support it all do it a different way so tricky to implement.

Is this for SQLite? SQLite only has insert or replace which is not the same as upsert unfortunately. I think for SQLite if you have some primary key it's best to check if that key already has a record first and retrieve it and base your insert/update decision on that, while of course being careful of concurrency.

from requery.

eugenkiss avatar eugenkiss commented on May 13, 2024

Yes, it's for SQLite in Android. Thanks for your tips. I've create two new versions of upsert (or rather a rough approximation thereof as you correctly mentioned that SQLite does not really have upsert support) extension functions.

fun <E: Persistable, K> SingleEntityStore<Persistable>.upsert(clazz: Class<E>, key: K, entity: E): Single<E> {
    return findByKey(clazz, key).toObservable().materialize().flatMap { notification ->
        if (notification.isOnError) {
            insert(entity).toObservable()
        } else {
            update(entity).toObservable()
        }
    }.toSingle()
}
fun <E: Persistable, K> BlockingEntityStore<Persistable>.upsert(clazz: Class<E>, key: K, entity: E): E {
    try {
        findByKey(clazz, key) == null
    } catch (e: NoSuchElementException) {
        return insert(entity)
    }
    return update(entity)
}

With the first one I had some concurrency problems (where I upserted a list of entities). I wasn't patient enough to fix them so I created the second extension function for a blocking version which now seems to work alright.

By the way, there seems to still be something fishy when I look at Requery's beforeExecuteQuery logs. Basically, I have five entities that I upsert and there are four distinct entities in the database. The first and the last of the five entities represent the same one in the database. The logs always show only four select statements but five insert or update statements. Can it be that a the last select is not executed because the entity is found in the cache? If yes, maybe that should be indicated in the logs?

from requery.

npurushe avatar npurushe commented on May 13, 2024

Hi yes if you use findByKey it's possible there are no queries made using the default caching strategy. I agree default logger should be extended to log that.

from requery.

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.