搭建自己的SQLiteDataBase框架(三)

搭建自己的SQLiteDataBase框架(三)

标签(空格分隔): 未分类


  • 无表结构的对象存储
@Table(name="developer")
class Developer{
    @Column(id = true)
    private String id;
    @Column
    private String name;
    @Column
    private int age;
    @Column(type = Column.ColumnType.SERIALIZABLE)
    private ArrayList<Skill> skills;
}

public class Skill implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String name;
    private String desc;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
}

相应的建表语句也要进行修改,其余建表语句不变

//确定字段名,字段类型
    public static String getOneColumnStmt(Field field) {
        if (field.isAnnotationPresent(Column.class)) {
            Column column = field.getAnnotation(Column.class);
            //字段名
            String name = column.name();
            //加[]防止关键字冲突
            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 {
                //TODO 添加Serializable类型
                 Column.ColumnType columnType = column.type();
                 if (columnType == Column.ColumnType.SERIALIZABLE){
                    type = " BLOB ";
                } 
            }
            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)));
                        }
                    }
                }
            }
        } 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)))));
                            } 
                        }

                    }
                }

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

测试以上功能:


 public void add() {
            Developer developer = new Developer();
            developer.setId("00001");
            developer.setName("Stay");
            developer.setAge(17);
            Skill skill = new Skill();
            skill.setName("coding");
            skill.setDesc("android");
            ArrayList<Skill> skills = new ArrayList<Skill>();
            skills.add(skill);
            developer.setSkills(skills);
            DBManager.getInstance().getDao(Developer.class).newOrUpdate(developer);
        }

        public void queryCompanyById() {
            Developer developer = DBManager.getInstance().getDao(Developer.class).queryById("00001");
            if (developer != null) {
                System.out.println(developer.toString());
            }
        }

建表语句
11-16 17:26:13.034 26324-26324/? I/System.out: create table if not exists developer ( [age] Integer ,[id] TEXT  primary key ,[name] TEXT ,[skills] BLOB )
输出结果
11-16 17:26:13.105 26324-26324/? I/System.out: id:00001,name:Stay,age:17,skills:[{"desc":"android","name":"coding"}]

正常,至此,又完善了一步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值