Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement single step undo for SQLite DB changes?

I am trying to adapt my application from a confirmation model to an undo model. For those of you who don't know, this is where you can delete something with one click but if it was a mistake you can undo it just as easily, as opposed to interrupting the user every time he/she wants to do something to ask the annoying "Are you sure you want to...?" question via dialog.

My app is backed by the Android SQLite DB and I want to be able to undo a limited set of delete and update operations. Also, I only need to be able to undo one sequential change and the information does not have to stick arround for very long.

Everything I read on undo/redo says to use a command model to store the data. My question is how can I store the database changes in a lightweight restorable way?

like image 924
CodeFusionMobile Avatar asked Nov 01 '10 14:11

CodeFusionMobile


1 Answers

The idea of the command pattern is, that every command knows how it can be undone. For instance, an AddPersonCommand would add a new record to the Persons-table in your database. To undo this command, you would have to delete that person again.

Depending on the type of application and the complexity of the database, you can just write the changes to the database like you normally would. You always keep the last X command objects (X being the number of actions that can be undone), and if necessary, invoke their undo-method.

like image 84
Björn Pollex Avatar answered Oct 04 '22 15:10

Björn Pollex