From f5ca5e3a7dca87ffdbc2ae09cb178880d1bf141e Mon Sep 17 00:00:00 2001 From: Alfredo Matos Date: Fri, 25 Sep 2015 16:30:39 +0100 Subject: [PATCH 1/2] Add MultiColumn Unique Table constraint support --- .../main/java/com/orm/SchemaGenerator.java | 19 +++++++++++++++++++ library/src/main/java/com/orm/dsl/Unique.java | 1 + 2 files changed, 20 insertions(+) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index c0510478..4d897d58 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collections; @@ -153,6 +154,24 @@ private void createTable(Class table, SQLiteDatabase sqLiteDatabase) { } } + if (table.isAnnotationPresent(Unique.class)) { + String constraint = table.getAnnotation(Unique.class).value(); + + sb.append(", UNIQUE("); + + String[] constraintFields = constraint.split(","); + for(int i = 0; i < constraintFields.length; i++) { + String columnName = NamingHelper.toSQLNameDefault(constraintFields[i]); + sb.append(columnName); + + if(i < (constraintFields.length -1)) { + sb.append(","); + } + } + + sb.append(") ON CONFLICT REPLACE"); + } + sb.append(" ) "); Log.i("Sugar", "Creating table " + tableName); diff --git a/library/src/main/java/com/orm/dsl/Unique.java b/library/src/main/java/com/orm/dsl/Unique.java index 7384b749..76d82559 100644 --- a/library/src/main/java/com/orm/dsl/Unique.java +++ b/library/src/main/java/com/orm/dsl/Unique.java @@ -5,4 +5,5 @@ @Retention(RetentionPolicy.RUNTIME) public @interface Unique { + String value(); } From 98f8df508cb23917b661ce9046695ce2d8f0c159 Mon Sep 17 00:00:00 2001 From: Ovadia Babayev Date: Wed, 28 Oct 2015 12:11:00 +0200 Subject: [PATCH 2/2] Filtered classes by DOMAIN_PACKAGE_NAME --- .../main/java/com/orm/util/ReflectionUtil.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/orm/util/ReflectionUtil.java b/library/src/main/java/com/orm/util/ReflectionUtil.java index 2c71d631..1f4c6404 100644 --- a/library/src/main/java/com/orm/util/ReflectionUtil.java +++ b/library/src/main/java/com/orm/util/ReflectionUtil.java @@ -306,11 +306,12 @@ private static SharedPreferences getMultiDexPreferences(Context context) { } private static List getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException { + String packageName = ManifestHelper.getDomainPackageName(context); List paths = getSourcePaths(context); List classNames = new ArrayList<>(); DexFile dexfile = null; try { - for (int i = 0; i getAllClasses(Context context) throws PackageManager Enumeration dexEntries = dexfile.entries(); while (dexEntries.hasMoreElements()) { - classNames.add(dexEntries.nextElement()); + String className = dexEntries.nextElement(); + if (className.startsWith(packageName)) { + classNames.add(className); + } } } } catch (NullPointerException e) { @@ -335,12 +339,18 @@ private static List getAllClasses(Context context) throws PackageManager for (File filePath : classDirectory.listFiles()) { populateFiles(filePath, fileNames, ""); } - classNames.addAll(fileNames); + + for (String className : fileNames) { + if (className.startsWith(packageName)) { + classNames.add(className); + } + } } } } finally { if (null != dexfile) dexfile.close(); } + return classNames; } @@ -372,6 +382,7 @@ public static List getSourcePaths(Context context) throws PackageManager return sourcePaths; } + private static void populateFiles(File path, List fileNames, String parent) { if (path.isDirectory()) { for (File newPath : path.listFiles()) {