Skip to content

Commit

Permalink
Fixes issue from Pull Request pardom-zz#175 with custom Id column name.
Browse files Browse the repository at this point in the history
Unique Groups and Index Groups caused a conflict with allowing for a custom Id column name.

This was discovered in Pull Request pardom-zz#175.
  • Loading branch information
joshuapinter committed Mar 3, 2014
1 parent 4fb901f commit 0fca7ec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/com/activeandroid/TableInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;

import android.text.TextUtils;
import android.util.Log;

import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
Expand All @@ -49,26 +50,33 @@ public TableInfo(Class<? extends Model> type) {
mType = type;

final Table tableAnnotation = type.getAnnotation(Table.class);
if (tableAnnotation != null) {

if (tableAnnotation != null) {
mTableName = tableAnnotation.name();
mIdName = tableAnnotation.id();
}
else {
mTableName = type.getSimpleName();
}
}

List<Field> fields = new LinkedList<Field>(ReflectionUtils.getDeclaredColumnFields(type));
Collections.reverse(fields);
// Manually add the id column since it is not declared like the other columns.
Field idField = getIdField(type);
mColumnNames.put(idField, mIdName);

for (Field field : fields) {
final Column columnAnnotation = field.getAnnotation(Column.class);
String columnName = columnAnnotation.name();
if (TextUtils.isEmpty(columnName)) {
columnName = field.getName();
}
List<Field> fields = new LinkedList<Field>(ReflectionUtils.getDeclaredColumnFields(type));
Collections.reverse(fields);

mColumnNames.put(field, columnName);
}
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
final Column columnAnnotation = field.getAnnotation(Column.class);
String columnName = columnAnnotation.name();
if (TextUtils.isEmpty(columnName)) {
columnName = field.getName();
}

mColumnNames.put(field, columnName);
}
}

}

Expand All @@ -95,4 +103,22 @@ public Collection<Field> getFields() {
public String getColumnName(Field field) {
return mColumnNames.get(field);
}


private Field getIdField(Class<?> type) {
if (type.equals(Model.class)) {
try {
return type.getDeclaredField("mId");
}
catch (NoSuchFieldException e) {
Log.e("Impossible!", e.toString());
}
}
else if (type.getSuperclass() != null) {
return getIdField(type.getSuperclass());
}

return null;
}

}
8 changes: 8 additions & 0 deletions src/com/activeandroid/util/SQLiteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public static void createUniqueColumnDefinition(TableInfo tableInfo, Field field
final String name = tableInfo.getColumnName(field);
final Column column = field.getAnnotation(Column.class);

if (field.getName().equals("mId")) {
return;
}

String[] groups = column.uniqueGroups();
ConflictAction[] conflictActions = column.onUniqueConflicts();
if (groups.length != conflictActions.length)
Expand Down Expand Up @@ -196,6 +200,10 @@ public static void createIndexColumnDefinition(TableInfo tableInfo, Field field)
final String name = tableInfo.getColumnName(field);
final Column column = field.getAnnotation(Column.class);

if (field.getName().equals("mId")) {
return;
}

if (column.index()) {
List<String> list = new ArrayList<String>();
list.add(name);
Expand Down

0 comments on commit 0fca7ec

Please sign in to comment.