@@ -413,7 +413,6 @@ public static <T extends Tableable> ArrayList<T> all(Class<T> modelClass) {
413413 return sqLiteManager .selectAll (Utils .getTableName (modelClass ), modelClass , null , null , null , null , null , null , null );
414414 }
415415
416-
417416 private <T extends Tableable > ArrayList <T > selectAll (String name , Class <T > modelClass , String [] args ,
418417 String condition , SortOrder sortOrder ,
419418 Integer limit , String columnWithForeignKey , String [] columns , String sortColumn ) {
@@ -448,98 +447,48 @@ private <T extends Tableable> ArrayList<T> selectAll(String name, Class<T> model
448447
449448 ArrayList <T > tableModels = new ArrayList <>();
450449
450+ Field [] fields = modelClass .getFields ();
451+
452+ Arrays .sort (fields , new Comparator <Field >() {
453+ @ Override
454+ public int compare (Field field , Field t1 ) {
455+ return field .getName ().compareTo (t1 .getName ());
456+ }
457+ });
458+
451459 if (cursor .moveToFirst ()) {
452460 do {
453- T tableModel ;
461+ final T tableModel ;
454462 try {
455463 tableModel = modelClass .newInstance ();
456464 } catch (Exception e ) {
457465 throw new RuntimeException ("Not successfully instantiated:" + " " + e .getMessage ());
458466 }
459-
460- Field [] fields = tableModel .getClass ().getFields ();
461-
462- Arrays .sort (fields , new Comparator <Field >() {
463- @ Override
464- public int compare (Field field , Field t1 ) {
465- return field .getName ().compareTo (t1 .getName ());
466- }
467- });
468467 int index = 0 ;
469468
470- String [] names = cursor .getColumnNames ();
471-
472- for (Field field : fields ) {
473- if (!field .isAnnotationPresent (Column .class )) continue ;
474- String simpleNameOfDataType = (field .isAnnotationPresent (ForeignKey .class ))
475- ? SQLiteTypes .INTEGER_NULLABLE .getJavaType () : field .getType ().getSimpleName ();
476-
477- try {
478- switch (simpleNameOfDataType ) {
479- case "int" :
480- field .set (tableModel , cursor .getInt (index ++));
481- break ;
482- case "String" :
483- field .set (tableModel , cursor .getString (index ++));
484- break ;
485- case "double" :
486- field .set (tableModel , cursor .getDouble (index ++));
487- break ;
488- case "float" :
489- field .set (tableModel , cursor .getFloat (index ++));
490- break ;
491- case "short" :
492- field .set (tableModel , cursor .getShort (index ++));
493- break ;
494- case "long" :
495- field .set (tableModel , cursor .getLong (index ++));
496- break ;
497- case "byte" :
498- field .set (tableModel , cursor .getBlob (index ++));
499- break ;
500- case "boolean" :
501- field .set (tableModel , cursor .getInt (index ++));
502- break ;
503- case "Integer" :
504- field .set (tableModel , cursor .getInt (index ++));
505- break ;
506- case "Double" :
507- field .set (tableModel , cursor .getDouble (index ++));
508- break ;
509- case "Float" :
510- field .set (tableModel , cursor .getFloat (index ++));
511- break ;
512- case "Short" :
513- field .set (tableModel , cursor .getShort (index ++));
514- break ;
515- case "Long" :
516- field .set (tableModel , cursor .getLong (index ++));
517- break ;
518- case "char" :
519- field .set (tableModel , cursor .getString (index ++));
520- break ;
521- case "Byte" :
522- field .set (tableModel , cursor .getBlob (index ++));
523- break ;
524- case "Boolean" :
525- field .set (tableModel , cursor .getInt (index ++));
526- break ;
527- default :
528- throw new UnknownDatatypeException (TAG + ": " + field .getType ().getSimpleName () + " and " + simpleNameOfDataType + " is not supported. Unknown type from Cursor!" );
469+ for (final Field field : fields ) {
470+ String simpleNameOfDataType = field .getType ().getSimpleName ();
471+
472+ SqlResponse result = Utils .readingSwitchAction (simpleNameOfDataType , field , tableModel , index , cursor , new AbstractDefaultCase () {
473+ @ Override
474+ public void onDefault (Field field , int indexx , Cursor cursorr ) {
475+ try {
476+ field .set (tableModel , find ((Tableable ) field .getType ().newInstance (), cursorr .getInt (indexx )));
477+ } catch (Exception e ) {
478+ throw new SqLiteManagerException (e .getMessage ());
479+ }
529480 }
530- } catch ( Exception e ) {
531- Log . e ( TAG , e . getMessage ()) ;
532- }
481+ });
482+ if ( result == SqlResponse . Failed ) continue ;
483+ index ++;
533484 }
534-
535485 tableModels .add (tableModel );
536486 } while (cursor .moveToNext ());
537487 }
538488 cursor .close ();
539489 return tableModels ;
540490 }
541491
542-
543492 public static SqlResponse deleteTable (String tableName ) {
544493 if (Utils .tableExist (tableName ))
545494 sqLiteManager .getWritableDatabase ().execSQL ("DROP TABLE IF EXISTS " + tableName );
@@ -650,11 +599,8 @@ public Select limit(int limit) {
650599 return this ;
651600 }
652601
653- //TODO: Complete these limit method properly
654- public Select limit (int from , int to ) {
655- this .limit = to ;
656- return this ;
657- }
602+ //Todo: Add first here. Which will return one Model
603+ //Todo: Add fill Cursor
658604
659605 public Select innerJoin (String columnName ) {
660606 this .columnWithForeignKey = columnName ;
@@ -706,7 +652,6 @@ public Builder setWillBeUpdated(boolean willBeUpdated) {
706652 }
707653 }
708654
709-
710655 public static <T extends Tableable > T find (Class <T > clazz , Integer id ) {
711656 try {
712657 return find (clazz .newInstance (), id );
@@ -740,79 +685,28 @@ static <T extends Tableable> T find(T tableModel, Integer id) {
740685 null
741686 );
742687
688+ Field [] fields = tableModel .getClass ().getFields ();
743689
744- if (cursor .moveToFirst ()) {
745-
746- Field [] fields = tableModel .getClass ().getFields ();
690+ Arrays .sort (fields , new Comparator <Field >() {
691+ @ Override
692+ public int compare (Field field , Field t1 ) {
693+ return field .getName ().compareTo (t1 .getName ());
694+ }
695+ });
747696
748- Arrays .sort (fields , new Comparator <Field >() {
749- @ Override
750- public int compare (Field field , Field t1 ) {
751- return field .getName ().compareTo (t1 .getName ());
752- }
753- });
697+ if (cursor .moveToFirst ()) {
754698 int index = 0 ;
755-
756- for (Field field : fields ) {
757- if (!field .isAnnotationPresent (Column .class )) continue ;
699+ for (final Field field : fields ) {
758700 String simpleNameOfDataType = (field .isAnnotationPresent (ForeignKey .class ))
759701 ? SQLiteTypes .INTEGER_NULLABLE .getJavaType () : field .getType ().getSimpleName ();
760- try {
761- switch (simpleNameOfDataType ) {
762- case "int" :
763- field .set (tableModel , cursor .getInt (index ++));
764- break ;
765- case "String" :
766- field .set (tableModel , cursor .getString (index ++));
767- break ;
768- case "double" :
769- field .set (tableModel , cursor .getDouble (index ++));
770- break ;
771- case "float" :
772- field .set (tableModel , cursor .getFloat (index ++));
773- break ;
774- case "short" :
775- field .set (tableModel , cursor .getShort (index ++));
776- break ;
777- case "long" :
778- field .set (tableModel , cursor .getLong (index ++));
779- break ;
780- case "byte" :
781- field .set (tableModel , cursor .getBlob (index ++));
782- break ;
783- case "boolean" :
784- field .set (tableModel , cursor .getInt (index ++));
785- break ;
786- case "Integer" :
787- field .set (tableModel , cursor .getInt (index ++));
788- break ;
789- case "Double" :
790- field .set (tableModel , cursor .getDouble (index ++));
791- break ;
792- case "Float" :
793- field .set (tableModel , cursor .getFloat (index ++));
794- break ;
795- case "Short" :
796- field .set (tableModel , cursor .getShort (index ++));
797- break ;
798- case "Long" :
799- field .set (tableModel , cursor .getLong (index ++));
800- break ;
801- case "char" :
802- field .set (tableModel , cursor .getString (index ++));
803- break ;
804- case "Byte" :
805- field .set (tableModel , cursor .getBlob (index ++));
806- break ;
807- case "Boolean" :
808- field .set (tableModel , cursor .getInt (index ++));
809- break ;
810- default :
811- throw new UnknownDatatypeException (TAG + ": " + field .getType ().getSimpleName () + " and " + simpleNameOfDataType + " is not supported. Unknown type from Cursor!" );
702+ SqlResponse response = Utils .readingSwitchAction (simpleNameOfDataType , field , tableModel , index , cursor , new AbstractDefaultCase () {
703+ @ Override
704+ public void onDefault (Field field , int indexx , Cursor cursor ) {
705+ throw new SqLiteManagerException (field .getType ().getSimpleName () + " is not supported. Unknown type from Cursor!" );
812706 }
813- } catch ( Exception e ) {
814- Log . e ( TAG , e . getMessage ()) ;
815- }
707+ });
708+ if ( response == SqlResponse . Failed ) continue ;
709+ index ++;
816710 }
817711 cursor .close ();
818712 return tableModel ;
0 commit comments