Java Code Examples for com.j256.ormlite.table.TableInfo#getIdField()

The following examples show how to use com.j256.ormlite.table.TableInfo#getIdField() . 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: BaseJdbcDatabaseTypeTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testCreateColumnArg() throws Exception {
	if (connectionSource == null) {
		return;
	}
	List<String> additionalArgs = new ArrayList<String>();
	List<String> moreStmts = new ArrayList<String>();
	List<String> queriesAfter = new ArrayList<String>();
	TableInfo<StringId, String> tableInfo = new TableInfo<StringId, String>(databaseType, StringId.class);
	FieldType fieldType = tableInfo.getIdField();
	StringBuilder sb = new StringBuilder();
	databaseType.appendColumnArg(null, sb, fieldType, additionalArgs, null, moreStmts, queriesAfter);
	assertTrue(sb.toString().contains(fieldType.getColumnName()));
	if (!sb.toString().contains("PRIMARY KEY")) {
		databaseType.addPrimaryKeySql(new FieldType[] { fieldType }, additionalArgs, null, moreStmts, queriesAfter);
		assertEquals(1, additionalArgs.size());
		assertTrue(additionalArgs.get(0).contains("PRIMARY KEY"));
	}
}
 
Example 2
Source File: MappedQueryForFieldEq.java    From ormlite-core with ISC License 5 votes vote down vote up
public static <T, ID> MappedQueryForFieldEq<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo,
		FieldType idFieldType) throws SQLException {
	if (idFieldType == null) {
		idFieldType = tableInfo.getIdField();
		if (idFieldType == null) {
			throw new SQLException("Cannot query-for-id with " + tableInfo.getDataClass()
					+ " because it doesn't have an id field");
		}
	}
	DatabaseType databaseType = dao.getConnectionSource().getDatabaseType();
	String statement = buildStatement(databaseType, tableInfo, idFieldType);
	return new MappedQueryForFieldEq<T, ID>(dao, tableInfo, statement, new FieldType[] { idFieldType },
			tableInfo.getFieldTypes(), "query-for-id");
}
 
Example 3
Source File: MappedUpdateId.java    From ormlite-core with ISC License 5 votes vote down vote up
public static <T, ID> MappedUpdateId<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo) throws SQLException {
	FieldType idField = tableInfo.getIdField();
	if (idField == null) {
		throw new SQLException(
				"Cannot update-id in " + tableInfo.getDataClass() + " because it doesn't have an id field");
	}
	StringBuilder sb = new StringBuilder(64);
	DatabaseType databaseType = dao.getConnectionSource().getDatabaseType();
	appendTableName(databaseType, sb, "UPDATE ", tableInfo);
	sb.append("SET ");
	appendFieldColumnName(databaseType, sb, idField, null);
	sb.append("= ? ");
	appendWhereFieldEq(databaseType, idField, sb, null);
	return new MappedUpdateId<T, ID>(dao, tableInfo, sb.toString(), new FieldType[] { idField, idField });
}
 
Example 4
Source File: MappedRefresh.java    From ormlite-core with ISC License 5 votes vote down vote up
public static <T, ID> MappedRefresh<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo) throws SQLException {
	FieldType idField = tableInfo.getIdField();
	if (idField == null) {
		throw new SQLException(
				"Cannot refresh " + tableInfo.getDataClass() + " because it doesn't have an id field");
	}
	DatabaseType databaseType = dao.getConnectionSource().getDatabaseType();
	String statement = buildStatement(databaseType, tableInfo, idField);
	return new MappedRefresh<T, ID>(dao, tableInfo, statement, new FieldType[] { tableInfo.getIdField() },
			tableInfo.getFieldTypes());
}
 
Example 5
Source File: MappedDeleteCollection.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Delete all of the objects in the collection. This builds a {@link MappedDeleteCollection} on the fly because the
 * datas could be variable sized.
 */
public static <T, ID> int deleteObjects(Dao<T, ID> dao, TableInfo<T, ID> tableInfo,
		DatabaseConnection databaseConnection, Collection<T> datas, ObjectCache objectCache) throws SQLException {
	MappedDeleteCollection<T, ID> deleteCollection = MappedDeleteCollection.build(dao, tableInfo, datas.size());
	Object[] fieldObjects = new Object[datas.size()];
	FieldType idField = tableInfo.getIdField();
	int objC = 0;
	for (T data : datas) {
		fieldObjects[objC] = idField.extractJavaFieldToSqlArgValue(data);
		objC++;
	}
	return updateRows(databaseConnection, tableInfo.getDataClass(), deleteCollection, fieldObjects, objectCache);
}
 
Example 6
Source File: MappedDeleteCollection.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Delete all of the objects in the collection. This builds a {@link MappedDeleteCollection} on the fly because the
 * ids could be variable sized.
 */
public static <T, ID> int deleteIds(Dao<T, ID> dao, TableInfo<T, ID> tableInfo,
		DatabaseConnection databaseConnection, Collection<ID> ids, ObjectCache objectCache) throws SQLException {
	MappedDeleteCollection<T, ID> deleteCollection = MappedDeleteCollection.build(dao, tableInfo, ids.size());
	Object[] fieldObjects = new Object[ids.size()];
	FieldType idField = tableInfo.getIdField();
	int objC = 0;
	for (ID id : ids) {
		fieldObjects[objC] = idField.convertJavaFieldToSqlArgValue(id);
		objC++;
	}
	return updateRows(databaseConnection, tableInfo.getDataClass(), deleteCollection, fieldObjects, objectCache);
}
 
Example 7
Source File: MappedDeleteCollection.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * This is private because the execute is the only method that should be called here.
 */
private static <T, ID> MappedDeleteCollection<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo, int dataSize)
		throws SQLException {
	FieldType idField = tableInfo.getIdField();
	if (idField == null) {
		throw new SQLException(
				"Cannot delete " + tableInfo.getDataClass() + " because it doesn't have an id field defined");
	}
	StringBuilder sb = new StringBuilder(128);
	DatabaseType databaseType = dao.getConnectionSource().getDatabaseType();
	appendTableName(databaseType, sb, "DELETE FROM ", tableInfo);
	FieldType[] argFieldTypes = new FieldType[dataSize];
	appendWhereIds(databaseType, idField, sb, dataSize, argFieldTypes);
	return new MappedDeleteCollection<T, ID>(dao, tableInfo, sb.toString(), argFieldTypes);
}
 
Example 8
Source File: BaseMappedStatement.java    From ormlite-core with ISC License 5 votes vote down vote up
protected BaseMappedStatement(Dao<T, ID> dao, TableInfo<T, ID> tableInfo, String statement,
		FieldType[] argFieldTypes) {
	this.dao = dao;
	this.connectionSource = dao.getConnectionSource();
	this.tableInfo = tableInfo;
	this.clazz = tableInfo.getDataClass();
	this.idField = tableInfo.getIdField();
	this.statement = statement;
	this.argFieldTypes = argFieldTypes;
}
 
Example 9
Source File: MappedDelete.java    From ormlite-core with ISC License 5 votes vote down vote up
public static <T, ID> MappedDelete<T, ID> build(Dao<T, ID> dao, TableInfo<T, ID> tableInfo) throws SQLException {
	FieldType idField = tableInfo.getIdField();
	if (idField == null) {
		throw new SQLException(
				"Cannot delete from " + tableInfo.getDataClass() + " because it doesn't have an id field");
	}
	StringBuilder sb = new StringBuilder(64);
	DatabaseType databaseType = dao.getConnectionSource().getDatabaseType();
	appendTableName(databaseType, sb, "DELETE FROM ", tableInfo);
	appendWhereFieldEq(databaseType, idField, sb, null);
	return new MappedDelete<T, ID>(dao, tableInfo, sb.toString(), new FieldType[] { idField });
}
 
Example 10
Source File: Where.java    From ormlite-core with ISC License 5 votes vote down vote up
protected Where(TableInfo<T, ID> tableInfo, StatementBuilder<T, ID> statementBuilder, DatabaseType databaseType) {
	// limit the constructor scope
	this.tableInfo = tableInfo;
	this.statementBuilder = statementBuilder;
	this.idFieldType = tableInfo.getIdField();
	if (idFieldType == null) {
		this.idColumnName = null;
	} else {
		this.idColumnName = idFieldType.getColumnName();
	}
	this.databaseType = databaseType;
}
 
Example 11
Source File: QueryBuilder.java    From ormlite-core with ISC License 4 votes vote down vote up
public QueryBuilder(DatabaseType databaseType, TableInfo<T, ID> tableInfo, Dao<T, ID> dao) {
	super(databaseType, tableInfo, dao, StatementType.SELECT);
	this.idField = tableInfo.getIdField();
	this.selectIdColumn = (idField != null);
}