极速开发框架dhroid之sqlite优化

前端之家收集整理的这篇文章主要介绍了极速开发框架dhroid之sqlite优化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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数据插入数据库,读取使用 .fromJson( String json,sans-serif; font-size:14px; line-height:21px">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下载

原文链接:https://www.f2er.com/sqlite/200496.html

猜你在找的Sqlite相关文章