From 9681d06dcfd370c21bd6e00eb3acbe292e825914 Mon Sep 17 00:00:00 2001 From: Alexandr Kozlov Date: Fri, 25 Oct 2013 18:36:05 +0300 Subject: [PATCH 1/2] Add method in Table annotation for set column Id name. This fixes Issue #83 example: @Table(name = "Items", id = BaseColumns._ID) public class Item extends Model {...} Conflicts: src/com/activeandroid/Model.java src/com/activeandroid/TableInfo.java --- src/com/activeandroid/Model.java | 19 +++++----- src/com/activeandroid/TableInfo.java | 13 +++++-- src/com/activeandroid/annotation/Table.java | 3 ++ src/com/activeandroid/util/SQLiteUtils.java | 35 ++++++++++--------- .../src/com/activeandroid/test/MockModel.java | 2 +- .../activeandroid/test/query/FromTest.java | 4 +-- 6 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/com/activeandroid/Model.java b/src/com/activeandroid/Model.java index 4f18ecb89..40f3ac045 100644 --- a/src/com/activeandroid/Model.java +++ b/src/com/activeandroid/Model.java @@ -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; @@ -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(); } ////////////////////////////////////////////////////////////////////////////////////// @@ -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() @@ -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() @@ -161,11 +160,13 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) { // Convenience methods public static void delete(Class 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 load(Class 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 @@ -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; diff --git a/src/com/activeandroid/TableInfo.java b/src/com/activeandroid/TableInfo.java index e3a190224..d58ceb479 100644 --- a/src/com/activeandroid/TableInfo.java +++ b/src/com/activeandroid/TableInfo.java @@ -37,6 +37,7 @@ public final class TableInfo { private Class mType; private String mTableName; + private String mIdName = Table.DEFAULT_ID_NAME; private Map mColumnNames = new LinkedHashMap(); @@ -50,6 +51,7 @@ public TableInfo(Class type) { final Table tableAnnotation = type.getAnnotation(Table.class); if (tableAnnotation != null) { mTableName = tableAnnotation.name(); + mIdName = tableAnnotation.id(); } else { mTableName = type.getSimpleName(); @@ -57,16 +59,17 @@ public TableInfo(Class type) { List fields = new LinkedList(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); } + } ////////////////////////////////////////////////////////////////////////////////////// @@ -81,6 +84,10 @@ public String getTableName() { return mTableName; } + public String getIdName() { + return mIdName; + } + public Collection getFields() { return mColumnNames.keySet(); } @@ -88,4 +95,4 @@ public Collection getFields() { public String getColumnName(Field field) { return mColumnNames.get(field); } -} \ No newline at end of file +} diff --git a/src/com/activeandroid/annotation/Table.java b/src/com/activeandroid/annotation/Table.java index 4bb6deaf3..541dfbe92 100644 --- a/src/com/activeandroid/annotation/Table.java +++ b/src/com/activeandroid/annotation/Table.java @@ -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; } diff --git a/src/com/activeandroid/util/SQLiteUtils.java b/src/com/activeandroid/util/SQLiteUtils.java index c6b4c6602..541f8493d 100644 --- a/src/com/activeandroid/util/SQLiteUtils.java +++ b/src/com/activeandroid/util/SQLiteUtils.java @@ -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) type).getTableName()); - definition.append("(Id)"); + definition.append("("+tableInfo.getIdName()+")"); definition.append(" ON DELETE "); definition.append(column.onDelete().toString().replace("_", " ")); definition.append(" ON UPDATE "); @@ -302,6 +303,8 @@ else if (ReflectionUtils.isSubclassOf(type, Enum.class)) { @SuppressWarnings("unchecked") public static List processCursor(Class type, Cursor cursor) { + TableInfo tableInfo = Cache.getTableInfo(type); + String idName = tableInfo.getIdName(); final List entities = new ArrayList(); try { @@ -309,7 +312,7 @@ public static List processCursor(Class typ 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(); } diff --git a/tests/src/com/activeandroid/test/MockModel.java b/tests/src/com/activeandroid/test/MockModel.java index 718e945b7..4ff5307d3 100644 --- a/tests/src/com/activeandroid/test/MockModel.java +++ b/tests/src/com/activeandroid/test/MockModel.java @@ -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 { } diff --git a/tests/src/com/activeandroid/test/query/FromTest.java b/tests/src/com/activeandroid/test/query/FromTest.java index 7340e0915..09385f459 100644 --- a/tests/src/com/activeandroid/test/query/FromTest.java +++ b/tests/src/com/activeandroid/test/query/FromTest.java @@ -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 { } } From b49c910e1abd9993317d31c8e9230ff27b227753 Mon Sep 17 00:00:00 2001 From: Joshua Pinter Date: Sun, 2 Mar 2014 12:40:20 -0700 Subject: [PATCH 2/2] Add "Running the Test Suite". To both, the Documentation index as well as the Contributing section. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 12ed534c6..7ba14fbaa 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ ActiveAndroid does so much more than this though. Accessing the database is a ha * [Using the content provider](http://github.com/pardom/ActiveAndroid/wiki/Using-the-content-provider) * [Schema migrations](http://github.com/pardom/ActiveAndroid/wiki/Schema-migrations) * [Pre-populated-databases](http://github.com/pardom/ActiveAndroid/wiki/Pre-populated-databases) +* [Running the Test Suite](https://github.com/pardom/ActiveAndroid/wiki/Running-the-Test-Suite) ## License @@ -40,6 +41,9 @@ Please fork this repository and contribute back using [pull requests](http://git Any contributions, large or small, major features, bug fixes, unit tests are welcomed and appreciated but will be thoroughly reviewed and discussed. +You can run the test suite by following the instructions on the [Running the Test Suite](https://github.com/pardom/ActiveAndroid/wiki/Running-the-Test-Suite) Wiki page. + + ## Author Michael Pardo | www.michaelpardo.com | www.activeandroid.com