emilsjolander / sprinkles

Sprinkles is a boiler-plate-reduction-library for dealing with databases in android applications
Apache License 2.0
772 stars 84 forks source link

save() and saveAsync() clean fields that is not setted in object instance #79

Open magnumrocha opened 10 years ago

magnumrocha commented 10 years ago

When I save a model that already exists in database, but creating a new instance and set just some fields (the primary key and some fields that I want update the value in database register), Sprinkles is cleaning the fields that is not setted on object instance.

Eg.:

ModelClass obj = new ModelClass(); obj.primaryKeyField = 1; obj.someField = "new value"; obj.saveAsync(...);

Suppose that existe another field called "anotherField" on Model Class. The anotherField value is cleaned on database.

lsjwzh commented 10 years ago

you must fetch the model with primaryKeyField=1 and then set someField at this model. like this: ModelClass obj = Query.one(ModelClass.class,"Select * from modelclass where"+primaryKeyField+"=1"); if(obj==null){ obj = new ModelClass(); obj.primaryKeyField = 1; }

obj.someField = "new value"; obj.saveAsync(...);

magnumrocha commented 10 years ago

Ok @lsjwzh,

I understand this solution. It's works fine. But, why not update the register with the existent id in save() and saveAssync() method ?? without execute a query before...

Instead to do a query on database and a write action, do only a write action. This help to avoid unnecessary disk operation.

What do you think about ?

lsjwzh commented 10 years ago

Hi, magnumrocha , Do you mean to update the fields which has not null value,instead to update all fields?

magnumrocha commented 10 years ago

Yes @lsjwzh, exactly it.

Update only the fields setted on Model instance.

lsjwzh commented 10 years ago

I also like this feature!!!! But, it need some extra works to maintain the state. And may be need to intercept POJO's setter. I'm doing some works to improve sprinkles,i have implemented some features like relationship,entity cache,auto create table/columns,fluent query api. And i also consider "changes monitor",but i remain have no good idea on it.

lsjwzh commented 10 years ago

Hi,@magnumrocha How do you think about adding an extra method to do this in Sprinkles? saveNotNull or other name?

magnumrocha commented 10 years ago

Hi @lsjwzh,

I guess that saveNotNull() is a good name, but, just to know about: the original methods: save() and saveAssync() can't be updated with new rules ??