Java Code Examples for com.j256.ormlite.dao.Dao#query()
The following examples show how to use
com.j256.ormlite.dao.Dao#query() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testLimitAfterSelect() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); // no limit the default List<Foo> results = fooDao.query(qb.prepare()); assertEquals(2, results.size()); assertEquals(foo1, results.get(0)); assertEquals(foo2, results.get(1)); qb.limit(1L); results = fooDao.query(qb.prepare()); assertEquals(1, results.size()); assertEquals(foo1, results.get(0)); // set back to no-limit qb.limit(null); results = fooDao.query(qb.prepare()); assertEquals(2, results.size()); assertEquals(foo1, results.get(0)); assertEquals(foo2, results.get(1)); }
Example 2
Source File: QueryBuilderTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testOffsetWorks() 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)); assertEquals(2, dao.queryForAll().size()); QueryBuilder<Foo, Integer> qb = dao.queryBuilder(); long offset = 1; long limit = 2; qb.offset(offset); qb.limit(limit); List<Foo> results = dao.query(qb.prepare()); assertEquals(1, results.size()); }
Example 3
Source File: DatabaseHelper.java From AndroidAPS with GNU Affero General Public License v3.0 | 6 votes |
public List<TempTarget> getTemptargetsDataFromTime(long mills, boolean ascending) { try { Dao<TempTarget, Long> daoTempTargets = getDaoTempTargets(); List<TempTarget> tempTargets; QueryBuilder<TempTarget, Long> queryBuilder = daoTempTargets.queryBuilder(); queryBuilder.orderBy("date", ascending); Where where = queryBuilder.where(); where.ge("date", mills); PreparedQuery<TempTarget> preparedQuery = queryBuilder.prepare(); tempTargets = daoTempTargets.query(preparedQuery); return tempTargets; } catch (SQLException e) { log.error("Unhandled exception", e); } return new ArrayList<TempTarget>(); }
Example 4
Source File: DatabaseHelper.java From AndroidAPS with GNU Affero General Public License v3.0 | 6 votes |
public List<ProfileSwitch> getProfileSwitchData(long from, boolean ascending) { try { Dao<ProfileSwitch, Long> daoProfileSwitch = getDaoProfileSwitch(); List<ProfileSwitch> profileSwitches; QueryBuilder<ProfileSwitch, Long> queryBuilder = daoProfileSwitch.queryBuilder(); queryBuilder.orderBy("date", ascending); queryBuilder.limit(100L); Where where = queryBuilder.where(); where.ge("date", from); PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare(); profileSwitches = daoProfileSwitch.query(preparedQuery); //add last one without duration ProfileSwitch last = getLastProfileSwitchWithoutDuration(); if (last != null) { if (!profileSwitches.contains(last)) profileSwitches.add(last); } return profileSwitches; } catch (SQLException e) { log.error("Unhandled exception", e); } return new ArrayList<>(); }
Example 5
Source File: DatabaseHelper.java From AndroidAPS with GNU Affero General Public License v3.0 | 6 votes |
@Nullable private ProfileSwitch getLastProfileSwitchWithoutDuration() { try { Dao<ProfileSwitch, Long> daoProfileSwitch = getDaoProfileSwitch(); List<ProfileSwitch> profileSwitches; QueryBuilder<ProfileSwitch, Long> queryBuilder = daoProfileSwitch.queryBuilder(); queryBuilder.orderBy("date", false); queryBuilder.limit(1L); Where where = queryBuilder.where(); where.eq("durationInMinutes", 0); PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare(); profileSwitches = daoProfileSwitch.query(preparedQuery); if (profileSwitches.size() > 0) return profileSwitches.get(0); else return null; } catch (SQLException e) { log.error("Unhandled exception", e); } return null; }
Example 6
Source File: DatabaseHelper.java From AndroidAPS with GNU Affero General Public License v3.0 | 6 votes |
public List<ProfileSwitch> getProfileSwitchEventsFromTime(long mills, boolean ascending) { try { Dao<ProfileSwitch, Long> daoProfileSwitch = getDaoProfileSwitch(); List<ProfileSwitch> profileSwitches; QueryBuilder<ProfileSwitch, Long> queryBuilder = daoProfileSwitch.queryBuilder(); queryBuilder.orderBy("date", ascending); queryBuilder.limit(100L); Where where = queryBuilder.where(); where.ge("date", mills); PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare(); profileSwitches = daoProfileSwitch.query(preparedQuery); return profileSwitches; } catch (SQLException e) { log.error("Unhandled exception", e); } return new ArrayList<>(); }
Example 7
Source File: DatabaseHelper.java From AndroidAPS with GNU Affero General Public License v3.0 | 6 votes |
public List<ProfileSwitch> getProfileSwitchEventsFromTime(long from, long to, boolean ascending) { try { Dao<ProfileSwitch, Long> daoProfileSwitch = getDaoProfileSwitch(); List<ProfileSwitch> profileSwitches; QueryBuilder<ProfileSwitch, Long> queryBuilder = daoProfileSwitch.queryBuilder(); queryBuilder.orderBy("date", ascending); queryBuilder.limit(100L); Where where = queryBuilder.where(); where.between("date", from, to); PreparedQuery<ProfileSwitch> preparedQuery = queryBuilder.prepare(); profileSwitches = daoProfileSwitch.query(preparedQuery); return profileSwitches; } catch (SQLException e) { log.error("Unhandled exception", e); } return new ArrayList<>(); }
Example 8
Source File: TreatmentService.java From AndroidAPS with GNU Affero General Public License v3.0 | 6 votes |
/** * finds treatment by its NS Id. * * @param _id * @return */ @Nullable public Treatment findByNSId(String _id) { try { Dao<Treatment, Long> daoTreatments = getDao(); QueryBuilder<Treatment, Long> queryBuilder = daoTreatments.queryBuilder(); Where where = queryBuilder.where(); where.eq("_id", _id); queryBuilder.limit(10L); PreparedQuery<Treatment> preparedQuery = queryBuilder.prepare(); List<Treatment> trList = daoTreatments.query(preparedQuery); if (trList.size() != 1) { //log.debug("Treatment findTreatmentById query size: " + trList.size()); return null; } else { //log.debug("Treatment findTreatmentById found: " + trList.get(0).log()); return trList.get(0); } } catch (SQLException e) { log.error("Unhandled exception", e); } return null; }
Example 9
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testOffsetNoLimit() throws Exception { Dao<Foo, Object> dao = createDao(Foo.class, true); Foo foo1 = new Foo(); foo1.id = "stuff1"; assertEquals(1, dao.create(foo1)); Foo foo2 = new Foo(); foo2.id = "stuff2"; assertEquals(1, dao.create(foo2)); assertEquals(2, dao.queryForAll().size()); QueryBuilder<Foo, Object> qb = dao.queryBuilder(); qb.offset(1L); try { dao.query(qb.prepare()); fail("expected exception"); } catch (SQLException e) { // expected } }
Example 10
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testSetWhere() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); Where<Foo, String> where = qb.where(); where.eq(Foo.ID_COLUMN_NAME, foo1.id); List<Foo> list = fooDao.query(qb.prepare()); assertEquals(1, list.size()); assertEquals(foo1, list.get(0)); qb = fooDao.queryBuilder(); qb.setWhere(where); list = fooDao.query(qb.prepare()); assertEquals(1, list.size()); assertEquals(foo1, list.get(0)); }
Example 11
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testOffsetWithLimit() throws Exception { Dao<Foo, Object> dao = createDao(Foo.class, true); Foo foo1 = new Foo(); foo1.id = "stuff1"; assertEquals(1, dao.create(foo1)); Foo foo2 = new Foo(); foo2.id = "stuff2"; assertEquals(1, dao.create(foo2)); assertEquals(2, dao.queryForAll().size()); QueryBuilder<Foo, Object> qb = dao.queryBuilder(); long offset = 1; long limit = 2; qb.offset(offset); qb.limit(limit); List<Foo> results = dao.query(qb.prepare()); assertEquals(1, results.size()); }
Example 12
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testBetween() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); qb.where().between(Foo.VAL_COLUMN_NAME, LOW_VAL, HIGH_VAL); List<Foo> results = fooDao.query(qb.prepare()); assertEquals(2, results.size()); assertEquals(foo1, results.get(0)); assertEquals(foo2, results.get(1)); qb.where().between(Foo.VAL_COLUMN_NAME, LOW_VAL + 1, HIGH_VAL); results = fooDao.query(qb.prepare()); assertEquals(1, results.size()); assertEquals(foo2, results.get(0)); qb.where().between(Foo.VAL_COLUMN_NAME, LOW_VAL, HIGH_VAL - 1); results = fooDao.query(qb.prepare()); assertEquals(1, results.size()); assertEquals(foo1, results.get(0)); qb.where().between(Foo.VAL_COLUMN_NAME, LOW_VAL + 1, HIGH_VAL - 1); results = fooDao.query(qb.prepare()); assertEquals(0, results.size()); }
Example 13
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testSelectArgsNotSet() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); SelectArg idSelectArg = new SelectArg(); qb.where().eq(Foo.ID_COLUMN_NAME, idSelectArg); try { fooDao.query(qb.prepare()); fail("expected exception"); } catch (SQLException e) { // expected System.err.println("Expected: " + e); e.printStackTrace(); } }
Example 14
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testExists() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> innerQb = fooDao.queryBuilder(); innerQb.where().idEq(foo1.id); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); qb.where().exists(innerQb); List<Foo> results = fooDao.query(qb.prepare()); assertEquals(2, results.size()); assertEquals(foo1, results.get(0)); assertEquals(foo2, results.get(1)); }
Example 15
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testInIterable() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); qb.where().in(Foo.ID_COLUMN_NAME, Arrays.asList(foo1.id, foo2.id)); List<Foo> results = fooDao.query(qb.prepare()); assertEquals(2, results.size()); assertEquals(foo1, results.get(0)); assertEquals(foo2, results.get(1)); }
Example 16
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testIn() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); qb.where().in(Foo.ID_COLUMN_NAME, foo1.id, foo2.id); List<Foo> results = fooDao.query(qb.prepare()); assertEquals(2, results.size()); assertEquals(foo1, results.get(0)); assertEquals(foo2, results.get(1)); }
Example 17
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testNotExists() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> innerQb = fooDao.queryBuilder(); innerQb.where().idEq(foo1.id); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); qb.where().not().exists(innerQb); List<Foo> results = fooDao.query(qb.prepare()); assertEquals(0, results.size()); }
Example 18
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testMissingAnd() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); qb.where().eq(Foo.ID_COLUMN_NAME, foo1.id).eq(Foo.ID_COLUMN_NAME, foo1.id); try { fooDao.query(qb.prepare()); fail("expected exception"); } catch (IllegalStateException e) { // expected } }
Example 19
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testNoWhereOperations() throws Exception { Dao<Foo, String> fooDao = createTestData(); QueryBuilder<Foo, String> qb = fooDao.queryBuilder(); qb.where(); try { fooDao.query(qb.prepare()); fail("expected exception"); } catch (IllegalStateException e) { // expected } }
Example 20
Source File: JdbcQueryBuilderTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testWherePrepare() throws Exception { Dao<Foo, String> fooDao = createTestData(); List<Foo> results = fooDao.query(fooDao.queryBuilder() .where() .eq(Foo.ID_COLUMN_NAME, foo1.id) .and() .eq(Foo.VAL_COLUMN_NAME, foo1.val) .prepare()); assertEquals(1, results.size()); assertEquals(foo1, results.get(0)); }