Skip to content

Commit e4b06f2

Browse files
committed
Added static delete method to lib
1 parent b95895a commit e4b06f2

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

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

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ private Cursor selectManyModelCursor(String name, String[] args,
463463

464464
if (sortColumn != null)
465465
sortText = sortColumn + " " + sortOrder.getKeyWord();
466-
String strLimit = (limit == null) ? null : limit.toString();
466+
String strLimit = (limit == null || limit == -1) ? null : limit.toString();
467467

468468
Cursor cursor;
469469

@@ -691,8 +691,10 @@ public Builder setDBName(String databaseName) {
691691
}
692692

693693
public SQLiteManager buildDatabase() {
694-
if (sqLiteManager != null)
695-
throw new RuntimeException("SQLiteManager has already been initialized once");
694+
if (sqLiteManager != null) {
695+
Log.e(TAG, "SQLiteManager has already been initialized once");
696+
return sqLiteManager;
697+
}
696698
sqLiteManager = new SQLiteManager(this);
697699
return sqLiteManager;
698700
}
@@ -723,8 +725,9 @@ static <T extends Tableable> int update(T tableModel) {
723725
if (idValue == 0) return -1;
724726
whereArgs = new String[]{String.format(Locale.getDefault(), "%d", idValue)};
725727
}
726-
727-
contentValues.put(colName, field.get(tableModel).toString());
728+
Object o = field.get(tableModel);
729+
if (o == null) continue;
730+
contentValues.put(colName, o.toString());
728731
} catch (Exception e) {
729732
throw new SqLiteManagerException(e.getMessage());
730733
}
@@ -786,7 +789,6 @@ public int compare(Field field, Field t1) {
786789
long response = Utils.readingSwitchAction(simpleNameOfDataType, field, tableModel, index, cursor, new AbstractDefaultCase() {
787790
@Override
788791
public void onDefault(Field field, int indexx, Cursor cursorr) {
789-
790792
try {
791793
field.set(tableModel, find((Tableable) field.getType().newInstance(), cursorr.getInt(indexx)));
792794
} catch (Exception e) {
@@ -801,7 +803,7 @@ public void onDefault(Field field, int indexx, Cursor cursorr) {
801803
return tableModel;
802804
}
803805
cursor.close();
804-
return tableModel;
806+
return null;
805807
}
806808

807809
static <T extends Tableable> long delete(T tableModel) {
@@ -818,4 +820,34 @@ static <T extends Tableable> long delete(T tableModel) {
818820
}
819821
return result;
820822
}
823+
824+
public static long delete(String tableName, Integer id) {
825+
826+
Field field = Utils.getPrimaryKeyField(Utils.getTableClass(tableName, sqLiteManager.tableTypesList));
827+
if (field == null)
828+
throw new SqLiteManagerException("No primary key found in table " + tableName.getClass().getSimpleName());
829+
String prmrKeyFieldName = field.getName();
830+
long result;
831+
try {
832+
result = sqLiteManager.getWritableDatabase().delete(tableName, prmrKeyFieldName + "=?", new String[]{id.toString()});
833+
} catch (Exception e) {
834+
throw new SqLiteManagerException(e.getMessage());
835+
}
836+
return result;
837+
}
838+
839+
public static boolean exists(String tableName, Integer id) {
840+
String columnName = Utils.getMemberColumnName(Utils.getPrimaryKeyField(Utils.getTableClass(tableName, sqLiteManager.tableTypesList)));
841+
Cursor cursor = sqLiteManager.getReadableDatabase().rawQuery("SELECT * FROM " + tableName + " WHERE " +
842+
columnName + " = ?", new String[]{id.toString()});
843+
844+
int result = 0;
845+
if (cursor.moveToFirst())
846+
result = cursor.getInt(cursor.getColumnIndex(columnName));
847+
848+
cursor.close();
849+
850+
return result > 0;
851+
}
852+
821853
}

0 commit comments

Comments
 (0)