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 FTS support #272

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/com/activeandroid/Model.java
Expand Up @@ -137,7 +137,11 @@ else if (fieldType.equals(Byte[].class) || fieldType.equals(byte[].class)) {
values.put(fieldName, (byte[]) value);
}
else if (ReflectionUtils.isModel(fieldType)) {
values.put(fieldName, ((Model) value).getId());
Long fId = ((Model) value).getId();
if (fId == null) {
fId = ((Model) value).save();
}
values.put(fieldName, fId);
}
else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
values.put(fieldName, ((Enum<?>) value).name());
Expand Down Expand Up @@ -178,11 +182,11 @@ public static <T extends Model> T load(Class<T> type, long id) {
// Model population

public final void loadFromCursor(Cursor cursor) {
/**
* Obtain the columns ordered to fix issue #106 (https://github.com/pardom/ActiveAndroid/issues/106)
* when the cursor have multiple columns with same name obtained from join tables.
*/
List<String> columnsOrdered = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
/**
* Obtain the columns ordered to fix issue #106 (https://github.com/pardom/ActiveAndroid/issues/106)
* when the cursor have multiple columns with same name obtained from join tables.
*/
List<String> columnsOrdered = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
for (Field field : mTableInfo.getFields()) {
final String fieldName = mTableInfo.getColumnName(field);
Class<?> fieldType = field.getType();
Expand Down Expand Up @@ -244,7 +248,8 @@ else if (ReflectionUtils.isModel(fieldType)) {

Model entity = Cache.getEntity(entityType, entityId);
if (entity == null) {
entity = new Select().from(entityType).where(idName+"=?", entityId).executeSingle();
TableInfo foreignTableInfo = Cache.getTableInfo(entityType);
entity = new Select("rowid, *").from(entityType).where(foreignTableInfo.getIdName()+"=?", entityId).executeSingle();
}

value = entity;
Expand Down Expand Up @@ -303,7 +308,7 @@ public boolean equals(Object obj) {
if (obj instanceof Model && this.mId != null) {
final Model other = (Model) obj;

return this.mId.equals(other.mId)
return this.mId.equals(other.mId)
&& (this.mTableInfo.getTableName().equals(other.mTableInfo.getTableName()));
} else {
return this == obj;
Expand Down
77 changes: 44 additions & 33 deletions src/com/activeandroid/TableInfo.java
Expand Up @@ -38,7 +38,8 @@ public final class TableInfo {

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

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

Expand All @@ -47,38 +48,44 @@ public final class TableInfo {
//////////////////////////////////////////////////////////////////////////////////////

public TableInfo(Class<? extends Model> type) {
mType = type;

final Table tableAnnotation = type.getAnnotation(Table.class);

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

final Table tableAnnotation = type.getAnnotation(Table.class);

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

if (mFTS.isEmpty()) {
// Manually add the id column since it is not declared like the other columns.
Field idField = getIdField(type);
mColumnNames.put(idField, mIdName);
} else {
// If the table is a FTS virtual table ignore the key and use the hidden rowid column instead.
mIdName = "rowid";
}

List<Field> fields = new LinkedList<Field>(ReflectionUtils.getDeclaredColumnFields(type));
Collections.reverse(fields);

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();
}

// Manually add the id column since it is not declared like the other columns.
Field idField = getIdField(type);
mColumnNames.put(idField, mIdName);

List<Field> fields = new LinkedList<Field>(ReflectionUtils.getDeclaredColumnFields(type));
Collections.reverse(fields);

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);
}
}
mColumnNames.put(field, columnName);
}
}

}
}

//////////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
Expand All @@ -92,9 +99,13 @@ public String getTableName() {
return mTableName;
}

public String getIdName() {
return mIdName;
}
public String getIdName() {
return mIdName;
}

public String getFTS() {
return mFTS;
}

public Collection<Field> getFields() {
return mColumnNames.keySet();
Expand Down
6 changes: 5 additions & 1 deletion src/com/activeandroid/annotation/Table.java
Expand Up @@ -25,7 +25,11 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {

public static final String DEFAULT_ID_NAME = "Id";
public static final String DEFAULT_ID_NAME = "Id";
public static final String DEFAULT_FTS = "";

public String name();
public String id() default DEFAULT_ID_NAME;
public String fts() default DEFAULT_FTS;

}