Skip to content

Commit f67c28e

Browse files
committed
Seperated switch case block and automated foreign key problem
1 parent 76ae5c2 commit f67c28e

2 files changed

Lines changed: 110 additions & 155 deletions

File tree

sqlitemanager/src/main/java/com/sqlitemanager/SQLiteManager.java

Lines changed: 42 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

sqlitemanager/src/main/java/com/sqlitemanager/Utils.java

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.sqlitemanager;
22

3+
import android.database.Cursor;
4+
import android.util.Log;
5+
36
import com.sqlitemanager.Annotations.Column;
7+
import com.sqlitemanager.Annotations.ForeignKey;
48
import com.sqlitemanager.Annotations.PrimaryKey;
59
import com.sqlitemanager.Annotations.TableName;
610
import com.sqlitemanager.DbPackModels.ColumnAnnotationModel;
@@ -48,21 +52,78 @@ static Class getTableClass(String tableName, ArrayList<Class> typeList) throws W
4852
throw new WrongTableNameException(TAG + " Table type not found");
4953
}
5054

51-
public static String getTableName(String className, ArrayList<Class> typeList) {
52-
//className += " ";
53-
return className;
54-
}
55-
56-
public static Field getPrimaryKeyField(Class clazz) {
55+
static Field getPrimaryKeyField(Class clazz) {
5756
Field[] fields = clazz.getFields();
5857

5958
for (Field item : fields) {
6059
if (item.isAnnotationPresent(PrimaryKey.class)) {
6160
return item;
6261
}
6362
}
64-
6563
return null;
6664
}
6765

66+
static <T extends Tableable> SqlResponse readingSwitchAction(String simpleNameOfDataType, final Field field, T tableModel, int index, Cursor cursor, AbstractDefaultCase defaultCase) {
67+
if (!field.isAnnotationPresent(Column.class)) return SqlResponse.Failed;
68+
69+
70+
try {
71+
switch (simpleNameOfDataType) {
72+
case "int":
73+
field.set(tableModel, cursor.getInt(index));
74+
break;
75+
case "String":
76+
field.set(tableModel, cursor.getString(index));
77+
break;
78+
case "double":
79+
field.set(tableModel, cursor.getDouble(index));
80+
break;
81+
case "float":
82+
field.set(tableModel, cursor.getFloat(index));
83+
break;
84+
case "short":
85+
field.set(tableModel, cursor.getShort(index));
86+
break;
87+
case "long":
88+
field.set(tableModel, cursor.getLong(index));
89+
break;
90+
case "byte":
91+
field.set(tableModel, cursor.getBlob(index));
92+
break;
93+
case "boolean":
94+
field.set(tableModel, cursor.getInt(index));
95+
break;
96+
case "Integer":
97+
field.set(tableModel, cursor.getInt(index));
98+
break;
99+
case "Double":
100+
field.set(tableModel, cursor.getDouble(index));
101+
break;
102+
case "Float":
103+
field.set(tableModel, cursor.getFloat(index));
104+
break;
105+
case "Short":
106+
field.set(tableModel, cursor.getShort(index));
107+
break;
108+
case "Long":
109+
field.set(tableModel, cursor.getLong(index));
110+
break;
111+
case "char":
112+
field.set(tableModel, cursor.getString(index));
113+
break;
114+
case "Byte":
115+
field.set(tableModel, cursor.getBlob(index));
116+
break;
117+
case "Boolean":
118+
field.set(tableModel, cursor.getInt(index));
119+
break;
120+
default:
121+
defaultCase.onDefault(field, index, cursor);
122+
}
123+
} catch (Exception e) {
124+
Log.e(TAG, e.getMessage());
125+
}
126+
return SqlResponse.Successful;
127+
}
128+
68129
}

0 commit comments

Comments
 (0)