Java Code Examples for com.j256.ormlite.dao.Dao#queryForAll()

The following examples show how to use com.j256.ormlite.dao.Dao#queryForAll() . 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: IntTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testIntPrimitiveNull() throws Exception {
	Dao<LocalIntObj, Object> objDao = createDao(LocalIntObj.class, true);
	LocalIntObj foo = new LocalIntObj();
	foo.intField = null;
	assertEquals(1, objDao.create(foo));
	// overlapping table
	Dao<LocalInt, Object> dao = createDao(LocalInt.class, false);
	List<LocalInt> all = dao.queryForAll();
	assertEquals(1, all.size());
	assertEquals(0, all.get(0).intField);
}
 
Example 2
Source File: ShortTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testShortPrimitiveNull() throws Exception {
	Dao<LocalShortObj, Object> objDao = createDao(LocalShortObj.class, true);
	LocalShortObj foo = new LocalShortObj();
	foo.shortField = null;
	assertEquals(1, objDao.create(foo));
	Dao<LocalShort, Object> dao = createDao(LocalShort.class, false);
	List<LocalShort> all = dao.queryForAll();
	assertEquals(1, all.size());
	assertEquals(0, all.get(0).shortField);
}
 
Example 3
Source File: BooleanTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testBooleanPrimitiveNull() throws Exception {
	Dao<LocalBooleanObj, Object> objDao = createDao(LocalBooleanObj.class, true);
	LocalBooleanObj foo = new LocalBooleanObj();
	foo.bool = null;
	assertEquals(1, objDao.create(foo));
	Dao<LocalBoolean, Object> dao = createDao(LocalBoolean.class, false);
	List<LocalBoolean> all = dao.queryForAll();
	assertEquals(1, all.size());
	assertFalse(all.get(0).bool);
}
 
Example 4
Source File: BigDecimalTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testBigDecimalInvalidDbValue() throws Exception {
	Dao<LocalBigDecimal, Object> dao = createDao(LocalBigDecimal.class, true);
	Dao<NotBigDecimal, Object> notDao = createDao(NotBigDecimal.class, false);

	NotBigDecimal notFoo = new NotBigDecimal();
	notFoo.bigDecimal = "not valid form";
	assertEquals(1, notDao.create(notFoo));

	dao.queryForAll();
}
 
Example 5
Source File: TableUtilsTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testMissingCreate() throws Exception {
	Dao<LocalFoo, Integer> fooDao = createDao(LocalFoo.class, false);
	try {
		fooDao.queryForAll();
		fail("Should have thrown");
	} catch (SQLException e) {
		// expected
	}
}
 
Example 6
Source File: DoubleTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDoublePrimitiveNull() throws Exception {
	Dao<LocalDoubleObj, Object> objDao = createDao(LocalDoubleObj.class, true);
	LocalDoubleObj foo = new LocalDoubleObj();
	foo.doubleField = null;
	assertEquals(1, objDao.create(foo));
	Dao<LocalDouble, Object> dao = createDao(LocalDouble.class, false);
	List<LocalDouble> all = dao.queryForAll();
	assertEquals(1, all.size());
	assertEquals(0.0F, all.get(0).doubleField, 0.0F);
}
 
Example 7
Source File: FloatTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testFloatPrimitiveNull() throws Exception {
	Dao<LocalFloatObj, Object> objDao = createDao(LocalFloatObj.class, true);
	LocalFloatObj foo = new LocalFloatObj();
	foo.floatField = null;
	assertEquals(1, objDao.create(foo));
	Dao<LocalFloat, Object> dao = createDao(LocalFloat.class, false);
	List<LocalFloat> all = dao.queryForAll();
	assertEquals(1, all.size());
	assertEquals(0.0F, all.get(0).floatField, 0.0F);
}
 
Example 8
Source File: EnumStringTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testUnknownEnumValue() throws Exception {
	Dao<LocalEnumString, Object> dao = createDao(LocalEnumString.class, true);
	LocalEnumString localEnumString = new LocalEnumString();
	localEnumString.ourEnum = OurEnum.FIRST;
	assertEquals(1, dao.create(localEnumString));
	assertEquals(1, dao.updateRaw("UPDATE Foo set ourEnum = 'THIRD'"));
	dao.queryForAll();
}
 
Example 9
Source File: DateStringTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDateStringFormatNotDayAlign() throws Exception {
	Dao<DateStringFormat, Object> dao = createDao(DateStringFormat.class, true);
	DateStringFormat dateStringFormat = new DateStringFormat();
	dateStringFormat.date = new SimpleDateFormat("yyyy-MM-dd HH").parse("2012-09-01 12");
	assertEquals(1, dao.create(dateStringFormat));

	List<DateStringFormat> results = dao.queryForAll();
	assertEquals(1, results.size());
	assertFalse(dateStringFormat.date.equals(results.get(0).date));
}
 
Example 10
Source File: BigDecimalNumericTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testBigDecimalInvalidDbValue() throws Exception {
	Dao<LocalBigDecimalNumeric, Object> dao = createDao(LocalBigDecimalNumeric.class, true);
	Dao<NotBigDecimalNumeric, Object> notDao = createDao(NotBigDecimalNumeric.class, false);

	NotBigDecimalNumeric notFoo = new NotBigDecimalNumeric();
	notFoo.bigDecimal = "not valid form";
	assertEquals(1, notDao.create(notFoo));

	dao.queryForAll();
}
 
Example 11
Source File: TransactionManagerTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testTransactionWithinTransactionFails() throws Exception {
	if (connectionSource == null) {
		return;
	}
	final TransactionManager mgr = new TransactionManager(connectionSource);
	final Dao<Foo, Integer> dao = createDao(Foo.class, true);
	try {
		mgr.callInTransaction(new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				dao.create(new Foo());
				mgr.callInTransaction(new Callable<Void>() {
					@Override
					public Void call() throws Exception {
						dao.create(new Foo());
						throw new SQLException("Exception ahoy!");
					}
				});
				return null;
			}
		});
		fail("Should have thrown");
	} catch (SQLException se) {
		// ignored
	}
	List<Foo> results = dao.queryForAll();
	assertNotNull(results);
	assertEquals(0, results.size());
}
 
Example 12
Source File: BigIntegerTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testBigIntegerNull() throws Exception {
	Dao<LocalBigInteger, Object> dao = createDao(LocalBigInteger.class, true);
	LocalBigInteger foo = new LocalBigInteger();
	assertEquals(1, dao.create(foo));

	List<LocalBigInteger> results = dao.queryForAll();
	assertEquals(1, results.size());
	assertNull(results.get(0).bigInteger);
}
 
Example 13
Source File: DateIntegerTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDateIntegerSeconds() throws Exception {
	Dao<LocalDateInteger, Object> dao = createDao(LocalDateInteger.class, true);
	long now = System.currentTimeMillis();
	Date val = new Date(now - (now % 1000) + 1 /* 1 extra ms here */);
	LocalDateInteger foo = new LocalDateInteger();
	foo.date = val;
	assertEquals(1, dao.create(foo));
	List<LocalDateInteger> results = dao.queryForAll();
	assertNotNull(results);
	assertEquals(1, results.size());
	// this is always false because the above date has some millis
	assertFalse(results.get(0).date.equals(val));
	assertEquals(new Date(now - (now % 1000)), results.get(0).date);
}
 
Example 14
Source File: EnumToStringTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testEnumStringCreateGet() throws Exception {
	Class<LocalEnumToString> clazz = LocalEnumToString.class;
	Dao<LocalEnumToString, Object> dao = createDao(clazz, true);
	LocalEnumToString foo1 = new LocalEnumToString();
	foo1.ourEnum = OurEnum.FIRST;
	assertEquals(1, dao.create(foo1));
	LocalEnumToString foo2 = new LocalEnumToString();
	foo2.ourEnum = OurEnum.SECOND;
	assertEquals(1, dao.create(foo2));
	List<LocalEnumToString> results = dao.queryForAll();
	assertEquals(2, results.size());
	assertEquals(foo1.ourEnum, results.get(0).ourEnum);
	assertEquals(foo2.ourEnum, results.get(1).ourEnum);
}
 
Example 15
Source File: EnumStringTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testUnknownValueAnnotation() throws Exception {
	Dao<LocalUnknownEnum, Object> dao = createDao(LocalUnknownEnum.class, true);
	LocalUnknownEnum localUnknownEnum = new LocalUnknownEnum();
	localUnknownEnum.ourEnum = OurEnum.SECOND;
	assertEquals(1, dao.create(localUnknownEnum));
	assertEquals(1, dao.updateRaw("UPDATE Foo set ourEnum = 'THIRD'"));
	List<LocalUnknownEnum> unknowns = dao.queryForAll();
	assertEquals(1, unknowns.size());
	assertEquals(OurEnum.FIRST, unknowns.get(0).ourEnum);
}
 
Example 16
Source File: TableUtilsTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDropThenQuery() throws Exception {
	Dao<LocalFoo, Integer> fooDao = createDao(LocalFoo.class, true);
	assertEquals(0, fooDao.queryForAll().size());
	dropTable(LocalFoo.class, true);
	try {
		fooDao.queryForAll();
		fail("Should have thrown");
	} catch (SQLException e) {
		// expected
	}
}
 
Example 17
Source File: GetCourseDownloads.java    From file-downloader with Apache License 2.0 5 votes vote down vote up
public void getCourseDownloads(Context context, OnGetCourseDownloadsListener onGetCourseDownloadsListener) {

        List<CoursePreviewInfo> coursePreviewInfos = null;

        try {
            Dao<CoursePreviewInfo, Integer> dao = CourseDbHelper.getInstance(context).getDao(CoursePreviewInfo.class);
            coursePreviewInfos = dao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (coursePreviewInfos != null) {

                // init DownloadFiles
                for (CoursePreviewInfo coursePreviewInfo : coursePreviewInfos) {
                    if (coursePreviewInfo == null) {
                        continue;
                    }
                    coursePreviewInfo.init();
                }

                onGetCourseDownloadsListener.onGetCourseDownloadsSucceed(coursePreviewInfos);
            } else {
                onGetCourseDownloadsListener.onGetCourseDownloadsFailed();
            }
        }

    }
 
Example 18
Source File: BaseTypeTest.java    From ormlite-core with ISC License 4 votes vote down vote up
protected <T, ID> void testType(Dao<T, ID> dao, T foo, Class<T> clazz, Object javaVal, Object defaultSqlVal,
		Object sqlArg, String defaultValStr, DataType dataType, String columnName, boolean isValidGeneratedType,
		boolean isAppropriateId, boolean isEscapedValue, boolean isPrimitive, boolean isSelectArgRequired,
		boolean isStreamType, boolean isComparable, boolean isConvertableId) throws Exception {
	DataPersister dataPersister = dataType.getDataPersister();
	DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME);
	CompiledStatement stmt = null;
	if (sqlArg != null) {
		assertEquals(defaultSqlVal.getClass(), sqlArg.getClass());
	}
	try {
		stmt = conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
				DatabaseConnection.DEFAULT_RESULT_FLAGS, true);
		DatabaseResults results = stmt.runQuery(null);
		assertTrue(results.next());
		int colNum = results.findColumn(columnName);
		Field field = clazz.getDeclaredField(columnName);
		FieldType fieldType = FieldType.createFieldType(databaseType, TABLE_NAME, field, clazz);
		assertEquals(dataType.getDataPersister(), fieldType.getDataPersister());
		Class<?>[] classes = fieldType.getDataPersister().getAssociatedClasses();
		if (classes.length > 0) {
			assertTrue(classes[0].isAssignableFrom(fieldType.getType()));
		}
		assertTrue(fieldType.getDataPersister().isValidForField(field));
		if (javaVal instanceof byte[]) {
			assertTrue(Arrays.equals((byte[]) javaVal,
					(byte[]) dataPersister.resultToJava(fieldType, results, colNum)));
		} else {
			Map<String, Integer> colMap = new HashMap<String, Integer>();
			colMap.put(columnName, colNum);
			Object result = fieldType.resultToJava(results, colMap);
			assertEquals(javaVal, result);
		}
		if (dataType == DataType.SERIALIZABLE) {
			try {
				dataPersister.parseDefaultString(fieldType, "");
				fail("parseDefaultString should have thrown for " + dataType);
			} catch (SQLException e) {
				// expected
			}
		} else if (defaultValStr != null) {
			Object parsedDefault = dataPersister.parseDefaultString(fieldType, defaultValStr);
			assertEquals(defaultSqlVal.getClass(), parsedDefault.getClass());
			if (dataType == DataType.BYTE_ARRAY || dataType == DataType.STRING_BYTES) {
				assertTrue(Arrays.equals((byte[]) defaultSqlVal, (byte[]) parsedDefault));
			} else {
				assertEquals(defaultSqlVal, parsedDefault);
			}
		}
		if (sqlArg == null) {
			// noop
		} else if (sqlArg instanceof byte[]) {
			assertTrue(Arrays.equals((byte[]) sqlArg, (byte[]) dataPersister.javaToSqlArg(fieldType, javaVal)));
		} else {
			assertEquals(sqlArg, dataPersister.javaToSqlArg(fieldType, javaVal));
		}
		assertEquals(isValidGeneratedType, dataPersister.isValidGeneratedType());
		assertEquals(isAppropriateId, dataPersister.isAppropriateId());
		assertEquals(isEscapedValue, dataPersister.isEscapedValue());
		assertEquals(isEscapedValue, dataPersister.isEscapedDefaultValue());
		assertEquals(isPrimitive, dataPersister.isPrimitive());
		assertEquals(isSelectArgRequired, dataPersister.isArgumentHolderRequired());
		assertEquals(isStreamType, dataPersister.isStreamType());
		assertEquals(isComparable, dataPersister.isComparable());
		if (isConvertableId) {
			assertNotNull(dataPersister.convertIdNumber(10));
		} else {
			assertNull(dataPersister.convertIdNumber(10));
		}
		List<T> list = dao.queryForAll();
		assertEquals(1, list.size());
		assertTrue(dao.objectsEqual(foo, list.get(0)));
		// if we have a value then look for it, floats don't find any results because of rounding issues
		if (javaVal != null && dataPersister.isComparable() && dataType != DataType.FLOAT
				&& dataType != DataType.FLOAT_OBJ) {
			// test for inline arguments
			list = dao.queryForMatching(foo);
			assertEquals(1, list.size());
			assertTrue(dao.objectsEqual(foo, list.get(0)));
			// test for SelectArg arguments
			list = dao.queryForMatchingArgs(foo);
			assertEquals(1, list.size());
			assertTrue(dao.objectsEqual(foo, list.get(0)));
		}
		if (dataType == DataType.STRING_BYTES || dataType == DataType.BYTE_ARRAY
				|| dataType == DataType.SERIALIZABLE) {
			// no converting from string to value
		} else {
			// test string conversion
			String stringVal = results.getString(colNum);
			Object convertedJavaVal = fieldType.convertStringToJavaField(stringVal, 0);
			assertEquals(javaVal, convertedJavaVal);
		}
	} finally {
		if (stmt != null) {
			stmt.close();
		}
		connectionSource.releaseConnection(conn);
	}
}
 
Example 19
Source File: QueryBuilderTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMixAndOrInline() throws Exception {
	Dao<Foo, String> dao = createDao(Foo.class, true);

	Foo foo1 = new Foo();
	foo1.val = 10;
	foo1.stringField = "zip";
	assertEquals(1, dao.create(foo1));
	Foo foo2 = new Foo();
	foo2.val = foo1.val;
	foo2.stringField = foo1.stringField + "zap";
	assertEquals(1, dao.create(foo2));

	/*
	 * Inline
	 */
	QueryBuilder<Foo, String> qb = dao.queryBuilder();
	Where<Foo, String> where = qb.where();
	where.eq(Foo.VAL_COLUMN_NAME, foo1.val)
			.and()
			.eq(Foo.STRING_COLUMN_NAME, foo1.stringField)
			.or()
			.eq(Foo.STRING_COLUMN_NAME, foo2.stringField);

	List<Foo> results = dao.queryForAll();
	assertEquals(2, results.size());
	assertEquals(foo1.id, results.get(0).id);
	assertEquals(foo2.id, results.get(1).id);

	/*
	 * Arguments
	 */
	qb = dao.queryBuilder();
	where = qb.where();
	where.and(where.eq(Foo.VAL_COLUMN_NAME, foo1.val), //
			where.or(where.eq(Foo.STRING_COLUMN_NAME, foo1.stringField), //
					where.eq(Foo.STRING_COLUMN_NAME, foo2.stringField)));

	results = dao.queryForAll();
	assertEquals(2, results.size());
	assertEquals(foo1.id, results.get(0).id);
	assertEquals(foo2.id, results.get(1).id);

	/*
	 * Multiple lines
	 */
	qb = dao.queryBuilder();
	where = qb.where();
	where.eq(Foo.VAL_COLUMN_NAME, foo1.val);
	where.and();
	where.eq(Foo.STRING_COLUMN_NAME, foo1.stringField);
	where.or();
	where.eq(Foo.STRING_COLUMN_NAME, foo2.stringField);

	results = dao.queryForAll();
	assertEquals(2, results.size());
	assertEquals(foo1.id, results.get(0).id);
	assertEquals(foo2.id, results.get(1).id);

	/*
	 * Postfix
	 */
	qb = dao.queryBuilder();
	where = qb.where();
	where.eq(Foo.VAL_COLUMN_NAME, foo1.val);
	where.eq(Foo.STRING_COLUMN_NAME, foo1.stringField);
	where.eq(Foo.STRING_COLUMN_NAME, foo2.stringField);
	where.or(2);
	where.and(2);

	results = dao.queryForAll();
	assertEquals(2, results.size());
	assertEquals(foo1.id, results.get(0).id);
	assertEquals(foo2.id, results.get(1).id);
}
 
Example 20
Source File: MainModule.java    From TinkerTime with GNU General Public License v3.0 4 votes vote down vote up
@Singleton
@Provides
ConfigData getConfigData(Dao<ConfigData, Integer> configDao) throws SQLException{
	List<ConfigData> configs = configDao.queryForAll();
	return !configs.isEmpty() ? configs.get(0) : new ConfigData(configDao);
}