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

added index support #749

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions library/src/main/java/com/orm/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -200,7 +201,7 @@ private void addColumns(Class<?> table, SQLiteDatabase sqLiteDatabase) {
}
}

protected String createTableSQL(Class<?> table) {
protected Pair<String, List<String>> createTableSQL(Class<?> table) {
if(ManifestHelper.isDebugEnabled()) {
Log.i(SUGAR, "Create table if not exists");
}
Expand All @@ -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<String> indexStatements = new ArrayList<>();
for (Field column : fields) {
String columnName = NamingHelper.toColumnName(column);
String columnType = QueryBuilder.getColumnType(column.getType());
Expand Down Expand Up @@ -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)) {
Expand All @@ -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<String, List<String>> createSQL = createTableSQL(table);
String tableCreate = createSQL.first;
List<String> 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();
}
Expand Down
13 changes: 13 additions & 0 deletions library/src/main/java/com/orm/annotation/Index.java
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 14 additions & 13 deletions library/src/test/java/com/orm/SchemaGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

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;
import com.orm.model.MultiColumnUniqueModel;
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;
Expand All @@ -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<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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<String, List<String>> 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
Expand Down