Esempio n. 1
0
 private void checkTableExist(Class<?> clazz) {
   if (!tableIsExist(TableInfo.get(clazz))) {
     String sql = SqlBuilder.getCreatTableSQL(clazz);
     debugSql(sql);
     db.execSQL(sql);
   }
 }
Esempio n. 2
0
  private boolean tableIsExist(TableInfo table) {
    if (table.isCheckDatabese()) return true;

    Cursor cursor = null;
    try {
      String sql =
          "SELECT COUNT(*) AS c FROM sqlite_master WHERE type ='table' AND name ='"
              + table.getTableName()
              + "' ";
      debugSql(sql);
      cursor = db.rawQuery(sql, null);
      if (cursor != null && cursor.moveToNext()) {
        int count = cursor.getInt(0);
        if (count > 0) {
          table.setCheckDatabese(true);
          return true;
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (cursor != null) cursor.close();
      cursor = null;
    }

    return false;
  }
Esempio n. 3
0
 private void exeSqlInfo(SqlInfo sqlInfo) {
   if (sqlInfo != null) {
     debugSql(sqlInfo.getSql());
     db.execSQL(sqlInfo.getSql(), sqlInfo.getBindArgsAsArray());
   } else {
     Log.e(TAG, "sava error:sqlInfo is null");
   }
 }
Esempio n. 4
0
 /**
  * 根据条件查找,同时查找“多对一”的数据(只查找findClass中的类的数据)
  *
  * @param id
  * @param clazz
  * @param findClass 要查找的类
  */
 public <T> T findWithManyToOneById(Object id, Class<T> clazz, Class<?>... findClass) {
   checkTableExist(clazz);
   String sql = SqlBuilder.getSelectSQL(clazz, id);
   debugSql(sql);
   DbModel dbModel = findDbModelBySQL(sql);
   if (dbModel != null) {
     T entity = CursorUtils.dbModel2Entity(dbModel, clazz);
     return loadManyToOne(entity, clazz, findClass);
   }
   return null;
 }
Esempio n. 5
0
 /**
  * 根据sql语句查找数据,这个一般用于数据统计
  *
  * @param strSQL
  */
 public DbModel findDbModelBySQL(String strSQL) {
   debugSql(strSQL);
   Cursor cursor = db.rawQuery(strSQL, null);
   try {
     if (cursor.moveToNext()) {
       return CursorUtils.getDbModel(cursor);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     cursor.close();
   }
   return null;
 }
Esempio n. 6
0
 public List<DbModel> findDbModelListBySQL(String strSQL) {
   debugSql(strSQL);
   Cursor cursor = db.rawQuery(strSQL, null);
   List<DbModel> dbModelList = new ArrayList<DbModel>();
   try {
     while (cursor.moveToNext()) {
       dbModelList.add(CursorUtils.getDbModel(cursor));
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     cursor.close();
   }
   return dbModelList;
 }
Esempio n. 7
0
 /**
  * 根据主键查找数据(默认不查询多对一或者一对多的关联数据)
  *
  * @param id
  * @param clazz
  */
 public <T> T findById(Object id, Class<T> clazz) {
   checkTableExist(clazz);
   SqlInfo sqlInfo = SqlBuilder.getSelectSqlAsSqlInfo(clazz, id);
   if (sqlInfo != null) {
     debugSql(sqlInfo.getSql());
     Cursor cursor = db.rawQuery(sqlInfo.getSql(), sqlInfo.getBindArgsAsStringArray());
     try {
       if (cursor.moveToNext()) {
         return CursorUtils.getEntity(cursor, clazz, this);
       }
     } catch (Exception e) {
       e.printStackTrace();
     } finally {
       cursor.close();
     }
   }
   return null;
 }
Esempio n. 8
0
 /**
  * 根据条件查找所有数据
  *
  * @param clazz
  * @param strSQL
  */
 private <T> List<T> findAllBySql(Class<T> clazz, String strSQL) {
   checkTableExist(clazz);
   debugSql(strSQL);
   Cursor cursor = db.rawQuery(strSQL, null);
   try {
     List<T> list = new ArrayList<T>();
     while (cursor.moveToNext()) {
       T t = CursorUtils.getEntity(cursor, clazz, this);
       list.add(t);
     }
     return list;
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (cursor != null) cursor.close();
     cursor = null;
   }
   return null;
 }
Esempio n. 9
0
 /**
  * 根据条件删除数据
  *
  * @param clazz
  * @param strWhere 条件为空的时候 将会删除所有的数据
  */
 public void deleteByWhere(Class<?> clazz, String strWhere) {
   checkTableExist(clazz);
   String sql = SqlBuilder.buildDeleteSql(clazz, strWhere);
   debugSql(sql);
   db.execSQL(sql);
 }