Exemple #1
0
  public List<Remind> getStoredData(int filter) {
    try {
      switch (filter) {
        case DatabaseHelper.FILTER_ALL_REMINDS:
          return SortUtil.sort(remindDao.queryForAll(), PreferenceUtil.getUsedSortMode());

        case DatabaseHelper.FILTER_WAITING_REMINDS:
          return SortUtil.sort(
              remindDao
                  .queryBuilder()
                  .where()
                  .eq(DatabaseHelper.COLUMN_STATUS, RemindUtil.STATUS_WAIT)
                  .query(),
              PreferenceUtil.getUsedSortMode());

        case DatabaseHelper.FILTER_DONE_REMINDS:
          return SortUtil.sort(
              remindDao
                  .queryBuilder()
                  .where()
                  .eq(DatabaseHelper.COLUMN_STATUS, RemindUtil.STATUS_DONE)
                  .query(),
              PreferenceUtil.getUsedSortMode());
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return null;
  }
  @Test
  public void testJoinGroup() throws Exception {
    Dao<Bar, Integer> barDao = createDao(Bar.class, true);
    Dao<Baz, Integer> bazDao = createDao(Baz.class, true);

    Bar bar1 = new Bar();
    bar1.val = 2234;
    assertEquals(1, barDao.create(bar1));
    Bar bar2 = new Bar();
    bar2.val = 324322234;
    assertEquals(1, barDao.create(bar2));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));
    Baz baz2 = new Baz();
    baz2.bar = bar2;
    assertEquals(1, bazDao.create(baz2));

    QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
    barQb.where().eq(Bar.VAL_FIELD, bar1.val);
    barQb.groupBy(Bar.ID_FIELD);
    List<Baz> results = bazDao.queryBuilder().query();
    assertEquals(2, results.size());
    results = bazDao.queryBuilder().groupBy(Baz.ID_FIELD).join(barQb).query();
    assertEquals(1, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);
  }
  @Test
  public void testJoinOrder() throws Exception {
    Dao<Bar, Integer> barDao = createDao(Bar.class, true);
    Dao<Baz, Integer> bazDao = createDao(Baz.class, true);

    Bar bar1 = new Bar();
    bar1.val = 2234;
    assertEquals(1, barDao.create(bar1));
    Bar bar2 = new Bar();
    bar2.val = 324322234;
    assertEquals(1, barDao.create(bar2));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));
    Baz baz2 = new Baz();
    baz2.bar = bar2;
    assertEquals(1, bazDao.create(baz2));

    QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
    barQb.orderBy(Bar.VAL_FIELD, true);
    List<Baz> results = bazDao.queryBuilder().join(barQb).query();
    assertEquals(2, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);
    assertEquals(bar2.id, results.get(1).bar.id);

    // reset the query to change the order direction
    barQb.reset();
    barQb.orderBy(Bar.VAL_FIELD, false);
    results = bazDao.queryBuilder().join(barQb).query();
    assertEquals(2, results.size());
    assertEquals(bar2.id, results.get(0).bar.id);
    assertEquals(bar1.id, results.get(1).bar.id);
  }
  @Test
  public void testLeftJoin() throws Exception {
    Dao<Bar, Integer> barDao = createDao(Bar.class, true);
    Dao<Baz, Integer> bazDao = createDao(Baz.class, true);

    Bar bar1 = new Bar();
    bar1.val = 2234;
    assertEquals(1, barDao.create(bar1));
    Bar bar2 = new Bar();
    bar2.val = 324322234;
    assertEquals(1, barDao.create(bar2));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));
    Baz baz2 = new Baz();
    baz2.bar = bar2;
    assertEquals(1, bazDao.create(baz2));
    Baz baz3 = new Baz();
    // no bar
    assertEquals(1, bazDao.create(baz3));

    QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
    List<Baz> results = bazDao.queryBuilder().query();
    assertEquals(3, results.size());
    results = bazDao.queryBuilder().join(barQb).query();
    assertEquals(2, results.size());

    results = bazDao.queryBuilder().leftJoin(barQb).query();
    assertEquals(3, results.size());

    results = bazDao.queryBuilder().join(barQb, JoinType.LEFT, JoinWhereOperation.AND).query();
    assertEquals(3, results.size());
  }
  @Test
  public void testQueryForForeign() throws Exception {
    Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
    Dao<Foreign, Object> foreignDao = createDao(Foreign.class, true);

    Foo foo = new Foo();
    foo.val = 1231;
    assertEquals(1, fooDao.create(foo));

    Foreign foreign = new Foreign();
    foreign.foo = foo;
    assertEquals(1, foreignDao.create(foreign));

    // use the auto-extract method to extract by id
    List<Foreign> results =
        foreignDao.queryBuilder().where().eq(Foreign.FOO_COLUMN_NAME, foo).query();
    assertEquals(1, results.size());
    assertEquals(foreign.id, results.get(0).id);

    // query for the id directly
    List<Foreign> results2 =
        foreignDao.queryBuilder().where().eq(Foreign.FOO_COLUMN_NAME, foo.id).query();
    assertEquals(1, results2.size());
    assertEquals(foreign.id, results2.get(0).id);
  }
  @Test
  public void testQueryRawColumns() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);
    Foo foo1 = new Foo();
    foo1.val = 1;
    foo1.equal = 10;
    assertEquals(1, dao.create(foo1));
    Foo foo2 = new Foo();
    foo2.val = 10;
    foo2.equal = 5;
    assertEquals(1, dao.create(foo2));
    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
    qb.selectRaw("COUNT(*)");
    GenericRawResults<String[]> rawResults = dao.queryRaw(qb.prepareStatementString());
    List<String[]> results = rawResults.getResults();
    assertEquals(1, results.size());
    // 2 rows inserted
    assertEquals("2", results.get(0)[0]);

    qb = dao.queryBuilder();
    qb.selectRaw("MIN(val)", "MAX(val)");
    rawResults = dao.queryRaw(qb.prepareStatementString());
    results = rawResults.getResults();
    assertEquals(1, results.size());
    String[] result = results.get(0);
    assertEquals(2, result.length);
    // foo1 has the maximum value
    assertEquals(Integer.toString(foo1.val), result[0]);
    // foo2 has the maximum value
    assertEquals(Integer.toString(foo2.val), result[1]);
  }
 @Test
 public void testOrderByRawArg() throws Exception {
   Dao<Foo, Integer> dao = createDao(Foo.class, true);
   Foo foo1 = new Foo();
   foo1.val = 1;
   assertEquals(1, dao.create(foo1));
   Foo foo2 = new Foo();
   foo2.val = 2;
   assertEquals(1, dao.create(foo2));
   List<Foo> results =
       dao.queryBuilder()
           .orderByRaw(
               "(" + Foo.VAL_COLUMN_NAME + " = ? ) DESC", new SelectArg(SqlType.INTEGER, 2))
           .query();
   assertEquals(2, results.size());
   assertEquals(foo2.id, results.get(0).id);
   assertEquals(foo1.id, results.get(1).id);
   results =
       dao.queryBuilder()
           .orderByRaw("(" + Foo.VAL_COLUMN_NAME + " = ? )", new SelectArg(SqlType.INTEGER, 2))
           .query();
   assertEquals(2, results.size());
   assertEquals(foo1.id, results.get(0).id);
   assertEquals(foo2.id, results.get(1).id);
 }
  @Test
  public void testPickTheRightJoinReverse() throws Exception {
    Dao<One, Integer> oneDao = createDao(One.class, true);
    Dao<Two, Integer> twoDao = createDao(Two.class, true);

    One one1 = new One();
    one1.val = 2234;
    assertEquals(1, oneDao.create(one1));
    One one2 = new One();
    one2.val = 324322234;
    assertEquals(1, oneDao.create(one2));

    Two two1 = new Two();
    two1.val = 431231232;
    two1.one = one1;
    assertEquals(1, twoDao.create(two1));
    Two two2 = new Two();
    two2.one = one2;
    assertEquals(1, twoDao.create(two2));

    QueryBuilder<Two, Integer> twoQb = twoDao.queryBuilder();
    twoQb.where().eq(Two.VAL_FIELD, two1.val);
    List<One> results = oneDao.queryBuilder().query();
    assertEquals(2, results.size());
    results = oneDao.queryBuilder().join(twoQb).query();
    assertEquals(1, results.size());
    assertEquals(two1.one.id, results.get(0).id);
  }
  @Test
  public void testMaxJoin() throws Exception {
    Dao<Foo, Object> dao = createDao(Foo.class, true);

    Foo foo1 = new Foo();
    foo1.val = 10;
    assertEquals(1, dao.create(foo1));
    Foo foo2 = new Foo();
    foo2.val = 20;
    assertEquals(1, dao.create(foo2));
    Foo foo3 = new Foo();
    foo3.val = 30;
    assertEquals(1, dao.create(foo3));
    Foo foo4 = new Foo();
    foo4.val = 40;
    assertEquals(1, dao.create(foo4));

    QueryBuilder<Foo, Object> iqb = dao.queryBuilder();
    iqb.selectRaw("max(id)");

    QueryBuilder<Foo, Object> oqb = dao.queryBuilder();
    Foo result = oqb.where().in(Foo.ID_COLUMN_NAME, iqb).queryForFirst();
    assertNotNull(result);
    assertEquals(foo4.id, result.id);
  }
  @Test
  public void testBaseClassComparison() throws Exception {
    Dao<Bar, String> barDao = createDao(Bar.class, true);
    Dao<Baz, String> bazDao = createDao(Baz.class, true);

    BarSuperClass bar1 = new BarSuperClass();
    bar1.val = 10;
    assertEquals(1, barDao.create(bar1));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    assertEquals(1, bazDao.create(baz1));

    List<Baz> results = bazDao.queryBuilder().where().eq(Baz.BAR_FIELD, bar1).query();
    assertEquals(1, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);

    try {
      // we allow a super class of the field but _not_ a sub class
      results = bazDao.queryBuilder().where().eq(Baz.BAR_FIELD, new Object()).query();
      fail("Should have thrown");
    } catch (SQLException e) {
      // expected
    }
  }
 @Test
 public void testShortCuts() throws Exception {
   Dao<Foo, Integer> dao = createDao(Foo.class, true);
   Foo foo1 = new Foo();
   assertEquals(1, dao.create(foo1));
   Foo foo2 = new Foo();
   assertEquals(1, dao.create(foo2));
   List<Foo> results = dao.queryBuilder().where().eq(Foo.ID_COLUMN_NAME, foo2.id).query();
   assertEquals(1, results.size());
   assertEquals(foo2.id, results.get(0).id);
   Iterator<Foo> iterator = dao.queryBuilder().where().eq(Foo.ID_COLUMN_NAME, foo2.id).iterator();
   assertTrue(iterator.hasNext());
   assertEquals(foo2.id, iterator.next().id);
   assertFalse(iterator.hasNext());
 }
  @Test
  public void testUtf8() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);

    Foo foo = new Foo();
    foo.stringField = "اعصاب";
    assertEquals(1, dao.create(foo));

    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();

    List<Foo> results =
        qb.where().like(Foo.STRING_COLUMN_NAME, '%' + foo.stringField + '%').query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(foo.id, results.get(0).id);
    assertEquals(foo.stringField, results.get(0).stringField);

    qb.reset();
    results =
        qb.where().like(Foo.STRING_COLUMN_NAME, new SelectArg('%' + foo.stringField + '%')).query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(foo.id, results.get(0).id);
    assertEquals(foo.stringField, results.get(0).stringField);
  }
  @Test
  public void testCountOf() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);

    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
    assertEquals(0, qb.countOf());

    Foo foo1 = new Foo();
    int val = 123213;
    foo1.val = val;
    assertEquals(1, dao.create(foo1));
    assertEquals(1, qb.countOf());

    Foo foo2 = new Foo();
    foo2.val = val;
    assertEquals(1, dao.create(foo2));
    assertEquals(2, qb.countOf());

    String distinct = "DISTINCT(" + Foo.VAL_COLUMN_NAME + ")";
    assertEquals(1, qb.countOf(distinct));

    qb.setCountOf(distinct);
    assertEquals(1, dao.countOf(qb.prepare()));

    distinct = "DISTINCT(" + Foo.ID_COLUMN_NAME + ")";
    assertEquals(2, qb.countOf(distinct));

    qb.setCountOf(distinct);
    assertEquals(2, dao.countOf(qb.prepare()));
  }
  @Test
  public void testSpecificJoinLoggingBug() throws Exception {
    /*
     * Test trying to specifically reproduce a reported bug. The query built in the logs was enough to show that
     * either the bug has already been fixed or the test is not reproducing the problem adequately.
     */

    Dao<Category, Integer> categoryDao = createDao(Category.class, true);
    Dao<Criterion, Integer> criterionDao = createDao(Criterion.class, true);

    QueryBuilder<Criterion, Integer> criteriaQb = criterionDao.queryBuilder();
    criteriaQb.where().eq("active", Boolean.valueOf(true));

    QueryBuilder<Category, Integer> categoryQb = categoryDao.queryBuilder();
    categoryQb.orderByRaw("id").join(criteriaQb).query();
  }
  @Test
  public void testHavingOrderBy() throws Exception {
    Dao<Foo, Object> fooDao = createDao(Foo.class, true);

    Foo foo1 = new Foo();
    foo1.val = 10;
    assertEquals(1, fooDao.create(foo1));
    Foo foo2 = new Foo();
    foo2.val = 20;
    assertEquals(1, fooDao.create(foo2));
    Foo foo3 = new Foo();
    foo3.val = 30;
    assertEquals(1, fooDao.create(foo3));
    Foo foo4 = new Foo();
    foo4.val = 40;
    assertEquals(1, fooDao.create(foo4));

    QueryBuilder<Foo, Object> qb = fooDao.queryBuilder();
    qb.groupBy(Foo.ID_COLUMN_NAME);
    qb.orderBy(Foo.VAL_COLUMN_NAME, false);
    qb.having("val < " + foo3.val);
    List<Foo> results = qb.query();
    assertEquals(2, results.size());
    assertEquals(foo2.val, results.get(0).val);
    assertEquals(foo1.val, results.get(1).val);
  }
 @Test
 public void testReservedWords() throws Exception {
   Dao<Reserved, Integer> dao = createDao(Reserved.class, true);
   QueryBuilder<Reserved, Integer> sb = dao.queryBuilder();
   sb.where().eq(Reserved.FIELD_NAME_GROUP, "something");
   sb.query();
 }
Exemple #17
0
  public static List<TodoItem> markOverdueItems() {
    List<TodoItem> items = new ArrayList<>();
    List<TodoItem> overdueItems = new ArrayList<>();

    final Dao<TodoItem, Integer> todoDao;
    try {
      todoDao = TodoApp.get().getDbHelper().getTodoDao();

      QueryBuilder<TodoItem, Integer> todoQb = todoDao.queryBuilder();
      PreparedQuery<TodoItem> preparedQuery =
          todoQb.where().eq(TodoEntry.COLUMN_STATE, TodoState.Undone).prepare();
      items = todoDao.query(preparedQuery);

      Date currentDate = new Date();
      for (TodoItem item : items) {
        if (item.getDue().before(currentDate)) {
          item.setState(TodoState.Overdue);
          createOrUpdateItem(item);
          overdueItems.add(item);
        }
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
    return overdueItems;
  }
  @Test
  public void testHaving() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);

    Foo foo = new Foo();
    int val1 = 243342;
    foo.val = val1;
    assertEquals(1, dao.create(foo));
    foo = new Foo();
    foo.val = val1;
    assertEquals(1, dao.create(foo));
    foo = new Foo();
    // only one of these
    int val2 = 6543;
    foo.val = val2;
    assertEquals(1, dao.create(foo));

    QueryBuilder<Foo, Integer> qb = dao.queryBuilder();
    qb.selectColumns(Foo.VAL_COLUMN_NAME);
    qb.groupBy(Foo.VAL_COLUMN_NAME);
    qb.having("COUNT(VAL) > 1");
    GenericRawResults<String[]> results = dao.queryRaw(qb.prepareStatementString());
    List<String[]> list = results.getResults();
    // only val2 has 2 of them
    assertEquals(1, list.size());
    assertEquals(String.valueOf(val1), list.get(0)[0]);

    qb.having("COUNT(VAL) > 2");
    results = dao.queryRaw(qb.prepareStatementString());
    list = results.getResults();
    assertEquals(0, list.size());
  }
Exemple #19
0
 @Override
 public void filter(ContainerRequestContext requestContext) throws IOException {
   UriInfo uriInfo = requestContext.getUriInfo();
   MultivaluedMap<String, String> params = uriInfo.getPathParameters();
   String formationId = params.getFirst("formationId");
   String year = params.getFirst("annee");
   String semester = params.getFirst("semestre");
   try {
     if (uePromoDao
             .queryBuilder()
             .where()
             .eq("formation_id", formationId)
             .and()
             .eq("year", year)
             .and()
             .eq("semester", semester)
             .countOf()
         == 0) {
       requestContext.abortWith(Response.status(Response.Status.NOT_FOUND).build());
     }
   } catch (SQLException e) {
     e.printStackTrace();
     requestContext.abortWith(Response.serverError().build());
   }
 }
 public List<Notebook> get(String title) {
   try {
     return notebookDaoOpe.queryBuilder().where().eq("title", title).query();
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return null;
 }
  @Test
  public void testJoinTwoColumns() throws Exception {
    Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
    Dao<StringColumnArg, Integer> scaDao = createDao(StringColumnArg.class, true);

    Foo foo1 = new Foo();
    foo1.val = 123213213;
    foo1.stringField = "stuff";
    fooDao.create(foo1);

    Foo foo2 = new Foo();
    foo2.stringField = "not stuff";
    fooDao.create(foo2);

    StringColumnArg sca1 = new StringColumnArg();
    sca1.str1 = foo1.stringField;
    scaDao.create(sca1);

    StringColumnArg sca2 = new StringColumnArg();
    sca2.str1 = foo2.stringField;
    scaDao.create(sca2);

    StringColumnArg sca3 = new StringColumnArg();
    sca3.str1 = "some other field";
    scaDao.create(sca3);

    QueryBuilder<Foo, Integer> fooQb = fooDao.queryBuilder();
    fooQb.where().eq(Foo.VAL_COLUMN_NAME, foo1.val);

    QueryBuilder<StringColumnArg, Integer> scaQb = scaDao.queryBuilder();
    scaQb.join(StringColumnArg.STR1_FIELD, Foo.STRING_COLUMN_NAME, fooQb);
    List<StringColumnArg> results = scaQb.query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(sca1.id, results.get(0).id);

    fooQb.reset();
    fooQb.where().eq(Foo.VAL_COLUMN_NAME, foo2.val);

    scaQb.reset();
    scaQb.join(StringColumnArg.STR1_FIELD, Foo.STRING_COLUMN_NAME, fooQb);
    results = scaQb.query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(sca2.id, results.get(0).id);
  }
 @Test(expected = SQLException.class)
 public void testQueryRawColumnsNotQuery() throws Exception {
   Dao<Foo, String> dao = createDao(Foo.class, true);
   QueryBuilder<Foo, String> qb = dao.queryBuilder();
   qb.selectRaw("COUNT(*)");
   // we can't get Foo objects with the COUNT(*)
   dao.query(qb.prepare());
 }
  @Test
  public void testSimpleJoinOr() throws Exception {
    Dao<Bar, Integer> barDao = createDao(Bar.class, true);
    Dao<Baz, Integer> bazDao = createDao(Baz.class, true);

    Bar bar1 = new Bar();
    bar1.val = 2234;
    assertEquals(1, barDao.create(bar1));
    Bar bar2 = new Bar();
    bar2.val = 324322234;
    assertEquals(1, barDao.create(bar2));

    Baz baz1 = new Baz();
    baz1.bar = bar1;
    baz1.val = 423423;
    assertEquals(1, bazDao.create(baz1));
    Baz baz2 = new Baz();
    baz2.bar = bar2;
    baz2.val = 9570423;
    assertEquals(1, bazDao.create(baz2));

    QueryBuilder<Bar, Integer> barQb = barDao.queryBuilder();
    barQb.where().eq(Bar.VAL_FIELD, bar1.val);
    List<Baz> results = bazDao.queryBuilder().query();
    assertEquals(2, results.size());
    QueryBuilder<Baz, Integer> bazQb = bazDao.queryBuilder();
    bazQb.where().eq(Baz.VAL_FIELD, baz2.val);
    results = bazQb.joinOr(barQb).query();
    assertEquals(2, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);
    assertEquals(bar2.id, results.get(1).bar.id);

    bazQb.reset();
    bazQb.where().eq(Baz.VAL_FIELD, baz2.val);
    results = bazQb.join(barQb, JoinType.INNER, JoinWhereOperation.OR).query();
    assertEquals(2, results.size());
    assertEquals(bar1.id, results.get(0).bar.id);
    assertEquals(bar2.id, results.get(1).bar.id);

    // now do join which should be an AND
    bazQb.reset();
    bazQb.where().eq(Baz.VAL_FIELD, baz2.val);
    results = bazQb.join(barQb).query();
    // should find no results
    assertEquals(0, results.size());
  }
 @Override
 public long countByProject(int connection_id, long project_id) throws SQLException {
   QueryBuilder<RedmineRole, ?> builder = dao.queryBuilder();
   builder.setCountOf(true).where().eq(RedmineStatus.CONNECTION, connection_id)
   // .and()
   // .eq(RedmineStatus.PROJECT_ID, project_id)
   ;
   return dao.countOf(builder.prepare());
 }
Exemple #25
0
  public synchronized List<City> queryByProvinceID(int provinceID) {
    try {
      return mDao.queryBuilder().where().eq("provinceID", provinceID).query();
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return null;
  }
  private PreparedQuery<ProjectBean> makeProjectForUserQuery() {

    QueryBuilder<UserProjectMapping, Integer> userPostQb = userProjectMappingDao.queryBuilder();

    userPostQb.selectColumns(UserProjectMapping.PROJECT_ID_FIELD_NAME);
    SelectArg userSelectArg = new SelectArg();
    QueryBuilder<ProjectBean, Integer> projectQb = null;
    try {
      userPostQb.where().eq(UserProjectMapping.USER_ID_FIELD_NAME, userSelectArg);
      projectQb = projectDao.queryBuilder();
      projectQb.where().eq("isDisabled", false).and().in(ProjectBean.ID_FIELD_NAME, userPostQb);
      return projectQb.prepare();
    } catch (SQLException e) {
      logger.error(
          "Error creating prepared query for fetching user mapped projects : " + e.getMessage());
    }
    return null;
  }
  @Test
  public void testOrderByRawAndOrderBy() throws Exception {
    Dao<Foo, Integer> dao = createDao(Foo.class, true);
    Foo foo1 = new Foo();
    foo1.val = 1;
    foo1.equal = 10;
    assertEquals(1, dao.create(foo1));
    Foo foo2 = new Foo();
    foo2.val = 5;
    foo2.equal = 7;
    assertEquals(1, dao.create(foo2));
    Foo foo3 = new Foo();
    foo3.val = 7;
    foo3.equal = 5;
    assertEquals(1, dao.create(foo3));
    List<Foo> results =
        dao.queryBuilder()
            .orderByRaw("(" + Foo.VAL_COLUMN_NAME + "+" + Foo.EQUAL_COLUMN_NAME + ") DESC")
            .query();
    assertEquals(3, results.size());
    assertEquals(foo2.id, results.get(0).id);
    assertEquals(foo3.id, results.get(1).id);
    assertEquals(foo1.id, results.get(2).id);

    results =
        dao.queryBuilder()
            .orderByRaw("(" + Foo.VAL_COLUMN_NAME + "+" + Foo.EQUAL_COLUMN_NAME + ") DESC")
            .orderBy(Foo.VAL_COLUMN_NAME, false)
            .query();
    assertEquals(3, results.size());
    assertEquals(foo3.id, results.get(0).id);
    assertEquals(foo2.id, results.get(1).id);
    assertEquals(foo1.id, results.get(2).id);

    results =
        dao.queryBuilder()
            .orderBy(Foo.VAL_COLUMN_NAME, true)
            .orderByRaw("(" + Foo.VAL_COLUMN_NAME + "+" + Foo.EQUAL_COLUMN_NAME + ") DESC")
            .query();
    assertEquals(3, results.size());
    assertEquals(foo1.id, results.get(0).id);
    assertEquals(foo2.id, results.get(1).id);
    assertEquals(foo3.id, results.get(2).id);
  }
Exemple #28
0
 public static Person getByEmail(Dao<Person, ?> dao, String email) {
   try {
     return dao.queryBuilder()
         .where()
         .eq(Person.EMAIL_FIELD, new SelectArg(email.toLowerCase()))
         .queryForFirst();
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
 }
  @Test
  public void testRandomIsNull() throws Exception {
    Dao<SeralizableNull, Integer> dao = createDao(SeralizableNull.class, true);
    SeralizableNull sn1 = new SeralizableNull();
    assertEquals(1, dao.create(sn1));
    SeralizableNull sn2 = new SeralizableNull();
    sn2.serializable = "wow";
    assertEquals(1, dao.create(sn2));

    List<SeralizableNull> results =
        dao.queryBuilder().where().isNull(SeralizableNull.FIELD_NAME_SERIALIZABLE).query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(sn1.id, results.get(0).id);
    results = dao.queryBuilder().where().isNotNull(SeralizableNull.FIELD_NAME_SERIALIZABLE).query();
    assertNotNull(results);
    assertEquals(1, results.size());
    assertEquals(sn2.id, results.get(0).id);
  }
 @Test
 public void testClear() throws Exception {
   Dao<Foo, String> dao = createDao(Foo.class, false);
   QueryBuilder<Foo, String> qb = dao.queryBuilder();
   qb.selectColumns(Foo.VAL_COLUMN_NAME);
   qb.groupBy(Foo.VAL_COLUMN_NAME);
   qb.having("COUNT(VAL) > 1");
   qb.where().eq(Foo.ID_COLUMN_NAME, 1);
   qb.reset();
   assertEquals("SELECT * FROM `foo` ", qb.prepareStatementString());
 }