3

Since ORMLite is already mapping an Android Cursor to a POJO. I'm wondering if I could somehow leverage that instead of writing a copy constructor and manually map a Cursor to my POJO?

I could for example, use a content provider and map that result into the same POJO's backed by ORMLite's Dao. Perform operations on my dataset and then batch insert that data into my database using ORMLite. The advantage to this would be that I don't have to write all the CRUD ofc.

EDIT:

this is a fabricated example but the concept is here. What would be the best way to do something like this?

@DatabaseTable(tableName = "my_dict")
public class MyDictionary{
    @DatabaseField(columnName = COLUMN_ID, generatedId = true)
    Integer _ID;
    @DatabaseField(columnName = "word")
    String word;
    @DatabaseField(columnName = "app_id")
    String app_id;
    @DatabaseField(columnName = "frequency")
    Integer frequency;
    @DatabaseField(columnName = "locale")
    String locale;

    public MyDictionary(){}     
}

public void someCoolDatasetOperation(){

    Dao<MyDictionary, Long> dao = databaseHelper.getDao(MyDictionary.class);

    List<MyDictionary> inDb = dao.queryForAll();

    Cursor dictCursor = getContentResolver().query(WEB_SERVICE_URI,
            null,                        // The columns to return for each row
            null,                    // Selection criteria
            null,                     // Selection criteria
            null);                        // The sort order for the returned rows


    //iffy part don't know the correct/best way to do this?????
    AndroidDatabaseResults fabricatedResult = new AndroidDatabaseResults(dictCursor, null);     
    List<MyDictionary> fromContentProvider = dao.mapSelectStarRow(dictCursor);

    //Do Something with the lists
}
ejohansson
  • 2,832
  • 1
  • 23
  • 30
  • I don't quite understand the question. Can you provide some more details? – Gray Jan 04 '14 at 00:40
  • I'll rephrase my question. – ejohansson Jan 04 '14 at 05:49
  • I still don't get it. Why can't you use ORMLite directly as opposed to your content resolver? Is it that you think the `Cursor` is not visible because it can be extracted from the results. – Gray Jan 04 '14 at 19:10
  • I updated the code, because the cursor might be a result form a webservice and not a SQLite DB. So instead of writing constructor like `MyDictionary(Cursor c)` for each object in my database. My thought was since OrmLite already maps a cursor to a list of objects I could just pass it the cursor and have it give me the objects. – ejohansson Jan 04 '14 at 20:09

1 Answers1

0

I don't know a whole lot about ORMLite and I'm not positive the two will get along, but I'm working on library that can generate a POJO wrapper for cursor using an annotated class like you have there.

So if I were to try and use this library with your above example it might look like this:

@Entity
public class MyDictionary{
    @Attribute(sqliteName = COLUMN_ID, primaryKey = true, autoIncrement = true)
    Integer _ID;
    @Attribute(sqliteName = "word")
    String word;
    @Attribute(sqliteName = "app_id")
    String app_id;
    @Attribute(sqliteName = "frequency")
    Integer frequency;
    @Attribute(sqliteName = "locale")
    String locale;
}

public void someCoolDatasetOperation(){

    Dao<MyDictionary, Long> dao = databaseHelper.getDao(MyDictionary.class);

    List<MyDictionary> inDb = dao.queryForAll();

    Cursor dictCursor = getContentResolver().query(WEB_SERVICE_URI,
            null,                    // The columns to return for each row
            null,                    // Selection criteria
            null,                    // Selection criteria
            null);                   // The sort order for the returned rows

    List<MyDictionary> fromContentProvider = new ArrayList<MyDictionary();

    while(dictCursor.moveToNext()) {
        //MyDictionaryEntity is a generated class that contains getters and 
        //setters for all the attributes in MyDictionary
        fromContentProvider.add(new MyDictionaryEntity(dictCursor));
    }
}

I'm still actively developing it and haven't got around to too much documentation yet, but if you wanted to check it out: https://github.com/adecker89/Glowplug

AJD
  • 1,017
  • 1
  • 9
  • 14
  • Thanks, but I'm trying to Avoid passing the cursor to a constructor. I was thinking that since OrmLite already maps it to a list of objects I would not have to. And on a side note OrmLite already does what you are trying to accomplish, beware of using annotations on android see: http://stackoverflow.com/questions/7417426/why-are-annotations-under-android-such-a-performance-issue-slow – ejohansson Jan 09 '14 at 00:25
  • 1
    If you want a list of objects derived from a cursor you're going to be allocating each one. The generated helper class is only a wrapper for the cursor and is very small. The goal of the library is actually to be a helper when not using ORM and to make some querying and working with cursors easier. I thought this one use case might be helpful to you. – AJD Jan 09 '14 at 15:55