当前位置: 首页>>代码示例>>Java>>正文


Java DatabaseResults.findColumn方法代码示例

本文整理汇总了Java中com.j256.ormlite.support.DatabaseResults.findColumn方法的典型用法代码示例。如果您正苦于以下问题:Java DatabaseResults.findColumn方法的具体用法?Java DatabaseResults.findColumn怎么用?Java DatabaseResults.findColumn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.j256.ormlite.support.DatabaseResults的用法示例。


在下文中一共展示了DatabaseResults.findColumn方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testDateStringResultInvalid

import com.j256.ormlite.support.DatabaseResults; //导入方法依赖的package包/类
@Test(expected = SQLException.class)
public void testDateStringResultInvalid() throws Exception {
	Class<LocalString> clazz = LocalString.class;
	Dao<LocalString, Object> dao = createDao(clazz, true);
	LocalString foo = new LocalString();
	foo.string = "not a date format";
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadOnlyConnection(TABLE_NAME);
	CompiledStatement stmt = null;
	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(STRING_COLUMN);
		DataType.DATE_STRING.getDataPersister().resultToJava(null, results, colNum);
	} finally {
		if (stmt != null) {
			stmt.close();
		}
		connectionSource.releaseConnection(conn);
	}
}
 
开发者ID:j256,项目名称:ormlite-tests,代码行数:25,代码来源:BaseDataTypeTest.java

示例2: testQueryRawDatabaseResults

import com.j256.ormlite.support.DatabaseResults; //导入方法依赖的package包/类
@Test
public void testQueryRawDatabaseResults() throws Exception {
	Dao<Foo, Object> dao = createDao(Foo.class, true);
	Foo foo = new Foo();
	foo.val = 342234232;
	assertEquals(1, dao.create(foo));

	CloseableIterator<Foo> iterator =
			dao.iterator(dao.queryBuilder().where().eq(Foo.VAL_COLUMN_NAME, foo.val).prepare());
	try {
		DatabaseResults results = iterator.getRawResults();
		assertTrue(results.first());
		assertTrue(results.getColumnCount() >= 4);
		int valIndex = results.findColumn(Foo.VAL_COLUMN_NAME);
		assertEquals(foo.val, results.getInt(valIndex));
		assertFalse(results.next());
	} finally {
		iterator.closeQuietly();
	}
}
 
开发者ID:j256,项目名称:ormlite-core,代码行数:21,代码来源:CloseableIteratorTest.java

示例3: testDateStringResultInvalid

import com.j256.ormlite.support.DatabaseResults; //导入方法依赖的package包/类
@Test(expected = SQLException.class)
public void testDateStringResultInvalid() throws Exception {
	Class<LocalString> clazz = LocalString.class;
	Dao<LocalString, Object> dao = createDao(clazz, true);
	LocalString foo = new LocalString();
	foo.string = "not a date format";
	assertEquals(1, dao.create(foo));
	DatabaseConnection conn = connectionSource.getReadOnlyConnection(FOO_TABLE_NAME);
	CompiledStatement stmt = null;
	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(STRING_COLUMN);
		DataType.DATE_STRING.getDataPersister().resultToJava(null, results, colNum);
	} finally {
		if (stmt != null) {
			stmt.close();
		}
		connectionSource.releaseConnection(conn);
	}
}
 
开发者ID:j256,项目名称:ormlite-core,代码行数:24,代码来源:DateStringTypeTest.java

示例4: resultToJava

import com.j256.ormlite.support.DatabaseResults; //导入方法依赖的package包/类
/**
 * Get the result object from the results. A call through to {@link FieldConverter#resultToJava}.
 */
public <T> T resultToJava(DatabaseResults results, Map<String, Integer> columnPositions) throws SQLException {
	Integer dbColumnPos = columnPositions.get(columnName);
	if (dbColumnPos == null) {
		dbColumnPos = results.findColumn(columnName);
		columnPositions.put(columnName, dbColumnPos);
	}

	/*
	 * Subtle problem here. If the field is a foreign-field and/or a primitive and the value was null then we get 0
	 * from results.getInt() which mirrors the ResultSet. We have to specifically test to see if we have a null
	 * result with results.wasNull(...) afterwards so we return a null value to not create the sub-object or turn a
	 * Integer field into 0 instead of null.
	 * 
	 * But this presents a limitation. Sometimes people want to convert a result field value that is stored in the
	 * database as null into a non-null value when it comes out of the database. There is no way to do that because
	 * of the results.wasNull(...) checks after the fact then override the non-null value from the converter.
	 */
	@SuppressWarnings("unchecked")
	T converted = (T) fieldConverter.resultToJava(this, results, dbColumnPos);
	if (fieldConfig.isForeign()) {
		/*
		 * If your foreign field is a primitive and the value was null then this would return 0 from
		 * results.getInt(). We have to specifically test to see if we have a foreign field so if it is null we
		 * return a null value to not create the sub-object.
		 */
		if (results.wasNull(dbColumnPos)) {
			return null;
		}
	} else if (dataPersister.isPrimitive()) {
		if (fieldConfig.isThrowIfNull() && results.wasNull(dbColumnPos)) {
			throw new SQLException(
					"Results value for primitive field '" + field.getName() + "' was an invalid null value");
		}
	} else if (!fieldConverter.isStreamType() && results.wasNull(dbColumnPos)) {
		// we can't check if we have a null if this is a stream type
		return null;
	}
	return converted;
}
 
开发者ID:j256,项目名称:ormlite-core,代码行数:43,代码来源:FieldType.java

示例5: testType

import com.j256.ormlite.support.DatabaseResults; //导入方法依赖的package包/类
private void testType(Class<?> clazz, Object javaVal, Object sqlVal, 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;
	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);
		FieldType fieldType =
				FieldType.createFieldType(connectionSource, TABLE_NAME, clazz.getDeclaredField(columnName), clazz);
		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.STRING_BYTES || dataType == DataType.BYTE_ARRAY
				|| dataType == DataType.SERIALIZABLE) {
			try {
				dataPersister.parseDefaultString(fieldType, "");
				fail("parseDefaultString should have thrown for " + dataType);
			} catch (SQLException e) {
				// expected
			}
		} else if (defaultValStr != null) {
			assertEquals(sqlVal, dataPersister.parseDefaultString(fieldType, defaultValStr));
		}
		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(javaVal, dataPersister.sqlArgToJava(fieldType, sqlArg, 0));
		}
		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));
		}
		dataTypeSet.add(dataType);
	} finally {
		if (stmt != null) {
			stmt.close();
		}
		connectionSource.releaseConnection(conn);
	}
}
 
开发者ID:j256,项目名称:ormlite-tests,代码行数:66,代码来源:BaseDataTypeTest.java


注:本文中的com.j256.ormlite.support.DatabaseResults.findColumn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。