synchronized JSONObject fetchEvents ( Table table , final int limit ) { final String tName = table . getName ( ) ; Cursor cursor = null ; String lastId = null ; final JSONArray events = new JSONArray ( ) ; //noinspection TryFinallyCanBeTryWithResources try { final SQLiteDatabase db = dbHelper . getReadableDatabase ( ) ; cursor = db . rawQuery ( "SELECT * FROM " + tName + " ORDER BY " + KEY_CREATED_AT + " ASC LIMIT " + limit , null ) ; while ( cursor . moveToNext ( ) ) { if ( cursor . isLast ( ) ) { lastId = cursor . getString ( cursor . getColumnIndex ( "_id" ) ) ; } try { final JSONObject j = new JSONObject ( cursor . getString ( cursor . getColumnIndex ( KEY_DATA ) ) ) ; events . put ( j ) ; } catch ( final JSONException e ) { // Ignore } } } catch ( final SQLiteException e ) { getConfigLogger ( ) . verbose ( "Could not fetch records out of database " + tName + "." , e ) ; lastId = null ; } finally { dbHelper . close ( ) ; if ( cursor != null ) { cursor . close ( ) ; } } if ( lastId != null ) { try { final JSONObject ret = new JSONObject ( ) ; ret . put ( lastId , events ) ; return ret ; } catch ( JSONException e ) { // ignore } } return null ; }
Returns a JSONObject keyed with the lastId retrieved and a value of a JSONArray of the retrieved JSONObject events