Java Code Examples for com.j256.ormlite.dao.Dao#queryRaw()
The following examples show how to use
com.j256.ormlite.dao.Dao#queryRaw() .
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: JdbcRawResultsImplTest.java From ormlite-jdbc with ISC License | 6 votes |
@Test public void testCustomColumnNames() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, true); Foo foo = new Foo(); foo.val = 1213213; assertEquals(1, dao.create(foo)); final String idName = "SOME_ID"; final String valName = "SOME_VAL"; final AtomicBoolean gotResult = new AtomicBoolean(false); GenericRawResults<Object> results = dao.queryRaw("select id as " + idName + ", val as " + valName + " from foo", new RawRowMapper<Object>() { @Override public Object mapRow(String[] columnNames, String[] resultColumns) { assertEquals(idName, columnNames[0]); assertEquals(valName, columnNames[1]); gotResult.set(true); return new Object(); } }); List<Object> list = results.getResults(); assertNotNull(list); assertEquals(1, list.size()); assertTrue(gotResult.get()); }
Example 2
Source File: SelectIteratorTest.java From ormlite-core with ISC License | 6 votes |
@Test(expected = IllegalStateException.class) public void testIteratorRawResults() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, true); Foo foo1 = new Foo(); assertEquals(1, dao.create(foo1)); GenericRawResults<String[]> rawResults = dao.queryRaw("SELECT " + Foo.ID_COLUMN_NAME + " FROM FOO"); CloseableIterator<String[]> iterator = rawResults.closeableIterator(); try { assertTrue(iterator.hasNext()); iterator.next(); iterator.remove(); } finally { iterator.close(); } }
Example 3
Source File: RawResultsImplTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testQueryRaw() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, true); Foo foo = new Foo(); foo.val = 1; foo.equal = 10; assertEquals(1, dao.create(foo)); QueryBuilder<Foo, Integer> qb = dao.queryBuilder(); qb.where().eq(Foo.VAL_COLUMN_NAME, new SelectArg()); GenericRawResults<String[]> rawResults = dao.queryRaw(qb.prepareStatementString(), Integer.toString(foo.val)); List<String[]> results = rawResults.getResults(); assertEquals(1, results.size()); boolean found = false; String[] columnNames = rawResults.getColumnNames(); for (int i = 0; i < rawResults.getNumberColumns(); i++) { if (columnNames[i].equalsIgnoreCase(Foo.ID_COLUMN_NAME)) { assertEquals(Integer.toString(foo.id), results.get(0)[0]); found = true; } } assertTrue(found); }
Example 4
Source File: RawResultsImplTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testGetFirstResult() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, true); Foo foo1 = new Foo(); foo1.val = 342; assertEquals(1, dao.create(foo1)); Foo foo2 = new Foo(); foo2.val = 9045342; assertEquals(1, dao.create(foo2)); QueryBuilder<Foo, Integer> qb = dao.queryBuilder(); qb.selectRaw("MAX(" + Foo.VAL_COLUMN_NAME + ")"); GenericRawResults<String[]> results = dao.queryRaw(qb.prepareStatementString()); String[] result = results.getFirstResult(); int max = Integer.parseInt(result[0]); if (foo1.val > foo2.val) { assertEquals(foo1.val, max); } else { assertEquals(foo2.val, max); } }
Example 5
Source File: RawResultsImplTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testCustomColumnNames() throws Exception { Dao<Foo, Integer> dao = createDao(Foo.class, true); Foo foo = new Foo(); foo.val = 1213213; assertEquals(1, dao.create(foo)); final String idName = "SOME_ID"; final String valName = "SOME_VAL"; final AtomicBoolean gotResult = new AtomicBoolean(false); GenericRawResults<Object> results = dao.queryRaw("select id as " + idName + ", val as " + valName + " from foo", new RawRowMapper<Object>() { @Override public Object mapRow(String[] columnNames, String[] resultColumns) { assertEquals(idName, columnNames[0]); assertEquals(valName, columnNames[1]); gotResult.set(true); return new Object(); } }); assertEquals(1, results.getResults().size()); results.close(); assertTrue(gotResult.get()); }
Example 6
Source File: EnumToStringTypeTest.java From ormlite-core with ISC License | 6 votes |
@Test public void testEnumToStringValue() throws Exception { Class<LocalEnumToString> clazz = LocalEnumToString.class; Dao<LocalEnumToString, Object> dao = createDao(clazz, true); LocalEnumToString foo = new LocalEnumToString(); foo.ourEnum = OurEnum.SECOND; assertEquals(1, dao.create(foo)); GenericRawResults<String[]> results = dao.queryRaw("select * from " + TABLE_NAME); CloseableIterator<String[]> iterator = results.closeableIterator(); try { assertTrue(iterator.hasNext()); String[] result = iterator.next(); assertNotNull(result); assertEquals(1, result.length); assertFalse(OurEnum.SECOND.name().equals(result[0])); assertTrue(OurEnum.SECOND.toString().equals(result[0])); } finally { IOUtils.closeQuietly(iterator); } }
Example 7
Source File: SqliteDatabaseTypeTest.java From ormlite-jdbc with ISC License | 5 votes |
@Test public void testDateFormat() throws Exception { Dao<AllTypes, Object> dao = createDao(AllTypes.class, true); AllTypes all = new AllTypes(); all.dateField = new Date(); assertEquals(1, dao.create(all)); GenericRawResults<String[]> results = dao.queryRaw("select * from alltypes"); List<String[]> stringslist = results.getResults(); String[] names = results.getColumnNames(); for (String[] strings : stringslist) { for (int i = 0; i < strings.length; i++) { System.out.println(names[i] + "=" + strings[i]); } } }
Example 8
Source File: QueryBuilderTest.java From ormlite-core with ISC License | 5 votes |
@Test public void testQueryRawMax() throws Exception { Dao<Foo, Object> dao = createDao(Foo.class, true); Foo foo1 = new Foo(); foo1.stringField = "1"; foo1.val = 10; assertEquals(1, dao.create(foo1)); Foo foo2 = new Foo(); foo2.stringField = "1"; foo2.val = 20; assertEquals(1, dao.create(foo2)); Foo foo3 = new Foo(); foo3.stringField = "2"; foo3.val = 30; assertEquals(1, dao.create(foo3)); Foo foo4 = new Foo(); foo4.stringField = "2"; foo4.val = 40; assertEquals(1, dao.create(foo4)); QueryBuilder<Foo, Object> qb = dao.queryBuilder(); qb.selectRaw("string, max(val) as val"); qb.groupBy(Foo.STRING_COLUMN_NAME); GenericRawResults<Foo> results = dao.queryRaw(qb.prepareStatementString(), dao.getRawRowMapper()); assertNotNull(results); CloseableIterator<Foo> iterator = results.closeableIterator(); try { assertTrue(iterator.hasNext()); assertEquals(foo2.val, iterator.next().val); assertTrue(iterator.hasNext()); assertEquals(foo4.val, iterator.next().val); assertFalse(iterator.hasNext()); } finally { iterator.close(); } }
Example 9
Source File: RawResultsImplTest.java From ormlite-core with ISC License | 5 votes |
@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]); }
Example 10
Source File: RawResultsImplTest.java From ormlite-core with ISC License | 5 votes |
@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()); }
Example 11
Source File: QueryBuilderTest.java From ormlite-core with ISC License | 4 votes |
@Test public void testQueryColumnsPlusQueryRawMax() throws Exception { Dao<Foo, Object> dao = createDao(Foo.class, true); Foo foo1 = new Foo(); foo1.stringField = "1"; foo1.val = 10; assertEquals(1, dao.create(foo1)); Foo foo2 = new Foo(); foo2.stringField = "1"; foo2.val = 20; assertEquals(1, dao.create(foo2)); Foo foo3 = new Foo(); foo3.stringField = "2"; foo3.val = 40; assertEquals(1, dao.create(foo3)); Foo foo4 = new Foo(); foo4.stringField = "2"; foo4.val = 30; assertEquals(1, dao.create(foo4)); QueryBuilder<Foo, Object> qb = dao.queryBuilder(); qb.selectColumns(Foo.STRING_COLUMN_NAME); qb.selectRaw("MAX(val) AS val"); qb.groupBy(Foo.STRING_COLUMN_NAME); GenericRawResults<Foo> results = dao.queryRaw(qb.prepareStatementString(), dao.getRawRowMapper()); assertNotNull(results); CloseableIterator<Foo> iterator = results.closeableIterator(); try { assertTrue(iterator.hasNext()); Foo result = iterator.next(); assertEquals(foo2.val, result.val); assertEquals(foo2.stringField, result.stringField); assertTrue(iterator.hasNext()); result = iterator.next(); assertEquals(foo3.val, result.val); assertEquals(foo3.stringField, result.stringField); assertFalse(iterator.hasNext()); } finally { iterator.close(); } }