sockeqwe / sqlbrite-dao

DAO for SQLBrite
http://hannesdorfmann.com/android/sqlbrite-dao
Apache License 2.0
182 stars 22 forks source link

Generate create table script and filled ContentValues #2

Open davidbilik opened 9 years ago

davidbilik commented 9 years ago

Hi, I like your blog post about this library and I think this is a good way to go with SQLite handling in Android.

I have a question about possibilities of Annotation processing. Is it possible to generate CREATE TABLE SQL script for ObjectMappable annotated classes? It is something that could be resolved in compile time and user then dont have to write that boring code.

Also I think that huge boilerplate saver would be if Mapper could return filled ContentValues with values of object.

I dont have so much experiences with annotation processing so I dont know if there are some limitations.

Thanks for response and for great library.

sockeqwe commented 9 years ago

Hi, yes, generating the CREATE TABLE SQL statement is one of those things I had in mind to add, but I'm not sure if that is a good idea, because there are many things that may be more complex as it seems. For instance I have to add some kind of @PrimaryKey annotation and probably also some kind of @Index for creating indexes. Maybe also something like @NotNull for specifying a column as NOT NULL. So you see, basically we have to create annotations for the whole SQL CREATE TABLE syntax. But there are also some other things like inheritance that might make some things a little bit more tricky or need some restrictions like only one @PrimaryKey in the whole inheritance hierarchy for a certain class is allowed. Overall that seems to me a little bit to ORM alike and that is something I try to avoid.

However, I'm open for discussion. For instance we could add support for some simple things like @PrimaryKey and @NotNull and then the user can decides whether he wants to use this generated CREATE TABLE thing that only support a simple subset of CREATE TABLE syntax or to write the CREATE TABLE statement by hand (as seen in the current version of sqlbrite-dao).

I think that generating ContentValues from an object is a good idea. Haven't thought about it before because usually I pass the properties I want to change as parameter and not the concrete object to the DAO method, but it's possible and I think it would be a useful addition. Thanks for that!

However, as already mentioned in my blog post, I expect that square will implement also some kind of Mapper or annotation processing based solution to reduce writing boilerplace code, deal with Cursor and ContentValues. So I guess I will wait 1 or 2 more months to see if SQLBrite is going to implement first party support for that problems.

sockeqwe commented 9 years ago

something like this: MyObjectMapper.contentValues(myObject).build()

harinair commented 8 years ago

Sounds good. But you are following the builder pattern, probably you can add:

MyObjectMapper.contentValues().myObject(myObject).build()

harinair commented 8 years ago

When will this feature available? Is 0.3.0 available soon?

sockeqwe commented 8 years ago

I already wanted to release it last weekend, but didn't make it. This weekend it should be finished.

MyObjectMapper.contentValues().myObject(myObject).build() doesn't work because there could be a conflict if the class MyObject of which you want to generate the content values contains a field myObject.

Therefore the API will be MyClassMapper.contentValues(myObject).build()

harinair commented 8 years ago

Good point. Nice!

sockeqwe commented 8 years ago

Not implemented in latest 0.4.0 release in favor to add auto-value support in next release #17