极速开发框架dhroid之sqlite优化

来源:互联网 发布:淘宝卖家怎么登录 编辑:程序博客网 时间:2024/05/16 13:57
1、将sqlite的最轻量orm框架优化

2、添加方法同步锁

private Lock writeLock = new ReentrantLock();        private volatile boolean writeLocked = false;        private void lock() {                writeLock.lock();                writeLocked = true;        }        private void unlock() {                if (writeLocked) {                        writeLock.unlock();                        writeLocked = false;                }        }                /**         * 保存         * @param obj         */        public void save(Object obj) {                if (obj == null)                        return;                try{                        lock();                        checkOrCreateTable(obj.getClass());                        SqlProxy proxy = SqlProxy.save(obj);                        db.execSQL(proxy.getSql(), proxy.paramsArgs());                }finally{                        unlock();                }        }

3、添加数据库表字段动态增删改

/**         * 检查表         * @param clazz         */        private void checkOrCreateTable(Class<?> clazz) {                EntityInfo entity = EntityInfo.build(clazz);                if (entity.isChecked())                        return;                                Cursor cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' and name='"+entity.getTable()+"' order by name",null);                Integer count = cursor != null ? cursor.getCount() : null;                if(cursor != null)                         cursor.close();                if(count != null && count > 0){                        String [] columnNames = getColumnNames(db, entity.getTable());                        if(columnNames != null && columnNames.length > 0){                                if(changeTable(clazz, columnNames)){                                        String columns = "";                                        boolean first = true;                                        List<ColumnInfo> list = entity.getColumnList();                                        for(int i=0,length=columnNames.length;i<length;i++){                                                for(ColumnInfo mColumnInfo : list){                                                        if(mColumnInfo.getColumName().equals(columnNames)){                                                                columns += (first ? "" : ",")+columnNames;                                                                first = false;                                                        }                                                }                                        }                                                                                if(TextUtils.isEmpty(columns)){                                                //删除                                                db.execSQL("DROP TABLE " + entity.getTable());                                                //创建表                                                String sql = getCreatTableSQL(clazz);                                                db.execSQL(sql);                                        }else{                                                String tempTableName = entity.getTable() + "_temp";                                                //表重命名                                                db.execSQL("ALTER TABLE " + entity.getTable() + " RENAME TO " + tempTableName);                                                //创建表                                                String sql = getCreatTableSQL(clazz);                                                db.execSQL(sql);                                                //旧数据转移                                                db.execSQL("INSERT INTO "+entity.getTable()+"("+columns+") SELECT "+columns+" FROM "+tempTableName);                                                //删除临时表                                                db.execSQL("DROP TABLE "+tempTableName);                                        }                                }                        }else{                                //删除                                db.execSQL("DROP TABLE " + entity.getTable());                                //创建表                                String sql = getCreatTableSQL(clazz);                                db.execSQL(sql);                        }                }else{                        //创建表                        String sql = getCreatTableSQL(clazz);                        db.execSQL(sql);                }                entity.setChecked(true);        }

4、本框架没有链表查询功能,int、Integer、long、Long、float、Float、double、Double、Boolean、String、Date以外字段利用GSON中的Gson.toJson(Object src, Type typeOfSrc)获得json数据插入数据库,读取使用Gson.fromJson(String json, Type typeOfT)获得相应对象,具体看DEMO

/**         * 获取属性         * @param o         * @param info         * @return         */        public static Object getProperty(Object o,ColumnInfo info){                try {                        if(info.isBean()){                                Field f = info.getField();                                f.setAccessible(true);                                Object value = f.get(o);                                Gson gson = new GsonBuilder().create();                                return gson.toJson(value,f.getGenericType());                        }else{                                Field f = info.getField();                                f.setAccessible(true);                                return        f.get(o);                        }                } catch (Exception e) {                        e.printStackTrace();                }                return null;        }

/**         * 添加屬性         * @param o         * @param info         * @param value         */        public static void setProperty(Object o,ColumnInfo info,Object value){                try {                        if(info.isBean()){                                Field f = info.getField();                                Gson gson = new GsonBuilder().create();                                Object obj = gson.fromJson((String) value, f.getGenericType());                                f.setAccessible(true);                                f.set(o, obj);                        }else{                                Field f = info.getField();                                f.setAccessible(true);                                f.set(o, value);                        }                } catch (Exception e) {                        e.printStackTrace();                }        }

云盘DEMO下载

0 0
原创粉丝点击