Reputation: 2790
Hope all are fine. I am building an application whose database size is increasing. Increase
in terms of Data
and Tables and associated columns
. I have made a dynamic structure to send Models
to DatabaseHelper
but in the DatabaseHelper
i have to put code like instanceOf
to make appropriate ContentValues
. This is making me annoyed. Dynamic
is now going into sort of if else if
which of-course i do not want. Please see the following code-snippet to get the better idea.
* Generic Function to insert Persons, it could be Teacher or Student *
public void insertPersons(ArrayList<Person> persons) {
for(Person person : persons) {
insertOrUpdatePerson(person);
}
}
* This is making me annoyed to put instanceof. where is Dynamics now??? *
public void insertOrUpdatePerson(Person person) {
if(person instanceof Teacher)
insertOrUpdateTeacher(person);
else {
ClassStudent student = (ClassStudent) person;
insertOrUpdateStudent(student);
}
}
public void insertOrUpdateStudent(ClassStudent student) {
try {
if(student.getPk_id() > 0) {
updateRecord(getWritableDatabase(), TABLE_STUDENT_DATA, createContentValues(student), student);
} else {
int row_id = insertRecord(getWritableDatabase(), TABLE_STUDENT_DATA, createContentValues(student), student);
student.setPk_id(row_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void insertOrUpdateTeacher(Person person) {
try {
Teacher teacher = (Teacher) person;
Cursor cursor = getWritableDatabase().rawQuery("SELECT * FROM " + TABLE_TEACHERS + " WHERE " +
teacher_id + " = " + teacher.getPerson_id(), null);
if(cursor != null && cursor.moveToFirst()) {
teacher.setPk_id(cursor.getInt(cursor.getColumnIndexOrThrow(pk_id)));
}
if(teacher.getPk_id() > 0) {
updateRecord(getWritableDatabase(), TABLE_TEACHERS, createContentValues(teacher), teacher);
} else {
int row_id = insertRecord(getWritableDatabase(), TABLE_TEACHERS, createContentValues(teacher), teacher);
teacher.setPk_id(row_id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
* Multiple tables and multiple content values, code is making me feel bad *
private ContentValues createContentValues(ClassStudent student) {
ContentValues cv = new ContentValues();
cv.put(school_id, student.getSchool_id());
cv.put(student_id, student.getPerson_id());
return cv;
}
private ContentValues createContentValues(Teacher person) {
ContentValues cv = new ContentValues();
cv.put(teacher_id, person.getPerson_id());
cv.put(name, person.getPerson_name());
return cv;
}
What could be the other way to a situation like this? Should Model be responsible for making contentValues? OR what other architecture should i follow? I don't want to use ORMs
Upvotes: 0
Views: 205
Reputation: 136
I suggest you must use Interfaces to tackle this situation.
Create an interface with insert() and getContentValues() and implement them on Base classes.
Example:
public interface PersonInterface {
void insert(ContentValues values);
void getContentValues();
}
public class Person implements PersonInterface {
void insert(ContentValues values) {}
void getContentValues() {}
}
public class Teacher extends Person {
void insert(ContentValues values) {
// your Teacher record insert code goes here
}
void getContentValues() {
// return Teacher content values
}
}
public class Student extends Person {
void insert(ContentValues values) {
// your Student record insert code goes here
}
void getContentValues() {
// return Student content values
}
}
public class DBHelper {
public void insertPersons(ArrayList<Person> persons) {
for(Person person : persons) {
insertOrUpdatePerson(person);
}
public void insertOrUpdatePerson(Person person) {
person.insert(person.getContentValues());
}
}
Upvotes: 1
Reputation: 1773
For SQLite DB structure in android, i generally use the BoD Content provider generator . It is super easy to use wherein you just pass the json values of your table columns and it will automatically generate a content provider class for you. For each new table you can have your separate content provider and this makes the code more efficient.
Moreover, you can just use the command lines commands to generate the structure without importing anything in your gradle file. Please go through the link about which contains all the syntactical information that you would require. Thanks.
Upvotes: 0