Skip to content

Commit

Permalink
Add the function for changing table names with annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sinyu890807 committed Jul 30, 2015
1 parent a5e01ca commit cda0b0c
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 94 deletions.
2 changes: 1 addition & 1 deletion litepal/src/main/java/org/litepal/crud/DataHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ protected String getIntermediateTableName(DataSupport baseObj, String associated
* @return The table name of model.
*/
protected String getTableName(Class<?> modelClass) {
return BaseUtility.changeCase(modelClass.getSimpleName());
return BaseUtility.changeCase(DBUtility.getTableNameByClassName(modelClass.getName()));
}

/**
Expand Down
16 changes: 9 additions & 7 deletions litepal/src/main/java/org/litepal/crud/DataSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.litepal.exceptions.DataSupportException;
import org.litepal.tablemanager.Connector;
import org.litepal.util.BaseUtility;
import org.litepal.util.DBUtility;

import android.content.ContentValues;
import android.database.Cursor;
Expand Down Expand Up @@ -234,7 +235,7 @@ public static synchronized ClusterQuery offset(int value) {
* @return Count of the specified table.
*/
public static synchronized int count(Class<?> modelClass) {
return count(BaseUtility.changeCase(modelClass.getSimpleName()));
return count(BaseUtility.changeCase(DBUtility.getTableNameByClassName(modelClass.getName())));
}

/**
Expand Down Expand Up @@ -280,7 +281,7 @@ public static synchronized int count(String tableName) {
* @return The average value on a given column.
*/
public static synchronized double average(Class<?> modelClass, String column) {
return average(BaseUtility.changeCase(modelClass.getSimpleName()), column);
return average(BaseUtility.changeCase(DBUtility.getTableNameByClassName(modelClass.getName())), column);
}

/**
Expand Down Expand Up @@ -330,7 +331,7 @@ public static synchronized double average(String tableName, String column) {
* @return The maximum value on a given column.
*/
public static synchronized <T> T max(Class<?> modelClass, String columnName, Class<T> columnType) {
return max(BaseUtility.changeCase(modelClass.getSimpleName()), columnName, columnType);
return max(BaseUtility.changeCase(DBUtility.getTableNameByClassName(modelClass.getName())), columnName, columnType);
}

/**
Expand Down Expand Up @@ -383,7 +384,7 @@ public static synchronized <T> T max(String tableName, String columnName, Class<
* @return The minimum value on a given column.
*/
public static synchronized <T> T min(Class<?> modelClass, String columnName, Class<T> columnType) {
return min(BaseUtility.changeCase(modelClass.getSimpleName()), columnName, columnType);
return min(BaseUtility.changeCase(DBUtility.getTableNameByClassName(modelClass.getName())), columnName, columnType);
}

/**
Expand Down Expand Up @@ -436,7 +437,7 @@ public static synchronized <T> T min(String tableName, String columnName, Class<
* @return The sum value on a given column.
*/
public static synchronized <T> T sum(Class<?> modelClass, String columnName, Class<T> columnType) {
return sum(BaseUtility.changeCase(modelClass.getSimpleName()), columnName, columnType);
return sum(BaseUtility.changeCase(DBUtility.getTableNameByClassName(modelClass.getName())), columnName, columnType);
}

/**
Expand Down Expand Up @@ -811,7 +812,8 @@ public static synchronized int update(Class<?> modelClass, ContentValues values,
*/
public static synchronized int updateAll(Class<?> modelClass, ContentValues values,
String... conditions) {
return updateAll(BaseUtility.changeCase(modelClass.getSimpleName()), values, conditions);
return updateAll(BaseUtility.changeCase(DBUtility.getTableNameByClassName(
modelClass.getName())), values, conditions);
}

/**
Expand Down Expand Up @@ -1116,7 +1118,7 @@ protected String getClassName() {
* @return The corresponding table name of current model.
*/
protected String getTableName() {
return BaseUtility.changeCase(getClass().getSimpleName());
return BaseUtility.changeCase(DBUtility.getTableNameByClassName(getClassName()));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions sample/src/androidTest/java/com/litepaltest/model/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.io.Serializable;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

@Table(name = "Boo")
public class Book extends DataSupport implements Serializable{

private static final long serialVersionUID = 9040804172147110007L;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.litepaltest.model;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

@Table(name = "phone")
public class Cellphone extends DataSupport {

private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.util.List;
import java.util.Set;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

@Table(name = "class_room")
public class Classroom extends DataSupport {
private int _id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.litepaltest.model;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

@Table(name = "compu")
public class Computer extends DataSupport {

private long id;
Expand Down
2 changes: 2 additions & 0 deletions sample/src/androidTest/java/com/litepaltest/model/IdCard.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.litepaltest.model;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

@Table(name = "ic_card")
public class IdCard extends DataSupport {
private int id;
private String number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.litepaltest.model;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

public class Product extends DataSupport{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.util.Date;
import java.util.List;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

@Table(name = "stu")
public class Student extends DataSupport {
private int id;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.ArrayList;
import java.util.List;

import org.litepal.annotation.Table;
import org.litepal.crud.DataSupport;

@Table(name = "tea")
public class Teacher extends DataSupport {

private int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected boolean isIntermediateDataCorrect(String table1, String table2, long t

protected long getForeignKeyValue(String tableWithFK, String tableWithoutFK, long id) {
Cursor cursor = Connector.getDatabase().query(tableWithFK, null, "id = ?",
new String[] { String.valueOf(id) }, null, null, null);
new String[]{String.valueOf(id)}, null, null, null);
long foreignKeyId = 0;
if (cursor.moveToFirst()) {
foreignKeyId = cursor.getLong(cursor.getColumnIndexOrThrow(BaseUtility
Expand Down Expand Up @@ -107,9 +107,13 @@ protected boolean isDataExists(String table, long id) {
}

protected String getTableName(Object object) {
return object.getClass().getSimpleName();
return DBUtility.getTableNameByClassName(object.getClass().getName());
}

protected String getTableName(Class<?> c) {
return DBUtility.getTableNameByClassName(c.getName());
}

protected int getRowsCount(String tableName) {
int count = 0;
Cursor c = Connector.getDatabase().query(tableName, null, null, null, null, null, null);
Expand All @@ -121,7 +125,7 @@ protected int getRowsCount(String tableName) {
protected List<Book> getBooks(String[] columns, String selection, String[] selectionArgs,
String groupBy, String having, String orderBy, String limit) {
List<Book> books = new ArrayList<Book>();
Cursor cursor = Connector.getDatabase().query("Book", columns, selection, selectionArgs,
Cursor cursor = Connector.getDatabase().query(getTableName(Book.class), columns, selection, selectionArgs,
groupBy, having, orderBy, limit);
if (cursor.moveToFirst()) {
do {
Expand Down Expand Up @@ -151,7 +155,7 @@ protected List<Book> getBooks(String[] columns, String selection, String[] selec

protected Classroom getClassroom(long id) {
Classroom c = null;
Cursor cursor = Connector.getDatabase().query("Classroom", null, "id = ?",
Cursor cursor = Connector.getDatabase().query(getTableName(Classroom.class), null, "id = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor.moveToFirst()) {
c = new Classroom();
Expand All @@ -164,7 +168,7 @@ protected Classroom getClassroom(long id) {

protected IdCard getIdCard(long id) {
IdCard card = null;
Cursor cursor = Connector.getDatabase().query("IdCard", null, "id = ?",
Cursor cursor = Connector.getDatabase().query(getTableName(IdCard.class), null, "id = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor.moveToFirst()) {
card = new IdCard();
Expand All @@ -179,7 +183,7 @@ protected IdCard getIdCard(long id) {

protected Computer getComputer(long id) {
Computer computer = null;
Cursor cursor = Connector.getDatabase().query("Computer", null, "id = ?",
Cursor cursor = Connector.getDatabase().query(getTableName(Computer.class), null, "id = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor.moveToFirst()) {
computer = new Computer("", 0);
Expand All @@ -194,7 +198,7 @@ protected Computer getComputer(long id) {

protected Cellphone getCellPhone(long id) {
Cellphone cellPhone = null;
Cursor cursor = Connector.getDatabase().query("Cellphone", null, "id = ?",
Cursor cursor = Connector.getDatabase().query(getTableName(Cellphone.class), null, "id = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor.moveToFirst()) {
cellPhone = new Cellphone();
Expand All @@ -211,7 +215,7 @@ protected Cellphone getCellPhone(long id) {

protected Teacher getTeacher(long id) {
Teacher teacher = null;
Cursor cursor = Connector.getDatabase().query("Teacher", null, "id = ?",
Cursor cursor = Connector.getDatabase().query(getTableName(Teacher.class), null, "id = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor.moveToFirst()) {
teacher = new Teacher();
Expand All @@ -234,7 +238,7 @@ protected Teacher getTeacher(long id) {

protected Student getStudent(long id) {
Student student = null;
Cursor cursor = Connector.getDatabase().query("Student", null, "id = ?",
Cursor cursor = Connector.getDatabase().query(getTableName(Student.class), null, "id = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor.moveToFirst()) {
student = new Student();
Expand All @@ -249,7 +253,7 @@ protected Student getStudent(long id) {

protected List<Teacher> getTeachers(int[] ids) {
List<Teacher> teachers = new ArrayList<Teacher>();
Cursor cursor = Connector.getDatabase().query("Teacher", null, getWhere(ids), null, null,
Cursor cursor = Connector.getDatabase().query(getTableName(Teacher.class), null, getWhere(ids), null, null,
null, null);
if (cursor.moveToFirst()) {
Teacher t = new Teacher();
Expand All @@ -273,7 +277,7 @@ protected List<Teacher> getTeachers(int[] ids) {

protected List<Student> getStudents(int[] ids) {
List<Student> students = new ArrayList<Student>();
Cursor cursor = Connector.getDatabase().query("Student", null, getWhere(ids), null, null,
Cursor cursor = Connector.getDatabase().query(getTableName(Student.class), null, getWhere(ids), null, null,
null, null);
if (cursor.moveToFirst()) {
Student s = new Student();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.litepal.crud.DataSupport;
import org.litepal.exceptions.DataSupportException;
import org.litepal.util.DBUtility;

import android.database.sqlite.SQLiteException;

Expand Down Expand Up @@ -33,8 +34,19 @@ public class DeleteTest extends LitePalTestCase {
private IdCard johnCard;

private IdCard mikeCard;

private void createClassroomStudentsTeachers() {

private String studentTable;

private String teacherTable;

@Override
protected void setUp() throws Exception {
super.setUp();
studentTable = DBUtility.getTableNameByClassName(Student.class.getName());
teacherTable = DBUtility.getTableNameByClassName(Teacher.class.getName());
}

private void createClassroomStudentsTeachers() {
initGameRoom();
initRose();
initJude();
Expand Down Expand Up @@ -256,50 +268,50 @@ public void testDeleteCascadeM2MAssociationsWithNoParameter() {
int rowsAffected = jude.delete();
assertEquals(2, rowsAffected);
assertNull(getStudent(jude.getId()));
assertM2MFalse("student", "teacher", jude.getId(), mike.getId());
assertM2M("student", "teacher", rose.getId(), mike.getId());
assertM2M("student", "teacher", rose.getId(), john.getId());
assertM2MFalse(studentTable, teacherTable, jude.getId(), mike.getId());
assertM2M(studentTable, teacherTable, rose.getId(), mike.getId());
assertM2M(studentTable, teacherTable, rose.getId(), john.getId());
createStudentsTeachersWithAssociations();
rowsAffected = rose.delete();
assertEquals(3, rowsAffected);
assertNull(getStudent(rose.getId()));
assertM2MFalse("student", "teacher", rose.getId(), mike.getId());
assertM2MFalse("student", "teacher", rose.getId(), john.getId());
assertM2M("student", "teacher", jude.getId(), mike.getId());
assertM2MFalse(studentTable, teacherTable, rose.getId(), mike.getId());
assertM2MFalse(studentTable, teacherTable, rose.getId(), john.getId());
assertM2M(studentTable, teacherTable, jude.getId(), mike.getId());
}

public void testDeleteCascadeM2MAssociationsById() {
createStudentsTeachersWithAssociations();
int rowsAffected = DataSupport.delete(Teacher.class, john.getId());
assertEquals(2, rowsAffected);
assertNull(getTeacher(john.getId()));
assertM2MFalse("student", "teacher", rose.getId(), john.getId());
assertM2M("student", "teacher", rose.getId(), mike.getId());
assertM2M("student", "teacher", jude.getId(), mike.getId());
assertM2MFalse(studentTable, teacherTable, rose.getId(), john.getId());
assertM2M(studentTable, teacherTable, rose.getId(), mike.getId());
assertM2M(studentTable, teacherTable, jude.getId(), mike.getId());
createStudentsTeachersWithAssociations();
rowsAffected = DataSupport.delete(Teacher.class, mike.getId());
assertEquals(3, rowsAffected);
assertNull(getTeacher(mike.getId()));
assertM2MFalse("student", "teacher", rose.getId(), mike.getId());
assertM2MFalse("student", "teacher", jude.getId(), mike.getId());
assertM2M("student", "teacher", rose.getId(), john.getId());
assertM2MFalse(studentTable, teacherTable, rose.getId(), mike.getId());
assertM2MFalse(studentTable, teacherTable, jude.getId(), mike.getId());
assertM2M(studentTable, teacherTable, rose.getId(), john.getId());
}

public void testDeleteAllCascadeM2MAssociations() {
createStudentsTeachersWithAssociations();
int rowsAffected = DataSupport.deleteAll(Teacher.class, "id=?", ""+john.getId());
assertEquals(2, rowsAffected);
assertNull(getTeacher(john.getId()));
assertM2MFalse("student", "teacher", rose.getId(), john.getId());
assertM2M("student", "teacher", rose.getId(), mike.getId());
assertM2M("student", "teacher", jude.getId(), mike.getId());
assertM2MFalse(studentTable, teacherTable, rose.getId(), john.getId());
assertM2M(studentTable, teacherTable, rose.getId(), mike.getId());
assertM2M(studentTable, teacherTable, jude.getId(), mike.getId());
createStudentsTeachersWithAssociations();
rowsAffected = DataSupport.deleteAll(Teacher.class, "id=?", ""+mike.getId());
assertEquals(3, rowsAffected);
assertNull(getTeacher(mike.getId()));
assertM2MFalse("student", "teacher", rose.getId(), mike.getId());
assertM2MFalse("student", "teacher", jude.getId(), mike.getId());
assertM2M("student", "teacher", rose.getId(), john.getId());
assertM2MFalse(studentTable, teacherTable, rose.getId(), mike.getId());
assertM2MFalse(studentTable, teacherTable, jude.getId(), mike.getId());
assertM2M(studentTable, teacherTable, rose.getId(), john.getId());
}

public void testDeleteAllCascadeWithConditions() {
Expand Down Expand Up @@ -351,25 +363,25 @@ public void testDeleteAll() {

public void testDeleteAllRows() {
createStudentsTeachersWithIdCard();
int rowsCount = getRowsCount("teacher");
int rowsCount = getRowsCount(teacherTable);
int affectedRows = 0;
affectedRows = DataSupport.deleteAll(Teacher.class);
assertTrue(rowsCount <= affectedRows);
rowsCount = getRowsCount("student");
rowsCount = getRowsCount(studentTable);
affectedRows = DataSupport.deleteAll(Student.class);
assertTrue(rowsCount<= affectedRows);
rowsCount = getRowsCount("idcard");
rowsCount = getRowsCount(DBUtility.getTableNameByClassName(IdCard.class.getName()));
affectedRows = DataSupport.deleteAll(IdCard.class);
assertTrue(rowsCount<=affectedRows);
createStudentsTeachersWithAssociations();
rowsCount = getRowsCount("teacher");
rowsCount = getRowsCount(teacherTable);
affectedRows = DataSupport.deleteAll(Teacher.class);
assertTrue(rowsCount<=affectedRows);
rowsCount = getRowsCount("student");
rowsCount = getRowsCount(studentTable);
affectedRows = DataSupport.deleteAll(Student.class);
assertTrue(rowsCount<=affectedRows);
rowsCount = getRowsCount("student_teacher");
affectedRows = DataSupport.deleteAll("student_teacher");
rowsCount = getRowsCount(DBUtility.getIntermediateTableName(studentTable, teacherTable));
affectedRows = DataSupport.deleteAll(DBUtility.getIntermediateTableName(studentTable, teacherTable));
assertTrue(rowsCount<=affectedRows);
}

Expand Down
Loading

0 comments on commit cda0b0c

Please sign in to comment.