Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Added index creation
Browse files Browse the repository at this point in the history
Now creating INDEX where Annotation index=true
  • Loading branch information
ChristopheCVB committed Jul 22, 2014
1 parent 1f6b551 commit 500f563
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/com/roscopeco/ormdroid/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ static final class EntityMapping {
private ArrayList<String> mColumnNames = new ArrayList<String>();
private ArrayList<Field> mFields = new ArrayList<Field>();
private ArrayList<Field> mInverseFields = new ArrayList<Field>();
private ArrayList<Field> mIndexFields = new ArrayList<Field>();
boolean mSchemaCreated = false;

// Not concerned too much about reflective annotation access in this
Expand Down Expand Up @@ -208,6 +209,7 @@ static EntityMapping build(Class<? extends Entity> clz) {
Column colAnn = f.getAnnotation(Column.class);
boolean inverse = colAnn != null && !colAnn.inverse().equals("");
boolean force = colAnn != null && colAnn.forceMap();
boolean index = colAnn != null && colAnn.index();

int modifiers = f.getModifiers();
if (!Modifier.isStatic(modifiers) &&
Expand All @@ -216,6 +218,10 @@ static EntityMapping build(Class<? extends Entity> clz) {
!seenFields.contains(f.getName())) {

seenFields.add(f.getName());

if (index) {
mapping.mIndexFields.add(f);
}

if(!inverse) {

Expand Down Expand Up @@ -308,6 +314,17 @@ void createSchema(SQLiteDatabase db) {
String sql = b.toString();
Log.v(TAG, sql);
db.execSQL(sql);

for (int i = 0; i < len; i++) {
Field f = mFields.get(i);
if (mIndexFields.contains(f)) {
String colName = mColumnNames.get(i);
String sqlIndex = new StringBuilder().append("CREATE INDEX IF NOT EXISTS ").append(mTableName).append("_").append(colName).append("_index ON ").append(mTableName).append("(").append(colName).append(");").toString();
Log.v(TAG, sqlIndex);
db.execSQL(sqlIndex);
}
}

mSchemaCreated = true;
}

Expand Down

0 comments on commit 500f563

Please sign in to comment.