Skip to content

Commit

Permalink
1.0.0-beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
stdrc committed Jul 22, 2016
1 parent fd3bd71 commit 65d5367
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 140 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

group = 'com.github.richardchien'

android {
compileSdkVersion 24
Expand Down
6 changes: 6 additions & 0 deletions library/src/main/java/im/r_c/android/dbox/DBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
10 changes: 5 additions & 5 deletions sample/src/main/java/im/r_c/android/dbox/sample/Clazz.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 7 additions & 7 deletions sample/src/main/java/im/r_c/android/dbox/sample/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
198 changes: 95 additions & 103 deletions sample/src/main/java/im/r_c/android/dbox/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Clazz> clzBox = DBox.of(Clazz.class);
// DBox<Course> crsBox = DBox.of(Course.class);
// DBox<Student> stuBox = DBox.of(Student.class);
//
// Random r = new Random();
//
// List<Clazz> 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<Course> 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<Clazz> clzBox = DBox.of(Clazz.class);
DBox<Course> crsBox = DBox.of(Course.class);
DBox<Student> stuBox = DBox.of(Student.class);

Random r = new Random();

List<Clazz> 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<Course> 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<Student> list = DBox.of(Student.class)
.find(new DBoxCondition()
.between("id", "100", "120"))
.find(condition)
.orderByDesc("name")
.orderBy("id")
.results()
.some(new DBoxResults.Filter<Student>() {
@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<Course> list = DBox.of(Course.class)
// .findAll()
// .orderByDesc("name")
// .orderBy("id")
// .results()
// .some(new DBoxResults.Filter<Course>() {
// @Override
// public boolean filter(Course course) {
// return true;
// }
// });
Logger.d(list);

DBoxResults<Student> 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<Student>() {
@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();
}
}
Loading

0 comments on commit 65d5367

Please sign in to comment.