@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 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 <T> ArrayList<T> queryAll(Class<T> claxx) {
   acquireReference();
   try {
     SQLStatement stmt = new QueryBuilder(claxx).createStatement();
     return stmt.query(mHelper.getReadableDatabase(), claxx);
   } 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;
 }
 /** 主动删除其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 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 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 queryCount(QueryBuilder qb) {
   acquireReference();
   try {
     SQLiteDatabase db = mHelper.getReadableDatabase();
     SQLStatement stmt = qb.createStatementForCount();
     return stmt.queryForLong(db);
   } 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 11
0
 @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;
 }
Exemplo n.º 12
0
 @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;
 }
Exemplo n.º 13
0
 @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;
 }
Exemplo n.º 14
0
 @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;
 }
Exemplo n.º 15
0
 /** 删除从[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;
 }
Exemplo n.º 16
0
 /** refCountIsZero 降到0时自动触发释放各种资源 */
 @Override
 protected void onAllReferencesReleased() {
   mConfig = null;
   mHelper.close();
   mTableManager.clear();
 }
Exemplo n.º 17
0
 @Override
 public synchronized SQLiteDatabase getWritableDatabase() {
   return mHelper.getWritableDatabase();
 }
Exemplo n.º 18
0
 @Override
 public <T> ArrayList<T> query(QueryBuilder qb) {
   SQLiteDatabase db = mHelper.getReadableDatabase();
   // mTableManager.checkOrCreateTable(db, qb.getQueryClass());
   return qb.createStatement().query(db, qb.getQueryClass());
 }