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

Add method in Table annotation to set column Id name. #175

Merged
merged 1 commit into from
Mar 2, 2014
Merged
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
19 changes: 10 additions & 9 deletions src/com/activeandroid/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.activeandroid.annotation.Column;
import com.activeandroid.content.ContentProvider;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
Expand All @@ -37,17 +36,17 @@ public abstract class Model {
// PRIVATE MEMBERS
//////////////////////////////////////////////////////////////////////////////////////

@Column(name = "Id")
private Long mId = null;

private TableInfo mTableInfo;

private final TableInfo mTableInfo;
private final String idName;
//////////////////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////

public Model() {
mTableInfo = Cache.getTableInfo(getClass());
idName = mTableInfo.getIdName();
}

//////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -59,7 +58,7 @@ public final Long getId() {
}

public final void delete() {
Cache.openDatabase().delete(mTableInfo.getTableName(), "Id=?", new String[] { getId().toString() });
Cache.openDatabase().delete(mTableInfo.getTableName(), idName+"=?", new String[] { getId().toString() });
Cache.removeEntity(this);

Cache.getContext().getContentResolver()
Expand Down Expand Up @@ -150,7 +149,7 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
mId = db.insert(mTableInfo.getTableName(), null, values);
}
else {
db.update(mTableInfo.getTableName(), values, "Id=" + mId, null);
db.update(mTableInfo.getTableName(), values, idName+"=" + mId, null);
}

Cache.getContext().getContentResolver()
Expand All @@ -161,11 +160,13 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
// Convenience methods

public static void delete(Class<? extends Model> type, long id) {
new Delete().from(type).where("Id=?", id).execute();
TableInfo tableInfo = Cache.getTableInfo(type);
new Delete().from(type).where(tableInfo.getIdName()+"=?", id).execute();
}

public static <T extends Model> T load(Class<T> type, long id) {
return (T) new Select().from(type).where("Id=?", id).executeSingle();
TableInfo tableInfo = Cache.getTableInfo(type);
return (T) new Select().from(type).where(tableInfo.getIdName()+"=?", id).executeSingle();
}

// Model population
Expand Down Expand Up @@ -232,7 +233,7 @@ else if (ReflectionUtils.isModel(fieldType)) {

Model entity = Cache.getEntity(entityType, entityId);
if (entity == null) {
entity = new Select().from(entityType).where("Id=?", entityId).executeSingle();
entity = new Select().from(entityType).where(idName+"=?", entityId).executeSingle();
}

value = entity;
Expand Down
13 changes: 10 additions & 3 deletions src/com/activeandroid/TableInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public final class TableInfo {

private Class<? extends Model> mType;
private String mTableName;
private String mIdName = Table.DEFAULT_ID_NAME;

private Map<Field, String> mColumnNames = new LinkedHashMap<Field, String>();

Expand All @@ -50,23 +51,25 @@ public TableInfo(Class<? extends Model> type) {
final Table tableAnnotation = type.getAnnotation(Table.class);
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);

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

mColumnNames.put(field, columnName);
}

}

//////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -81,11 +84,15 @@ public String getTableName() {
return mTableName;
}

public String getIdName() {
return mIdName;
}

public Collection<Field> getFields() {
return mColumnNames.keySet();
}

public String getColumnName(Field field) {
return mColumnNames.get(field);
}
}
}
3 changes: 3 additions & 0 deletions src/com/activeandroid/annotation/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {

public static final String DEFAULT_ID_NAME = "Id";
public String name();
public String id() default DEFAULT_ID_NAME;
}
35 changes: 19 additions & 16 deletions src/com/activeandroid/util/SQLiteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,30 +263,31 @@ else if (ReflectionUtils.isSubclassOf(type, Enum.class)) {
}

if (!TextUtils.isEmpty(definition)) {
if (column.length() > -1) {
definition.append("(");
definition.append(column.length());
definition.append(")");
}

if (name.equals("Id")) {
if (name.equals(tableInfo.getIdName())) {
definition.append(" PRIMARY KEY AUTOINCREMENT");
}
}else if(column!=null){
if (column.length() > -1) {
definition.append("(");
definition.append(column.length());
definition.append(")");
}

if (column.notNull()) {
definition.append(" NOT NULL ON CONFLICT ");
definition.append(column.onNullConflict().toString());
}
if (column.notNull()) {
definition.append(" NOT NULL ON CONFLICT ");
definition.append(column.onNullConflict().toString());
}

if (column.unique()) {
definition.append(" UNIQUE ON CONFLICT ");
definition.append(column.onUniqueConflict().toString());
if (column.unique()) {
definition.append(" UNIQUE ON CONFLICT ");
definition.append(column.onUniqueConflict().toString());
}
}

if (FOREIGN_KEYS_SUPPORTED && ReflectionUtils.isModel(type)) {
definition.append(" REFERENCES ");
definition.append(Cache.getTableInfo((Class<? extends Model>) type).getTableName());
definition.append("(Id)");
definition.append("("+tableInfo.getIdName()+")");
definition.append(" ON DELETE ");
definition.append(column.onDelete().toString().replace("_", " "));
definition.append(" ON UPDATE ");
Expand All @@ -302,14 +303,16 @@ else if (ReflectionUtils.isSubclassOf(type, Enum.class)) {

@SuppressWarnings("unchecked")
public static <T extends Model> List<T> processCursor(Class<? extends Model> type, Cursor cursor) {
TableInfo tableInfo = Cache.getTableInfo(type);
String idName = tableInfo.getIdName();
final List<T> entities = new ArrayList<T>();

try {
Constructor<?> entityConstructor = type.getConstructor();

if (cursor.moveToFirst()) {
do {
Model entity = Cache.getEntity(type, cursor.getLong(cursor.getColumnIndex("Id")));
Model entity = Cache.getEntity(type, cursor.getLong(cursor.getColumnIndex(idName)));
if (entity == null) {
entity = (T) entityConstructor.newInstance();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/com/activeandroid/test/MockModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
import com.activeandroid.Model;
import com.activeandroid.annotation.Table;

@Table(name = "MockModel")
@Table(name = "MockModel", id = "Id")
public class MockModel extends Model {
}
4 changes: 2 additions & 2 deletions tests/src/com/activeandroid/test/query/FromTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ private From from() {
return new Select().all().from(MockModel.class);
}

@Table(name = "JoinModel")
@Table(name = "JoinModel", id = "Id")
private static class JoinModel extends Model {
}

@Table(name = "JoinModel2")
@Table(name = "JoinModel2", id = "Id")
private static class JoinModel2 extends Model {
}
}