Skip to content

Commit

Permalink
Merge pull request #410 from RoyMontoya/SugarRecord
Browse files Browse the repository at this point in the history
added getSugarDatabase to simplify Database calls
  • Loading branch information
sibelius committed Oct 28, 2015
2 parents ed73852 + 0585792 commit ef0445f
Showing 1 changed file with 48 additions and 66 deletions.
114 changes: 48 additions & 66 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@

public class SugarRecord {

public static final String SUGAR = "Sugar";
private Long id = null;

private static SQLiteDatabase getSugarDataBase() {
return getSugarContext().getSugarDb().getDB();
}

public static <T> int deleteAll(Class<T> type) {
return deleteAll(type, null);
}

public static <T> int deleteAll(Class<T> type, String whereClause, String... whereArgs) {
SugarDb db = getSugarContext().getSugarDb();
SQLiteDatabase sqLiteDatabase = db.getDB();
return sqLiteDatabase.delete(NamingHelper.toSQLName(type), whereClause, whereArgs);
return getSugarDataBase().delete(NamingHelper.toSQLName(type), whereClause, whereArgs);
}

@SuppressWarnings("deprecation")
Expand All @@ -46,7 +49,7 @@ public static <T> void saveInTx(T... objects) {

@SuppressWarnings("deprecation")
public static <T> void saveInTx(Collection<T> objects) {
SQLiteDatabase sqLiteDatabase = getSugarContext().getSugarDb().getDB();
SQLiteDatabase sqLiteDatabase = getSugarDataBase();
try {
sqLiteDatabase.beginTransaction();
sqLiteDatabase.setLockingEnabled(false);
Expand All @@ -55,7 +58,7 @@ public static <T> void saveInTx(Collection<T> objects) {
}
sqLiteDatabase.setTransactionSuccessful();
} catch (Exception e) {
Log.i("Sugar", "Error in saving in transaction " + e.getMessage());
Log.i(SUGAR, "Error in saving in transaction " + e.getMessage());
} finally {
sqLiteDatabase.endTransaction();
sqLiteDatabase.setLockingEnabled(true);
Expand All @@ -69,7 +72,7 @@ public static <T> int deleteInTx(T... objects) {

@SuppressWarnings("deprecation")
public static <T> int deleteInTx(Collection<T> objects) {
SQLiteDatabase sqLiteDatabase = getSugarContext().getSugarDb().getDB();
SQLiteDatabase sqLiteDatabase = getSugarDataBase();
int deletedRows = 0;
try {
sqLiteDatabase.beginTransaction();
Expand All @@ -82,7 +85,7 @@ public static <T> int deleteInTx(Collection<T> objects) {
sqLiteDatabase.setTransactionSuccessful();
} catch (Exception e) {
deletedRows = 0;
Log.i("Sugar", "Error in deleting in transaction " + e.getMessage());
Log.i(SUGAR, "Error in deleting in transaction " + e.getMessage());
} finally {
sqLiteDatabase.endTransaction();
sqLiteDatabase.setLockingEnabled(true);
Expand Down Expand Up @@ -140,69 +143,53 @@ public static <T> Iterator<T> findAsIterator(Class<T> type, String whereClause,
}

public static <T> Iterator<T> findWithQueryAsIterator(Class<T> type, String query, String... arguments) {
SugarDb db = getSugarContext().getSugarDb();
SQLiteDatabase sqLiteDatabase = db.getDB();
Cursor c = sqLiteDatabase.rawQuery(query, arguments);
return new CursorIterator<T>(type, c);
Cursor cursor = getSugarDataBase().rawQuery(query, arguments);
return new CursorIterator<T>(type, cursor);
}

public static <T> Iterator<T> findAsIterator(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
SugarDb db = getSugarContext().getSugarDb();
SQLiteDatabase sqLiteDatabase = db.getDB();
Cursor c = sqLiteDatabase.query(NamingHelper.toSQLName(type), null, whereClause, whereArgs,
Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs,
groupBy, null, orderBy, limit);
return new CursorIterator<T>(type, c);
return new CursorIterator<T>(type, cursor);
}

public static <T> List<T> find(Class<T> type, String whereClause, String... whereArgs) {
return find(type, whereClause, whereArgs, null, null, null);
}

public static <T> List<T> findWithQuery(Class<T> type, String query, String... arguments) {
SugarDb db = getSugarContext().getSugarDb();
SQLiteDatabase sqLiteDatabase = db.getDB();
T entity;
List<T> toRet = new ArrayList<T>();
Cursor c = sqLiteDatabase.rawQuery(query, arguments);
Cursor cursor = getSugarDataBase().rawQuery(query, arguments);

try {
while (c.moveToNext()) {
entity = type.getDeclaredConstructor().newInstance();
inflate(c, entity, getSugarContext().getEntitiesMap());
toRet.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
c.close();
}

return toRet;
return getEntitiesFromCursor(cursor, type);
}

public static void executeQuery(String query, String... arguments) {
getSugarContext().getSugarDb().getDB().execSQL(query, arguments);
getSugarDataBase().execSQL(query, arguments);
}

public static <T> List<T> find(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
SugarDb db = getSugarContext().getSugarDb();
SQLiteDatabase sqLiteDatabase = db.getDB();
T entity;
List<T> toRet = new ArrayList<T>();
Cursor c = sqLiteDatabase.query(NamingHelper.toSQLName(type), null, whereClause, whereArgs,
Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs,
groupBy, null, orderBy, limit);

return getEntitiesFromCursor(cursor, type);
}

public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type){
T entity;
List<T> result = new ArrayList<T>();
try {
while (c.moveToNext()) {
while (cursor.moveToNext()) {
entity = type.getDeclaredConstructor().newInstance();
inflate(c, entity, getSugarContext().getEntitiesMap());
toRet.add(entity);
inflate(cursor, entity, getSugarContext().getEntitiesMap());
result.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
c.close();
cursor.close();
}
return toRet;

return result;
}

public static <T> long count(Class<?> type) {
Expand All @@ -214,17 +201,14 @@ public static <T> long count(Class<?> type, String whereClause, String[] whereAr
}

public static <T> long count(Class<?> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
SugarDb db = getSugarContext().getSugarDb();
SQLiteDatabase sqLiteDatabase = db.getDB();

long toRet = -1;
long result = -1;
String filter = (!TextUtils.isEmpty(whereClause)) ? " where " + whereClause : "";
SQLiteStatement sqliteStatement;
try {
sqliteStatement = sqLiteDatabase.compileStatement("SELECT count(*) FROM " + NamingHelper.toSQLName(type) + filter);
sqliteStatement = getSugarDataBase().compileStatement("SELECT count(*) FROM " + NamingHelper.toSQLName(type) + filter);
} catch (SQLiteException e) {
e.printStackTrace();
return toRet;
return result;
}

if (whereArgs != null) {
Expand All @@ -234,16 +218,16 @@ public static <T> long count(Class<?> type, String whereClause, String[] whereAr
}

try {
toRet = sqliteStatement.simpleQueryForLong();
result = sqliteStatement.simpleQueryForLong();
} finally {
sqliteStatement.close();
}

return toRet;
return result;
}

public static long save(Object object) {
return save(getSugarContext().getSugarDb().getDB(), object);
return save(getSugarDataBase(), object);
}

static long save(SQLiteDatabase db, Object object) {
Expand Down Expand Up @@ -281,7 +265,7 @@ static long save(SQLiteDatabase db, Object object) {
((SugarRecord) object).setId(id);
}

Log.i("Sugar", object.getClass().getSimpleName() + " saved : " + id);
Log.i(SUGAR, object.getClass().getSimpleName() + " saved : " + id);

return id;
}
Expand Down Expand Up @@ -316,11 +300,10 @@ public boolean delete() {
Long id = getId();
Class<?> type = getClass();
if (id != null && id > 0L) {
SQLiteDatabase db = getSugarContext().getSugarDb().getDB();
Log.i("Sugar", type.getSimpleName() + " deleted : " + id);
return db.delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1;
Log.i(SUGAR, type.getSimpleName() + " deleted : " + id);
return getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1;
} else {
Log.i("Sugar", "Cannot delete object: " + type.getSimpleName() + " - object has not been saved");
Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved");
return false;
}
}
Expand All @@ -333,31 +316,30 @@ public static boolean delete(Object object) {
field.setAccessible(true);
Long id = (Long) field.get(object);
if (id != null && id > 0L) {
SQLiteDatabase db = getSugarContext().getSugarDb().getDB();
boolean deleted = db.delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1;
Log.i("Sugar", type.getSimpleName() + " deleted : " + id);
boolean deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1;
Log.i(SUGAR, type.getSimpleName() + " deleted : " + id);
return deleted;
} else {
Log.i("Sugar", "Cannot delete object: " + object.getClass().getSimpleName() + " - object has not been saved");
Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - object has not been saved");
return false;
}
} catch (NoSuchFieldException e) {
Log.i("Sugar", "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id");
Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id");
return false;
} catch (IllegalAccessException e) {
Log.i("Sugar", "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access id");
Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access id");
return false;
}
} else if (SugarRecord.class.isAssignableFrom(type)) {
return ((SugarRecord) object).delete();
} else {
Log.i("Sugar", "Cannot delete object: " + object.getClass().getSimpleName() + " - not persisted");
Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - not persisted");
return false;
}
}

public long save() {
return save(getSugarContext().getSugarDb().getDB(), this);
return save(getSugarDataBase(), this);
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit ef0445f

Please sign in to comment.