use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class TypeResolver method resolveVar.
private static Type resolveVar(Type resolved, String varName, Type declaringType, EntityType context) {
// get parameter index of var in declaring type
int index = -1;
for (int i = 0; i < declaringType.getParameters().size(); i++) {
Type param = unwrap(declaringType.getParameters().get(i));
if (Objects.equals(getVarName(param), varName)) {
index = i;
}
}
if (index == -1) {
throw new IllegalStateException("Did not find type " + varName + " in " + declaringType + " for " + context);
}
Supertype type = context.getSuperType();
while (!type.getEntityType().equals(declaringType)) {
type = type.getEntityType().getSuperType();
}
if (!type.getType().getParameters().isEmpty()) {
return type.getType().getParameters().get(index);
} else {
// raw type
return resolved;
}
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class AbstractDomainExporter method serialize.
private void serialize(Map<Class<?>, EntityType> types, Serializer serializer) throws IOException {
for (EntityType entityType : types.values()) {
if (serialized.add(entityType)) {
Type type = typeMappings.getPathType(entityType, entityType, true);
String packageName = type.getPackageName();
String className = packageName.length() > 0 ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
write(serializer, className.replace('.', '/') + ".java", entityType);
}
}
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class JPADomainExporter method handleProperty.
private void handleProperty(EntityType entityType, Class<?> cl, Attribute<?, ?> p) throws NoSuchMethodException, ClassNotFoundException {
Class<?> clazz = Object.class;
try {
clazz = p.getJavaType();
} catch (MappingException e) {
// ignore
}
Type propertyType = getType(cl, clazz, p.getName());
AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
propertyType = getTypeOverride(propertyType, annotated);
if (propertyType == null) {
return;
}
if (p.isCollection()) {
if (p instanceof MapAttribute) {
MapAttribute<?, ?, ?> map = (MapAttribute<?, ?, ?>) p;
Type keyType = typeFactory.get(map.getKeyJavaType());
Type valueType = typeFactory.get(map.getElementType().getJavaType());
valueType = getPropertyType(p, valueType);
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
} else {
Type valueType = typeFactory.get(((PluralAttribute<?, ?, ?>) p).getElementType().getJavaType());
valueType = getPropertyType(p, valueType);
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), valueType));
}
} else {
propertyType = getPropertyType(p, propertyType);
}
Property property = createProperty(entityType, p.getName(), propertyType, annotated);
entityType.addProperty(property);
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class MetaDataExporter method handleColumn.
private void handleColumn(EntityType classModel, String tableName, ResultSet columns) throws SQLException {
String columnName = normalize(columns.getString("COLUMN_NAME"));
String normalizedColumnName = namingStrategy.normalizeColumnName(columnName);
int columnType = columns.getInt("DATA_TYPE");
String typeName = columns.getString("TYPE_NAME");
Number columnSize = (Number) columns.getObject("COLUMN_SIZE");
Number columnDigits = (Number) columns.getObject("DECIMAL_DIGITS");
int columnIndex = columns.getInt("ORDINAL_POSITION");
int nullable = columns.getInt("NULLABLE");
String columnDefaultValue = columns.getString("COLUMN_DEF");
String propertyName = namingStrategy.getPropertyName(normalizedColumnName, classModel);
Class<?> clazz = configuration.getJavaType(columnType, typeName, columnSize != null ? columnSize.intValue() : 0, columnDigits != null ? columnDigits.intValue() : 0, tableName, columnName);
if (clazz == null) {
clazz = Object.class;
}
TypeCategory fieldType = TypeCategory.get(clazz.getName());
if (Number.class.isAssignableFrom(clazz)) {
fieldType = TypeCategory.NUMERIC;
} else if (Enum.class.isAssignableFrom(clazz)) {
fieldType = TypeCategory.ENUM;
}
Type typeModel = new ClassType(fieldType, clazz);
Property property = createProperty(classModel, normalizedColumnName, propertyName, typeModel);
ColumnMetadata column = ColumnMetadata.named(normalizedColumnName).ofType(columnType).withIndex(columnIndex);
if (nullable == DatabaseMetaData.columnNoNulls) {
column = column.notNull();
}
if (columnSize != null) {
column = column.withSize(columnSize.intValue());
}
if (columnDigits != null) {
column = column.withDigits(columnDigits.intValue());
}
property.getData().put("COLUMN", column);
if (columnAnnotations) {
property.addAnnotation(new ColumnImpl(normalizedColumnName));
}
if (validationAnnotations) {
if (nullable == DatabaseMetaData.columnNoNulls && columnDefaultValue == null) {
property.addAnnotation(new NotNullImpl());
}
int size = columns.getInt("COLUMN_SIZE");
if (size > 0 && clazz.equals(String.class)) {
property.addAnnotation(new SizeImpl(0, size));
}
}
classModel.addProperty(property);
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class MetaDataExporter method createEntityType.
protected EntityType createEntityType(SchemaAndTable schemaAndTable, final String className) {
EntityType classModel;
if (beanSerializer == null) {
String packageName = normalizePackage(module.getPackageName(), schemaAndTable);
String simpleName = module.getPrefix() + className + module.getSuffix();
Type classTypeModel = new SimpleType(TypeCategory.ENTITY, packageName + "." + simpleName, packageName, simpleName, false, false);
classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
typeMappings.register(classModel, classModel);
} else {
String beanPackage = normalizePackage(beanPackageName, schemaAndTable);
String simpleName = module.getBeanPrefix() + className + module.getBeanSuffix();
Type classTypeModel = new SimpleType(TypeCategory.ENTITY, beanPackage + "." + simpleName, beanPackage, simpleName, false, false);
classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS));
Type mappedType = queryTypeFactory.create(classModel);
entityToWrapped.put(classModel, mappedType);
typeMappings.register(classModel, mappedType);
}
classModel.getData().put("schema", schemaAndTable.getSchema());
classModel.getData().put("table", schemaAndTable.getTable());
return classModel;
}
Aggregations