diff --git a/build.gradle b/build.gradle index 0db664c..b968587 100644 --- a/build.gradle +++ b/build.gradle @@ -6,6 +6,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/library/build.gradle b/library/build.gradle index 2104ff6..04f378c 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -1,4 +1,7 @@ apply plugin: 'com.android.library' +apply plugin: 'com.github.dcendents.android-maven' + +group = 'com.github.richardchien' android { compileSdkVersion 24 diff --git a/library/src/main/java/im/r_c/android/dbox/DBox.java b/library/src/main/java/im/r_c/android/dbox/DBox.java index 33ac2a6..3ee8e6c 100644 --- a/library/src/main/java/im/r_c/android/dbox/DBox.java +++ b/library/src/main/java/im/r_c/android/dbox/DBox.java @@ -378,6 +378,9 @@ private void handleObjectMapping(String field, int index, long idA, Object objB, private long getId(Object obj, Class clz) { try { Field f = clz.getDeclaredField(TableInfo.COLUMN_ID); + if (!f.isAccessible()) { + f.setAccessible(true); + } return f.getLong(obj); } catch (Exception e) { e.printStackTrace(); @@ -388,6 +391,9 @@ private long getId(Object obj, Class clz) { private void setId(Object obj, Class clz, long id) { try { Field f = clz.getDeclaredField(TableInfo.COLUMN_ID); + if (!f.isAccessible()) { + f.setAccessible(true); + } f.setLong(obj, id); } catch (Exception e) { e.printStackTrace(); diff --git a/sample/build.gradle b/sample/build.gradle index fdd7efd..e5aae98 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -24,4 +24,5 @@ dependencies { testCompile 'junit:junit:4.12' compile project(':library') compile "com.android.support:appcompat-v7:$support_version" + compile 'com.orhanobut:logger:1.15' } diff --git a/sample/src/main/java/im/r_c/android/dbox/sample/Clazz.java b/sample/src/main/java/im/r_c/android/dbox/sample/Clazz.java index 843ed45..7a69f5c 100644 --- a/sample/src/main/java/im/r_c/android/dbox/sample/Clazz.java +++ b/sample/src/main/java/im/r_c/android/dbox/sample/Clazz.java @@ -14,22 +14,22 @@ class Clazz { @Column(notNull = true) private String name; - public Clazz() { + Clazz() { } - public Clazz(String name) { + Clazz(String name) { this.name = name; } - public long getId() { + long getId() { return id; } - public String getName() { + String getName() { return name; } - public void setName(String name) { + void setName(String name) { this.name = name; } diff --git a/sample/src/main/java/im/r_c/android/dbox/sample/Course.java b/sample/src/main/java/im/r_c/android/dbox/sample/Course.java index 33a5873..4c7099d 100644 --- a/sample/src/main/java/im/r_c/android/dbox/sample/Course.java +++ b/sample/src/main/java/im/r_c/android/dbox/sample/Course.java @@ -17,31 +17,31 @@ class Course { @Column(notNull = true) private String name; - public Course() { + Course() { } - public Course(String code, String name) { + Course(String code, String name) { this.code = code; this.name = name; } - public long getId() { + long getId() { return id; } - public String getCode() { + String getCode() { return code; } - public void setCode(String code) { + void setCode(String code) { this.code = code; } - public String getName() { + String getName() { return name; } - public void setName(String name) { + void setName(String name) { this.name = name; } diff --git a/sample/src/main/java/im/r_c/android/dbox/sample/MainActivity.java b/sample/src/main/java/im/r_c/android/dbox/sample/MainActivity.java index c18bca6..43c3cce 100644 --- a/sample/src/main/java/im/r_c/android/dbox/sample/MainActivity.java +++ b/sample/src/main/java/im/r_c/android/dbox/sample/MainActivity.java @@ -1,11 +1,13 @@ package im.r_c.android.dbox.sample; import android.os.Bundle; -import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; -import android.util.Log; +import com.orhanobut.logger.Logger; + +import java.util.ArrayList; import java.util.List; +import java.util.Random; import im.r_c.android.dbox.DBox; import im.r_c.android.dbox.DBoxCondition; @@ -18,117 +20,107 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + Logger.init(TAG); + DBox.init(this, "Test.db"); -// // Save -// Student stu = new Student(); -// stu.setName("Richard"); -// Course c1 = new Course("1234", "CS"); -// Course c2 = new Course("1235", "IOT"); -// stu.addCourse(c1); -// stu.addCourse(c2); -// stu.setFavoriteCourses(new Course[]{c1}); -// Clazz clazz = new Clazz("Class 1"); -// stu.setClazz(clazz); -// DBox.of(Clazz.class).save(clazz); -// DBox.of(Course.class).save(c1); -// DBox.of(Course.class).save(c2); -// DBox.of(Student.class).save(stu); -// Log.d(TAG, "Save: " + clazz.getId() + ", " + c1.getId() + ", " + c2.getId() + ", " + stu.getId()); -// -// // Update -// stu.setName("Changed"); -// stu.setFavoriteCourses(new Course[]{c2}); -// boolean ok = DBox.of(Student.class).save(stu); -// Log.d(TAG, "Update: " + ok); -// -// // Remove -// ok = DBox.of(Course.class).remove(c1); -// Log.d(TAG, "Remove: " + ok); -// -// // Clear -// ok = DBox.of(Student.class).clear(); -// Log.d(TAG, "Clear: " + ok); -// -// // Drop -// ok = DBox.of(Student.class).drop(); -// Log.d(TAG, "Drop: " + ok); -// -// // Re-save after clear and drop -// DBox.of(Course.class).save(c1); -// DBox.of(Course.class).save(c2); -// DBox.of(Student.class).save(stu); -// Log.d(TAG, "Save: " + clazz.getId() + ", " + c1.getId() + ", " + c2.getId() + ", " + stu.getId()); - -// DBox clzBox = DBox.of(Clazz.class); -// DBox crsBox = DBox.of(Course.class); -// DBox stuBox = DBox.of(Student.class); -// -// Random r = new Random(); -// -// List clzList = new ArrayList<>(); -// int n = 300 + r.nextInt(6); -// for (int i = 0; i < n; i++) { -// Clazz clz = new Clazz("Clazz " + i); -// clzBox.save(clz); -// clzList.add(clz); -// } -// -// List crsList = new ArrayList<>(); -// n = 800 + r.nextInt(10); -// for (int i = 0; i < n; i++) { -// Course crs = new Course("C" + (1000 + i), "Course " + i); -// crsBox.save(crs); -// crsList.add(crs); -// } -// -// n = 10000 + r.nextInt(20); -// for (int i = 0; i < n; i++) { -// Student stu = new Student(); -// stu.setName("Student " + (i + r.nextInt(10000))); -// stu.addClazz(clzList.get(r.nextInt(clzList.size()))); -// stu.addClazz(clzList.get(r.nextInt(clzList.size()))); -// int crsCount = 10 + r.nextInt(4); -// int start = r.nextInt(crsList.size() - crsCount + 1); -// Course[] favCrs = new Course[r.nextInt(crsCount - 3)]; -// int favIdx = 0; -// for (int j = start; j < start + crsCount; j++) { -// stu.addCourse(crsList.get(j)); -// if (r.nextInt(100) > 50) { -// if (favIdx < favCrs.length) { -// favCrs[favIdx++] = crsList.get(j); -// } -// } -// } -// stu.setFavoriteCourses(favCrs); -// stuBox.save(stu); -// } - - Log.d(TAG, "start: " + SystemClock.elapsedRealtime()); + if (!getDatabasePath("Test.db").exists()) { + // Generate random data + + DBox clzBox = DBox.of(Clazz.class); + DBox crsBox = DBox.of(Course.class); + DBox stuBox = DBox.of(Student.class); + + Random r = new Random(); + + List clzList = new ArrayList<>(); + int n = 100 + r.nextInt(6); + for (int i = 0; i < n; i++) { + Clazz clz = new Clazz("Clazz " + r.nextInt(1000)); + clzBox.save(clz); + clzList.add(clz); + } + + List crsList = new ArrayList<>(); + n = 200 + r.nextInt(10); + for (int i = 0; i < n; i++) { + Course crs = new Course("C" + (1000 + i), "Course " + i); + crsBox.save(crs); + crsList.add(crs); + } + + n = 1000 + r.nextInt(20); + for (int i = 0; i < n; i++) { + Student stu = new Student(); + stu.setName("Student " + (i + r.nextInt(1000))); + stu.setClazz(clzList.get(r.nextInt(clzList.size()))); + int crsCount = 10 + r.nextInt(4); + int start = r.nextInt(crsList.size() - crsCount + 1); + Course[] favCrs = new Course[r.nextInt(crsCount - 3)]; + int favIdx = 0; + for (int j = start; j < start + crsCount; j++) { + stu.addCourse(crsList.get(j)); + if (r.nextInt(100) > 50) { + if (favIdx < favCrs.length) { + favCrs[favIdx++] = crsList.get(j); + } + } + } + stu.setFavoriteCourses(favCrs); + stuBox.save(stu); + } + } + + DBoxCondition condition = new DBoxCondition() + .between("id", "100", "110") + .or().in("id", "210", "220") + .or() + .beginGroup() + .not().equalTo("id", "110") + .and().notEqualTo("id", "120") + .beginGroup() + .greaterThan("id", "180") + .or().greaterThanOrEqualTo("id", "170") + .or().lessThan("id", "130") + .or().lessThanOrEqualTo("id", "140") + .or().compare("id", "=", "150") + .endGroup() + .contains("name", "Student") + .startsWith("name", "Student") + .endsWith("name", "1") + .like("name", "%t%") + .not().isNull("name") + .isNotNull("id") + .endGroup(); + List list = DBox.of(Student.class) - .find(new DBoxCondition() - .between("id", "100", "120")) + .find(condition) .orderByDesc("name") .orderBy("id") .results() .some(new DBoxResults.Filter() { @Override public boolean filter(Student student) { - return student.getFavoriteCourses() != null && student.getFavoriteCourses().length > 2; + return student != null && student.getFavoriteCourses() != null && student.getFavoriteCourses().length > 2; } }); - Log.d(TAG, "end: " + SystemClock.elapsedRealtime()); - -// List list = DBox.of(Course.class) -// .findAll() -// .orderByDesc("name") -// .orderBy("id") -// .results() -// .some(new DBoxResults.Filter() { -// @Override -// public boolean filter(Course course) { -// return true; -// } -// }); + Logger.d(list); + + DBoxResults results = DBox.of(Student.class).findAll().results(); + Logger.d(results.getFirst()); + Logger.d(results.getOne(1)); + Logger.d(results.getLast()); + Logger.d(results.getSome(0, 3)); + Logger.d(results.getSome(new DBoxResults.Filter() { + @Override + public boolean filter(Student student) { + return student != null && student.getCourseList().size() > 11; + } + })); + Logger.d(results.getAll()); + for (Student stu : results) { + Logger.d(stu); + } + results.close(); } } diff --git a/sample/src/main/java/im/r_c/android/dbox/sample/Student.java b/sample/src/main/java/im/r_c/android/dbox/sample/Student.java index 6540329..0e8c98d 100644 --- a/sample/src/main/java/im/r_c/android/dbox/sample/Student.java +++ b/sample/src/main/java/im/r_c/android/dbox/sample/Student.java @@ -26,55 +26,50 @@ class Student { private Course[] favoriteCourses; @ObjectColumn(Clazz.class) - private List clazzList; + private Clazz clazz; - public Student() { + Student() { courseList = new ArrayList<>(); - clazzList = new ArrayList<>(); } - public long getId() { + long getId() { return id; } - public String getName() { + String getName() { return name; } - public void setName(String name) { + void setName(String name) { this.name = name; } - public boolean addCourse(Course course) { + boolean addCourse(Course course) { return courseList.add(course); } - public boolean removeCourse(Course course) { + boolean removeCourse(Course course) { return courseList.remove(course); } - public Course[] getFavoriteCourses() { + List getCourseList() { + return courseList; + } + + Course[] getFavoriteCourses() { return favoriteCourses; } - public void setFavoriteCourses(Course[] favoriteCourses) { + void setFavoriteCourses(Course[] favoriteCourses) { this.favoriteCourses = favoriteCourses; } -// public Clazz getClazz() { -// return clazz; -// } -// -// public void setClazz(Clazz clazz) { -// this.clazz = clazz; -// } - - public boolean addClazz(Clazz clazz) { - return clazzList.add(clazz); + Clazz getClazz() { + return clazz; } - public boolean removeClazz(Clazz clazz) { - return clazzList.remove(clazz); + void setClazz(Clazz clazz) { + this.clazz = clazz; } @Override @@ -90,7 +85,7 @@ public boolean equals(Object o) { return false; // Probably incorrect - comparing Object[] arrays with Arrays.equals if (!Arrays.equals(favoriteCourses, student.favoriteCourses)) return false; - return clazzList != null ? clazzList.equals(student.clazzList) : student.clazzList == null; + return clazz != null ? clazz.equals(student.clazz) : student.clazz == null; } @@ -100,7 +95,7 @@ public int hashCode() { result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (courseList != null ? courseList.hashCode() : 0); result = 31 * result + Arrays.hashCode(favoriteCourses); - result = 31 * result + (clazzList != null ? clazzList.hashCode() : 0); + result = 31 * result + (clazz != null ? clazz.hashCode() : 0); return result; } @@ -111,7 +106,7 @@ public String toString() { ", name='" + name + '\'' + ", courseList=" + courseList + ", favoriteCourses=" + Arrays.toString(favoriteCourses) + - ", clazzList=" + clazzList + + ", clazz=" + clazz + '}'; } }