From d58ff455dedebc2bbfde61de220410e4ab1eefdb Mon Sep 17 00:00:00 2001 From: Lee Howett Date: Wed, 17 May 2017 11:37:01 -0700 Subject: [PATCH] added index support --- .../main/java/com/orm/SchemaGenerator.java | 32 ++++++++++++++----- .../main/java/com/orm/annotation/Index.java | 13 ++++++++ .../java/com/orm/SchemaGeneratorTest.java | 27 ++++++++-------- 3 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 library/src/main/java/com/orm/annotation/Index.java diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index c25407fc..2a2686cf 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -4,16 +4,17 @@ import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.util.Log; +import android.util.Pair; import com.orm.annotation.Column; import com.orm.annotation.MultiUnique; import com.orm.annotation.NotNull; import com.orm.annotation.Unique; -import com.orm.dsl.BuildConfig; +import com.orm.annotation.Index; import com.orm.helper.ManifestHelper; +import com.orm.helper.NamingHelper; import com.orm.util.KeyWordUtil; import com.orm.util.MigrationFileParser; -import com.orm.helper.NamingHelper; import com.orm.util.NumberComparator; import com.orm.util.QueryBuilder; import com.orm.util.ReflectionUtil; @@ -28,8 +29,8 @@ import java.util.Collections; import java.util.List; -import static com.orm.util.ReflectionUtil.getDomainClasses; import static com.orm.util.ContextUtil.getAssets; +import static com.orm.util.ReflectionUtil.getDomainClasses; public class SchemaGenerator { public static final String NULL = " NULL"; @@ -200,7 +201,7 @@ private void addColumns(Class table, SQLiteDatabase sqLiteDatabase) { } } - protected String createTableSQL(Class table) { + protected Pair> createTableSQL(Class table) { if(ManifestHelper.isDebugEnabled()) { Log.i(SUGAR, "Create table if not exists"); } @@ -216,6 +217,7 @@ protected String createTableSQL(Class table) { StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS "); sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT "); + List indexStatements = new ArrayList<>(); for (Field column : fields) { String columnName = NamingHelper.toColumnName(column); String columnType = QueryBuilder.getColumnType(column.getType()); @@ -257,6 +259,13 @@ protected String createTableSQL(Class table) { } } } + + if (column.isAnnotationPresent(Index.class)) { + String idxStr = column.getAnnotation(Index.class).unique() ? "CREATE UNIQUE INDEX" : "CREATE INDEX"; + StringBuilder indexBuilder = new StringBuilder(idxStr).append(" ").append(tableName).append("_").append(columnName).append("_IDX") + .append(" ON ").append(tableName).append(" (").append(columnName).append(")"); + indexStatements.add(indexBuilder.toString()); + } } if (table.isAnnotationPresent(MultiUnique.class)) { @@ -282,15 +291,22 @@ protected String createTableSQL(Class table) { Log.i(SUGAR, "Creating table " + tableName); } - return sb.toString(); + return new Pair<>(sb.toString(), indexStatements); } protected void createTable(Class table, SQLiteDatabase sqLiteDatabase) { - String createSQL = createTableSQL(table); + Pair> createSQL = createTableSQL(table); + String tableCreate = createSQL.first; + List indexStatements = createSQL.second; - if (!createSQL.isEmpty()) { + if (!tableCreate.isEmpty()) { try { - sqLiteDatabase.execSQL(createSQL); + sqLiteDatabase.execSQL(tableCreate); + + for (String indexStatement : indexStatements) { + Log.i(SUGAR, "Creating index " + indexStatement); + sqLiteDatabase.execSQL(indexStatement); + } } catch (SQLException e) { e.printStackTrace(); } diff --git a/library/src/main/java/com/orm/annotation/Index.java b/library/src/main/java/com/orm/annotation/Index.java new file mode 100644 index 00000000..3456167a --- /dev/null +++ b/library/src/main/java/com/orm/annotation/Index.java @@ -0,0 +1,13 @@ +package com.orm.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Created by Lee Howett on 2017-05-16. + */ + +@Retention(RetentionPolicy.RUNTIME) +public @interface Index { + boolean unique() default false; +} diff --git a/library/src/test/java/com/orm/SchemaGeneratorTest.java b/library/src/test/java/com/orm/SchemaGeneratorTest.java index 811d1aa1..56c0a47f 100644 --- a/library/src/test/java/com/orm/SchemaGeneratorTest.java +++ b/library/src/test/java/com/orm/SchemaGeneratorTest.java @@ -2,9 +2,11 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.util.Pair; import com.orm.app.ClientApp; import com.orm.dsl.BuildConfig; +import com.orm.helper.NamingHelper; import com.orm.model.AllAnotatedModel; import com.orm.model.EmptyModel; import com.orm.model.IntUniqueModel; @@ -12,7 +14,6 @@ import com.orm.model.StringFieldAnnotatedModel; import com.orm.model.StringFieldExtendedModel; import com.orm.model.StringFieldExtendedModelAnnotatedColumn; -import com.orm.helper.NamingHelper; import com.orm.model.TestRecord; import junit.framework.Assert; @@ -33,59 +34,59 @@ public final class SchemaGeneratorTest { @Test public void testEmptyTableCreation() throws Exception { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); - String createSQL = schemaGenerator.createTableSQL(EmptyModel.class); + Pair> createSQL = schemaGenerator.createTableSQL(EmptyModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(EmptyModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT ) ", - createSQL); + createSQL.first); } @Test public void testSimpleColumnTableCreation() throws Exception { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); - String createSQL = schemaGenerator.createTableSQL(StringFieldExtendedModel.class); + Pair> createSQL = schemaGenerator.createTableSQL(StringFieldExtendedModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(StringFieldExtendedModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "STRING TEXT ) ", - createSQL); + createSQL.first); - String createSQL2 = schemaGenerator.createTableSQL(StringFieldAnnotatedModel.class); + Pair> createSQL2 = schemaGenerator.createTableSQL(StringFieldAnnotatedModel.class); assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(StringFieldAnnotatedModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "STRING TEXT ) ", - createSQL2); + createSQL2.first); - String createSQL3 = schemaGenerator.createTableSQL(StringFieldExtendedModelAnnotatedColumn.class); + Pair> createSQL3 = schemaGenerator.createTableSQL(StringFieldExtendedModelAnnotatedColumn.class); assertEquals("CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(StringFieldExtendedModelAnnotatedColumn.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "anyName TEXT ) ", - createSQL3); + createSQL3.first); } @Test public void testUniqueTableCreation() { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); - String createSQL = schemaGenerator.createTableSQL(IntUniqueModel.class); + Pair> createSQL = schemaGenerator.createTableSQL(IntUniqueModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(IntUniqueModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "VALUE INTEGER UNIQUE ) ", - createSQL); + createSQL.first); } @Test public void testMultiColumnUniqueTableCreation() { SchemaGenerator schemaGenerator = SchemaGenerator.getInstance(); - String createSQL = schemaGenerator.createTableSQL(MultiColumnUniqueModel.class); + Pair> createSQL = schemaGenerator.createTableSQL(MultiColumnUniqueModel.class); assertEquals( "CREATE TABLE IF NOT EXISTS " + NamingHelper.toTableName(MultiColumnUniqueModel.class) + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT , " + "A INTEGER, B INTEGER, " + "UNIQUE(A, B) ON CONFLICT REPLACE ) ", - createSQL); + createSQL.first); } @Test