@Override
 public int deleteAll(Class<?> claxx) {
   acquireReference();
   try {
     SQLiteDatabase db = mHelper.getWritableDatabase();
     SQLStatement stmt = SQLBuilder.buildDeleteAllSql(claxx);
     int num = stmt.execDelete(db);
     // 删除关系映射
     final MapInfo mapTable = SQLBuilder.buildDelAllMappingSql(claxx);
     if (mapTable != null && !mapTable.isEmpty()) {
       Transaction.execute(
           db,
           new Worker<Boolean>() {
             @Override
             public Boolean doTransaction(SQLiteDatabase db) throws Exception {
               if (mapTable.delOldRelationSQL != null) {
                 for (SQLStatement st : mapTable.delOldRelationSQL) {
                   long rowId = st.execDelete(db);
                   if (Log.isPrint) {
                     Log.i(TAG, "Exec delete mapping success, nums: " + rowId);
                   }
                 }
               }
               return true;
             }
           });
     }
     return num;
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
 @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();
   }
 }
 @Override
 public boolean dropTable(String tableName) {
   acquireReference();
   try {
     return SQLBuilder.buildDropTable(tableName).execute(mHelper.getWritableDatabase());
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return false;
 }
 @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;
 }
 /** 主动删除其mapping数据 */
 @Override
 public int delete(Object entity) {
   acquireReference();
   try {
     return SQLBuilder.buildDeleteSql(entity)
         .execDeleteWithMapping(mHelper.getWritableDatabase(), entity, mTableManager);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
 @Override
 public int update(Object entity, ColumnsValue cvs, ConflictAlgorithm conflictAlgorithm) {
   acquireReference();
   try {
     SQLiteDatabase db = mHelper.getWritableDatabase();
     return SQLBuilder.buildUpdateSql(entity, cvs, conflictAlgorithm)
         .execUpdateWithMapping(db, entity, mTableManager);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
 @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 update(
     Collection<?> collection, ColumnsValue cvs, ConflictAlgorithm conflictAlgorithm) {
   acquireReference();
   try {
     if (!Checker.isEmpty(collection)) {
       SQLiteDatabase db = mHelper.getWritableDatabase();
       Object entity = collection.iterator().next();
       SQLStatement stmt = SQLBuilder.buildUpdateAllSql(entity, cvs, conflictAlgorithm);
       return stmt.execUpdateCollection(db, collection, cvs, mTableManager);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
 @Override
 public int save(Collection<?> collection) {
   acquireReference();
   try {
     if (!Checker.isEmpty(collection)) {
       SQLiteDatabase db = mHelper.getWritableDatabase();
       Object entity = collection.iterator().next();
       SQLStatement stmt = SQLBuilder.buildReplaceAllSql(entity);
       mTableManager.checkOrCreateTable(db, entity);
       return stmt.execInsertCollection(db, collection, mTableManager);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
  /**
   * 数据库分析 通过读数据库得到一张表的全部列名
   *
   * @param db
   * @param tableName
   * @return
   */
  public ArrayList<String> getAllColumnsFromSQLite(SQLiteDatabase db, final String tableName) {
    final EntityTable table = getTable(SQLiteColumn.class, false);
    final ArrayList<String> list = new ArrayList<String>();

    SQLStatement st = SQLBuilder.buildColumnsObtainAll(tableName);
    Querier.doQuery(
        db,
        st,
        new Querier.CursorParser() {
          @Override
          public void parseEachCursor(SQLiteDatabase db, Cursor c) throws Exception {
            SQLiteColumn col = new SQLiteColumn();
            DataUtil.injectDataToObject(c, col, table);
            list.add(col.name);
          }
        });

    return list;
  }
 /** 删除从[start,end]的数据 此方法暂不会删除关联映射表里的关系数据 */
 @Override
 public int delete(Class<?> claxx, long start, long end, String orderAscColumn) {
   acquireReference();
   try {
     if (start < 0 || end < start) {
       throw new RuntimeException("start must >=0 and smaller than end");
     }
     if (start != 0) {
       start -= 1;
     }
     end = end == Integer.MAX_VALUE ? -1 : end - start;
     SQLStatement stmt = SQLBuilder.buildDeleteSql(claxx, start, end, orderAscColumn);
     return stmt.execDelete(mHelper.getWritableDatabase());
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     releaseReference();
   }
   return SQLStatement.NONE;
 }
 /**
  * 初始化全部表及其列名,初始化失败,则无法进行下去。
  *
  * @param db
  */
 private void initAllTablesFromSQLite(SQLiteDatabase db) {
   synchronized (mSqlTableMap) {
     if (Checker.isEmpty(mSqlTableMap)) {
       if (Log.isPrint) {
         Log.i(TAG, "Initialize SQL table start--------------------->");
       }
       SQLStatement st = SQLBuilder.buildTableObtainAll();
       final EntityTable table = getTable(SQLiteTable.class, false);
       Querier.doQuery(
           db,
           st,
           new Querier.CursorParser() {
             @Override
             public void parseEachCursor(SQLiteDatabase db, Cursor c) throws Exception {
               SQLiteTable sqlTable = new SQLiteTable();
               DataUtil.injectDataToObject(c, sqlTable, table);
               ArrayList<String> colS = getAllColumnsFromSQLite(db, sqlTable.name);
               if (Checker.isEmpty(colS)) {
                 // 如果读数据库失败了,那么解析建表语句
                 Log.e(TAG, "读数据库失败了,开始解析建表语句");
                 colS = transformSqlToColumns(sqlTable.sql);
               }
               sqlTable.columns = new HashMap<String, Integer>();
               for (String col : colS) {
                 sqlTable.columns.put(col, 1);
               }
               if (Log.isPrint) {
                 Log.d(TAG, "Find One SQL Table: " + sqlTable);
               }
               mSqlTableMap.put(sqlTable.name, sqlTable);
             }
           });
       if (Log.isPrint) {
         Log.i(TAG, "Initialize SQL table end  ---------------------> " + mSqlTableMap.size());
       }
     }
   }
 }
 @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;
 }
 /** 建立新表 */
 private boolean createTable(SQLiteDatabase db, EntityTable table) {
   return SQLBuilder.buildCreateTable(table).execute(db);
 }
 /**
  * 检查表是否存在,存在的话检查是否需要改动,添加列字段。 注:sqlite仅仅支持表改名、表添加列两种alter方式。表中修改、刪除列是不被直接支持的。 不能新加主键:The column
  * may not have a PRIMARY KEY or UNIQUE constraint.
  *
  * <p>http://www.sqlite.org/lang_altertable.html
  */
 private boolean checkExistAndColumns(SQLiteDatabase db, EntityTable entityTable) {
   if (!Checker.isEmpty(mSqlTableMap)) {
     SQLiteTable sqlTable = mSqlTableMap.get(entityTable.name);
     if (sqlTable != null) {
       if (Log.isPrint) {
         Log.d(TAG, "Table [" + entityTable.name + "] Exist");
       }
       if (!sqlTable.isTableChecked) {
         // 表仅进行一次检查,检验是否有新字段加入。
         sqlTable.isTableChecked = true;
         if (Log.isPrint) {
           Log.i(TAG, "Table [" + entityTable.name + "] check column now.");
         }
         if (entityTable.key != null) {
           if (sqlTable.columns.get(entityTable.key.column) == null) {
             SQLStatement stmt = SQLBuilder.buildDropTable(sqlTable.name);
             stmt.execute(db);
             if (Log.isPrint) {
               Log.i(
                   TAG,
                   "Table ["
                       + entityTable.name
                       + "] Primary Key has changed, "
                       + "so drop and recreate it later.");
             }
             return false;
           }
         }
         if (entityTable.pmap != null) {
           ArrayList<String> newColumns = new ArrayList<String>();
           for (String col : entityTable.pmap.keySet()) {
             if (sqlTable.columns.get(col) == null) {
               newColumns.add(col);
             }
           }
           if (!Checker.isEmpty(newColumns)) {
             for (String col : newColumns) {
               sqlTable.columns.put(col, 1);
             }
             int sum = insertNewColunms(db, entityTable.name, newColumns);
             if (Log.isPrint) {
               if (sum > 0) {
                 Log.i(
                     TAG,
                     "Table ["
                         + entityTable.name
                         + "] add "
                         + sum
                         + " new column : "
                         + newColumns);
               } else {
                 Log.e(
                     TAG,
                     "Table ["
                         + entityTable.name
                         + "] add "
                         + sum
                         + " new column error : "
                         + newColumns);
               }
             }
           }
         }
       }
       return true;
     }
   }
   if (Log.isPrint) {
     Log.d(TAG, "Table [" + entityTable.name + "] Not Exist");
   }
   return false;
 }