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 MultiColumn Unique Table constraint support #389

Merged
merged 2 commits into from
Dec 2, 2015
Merged
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
19 changes: 19 additions & 0 deletions library/src/main/java/com/orm/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions library/src/main/java/com/orm/dsl/Unique.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

@Retention(RetentionPolicy.RUNTIME)
public @interface Unique {
String value();
}
17 changes: 14 additions & 3 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,12 @@ private static SharedPreferences getMultiDexPreferences(Context context) {
}

private static List<String> getAllClasses(Context context) throws PackageManager.NameNotFoundException, IOException {
String packageName = ManifestHelper.getDomainPackageName(context);
List<String> paths = getSourcePaths(context);
List<String> classNames = new ArrayList<>();
DexFile dexfile = null;
try {
for (int i = 0; i <paths.size(); i++){
for (int i = 0; i < paths.size(); i++) {
String path = paths.get(i);
if (path.endsWith(EXTRACTED_SUFFIX)) {
//NOT use new DexFile(path) here, because it will throw "permission error in /data/dalvik-cache"
Expand All @@ -321,7 +322,10 @@ private static List<String> getAllClasses(Context context) throws PackageManager

Enumeration<String> dexEntries = dexfile.entries();
while (dexEntries.hasMoreElements()) {
classNames.add(dexEntries.nextElement());
String className = dexEntries.nextElement();
if (className.startsWith(packageName)) {
classNames.add(className);
}
}
}
} catch (NullPointerException e) {
Expand All @@ -335,12 +339,18 @@ private static List<String> 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;
}

Expand Down Expand Up @@ -372,6 +382,7 @@ public static List<String> getSourcePaths(Context context) throws PackageManager

return sourcePaths;
}

private static void populateFiles(File path, List<String> fileNames, String parent) {
if (path.isDirectory()) {
for (File newPath : path.listFiles()) {
Expand Down