您的位置:首页 > 数据库

搭建自己的SQLiteDataBase框架(四)

2016-11-17 10:35 239 查看

搭建自己的SQLiteDataBase框架(四)

一对一关系,此时就有两种情况,1,只存两者的关联关系,2,将另一实体也存入数据库中

此时引入一对一关系,Developer.java如下:

@Table(name = "developer")
public class Developer implements Serializable {
@Column(id = true)
private String id;
@Column
private String name;
@Column
private int age;
//一对一关系,是否写入数据库
@Column(type = Column.ColumnType.TONE, autoRefresh = true)
private Company company;
@Column(type = Column.ColumnType.SERIALIZABLE)
private ArrayList<Skill> skills;
}

@Table(name = "company")
public class Company {
@Column(id = true)
private String id;
@Column
private String name;
@Column
private String url;
@Column
private String tel;
@Column
private String address;
private String group;
}


完善建表语句:添加一对一关系

//加[]防止关键字冲突
public static String getOneColumnStmt(Field field) {
if (field.isAnnotationPresent(Column.class)) {
Column column = field.getAnnotation(Column.class);
String name = column.value();
if (!TextUtils.isValidate(name)) {
name = "[" + field.getName() + "]";
} else {
name = "[" + name + "]";
}
String type = null;
Class<?> clz = field.getType();
if (clz == String.class) {
type = " TEXT ";
} else if (clz == int.class || clz == Integer.class) {
type = " Integer ";
} else {
Column.ColumnType columnType = column.type();
//添加一对一关系
if (columnType == Column.ColumnType.TONE) {
type = " TEXT ";
} else if (columnType == Column.ColumnType.SERIALIZABLE) {
type = " BLOB ";
} else if (columnType == Column.ColumnType.TMANY) {
//TODO TMANY

}
}
name += type;
if (column.id()) {
name += " primary key ";
}
return name;
}
return "";
}


完善增改查功能:

public <T> void newOrUpdate(T t) {
ContentValues contentValues = new ContentValues();
try {
String idValue = (String) id_field_.get(t);
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
field.setAccessible(true);
Class<?> clz = field.getType();
if (clz == String.class) {
System.out.println("key:" + DBUtils.getColumnName(field) + ",value:" + field.get(t).toString());
contentValues.put(DBUtils.getColumnName(field), field.get(t).toString());
} else if (clz == int.class || clz == Integer.class) {
contentValues.put(DBUtils.getColumnName(field), field.getInt(t));
} else {

Column column = field.getAnnotation(Column.class);
Column.ColumnType columnType = column.type();
if (!TextUtils.isValidate(columnType.name())) {
throw new IllegalArgumentException("you should set  type to the special column:" + t.getClass().getSimpleName() + "." + field.getName());
}
//增加Serializable类型
if (columnType == Column.ColumnType.SERIALIZABLE) {
contentValues.put(DBUtils.getColumnName(field), SerializeUtil.serialize(field.get(t)));
}else if (columnType == Column.ColumnType.TONE) {
Object tone = field.get(t);
if (tone == null) {
continue;
}
if (column.autoRefresh()) {
// save object to related table
DBManager.getInstance().getDao(tone.getClass()).newOrUpdate(tone);

}
if (tone.getClass().isAnnotationPresent(Table.class)) {
String idName = DBUtils.getIDColumnName(tone.getClass());
Field id_field = tone.getClass().getDeclaredField(idName);
id_field.setAccessible(true);
contentValues.put(DBUtils.getColumnName(field), id_field.get(tone).toString());
}
}

}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("newOrUpdate:" + e.toString());
}
newOrUpdate(mTableName, contentValues);
}

public T queryById(String id) {
Cursor cursor = rawQuery(mTableName, mIdName + "=?", new String[]{id});
T t = null;
if (cursor.moveToNext()) {
try {
t = clz.newInstance();
for (Field field : fields) {
if (field.isAnnotationPresent(Column.class)) {
field.setAccessible(true);
Class<?> clazz = field.getType();
if (clazz == int.class || clazz == Integer.class) {
field.setInt(t, cursor.getInt(cursor.getColumnIndex(DBUtils.getColumnName(field))));
} else if (clazz == String.class) {
field.set(t, cursor.getString(cursor.getColumnIndex(DBUtils.getColumnName(field))));
} else {

// TONE columnType.newInstance -- setId--set to T (lazy load)
// Serializable deserializable to object --set to T (lazy load)
Column column = field.getAnnotation(Column.class);
Column.ColumnType columnType = column.type();
if (!TextUtils.isValidate(columnType.name())) {
throw new IllegalArgumentException("you should set  type to the special column:" + t.getClass().getSimpleName() + "." + field.getName());
}
//增加Serializable类型
if (columnType == Column.ColumnType.SERIALIZABLE) {
field.set(t, SerializeUtil.deserialize(cursor.getBlob(cursor.getColumnIndex(DBUtils.getColumnName(field)))));
} else if (columnType == Column.ColumnType.TONE) {//增加一对一关系
String idtone = cursor.getString(cursor.getColumnIndex(DBUtils.getColumnName(field)));
if (!TextUtils.isValidate(idtone)) {
continue;
}
Object tone = null;
if (column.autoRefresh()) {
//去数据库查询
tone = DBManager.getInstance().getDao(field.getType()).queryById(idtone);
} else {
tone = field.getType().newInstance();
if (field.getType().isAnnotationPresent(Table.class)) {
String idName = DBUtils.getIDColumnName(field.getType());
Field id_field = field.getType().getDeclaredField(idName);
id_field.setAccessible(true);
id_field.set(tone, idtone);
}
}
field.set(t, tone);
}

}

}
}

} catch (Exception e) {
e.printStackTrace();
System.out.println("queryById:" + e.toString());
}
}
return t;
}


至此又完善了一步,距离成型越来越近了……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库