/**
   * Should return the same count as there are entries in the result set if the where-clause matches
   * several entries.
   */
  public void testCountWhereClause() {
    cleanTable();
    populateTable();

    From from = new Select().from(MockModel.class).where("intField = ?", 1);

    final List<MockModel> list = from.execute();
    final int count = from.count();

    assertEquals(2, count);
    assertEquals(list.size(), count);
  }
  /** Should return the same count as there are entries in the result set/table. */
  public void testCountTable() {
    cleanTable();
    populateTable();

    From from = new Select().from(MockModel.class);

    final List<MockModel> list = from.execute();
    final int count = from.count();

    assertEquals(3, count);
    assertEquals(list.size(), count);
  }
  /**
   * Should return the total number of rows, even if the rows are grouped. May seem weird, just test
   * it in an SQL explorer.
   */
  public void testCountGroupBy() {
    cleanTable();
    populateTable();

    From from = new Select().from(MockModel.class).groupBy("intField").having("intField = 1");

    final List<MockModel> list = from.execute();
    final int count = from.count();

    assertEquals(2, count);
    assertEquals(1, list.size());
  }
  private List<Tweet> getLocalTweetsPage(long newestId, int limit, String filterClause) {
    Util.assertNotUIThread();

    From partial =
        new Select().from(Tweet.class).where(newestId > 0 ? "remote_id < " + newestId : "");

    if (!filterClause.isEmpty()) {
      partial = partial.where(filterClause);
    }

    return partial.orderBy("remote_id DESC").limit(limit).execute();
  }
  Cursor retrieveDataFromRoster() {

    String resultRecords;
    Cursor resultCursor = null;

    BaseTabFragment.FragIds fragIds = BaseTabFragment.FragIds.values()[tabIdloader];

    switch (fragIds) {
      case CONTACTS_FRAGMENT:
        {
          String groupToExcludeRequest =
              new Select()
                  .from(RosterGroupModel.class)
                  .where(DbColumns.NameCol + "='" + SmackRosterManager.FIRST_SORTED_GROUP + "'")
                  .toSql();
          Cursor groupCursor = Cache.openDatabase().rawQuery(groupToExcludeRequest, null);
          List<RosterGroupModel> groupModelList =
              SQLiteUtils.processCursor(RosterGroupModel.class, groupCursor);
          groupCursor.close();
          long groupToExcludeId = 0;
          if (groupModelList != null && groupModelList.size() > 0) {
            groupToExcludeId = groupModelList.get(0).getId();
          }
          From from = new Select().from(RosterEntryModel.class);

          String sqlReqText = "";
          if (groupToExcludeId != 0) {
            sqlReqText += " RosterGroupModel != " + groupToExcludeId;
          }

          if (!TextUtils.isEmpty(title_to_search)) {
            if (!TextUtils.isEmpty(sqlReqText)) {
              sqlReqText += " and ";
            }
            sqlReqText += "Name LIKE '%" + title_to_search.toLowerCase(Locale.getDefault()) + "%'";
          }

          if (jidToExcludeList != null && jidToExcludeList.size() > 0) {
            for (String jid : jidToExcludeList) {
              sqlReqText += " and " + DbColumns.FromJidCol + " != '" + jid + "'";
            }
          }

          if (!TextUtils.isEmpty(sqlReqText)) {
            from.where(sqlReqText);
          }

          String queryResults = from.orderBy("Name ASC").toSql();
          resultCursor = Cache.openDatabase().rawQuery(queryResults, null);

          // listCursor = getContactsList(queryResults);
          // listCursor = fetchResultCursor(RosterEntryModel.class);
          // SQLiteUtils.processCursor(RosterEntryModel.class, listCursor);
        }
        break;
      case ROBOTS_FRAGMENT:
        {
          //  List<RosterEntry> rosterEntries =
          /*RosterGroupModel rosterGroupModel = new Select().from(RosterGroupModel.class)//.where("name = ?", getContext().getString(R.string.robots))
                  .orderBy("name ASC")
                  .executeSingle();
          if (rosterGroupModel != null) {
              listCursor = rosterGroupModel.items();
          }*/
          String groupTableName = Cache.getTableInfo(RosterGroupModel.class).getTableName();
          resultRecords =
              new Select()
                  .from(RosterEntryModel.class)
                  .where(
                      "RosterEntryModels.RosterGroupModel IN (SELECT _id"
                          + " FROM "
                          + groupTableName
                          + " WHERE name ='"
                          + SmackRosterManager.FIRST_SORTED_GROUP
                          + "')")
                  .orderBy("name ASC")
                  .toSql();

          //  tableName = Cache.getTableInfo(RosterGroupModel.class).getTableName();
          //  resultRecords = new Select().from(RosterGroupModel.class).where("name = ?",
          // getContext().getString(R.string.robots)).toSql();
          resultCursor = Cache.openDatabase().rawQuery(resultRecords, null);
        }
        break;
      case GROUPS_FRAGMENT:
        {
          /*List<RosterGroupModel> queryResults = new Select().from(RosterGroupModel.class)
                  .orderBy("name ASC").execute();

          for (RosterGroupModel group : queryResults) {
              group.items()
              Collections.sort(group.getEntries(), new EntrySortBasedOnName());
          }*/
          String tableName = Cache.getTableInfo(RosterGroupModel.class).getTableName();
          resultRecords = new Select("*").from(RosterGroupModel.class).toSql();
          resultCursor = Cache.openDatabase().rawQuery(resultRecords, null);
        }
        break;
    }

    return resultCursor;
  }