novoda/sqlite-provider

SQLiteContentProvider#bulkInsert() should return 0 in case of failure

SebastianEngel opened this issue · 4 comments

SQLiteContentProvider#bulkInsert() does not give any indication to the caller when the transaction was not successful. Whatever happens, it returns the count of initial values, whereas it should be "The number of values that were inserted", as done by android.content.ContentProvider#bulkInsert(), which returns 0, when an exception occurred.

I experienced this behaviour when I was inserting entities which contained the same value in unique columns. Nothing was inserted, but I expected some kind of UniqueConstraintException, which would be indicated with a return value of 0.

WE do upsert if you declare unique fields. This should probably be better handled. @devisnik could you take a look at this and define the 'ideal' flow. I take your point Sebastian but SQLiteProvider might be more clever then it should - i.e. should have failed rather then upsert.

I was just confused that the insert did not fail. I was double checking my database if these columns were really defined as unique. :) I expected some kind of indicator for the unique constraint conflict, as ContentProvider#insert does with returning 0, or SQLiteDatabase#insert which returns "the row ID of the newly inserted row, or -1 if an error occurred" or SQLiteDatabase#insertOrThrow which would throw an SQLException.

I think for developers it might be convenient to stay with the platform bevaviour, which would mean to indicate a problem. And maybe optionally offer an upsert method. Something like #insertOrUpdate. What do yo think?

True we definitely want to stay with the platform behaviour. insertOrUpdate would be good but that means we can not rely on the ContentProvider contract. We could pass it as part of the URI:

content://base/table?upsert=true

Closing as this repository is no longer under maintenance and it's about to be archived