1、将sqlite的最轻量orm框架优化
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(); } }
原文链接:https://www.f2er.com/sqlite/200496.html