Skip to content

Commit 6ab1e05

Browse files
committed
Merge pull request #175 from hube/add_id_annotation
Add method in Table annotation to set column Id name.
2 parents f659063 + 9681d06 commit 6ab1e05

File tree

6 files changed

+45
-31
lines changed

6 files changed

+45
-31
lines changed

src/com/activeandroid/Model.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import android.database.Cursor;
2121
import android.database.sqlite.SQLiteDatabase;
2222

23-
import com.activeandroid.annotation.Column;
2423
import com.activeandroid.content.ContentProvider;
2524
import com.activeandroid.query.Delete;
2625
import com.activeandroid.query.Select;
@@ -37,17 +36,17 @@ public abstract class Model {
3736
// PRIVATE MEMBERS
3837
//////////////////////////////////////////////////////////////////////////////////////
3938

40-
@Column(name = "Id")
4139
private Long mId = null;
4240

43-
private TableInfo mTableInfo;
44-
41+
private final TableInfo mTableInfo;
42+
private final String idName;
4543
//////////////////////////////////////////////////////////////////////////////////////
4644
// CONSTRUCTORS
4745
//////////////////////////////////////////////////////////////////////////////////////
4846

4947
public Model() {
5048
mTableInfo = Cache.getTableInfo(getClass());
49+
idName = mTableInfo.getIdName();
5150
}
5251

5352
//////////////////////////////////////////////////////////////////////////////////////
@@ -59,7 +58,7 @@ public final Long getId() {
5958
}
6059

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

6564
Cache.getContext().getContentResolver()
@@ -150,7 +149,7 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
150149
mId = db.insert(mTableInfo.getTableName(), null, values);
151150
}
152151
else {
153-
db.update(mTableInfo.getTableName(), values, "Id=" + mId, null);
152+
db.update(mTableInfo.getTableName(), values, idName+"=" + mId, null);
154153
}
155154

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

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

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

171172
// Model population
@@ -232,7 +233,7 @@ else if (ReflectionUtils.isModel(fieldType)) {
232233

233234
Model entity = Cache.getEntity(entityType, entityId);
234235
if (entity == null) {
235-
entity = new Select().from(entityType).where("Id=?", entityId).executeSingle();
236+
entity = new Select().from(entityType).where(idName+"=?", entityId).executeSingle();
236237
}
237238

238239
value = entity;

src/com/activeandroid/TableInfo.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class TableInfo {
3737

3838
private Class<? extends Model> mType;
3939
private String mTableName;
40+
private String mIdName = Table.DEFAULT_ID_NAME;
4041

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

@@ -50,23 +51,25 @@ public TableInfo(Class<? extends Model> type) {
5051
final Table tableAnnotation = type.getAnnotation(Table.class);
5152
if (tableAnnotation != null) {
5253
mTableName = tableAnnotation.name();
54+
mIdName = tableAnnotation.id();
5355
}
5456
else {
5557
mTableName = type.getSimpleName();
5658
}
5759

5860
List<Field> fields = new LinkedList<Field>(ReflectionUtils.getDeclaredColumnFields(type));
5961
Collections.reverse(fields);
60-
62+
6163
for (Field field : fields) {
6264
final Column columnAnnotation = field.getAnnotation(Column.class);
6365
String columnName = columnAnnotation.name();
6466
if (TextUtils.isEmpty(columnName)) {
6567
columnName = field.getName();
6668
}
67-
69+
6870
mColumnNames.put(field, columnName);
6971
}
72+
7073
}
7174

7275
//////////////////////////////////////////////////////////////////////////////////////
@@ -81,11 +84,15 @@ public String getTableName() {
8184
return mTableName;
8285
}
8386

87+
public String getIdName() {
88+
return mIdName;
89+
}
90+
8491
public Collection<Field> getFields() {
8592
return mColumnNames.keySet();
8693
}
8794

8895
public String getColumnName(Field field) {
8996
return mColumnNames.get(field);
9097
}
91-
}
98+
}

src/com/activeandroid/annotation/Table.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@
2424
@Target(ElementType.TYPE)
2525
@Retention(RetentionPolicy.RUNTIME)
2626
public @interface Table {
27+
28+
public static final String DEFAULT_ID_NAME = "Id";
2729
public String name();
30+
public String id() default DEFAULT_ID_NAME;
2831
}

src/com/activeandroid/util/SQLiteUtils.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -263,30 +263,31 @@ else if (ReflectionUtils.isSubclassOf(type, Enum.class)) {
263263
}
264264

265265
if (!TextUtils.isEmpty(definition)) {
266-
if (column.length() > -1) {
267-
definition.append("(");
268-
definition.append(column.length());
269-
definition.append(")");
270-
}
271266

272-
if (name.equals("Id")) {
267+
if (name.equals(tableInfo.getIdName())) {
273268
definition.append(" PRIMARY KEY AUTOINCREMENT");
274-
}
269+
}else if(column!=null){
270+
if (column.length() > -1) {
271+
definition.append("(");
272+
definition.append(column.length());
273+
definition.append(")");
274+
}
275275

276-
if (column.notNull()) {
277-
definition.append(" NOT NULL ON CONFLICT ");
278-
definition.append(column.onNullConflict().toString());
279-
}
276+
if (column.notNull()) {
277+
definition.append(" NOT NULL ON CONFLICT ");
278+
definition.append(column.onNullConflict().toString());
279+
}
280280

281-
if (column.unique()) {
282-
definition.append(" UNIQUE ON CONFLICT ");
283-
definition.append(column.onUniqueConflict().toString());
281+
if (column.unique()) {
282+
definition.append(" UNIQUE ON CONFLICT ");
283+
definition.append(column.onUniqueConflict().toString());
284+
}
284285
}
285286

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

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

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

310313
if (cursor.moveToFirst()) {
311314
do {
312-
Model entity = Cache.getEntity(type, cursor.getLong(cursor.getColumnIndex("Id")));
315+
Model entity = Cache.getEntity(type, cursor.getLong(cursor.getColumnIndex(idName)));
313316
if (entity == null) {
314317
entity = (T) entityConstructor.newInstance();
315318
}

tests/src/com/activeandroid/test/MockModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
import com.activeandroid.Model;
2020
import com.activeandroid.annotation.Table;
2121

22-
@Table(name = "MockModel")
22+
@Table(name = "MockModel", id = "Id")
2323
public class MockModel extends Model {
2424
}

tests/src/com/activeandroid/test/query/FromTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ private From from() {
166166
return new Select().all().from(MockModel.class);
167167
}
168168

169-
@Table(name = "JoinModel")
169+
@Table(name = "JoinModel", id = "Id")
170170
private static class JoinModel extends Model {
171171
}
172172

173-
@Table(name = "JoinModel2")
173+
@Table(name = "JoinModel2", id = "Id")
174174
private static class JoinModel2 extends Model {
175175
}
176176
}

0 commit comments

Comments
 (0)