Comments (3)
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.
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.
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)
- Preload associations HOT 1
- Is "type" word a reserved? HOT 2
- java.lang.NoClassDefFoundError in SchemaUpdater.update HOT 1
- override beforeExecuteQuery or add default where
- it is possible do delete all content without delete the tables?
- SELECT from WITH clause
- Issue updating from v1.5.1 to v1.6.1 for Android
- New to requery, not sure how to get entities from database
- Is requery support only in java 8? HOT 1
- [Question] Query's string for debug
- Possible to create field of Map type?
- how to set an attribute when it is a key generated type?
- Build error with AS 4.2 running java 11 HOT 1
- connection leak when rollback (reloaded) HOT 1
- How to create Entity with a dynamic table name
- Migrate away from JCenter HOT 3
- Migrate from RxJava2 to RxJava3
- kapt build error HOT 2
- Is this project dead? HOT 3
- Version no longer upgraded HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from requery.