Skip to content

Commit 085f0db

Browse files
committed
Seperated 'getCursor()' from 'getList()' in QueryBuilder
1 parent c677de4 commit 085f0db

2 files changed

Lines changed: 107 additions & 17 deletions

File tree

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

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public <T extends Tableable> SqlResponse delete(T tableModel) {
384384
return SqlResponse.Successful;
385385
}
386386

387-
//TODO: Complete this
387+
//TODO: Make it compatible with several foreign key!
388388
private String getStringForeignKey(String columnWithForeignKey, String mainTableName) {
389389
String refColName = "";
390390
String refTableName = "";
@@ -406,20 +406,18 @@ private String getStringForeignKey(String columnWithForeignKey, String mainTable
406406

407407
@SuppressWarnings("unchecked")
408408
public static <T extends Tableable> ArrayList<T> all(String tableName) {
409-
return sqLiteManager.selectAll(tableName, Utils.getTableClass(tableName, sqLiteManager.tableTypesList), null, null, null, null, null, null, null);
409+
return sqLiteManager.selectMany(tableName, Utils.getTableClass(tableName, sqLiteManager.tableTypesList), null, null, null, null, null, null);
410410
}
411411

412412
public static <T extends Tableable> ArrayList<T> all(Class<T> modelClass) {
413-
return sqLiteManager.selectAll(Utils.getTableName(modelClass), modelClass, null, null, null, null, null, null, null);
413+
return sqLiteManager.selectMany(Utils.getTableName(modelClass), modelClass, null, null, null, null, null, null);
414414
}
415415

416-
private <T extends Tableable> ArrayList<T> selectAll(String name, Class<T> modelClass, String[] args,
417-
String condition, SortOrder sortOrder,
418-
Integer limit, String columnWithForeignKey, String[] columns, String sortColumn) {
419-
416+
private Cursor selectManyCursor(String name, String[] args,
417+
String condition, SortOrder sortOrder,
418+
Integer limit, String columnWithForeignKey, String[] columns, String sortColumn) {
420419

421420
SQLiteDatabase sqLiteDatabase = sqLiteManager.getReadableDatabase();
422-
423421
String foreignKey = (columnWithForeignKey != null && !columnWithForeignKey.isEmpty()) ? getStringForeignKey(columnWithForeignKey, name) : "";
424422
String[] projectionn;
425423

@@ -444,6 +442,47 @@ private <T extends Tableable> ArrayList<T> selectAll(String name, Class<T> model
444442
sortText,
445443
strLimit
446444
);
445+
return cursor;
446+
}
447+
448+
private Cursor selectManyModelCursor(String name, String[] args,
449+
String condition, SortOrder sortOrder,
450+
Integer limit, String[] columns, String sortColumn) {
451+
452+
SQLiteDatabase sqLiteDatabase = sqLiteManager.getReadableDatabase();
453+
String[] projectionn;
454+
455+
if (columns == null) projectionn = null;
456+
else projectionn = getColumnsForSelect(sqLiteDatabase, name, columns);
457+
458+
String sortText = null;
459+
460+
if (sortColumn != null)
461+
sortText = sortColumn + " " + sortOrder.getKeyWord();
462+
String strLimit = (limit == null) ? null : limit.toString();
463+
464+
Cursor cursor;
465+
466+
cursor = sqLiteDatabase.query(
467+
name,
468+
projectionn,
469+
condition,
470+
args,
471+
null,
472+
null,
473+
sortText,
474+
strLimit
475+
);
476+
return cursor;
477+
}
478+
479+
private <T extends Tableable> ArrayList<T> selectMany(String name, Class<T> modelClass, String[] args,
480+
String condition, SortOrder sortOrder,
481+
Integer limit,
482+
String[] columns, String sortColumn) {
483+
484+
Cursor cursor = selectManyModelCursor(name, args, condition,
485+
sortOrder, limit, columns, sortColumn);
447486

448487
ArrayList<T> tableModels = new ArrayList<>();
449488

@@ -567,11 +606,17 @@ public Select(String tableName) {
567606

568607
public <T extends Tableable> Select(Class<T> tableClass) {
569608
this.tableClass = tableClass;
609+
this.tableName = Utils.getTableName(tableClass);
570610
}
571611

572612
@SuppressWarnings("unchecked")
573-
public <T extends Tableable> ArrayList<T> get() {
574-
return sqLiteManager.selectAll(tableName, tableClass, args,
613+
public <T extends Tableable> List<T> getList() {
614+
return sqLiteManager.selectMany(tableName, tableClass, args,
615+
condition, sortOrder, limit, columns, sortColumn);
616+
}
617+
618+
public Cursor getCursor() {
619+
return sqLiteManager.selectManyCursor(tableName, args,
575620
condition, sortOrder, limit, columnWithForeignKey, columns, sortColumn);
576621
}
577622

@@ -585,6 +630,10 @@ public Select where(String condition, String... args) {
585630
public Select sort(String sortColumn, SortOrder order) {
586631
this.sortOrder = order;
587632
this.sortColumn = sortColumn;
633+
if (SortOrder.RANDOM == order) {
634+
this.sortColumn = "";
635+
}
636+
588637
return this;
589638
}
590639

@@ -599,9 +648,10 @@ public Select limit(int limit) {
599648
return this;
600649
}
601650

602-
//Todo: Add first here. Which will return one Model
603-
//Todo: Add fill Cursor
651+
//Todo: Add 'first()' here. Which will return one Model
652+
604653

654+
//TODO: Make it compatible with several foreign key!
605655
public Select innerJoin(String columnName) {
606656
this.columnWithForeignKey = columnName;
607657
return this;
@@ -652,6 +702,39 @@ public Builder setWillBeUpdated(boolean willBeUpdated) {
652702
}
653703
}
654704

705+
706+
private static <T extends Tableable> int update(T tableModel) {
707+
SQLiteDatabase writable = sqLiteManager.getWritableDatabase();
708+
String whereClause = null;
709+
String[] whereArgs = null;
710+
711+
Field[] fields = tableModel.getClass().getFields();
712+
ContentValues contentValues = new ContentValues();
713+
714+
for (Field field : fields) {
715+
String colName;
716+
if (Utils.isColumn(field)) colName = Utils.getMemberColumnName(field);
717+
else continue;
718+
719+
try {
720+
if (field.isAnnotationPresent(PrimaryKey.class)) {
721+
whereClause = Utils.getMemberColumnName(field) + "=?";
722+
whereArgs = new String[]{field.get(tableModel).toString()};
723+
}
724+
725+
contentValues.put(colName, field.get(tableModel).toString());
726+
} catch (Exception e) {
727+
throw new SqLiteManagerException(e.getMessage());
728+
}
729+
}
730+
if (whereClause == null) {
731+
Log.e(TAG, "No primary key value found in object!");
732+
return -1;
733+
}
734+
return writable.updateWithOnConflict(Utils.getTableName(tableModel.getClass()), contentValues, whereClause, whereArgs, SQLiteDatabase.CONFLICT_IGNORE);
735+
}
736+
737+
655738
public static <T extends Tableable> T find(Class<T> clazz, Integer id) {
656739
try {
657740
return find(clazz.newInstance(), id);
@@ -669,7 +752,7 @@ public static <T extends Tableable> T find(String tableName, Integer id) {
669752
}
670753
}
671754

672-
static <T extends Tableable> T find(T tableModel, Integer id) {
755+
static <T extends Tableable> T find(final T tableModel, Integer id) {
673756
Cursor cursor;
674757
SQLiteDatabase sqLiteDatabase = sqLiteManager.getReadableDatabase();
675758
String name = Utils.getTableName(tableModel.getClass());
@@ -697,12 +780,17 @@ public int compare(Field field, Field t1) {
697780
if (cursor.moveToFirst()) {
698781
int index = 0;
699782
for (final Field field : fields) {
700-
String simpleNameOfDataType = (field.isAnnotationPresent(ForeignKey.class))
701-
? SQLiteTypes.INTEGER_NULLABLE.getJavaType() : field.getType().getSimpleName();
783+
String simpleNameOfDataType = field.getType().getSimpleName();
784+
702785
SqlResponse response = Utils.readingSwitchAction(simpleNameOfDataType, field, tableModel, index, cursor, new AbstractDefaultCase() {
703786
@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!");
787+
public void onDefault(Field field, int indexx, Cursor cursorr) {
788+
789+
try {
790+
field.set(tableModel, find((Tableable) field.getType().newInstance(), cursorr.getInt(indexx)));
791+
} catch (Exception e) {
792+
throw new SqLiteManagerException(e.getMessage());
793+
}
706794
}
707795
});
708796
if (response == SqlResponse.Failed) continue;

sqlitemanager/src/main/java/com/sqlitemanager/SQLiteTypes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
public enum SQLiteTypes {
8+
9+
//Todo: remove needless and unused code
810
INTEGER("int", "INTEGER", int.class),
911
INTEGER_NULLABLE("Integer", "INTEGER", Integer.class),
1012
STRING("String", "TEXT", String.class),

0 commit comments

Comments
 (0)