Пример #1
0
 public String getTableName() {
   if (clazzMapping == null) {
     return TableManager.getTableName(clazz);
   } else {
     return TableManager.getMapTableName(clazz, clazzMapping);
   }
 }
Пример #2
0
 /** 清空数据 */
 public synchronized void clear() {
   if (instance != null) {
     instance.mSqlTableList = null;
     instance.mEntityTableMap = null;
     instance = null;
   }
 }
 @Override
 public ArrayList<Relation> queryRelation(
     Class class1, Class class2, List<String> key1List, List<String> key2List) {
   acquireReference();
   try {
     SQLStatement stmt = SQLBuilder.buildQueryRelationSql(class1, class2, key1List, key2List);
     final EntityTable table1 = TableManager.getTable(class1);
     final EntityTable table2 = TableManager.getTable(class2);
     final ArrayList<Relation> list = new ArrayList<Relation>();
     Querier.doQuery(
         mHelper.getReadableDatabase(),
         stmt,
         new Querier.CursorParser() {
           @Override
           public void parseEachCursor(SQLiteDatabase db, Cursor c) throws Exception {
             Relation relation = new Relation();
             relation.key1 = c.getString(c.getColumnIndex(table1.name));
             relation.key2 = c.getString(c.getColumnIndex(table2.name));
             list.add(relation);
           }
         });
     return list;
   } finally {
     releaseReference();
   }
 }
Пример #4
0
 /**
  * 获取映射表信息(Entity Table) 注意映射表存储在MAP中,key 为 database name + table name, value 为 entity table。
  *
  * @return {@link EntityTable}
  */
 private EntityTable getMappingTable(String tableName, String column1, String column2) {
   EntityTable table = getEntityTable(dbName + tableName);
   if (table == null) {
     table = new EntityTable();
     table.name = tableName;
     table.pmap = new LinkedHashMap<String, Property>();
     table.pmap.put(column1, null);
     table.pmap.put(column2, null);
     TableManager.putEntityTable(dbName + tableName, table);
   }
   return table;
 }
 @Override
 public boolean dropTable(Object entity) {
   acquireReference();
   try {
     return SQLBuilder.buildDropTable(TableManager.getTable(entity))
         .execute(mHelper.getWritableDatabase());
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return false;
 }
 @Override
 public long save(Object entity) {
   acquireReference();
   try {
     SQLiteDatabase db = mHelper.getWritableDatabase();
     mTableManager.checkOrCreateTable(db, entity);
     return SQLBuilder.buildReplaceSql(entity).execInsertWithMapping(db, entity, mTableManager);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
 @Override
 public int insert(Collection<?> collection, ConflictAlgorithm conflictAlgorithm) {
   acquireReference();
   try {
     if (!Checker.isEmpty(collection)) {
       SQLiteDatabase db = mHelper.getWritableDatabase();
       Object entity = collection.iterator().next();
       SQLStatement stmt = SQLBuilder.buildInsertAllSql(entity, conflictAlgorithm);
       mTableManager.checkOrCreateTable(db, entity);
       return stmt.execInsertCollection(db, collection, mTableManager);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
 @Override
 public <T> T queryById(String id, Class<T> claxx) {
   acquireReference();
   try {
     SQLiteDatabase db = mHelper.getReadableDatabase();
     EntityTable table = TableManager.getTable(claxx);
     SQLStatement stmt =
         new QueryBuilder(claxx)
             .where(table.key.column + "=?", new String[] {id})
             .createStatement();
     ArrayList<T> list = stmt.query(db, claxx);
     if (!Checker.isEmpty(list)) {
       return list.get(0);
     }
   } finally {
     releaseReference();
   }
   return null;
 }
 @Override
 public int delete(Class<?> claxx, WhereBuilder where) {
   acquireReference();
   try {
     EntityTable table = TableManager.getTable(claxx);
     if (table.key != null && !Checker.isEmpty(table.mappingList)) {
       List<?> list =
           query(QueryBuilder.create(claxx).columns(new String[] {table.key.column}).where(where));
       delete(list);
     } else {
       return where.createStatementDelete(claxx).execDelete(mHelper.getWritableDatabase());
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
Пример #10
0
 @Override
 public int delete(final Collection<?> collection) {
   acquireReference();
   try {
     if (!Checker.isEmpty(collection)) {
       EntityTable table = TableManager.getTable(collection.iterator().next());
       if (table.key != null) {
         SQLStatement stmt = SQLBuilder.buildDeleteSql(collection);
         return stmt.execDeleteCollection(
             mHelper.getWritableDatabase(), collection, mTableManager);
       } else {
         Integer size =
             Transaction.execute(
                 mHelper.getWritableDatabase(),
                 new Worker<Integer>() {
                   @Override
                   public Integer doTransaction(SQLiteDatabase db) throws Exception {
                     for (Object entity : collection) {
                       SQLBuilder.buildDeleteSql(entity)
                           .execDeleteWithMapping(db, entity, mTableManager);
                     }
                     if (Log.isPrint) {
                       Log.i(TAG, "Exec delete(no primarykey) :" + collection.size());
                     }
                     return collection.size();
                   }
                 });
         return size == null ? 0 : size;
       }
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
Пример #11
0
 private <E, T> boolean keepMapping(Collection<E> col1, Collection<T> col2)
     throws IllegalAccessException, InstantiationException {
   Class claxx1 = col1.iterator().next().getClass();
   Class claxx2 = col2.iterator().next().getClass();
   EntityTable table1 = TableManager.getTable(claxx1);
   EntityTable table2 = TableManager.getTable(claxx2);
   if (table1.mappingList != null) {
     for (MapProperty mp : table1.mappingList) {
       Class itemClass;
       Class fieldClass = mp.field.getType();
       if (mp.isToMany()) {
         // N对多关系
         if (ClassUtil.isCollection(fieldClass)) {
           itemClass = FieldUtil.getGenericType(mp.field);
         } else {
           throw new RuntimeException(
               "OneToMany and ManyToMany Relation, You must use collection object");
         }
       } else {
         itemClass = fieldClass;
       }
       if (itemClass == claxx2) {
         ArrayList<String> key1List = new ArrayList<String>();
         HashMap<String, Object> map1 = new HashMap<String, Object>();
         // 构建第1个对象的key集合以及value映射
         for (Object o1 : col1) {
           if (o1 != null) {
             Object key1 = FieldUtil.get(table1.key.field, o1);
             if (key1 != null) {
               key1List.add(key1.toString());
               map1.put(key1.toString(), o1);
             }
           }
         }
         ArrayList<Relation> mapList = queryRelation(claxx1, claxx2, key1List, null);
         if (!Checker.isEmpty(mapList)) {
           HashMap<String, Object> map2 = new HashMap<String, Object>();
           // 构建第2个对象的value映射
           for (Object o2 : col2) {
             if (o2 != null) {
               Object key2 = FieldUtil.get(table2.key.field, o2);
               if (key2 != null) {
                 map2.put(key2.toString(), o2);
               }
             }
           }
           for (Relation m : mapList) {
             Object obj1 = map1.get(m.key1);
             Object obj2 = map2.get(m.key2);
             if (obj1 != null && obj2 != null) {
               if (mp.isToMany()) {
                 // N对多关系
                 if (ClassUtil.isCollection(fieldClass)) {
                   Collection col = (Collection) FieldUtil.get(mp.field, obj1);
                   if (col == null) {
                     col = (Collection) fieldClass.newInstance();
                     FieldUtil.set(mp.field, obj1, col);
                   }
                   col.add(obj2);
                 } else {
                   throw new RuntimeException(
                       "OneToMany and ManyToMany Relation, You must use collection object");
                 }
               } else {
                 FieldUtil.set(mp.field, obj1, obj2);
               }
             }
           }
           return true;
         }
       }
     }
   }
   return false;
 }
Пример #12
0
 /** refCountIsZero 降到0时自动触发释放各种资源 */
 @Override
 protected void onAllReferencesReleased() {
   mConfig = null;
   mHelper.close();
   mTableManager.clear();
 }