Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.

Support object mapping of query result rows #1

Closed
JakeWharton opened this issue Feb 19, 2015 · 10 comments
Closed

Support object mapping of query result rows #1

JakeWharton opened this issue Feb 19, 2015 · 10 comments

Comments

@JakeWharton
Copy link
Member

Nobody wants to work with a Query and Cursor. Add support for mapping rows to objects. This should support both table-based and custom view-based objects.

@ahmedre
Copy link
Contributor

ahmedre commented Mar 1, 2015

how about something like this -

@Mapper
public interface Mapper {
    @Column.Mapper(0) long getId(Cursor c);
    @Column.Mapper(1) String getText(Cursor c);
    @Column.Mapper(2) Date getWhen(Cursor c);
}

one good thing about this is it works even for joins (since it's just a numeric position) - the unfortunate piece is that working with column numbers is problematic.

unfortunately, the alternative of this is also not great and is problematic due to the bug about not being able to use table.column in getColumnIndexOrThrow:

@Mapper
public interface Mapper {
    @Column.Mapper("notes._id") long getId(Cursor c);
    @Column.Mapper("notes.text") String getText(Cursor c);
    @Column.Mapper("todos.whence") Date getWhen(Cursor c);
}

thoughts?

@yesitskev
Copy link

In my opinion, It would be best if we could come up with a solution where the model can define the table structure, builders and mappings (preferably most of the boiler plating done by an annotation processor). As a user(developer), I don't want to have to define a ton of interfaces, builders, abstract classes just to get some models out of the database. This is one of my concerns I have as I watch this library mature through its infancy.

@AlecKazakova
Copy link
Member

I agree with Kevin that we want to reduce boilerplate as much as possible, #16 is actually a great start on that. One thing we have to keep in mind is that we need something that works for every type of SELECT, things like SELECT count(*), SELCT table1.column, table2.column FROM table1 JOIN table2, SELECT *, SELECT table1.column AS alias, which are all pretty common occurrences.

@dany-kun
Copy link

dany-kun commented May 9, 2015

Would it be possible to add an optional parameter like the following to the create method and call it later in the Query.run method?

/** Convert the query Cursor to the user model */
  public interface CursorModel<T>{
    T convertToModel(Cursor c);
  }

This would not reduce the boilerplate but just move the cursor mapping out of the observable flow/subscribe methods implementation.

@meoyawn
Copy link

meoyawn commented May 23, 2015

@ahmedre that's just perfect, now we need to generate those Mappers from SQL strings, and look for type hints: initial creation script + all the migrations or assist the IDE plugin by hand

the second option is at least achieveable

@meoyawn
Copy link

meoyawn commented May 23, 2015

@ahmedre and what's the bug with getColumnIndexOrThrow?

@ahmedre
Copy link
Contributor

ahmedre commented May 23, 2015

@adelnizamutdinov getColumnIndexOrThrow doesn't work with table.column, only with column - see http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/database/AbstractCursor.java line 275 for details.

@sockeqwe sockeqwe mentioned this issue Jun 15, 2015
@patrickhammond
Copy link

A few months ago I put together https://github.com/madebyatomicrobot/vinyl to start to address some accumulated internal project pains with Cursors and mapping rows into objects. Not sure if something like that is what you would like to see in SQLBrite.

@PerfectCarl
Copy link

There is also sqlbrite dao

@JakeWharton
Copy link
Member Author

Our solution to this is https://github.com/square/sqldelight and efforts around object mapping will be focused there.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants