搭建自己的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; }
至此又完善了一步,距离成型越来越近了……
相关文章推荐
- 搭建自己的SQLiteDataBase框架(-)
- 搭建自己的SQLiteDataBase框架(二)
- 搭建自己的SQLiteDataBase框架(五)
- 搭建自己的SQLiteDataBase框架(三)
- 搭建自己的MVP框架
- 如何自己成功搭建一个SSM框架的WEB项目
- Volley框架的搭建,可以直接用到自己的APP中(参考别人的代码搭建的)
- Android中搭建自己的项目网络框架
- 创业互联网公司如何搭建自己的技术框架
- 搭建自己的volley网络请求框架
- 从框架到完整项目搭建,实战项目《约个球》(5)-为我们自己的框架导入网络的开源框架
- 创业互联网公司如何搭建自己的技术框架
- PHP搭建自己的web框架-路由
- 创业互联网公司如何搭建自己的技术框架
- 创业互联网公司如何搭建自己的技术框架
- ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防
- 搭建自己域名的hexo框架next风格的博客
- 利用Admin-LTE项目搭建自己前端的开发框架模板
- 创业互联网公司如何搭建自己的技术框架