Java Code Examples for com.j256.ormlite.dao.GenericRawResults#getResults()

The following examples show how to use com.j256.ormlite.dao.GenericRawResults#getResults() . 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: NotificationItemDaoImpl.java    From Notification-Analyser with MIT License 6 votes vote down vote up
private List<NotificationDateView> getSummaryLastPeriod(String rawQuery) throws SQLException {
    LinkedList<NotificationDateView> list = new LinkedList<NotificationDateView>();
    GenericRawResults<String[]> rawResults = this.queryRaw(rawQuery);
    List<String[]> results = rawResults.getResults();

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
    for (String[] result : results) {
        try {
            Date date = formatter.parse(result[0]);
            Integer notifications = Integer.parseInt(result[1]);
            list.add(new NotificationDateView(date, notifications));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
    Collections.reverse(list);
    return list;
}
 
Example 2
Source File: JdbcRawResultsImplTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@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 3
Source File: RawResultsImplTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@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: SqliteDatabaseTypeTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@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 5
Source File: RawResultsImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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 6
Source File: RawResultsImplTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@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());
}