Java Code Examples for com.j256.ormlite.field.FieldType#isForeignCollection()

The following examples show how to use com.j256.ormlite.field.FieldType#isForeignCollection() . 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: MappedCreate.java    From ormlite-core with ISC License 6 votes vote down vote up
private static boolean isFieldCreatable(DatabaseType databaseType, FieldType fieldType) {
	// we don't insert anything if it is a collection
	if (fieldType.isForeignCollection()) {
		// skip foreign collections
		return false;
	} else if (fieldType.isReadOnly()) {
		// ignore read-only fields
		return false;
	} else if (databaseType.isIdSequenceNeeded() && databaseType.isSelectSequenceBeforeInsert()) {
		// we need to query for the next value from the sequence and the idField is inserted afterwards
		return true;
	} else if (fieldType.isGeneratedId() && !fieldType.isSelfGeneratedId()
			&& !fieldType.isAllowGeneratedIdInsert()) {
		// skip generated-id fields because they will be auto-inserted
		return false;
	} else {
		return true;
	}
}
 
Example 2
Source File: UpdateBuilder.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Add a column to be set to a value for UPDATE statements. This will generate something like columnName = 'value'
 * with the value escaped if necessary.
 */
public UpdateBuilder<T, ID> updateColumnValue(String columnName, Object value) throws SQLException {
	FieldType fieldType = verifyColumnName(columnName);
	if (fieldType.isForeignCollection()) {
		throw new SQLException("Can't update foreign colletion field: " + columnName);
	}
	addUpdateColumnToList(columnName, new SetValue(columnName, fieldType, value));
	return this;
}
 
Example 3
Source File: MappedUpdate.java    From ormlite-core with ISC License 5 votes vote down vote up
private static boolean isFieldUpdatable(FieldType fieldType, FieldType idField) {
	if (fieldType == idField || fieldType.isForeignCollection() || fieldType.isReadOnly()) {
		return false;
	} else {
		return true;
	}
}
 
Example 4
Source File: QueryBuilder.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Add "ORDER BY" clause to the SQL query statement. This can be called multiple times to add additional "ORDER BY"
 * clauses. Ones earlier are applied first.
 */
public QueryBuilder<T, ID> orderBy(String columnName, boolean ascending) {
	FieldType fieldType = verifyColumnName(columnName);
	if (fieldType.isForeignCollection()) {
		throw new IllegalArgumentException("Can't orderBy foreign collection field: " + columnName);
	}
	addOrderBy(new OrderBy(columnName, ascending));
	return this;
}
 
Example 5
Source File: ApsTableUtils.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generate and return the list of statements to create a database table and any associated features.
 */
private static <T, ID> void addCreateTableStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		List<String> statements, List<String> queriesAfter, boolean ifNotExists) throws SQLException {
	StringBuilder sb = new StringBuilder(256);
	sb.append("CREATE TABLE ");
	if (ifNotExists && databaseType.isCreateIfNotExistsSupported()) {
		sb.append("IF NOT EXISTS ");
	}
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" (");
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	List<String> statementsAfter = new ArrayList<String>();
	// our statement will be set here later
	boolean first = true;
	for (FieldType fieldType : tableInfo.getFieldTypes()) {
		// skip foreign collections
		if (fieldType.isForeignCollection()) {
			continue;
		} else if (first) {
			first = false;
		} else {
			sb.append(", ");
		}
		String columnDefinition = fieldType.getColumnDefinition();
		if (columnDefinition == null) {
			// we have to call back to the database type for the specific create syntax
			databaseType.appendColumnArg(tableInfo.getTableName(), sb, fieldType, additionalArgs, statementsBefore,
					statementsAfter, queriesAfter);
		} else {
			// hand defined field
			databaseType.appendEscapedEntityName(sb, fieldType.getColumnName());
			sb.append(' ').append(columnDefinition).append(' ');
		}
	}
	// add any sql that sets any primary key fields
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	// add any sql that sets any unique fields
	databaseType.addUniqueComboSql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	for (String arg : additionalArgs) {
		// we will have spat out one argument already so we don't have to do the first dance
		sb.append(", ").append(arg);
	}
	sb.append(") ");
	databaseType.appendCreateTableSuffix(sb);
	statements.addAll(statementsBefore);
	statements.add(sb.toString());
	statements.addAll(statementsAfter);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, false);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, true);
}
 
Example 6
Source File: QueryBuilder.java    From ormlite-core with ISC License 4 votes vote down vote up
private void appendSelects(StringBuilder sb) {
	// the default
	type = StatementType.SELECT;

	// if no columns were specified then * is the default
	if (selectList == null) {
		if (addTableName) {
			appendTableQualifier(sb);
			sb.append('.');
		}
		sb.append("* ");
		resultFieldTypes = tableInfo.getFieldTypes();
		return;
	}

	boolean first = true;
	boolean hasId;
	if (isInnerQuery) {
		hasId = true;
	} else {
		hasId = false;
	}
	List<FieldType> fieldTypeList = new ArrayList<FieldType>(selectList.size() + 1);
	for (ColumnNameOrRawSql select : selectList) {
		if (select.getRawSql() != null) {
			// if any are raw-sql then that's our type
			type = StatementType.SELECT_RAW;
			if (first) {
				first = false;
			} else {
				sb.append(", ");
			}
			sb.append(select.getRawSql());
			continue;
		}
		FieldType fieldType = tableInfo.getFieldTypeByColumnName(select.getColumnName());
		/*
		 * If this is a foreign-collection then we add it to our field-list but _not_ to the select list because
		 * foreign collections don't have a column in the database.
		 */
		if (fieldType.isForeignCollection()) {
			fieldTypeList.add(fieldType);
			continue;
		}
		if (first) {
			first = false;
		} else {
			sb.append(", ");
		}
		appendFieldColumnName(sb, fieldType, fieldTypeList);
		if (fieldType == idField) {
			hasId = true;
		}
	}

	if (type != StatementType.SELECT_RAW) {
		// we have to add the idField even if it isn't in the columnNameSet
		if (!hasId && selectIdColumn) {
			if (!first) {
				sb.append(',');
			}
			appendFieldColumnName(sb, idField, fieldTypeList);
		}

		resultFieldTypes = fieldTypeList.toArray(new FieldType[fieldTypeList.size()]);
	}
	sb.append(' ');
}
 
Example 7
Source File: TableUtils.java    From ormlite-core with ISC License 4 votes vote down vote up
/**
 * Generate and return the list of statements to create a database table and any associated features.
 */
private static <T, ID> void addCreateTableStatements(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
		List<String> statements, List<String> queriesAfter, boolean ifNotExists, boolean logDetails)
		throws SQLException {
	StringBuilder sb = new StringBuilder(256);
	if (logDetails) {
		logger.info("creating table '{}'", tableInfo.getTableName());
	}
	sb.append("CREATE TABLE ");
	if (ifNotExists && databaseType.isCreateIfNotExistsSupported()) {
		sb.append("IF NOT EXISTS ");
	}
	if (tableInfo.getSchemaName() != null && tableInfo.getSchemaName().length() > 0){
		databaseType.appendEscapedEntityName(sb, tableInfo.getSchemaName());
		sb.append('.');
	}
	databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
	sb.append(" (");
	List<String> additionalArgs = new ArrayList<String>();
	List<String> statementsBefore = new ArrayList<String>();
	List<String> statementsAfter = new ArrayList<String>();
	// our statement will be set here later
	boolean first = true;
	for (FieldType fieldType : tableInfo.getFieldTypes()) {
		// skip foreign collections
		if (fieldType.isForeignCollection()) {
			continue;
		} else if (first) {
			first = false;
		} else {
			sb.append(", ");
		}
		String columnDefinition = fieldType.getColumnDefinition();
		if (columnDefinition == null) {
			// we have to call back to the database type for the specific create syntax
			databaseType.appendColumnArg(tableInfo.getTableName(), sb, fieldType, additionalArgs, statementsBefore,
					statementsAfter, queriesAfter);
		} else {
			// hand defined field
			databaseType.appendEscapedEntityName(sb, fieldType.getColumnName());
			sb.append(' ').append(columnDefinition).append(' ');
		}
	}
	// add any sql that sets any primary key fields
	databaseType.addPrimaryKeySql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	// add any sql that sets any unique fields
	databaseType.addUniqueComboSql(tableInfo.getFieldTypes(), additionalArgs, statementsBefore, statementsAfter,
			queriesAfter);
	for (String arg : additionalArgs) {
		// we will have spat out one argument already so we don't have to do the first dance
		sb.append(", ").append(arg);
	}
	sb.append(") ");
	databaseType.appendCreateTableSuffix(sb);
	statements.addAll(statementsBefore);
	statements.add(sb.toString());
	statements.addAll(statementsAfter);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, false, logDetails);
	addCreateIndexStatements(databaseType, tableInfo, statements, ifNotExists, true, logDetails);
}
 
Example 8
Source File: UpdateBuilder.java    From ormlite-core with ISC License 3 votes vote down vote up
/**
 * Add a column to be set to a value for UPDATE statements. This will generate something like 'columnName =
 * expression' where the expression is built by the caller.
 * 
 * <p>
 * The expression should have any strings escaped using the {@link #escapeValue(String)} or
 * {@link #escapeValue(StringBuilder, String)} methods and should have any column names escaped using the
 * {@link #escapeColumnName(String)} or {@link #escapeColumnName(StringBuilder, String)} methods.
 * </p>
 */
public UpdateBuilder<T, ID> updateColumnExpression(String columnName, String expression) throws SQLException {
	FieldType fieldType = verifyColumnName(columnName);
	if (fieldType.isForeignCollection()) {
		throw new SQLException("Can't update foreign colletion field: " + columnName);
	}
	addUpdateColumnToList(columnName, new SetExpression(columnName, fieldType, expression));
	return this;
}
 
Example 9
Source File: QueryBuilder.java    From ormlite-core with ISC License 3 votes vote down vote up
/**
 * Add "GROUP BY" clause to the SQL query statement. This can be called multiple times to add additional "GROUP BY"
 * clauses.
 * 
 * <p>
 * NOTE: Use of this means that the resulting objects may not have a valid ID column value so cannot be deleted or
 * updated.
 * </p>
 */
public QueryBuilder<T, ID> groupBy(String columnName) {
	FieldType fieldType = verifyColumnName(columnName);
	if (fieldType.isForeignCollection()) {
		throw new IllegalArgumentException("Can't groupBy foreign collection field: " + columnName);
	}
	addGroupBy(ColumnNameOrRawSql.withColumnName(columnName));
	return this;
}