Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting refactoring #731

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions library/src/main/java/com/orm/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.orm.annotation.MultiUnique;
import com.orm.annotation.NotNull;
import com.orm.annotation.Unique;
import com.orm.dsl.BuildConfig;
import com.orm.helper.ManifestHelper;
import com.orm.util.KeyWordUtil;
import com.orm.util.MigrationFileParser;
Expand Down Expand Up @@ -56,7 +55,6 @@ public void createDatabase(SQLiteDatabase sqLiteDatabase) {
public void afterTableCreated(Class<?> table, SQLiteDatabase sqLiteDatabase) {
String fileName = table.getSimpleName() + ".sql";
executeScript(sqLiteDatabase,"sugar_after_create/" ,fileName);

}

public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
Expand Down Expand Up @@ -102,7 +100,7 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe
List<String> files = Arrays.asList(getAssets().list("sugar_upgrades"));
Collections.sort(files, new NumberComparator());
for (String file : files) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "filename : " + file);
}

Expand All @@ -114,14 +112,14 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe
isSuccess = true;
}
} catch (NumberFormatException e) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "not a sugar script. ignored." + file);
}
}

}
} catch (IOException e) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.e(SUGAR, e.getMessage());
}
}
Expand All @@ -139,8 +137,8 @@ private void executeScript(SQLiteDatabase db,String path ,String file) {
sb.append(line);
}
MigrationFileParser migrationFileParser = new MigrationFileParser(sb.toString());
for(String statement: migrationFileParser.getStatements()){
if(ManifestHelper.isDebugEnabled()) {
for (String statement: migrationFileParser.getStatements()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i("Sugar script", statement);
}
if (!statement.isEmpty()) {
Expand All @@ -149,12 +147,12 @@ private void executeScript(SQLiteDatabase db,String path ,String file) {
}

} catch (IOException e) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.e(SUGAR, e.getMessage());
}
}

if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Script executed");
}
}
Expand Down Expand Up @@ -193,22 +191,22 @@ private void addColumns(Class<?> table, SQLiteDatabase sqLiteDatabase) {
}

for (String command : alterCommands) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i("Sugar", command);
}
sqLiteDatabase.execSQL(command);
}
}

protected String createTableSQL(Class<?> table) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Create table if not exists");
}
List<Field> fields = ReflectionUtil.getTableFields(table);
String tableName = NamingHelper.toTableName(table);

if(KeyWordUtil.isKeyword(tableName)) {
if(ManifestHelper.isDebugEnabled()) {
if (KeyWordUtil.isKeyword(tableName)) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "ERROR, SQLITE RESERVED WORD USED IN " + tableName);
}
}
Expand Down Expand Up @@ -265,11 +263,11 @@ protected String createTableSQL(Class<?> table) {
sb.append(", UNIQUE(");

String[] constraintFields = constraint.split(",");
for(int i = 0; i < constraintFields.length; i++) {
for (int i = 0; i < constraintFields.length; i++) {
String columnName = NamingHelper.toSQLNameDefault(constraintFields[i]);
sb.append(columnName);

if(i < (constraintFields.length -1)) {
if (i < (constraintFields.length -1)) {
sb.append(",");
}
}
Expand All @@ -278,7 +276,7 @@ protected String createTableSQL(Class<?> table) {
}

sb.append(" ) ");
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Creating table " + tableName);
}

Expand Down
9 changes: 4 additions & 5 deletions library/src/main/java/com/orm/SugarDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.orm.dsl.BuildConfig;
import com.orm.helper.ManifestHelper;
import com.orm.util.SugarCursorFactory;

Expand Down Expand Up @@ -63,7 +62,7 @@ public synchronized SQLiteDatabase getDB() {

@Override
public synchronized SQLiteDatabase getReadableDatabase() {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.d(LOG_TAG, "getReadableDatabase");
}
openedConnections++;
Expand All @@ -72,12 +71,12 @@ public synchronized SQLiteDatabase getReadableDatabase() {

@Override
public synchronized void close() {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.d(LOG_TAG, "getReadableDatabase");
}
openedConnections--;
if(openedConnections == 0) {
if(ManifestHelper.isDebugEnabled()) {
if (openedConnections == 0) {
if (ManifestHelper.isDebugEnabled()) {
Log.d(LOG_TAG, "closing");
}
super.close();
Expand Down
37 changes: 16 additions & 21 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static <T> int deleteInTx(Collection<T> objects) {
sqLiteDatabase.setTransactionSuccessful();
} catch (Exception e) {
deletedRows = 0;
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Error in deleting in transaction " + e.getMessage());
}
} finally {
Expand All @@ -134,7 +134,9 @@ public static <T> List<T> listAll(Class<T> type, String orderBy) {

public static <T> T findById(Class<T> type, Long id) {
List<T> list = find(type, "id=?", new String[]{String.valueOf(id)}, null, null, "1");
if (list.isEmpty()) return null;
if (list.isEmpty()) {
return null;
}
return list.get(0);
}

Expand Down Expand Up @@ -190,7 +192,6 @@ public static <T> List<T> find(Class<T> type, String whereClause, String... wher

public static <T> List<T> findWithQuery(Class<T> type, String query, String... arguments) {
Cursor cursor = getSugarDataBase().rawQuery(query, arguments);

return getEntitiesFromCursor(cursor, type);
}

Expand All @@ -199,7 +200,6 @@ public static void executeQuery(String query, String... arguments) {
}

public static <T> List<T> find(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {

String args[];
args = (whereArgs == null) ? null : replaceArgs(whereArgs);

Expand All @@ -219,11 +219,11 @@ public static <T> List<T> findOneToMany(Class<T> type, String relationFieldName,
return getEntitiesFromCursor(cursor, type, relationFieldName, relationObject);
}

public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type){
public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type) {
return getEntitiesFromCursor(cursor, type, null, null);
}

public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type, String relationFieldName, Object relationObject){
public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type, String relationFieldName, Object relationObject) {
T entity;
List<T> result = new ArrayList<>();
try {
Expand Down Expand Up @@ -370,7 +370,7 @@ static long update(SQLiteDatabase db, Object object) {
List<String> whereArgs = new ArrayList<>();

for (Field column : columns) {
if(column.isAnnotationPresent(Unique.class)) {
if (column.isAnnotationPresent(Unique.class)) {
try {
column.setAccessible(true);
String columnName = NamingHelper.toColumnName(column);
Expand Down Expand Up @@ -407,12 +407,12 @@ public boolean delete() {
Long id = getId();
Class<?> type = getClass();
if (id != null && id > 0L) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, type.getSimpleName() + " deleted : " + id);
}
return getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1;
} else {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved");
}
return false;
Expand All @@ -428,31 +428,31 @@ public static boolean delete(Object object) {
Long id = (Long) field.get(object);
if (id != null && id > 0L) {
boolean deleted = getSugarDataBase().delete(NamingHelper.toTableName(type), "Id=?", new String[]{id.toString()}) == 1;
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, type.getSimpleName() + " deleted : " + id);
}
return deleted;
} else {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - object has not been saved");
}
return false;
}
} catch (NoSuchFieldException e) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id");
}
return false;
} catch (IllegalAccessException e) {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
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 {
if(ManifestHelper.isDebugEnabled()) {
if (ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - not persisted");
}
return false;
Expand Down Expand Up @@ -534,17 +534,12 @@ public void remove() {
}
}

public static String[] replaceArgs(String[] args){

public static String[] replaceArgs(String[] args) {
String [] replace = new String[args.length];
for (int i=0; i<args.length; i++){

for (int i=0; i<args.length; i++) {
replace[i]= (args[i].equals("true")) ? replace[i]="1" : (args[i].equals("false")) ? replace[i]="0" : args[i];

}

return replace;

}

}
2 changes: 1 addition & 1 deletion library/src/main/java/com/orm/helper/MultiDexHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static List<String> getSourcePaths() throws PackageManager.NameNotFoundEx
sourcePaths.add(applicationInfo.sourceDir); //add the default apk path

if (instantRunDir.exists()) { //check if app using instant run
for(final File dexFile : instantRunDir.listFiles()) { //add all sources from instan-run
for (final File dexFile : instantRunDir.listFiles()) { //add all sources from instant-run
sourcePaths.add(dexFile.getAbsolutePath());
}
}
Expand Down
3 changes: 1 addition & 2 deletions library/src/main/java/com/orm/query/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public Select<T> where(String whereClause) {
}

public Select<T> where(Condition... condition) {

mergeConditions(condition, Condition.Type.AND);

return this;
}

Expand Down Expand Up @@ -130,6 +128,7 @@ public Select<T> where(String whereClause, String[] args) {
public Cursor getCursor() {
return SugarRecord.getCursor(record, whereClause, arguments, groupBy, orderBy, limit);
}

public List<T> list() {
if (arguments == null) {
arguments = convertArgs(args);
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/java/com/orm/util/MigrationFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class MigrationFileParser {
/**
* @param content
*/
public MigrationFileParser(String content){
public MigrationFileParser(String content) {
this.content = content.replaceAll("(\\/\\*([\\s\\S]*?)\\*\\/)|(--(.)*)|(\n)","");
}

public String[] getStatements(){
public String[] getStatements() {
return this.content.split(";");
}

Expand Down
6 changes: 4 additions & 2 deletions library/src/main/java/com/orm/util/NumberComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ protected int compareRight(String a, String b) {
bias = -1;
}
} else if (ca > cb) {
if (bias == 0)
if (bias == 0) {
bias = 1;
} else if ((ca == 0) && (cb == 0))
}
} else if ((ca == 0) && (cb == 0)) {
return bias;
}
ia++;
ib++;
}
Expand Down
18 changes: 13 additions & 5 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ private ReflectionUtil() { }

public static List<Field> getTableFields(Class table) {
List<Field> fieldList = SugarConfig.getFields(table);
if (fieldList != null) return fieldList;
if (fieldList != null) {
return fieldList;
}

if (ManifestHelper.isDebugEnabled()) {
Log.d("Sugar", "Fetching properties");
Expand Down Expand Up @@ -78,7 +80,7 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj
try {
field = columnType.getDeclaredField("id");
field.setAccessible(true);
if(columnValue != null) {
if (columnValue != null) {
values.put(columnName,String.valueOf(field.get(columnValue)));
} else {
values.putNull(columnName);
Expand Down Expand Up @@ -272,7 +274,9 @@ public static List<Class> getDomainClasses() {
try {
for (String className : getAllClasses()) {
Class domainClass = getDomainClass(className);
if (domainClass != null) domainClasses.add(domainClass);
if (domainClass != null) {
domainClasses.add(domainClass);
}
}
} catch (IOException | PackageManager.NameNotFoundException e) {
if (ManifestHelper.isDebugEnabled()) {
Expand Down Expand Up @@ -318,7 +322,9 @@ private static List<String> getAllClasses() throws PackageManager.NameNotFoundEx
try {
List<String> allClasses = MultiDexHelper.getAllClasses();
for (String classString : allClasses) {
if (classString.startsWith(packageName)) classNames.add(classString);
if (classString.startsWith(packageName)) {
classNames.add(classString);
}
}
} catch (NullPointerException e) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Expand All @@ -333,7 +339,9 @@ private static List<String> getAllClasses() throws PackageManager.NameNotFoundEx
populateFiles(filePath, fileNames, "");
}
for (String fileName : fileNames) {
if (fileName.startsWith(packageName)) classNames.add(fileName);
if (fileName.startsWith(packageName)) {
classNames.add(fileName);
}
}
}
}
Expand Down
Loading