ImmutableColumn(ImmutableTable parent, Map<String, Object> data) { super(parent, data); final Column prototype = new ColumnImpl(parent, data); this.enabled = prototype.isEnabled(); this.id = prototype.getId(); this.name = prototype.getName(); this.alias = prototype.getAlias(); this.nullable = prototype.isNullable(); this.nullableImplementation = prototype.getNullableImplementation(); this.autoincrement = prototype.isAutoIncrement(); this.typeMapper = prototype.getTypeMapper(); this.databaseType = prototype.getDatabaseType(); this.databaseTypeObject = prototype.findDatabaseType(); this.enumConstants = prototype.getEnumConstants(); this.decimalDigits = prototype.getDecimalDigits(); this.columnSize = prototype.getColumnSize(); this.ordinalPosition = prototype.getOrdinalPosition(); }
/** * Returns a list of all the enum constants in a particular column. * The list is created each time this method is called and is therefore * safe to edit without affecting the column. * <p> * If no enum constants was specified in the column, an exception is * thrown. * * @param column the column to retreive the constants from * @return list of the constants */ public static List<String> enumConstantsOf(Column column) { return Stream.of(column.getEnumConstants() .orElseThrow(() -> new RuntimeException( "Column '" + column.getId() + "' in table '" + column.getParentOrThrow().getId() + "' was marked as an enum but no enum constants was specified." )) .split(",") ).sorted().collect(toList()); }
.filter(t -> t.getId().equals(table.getId())) .flatMap(Table::columns) .filter(c -> c.getId().equals(column.getId())) .filter(c -> c.getDatabaseType().equals(column.getDatabaseType())) c.mutator().setTypeMapper(typeMapperClass); });
public static boolean usesOptional(Column col) { return col.isNullable() && HasNullable.ImplementAs.OPTIONAL == col.getNullableImplementation(); }
/** * Returns {@code true} if the two specified documents represents the same * element in the database. Two documents are considered same if they have * the same name and type and their parents are considered same. * * @param first the first document * @param second the second document * @return {@code true} if same, else {@code false} */ public static boolean isSame(Column first, Column second) { if (first.getId().equals(second.getId())) { final Table firstParent = first.getParentOrThrow(); final Table secondParent = second.getParentOrThrow(); return isSame(firstParent, secondParent); } else { return false; } }
private String readFromResultSet(File file, Column c, AtomicInteger position) { final Dbms dbms = c.getParentOrThrow().getParentOrThrow().getParentOrThrow(); dbmsTypeOf(dbmsHandlerComponent, c.getParentOrThrow().getParentOrThrow().getParentOrThrow()), c.findDatabaseType() ); final boolean isCustomTypeMapper = c.getTypeMapper().isPresent() && !TypeMapper.identity().getClass().isAssignableFrom(typeMapperClass) && !TypeMapper.primitive().getClass().isAssignableFrom(typeMapperClass); if (c.isNullable() && !NULL_AWARE_GETTERS.contains(mapping.getJavaClass())) { file.add(Import.of(ResultSetUtil.class).static_().setStaticMember("*")); file.add(Import.of(SimpleType.create(c.getDatabaseType()))); sb.append("(").append(shortName(c.getDatabaseType())).append(") "); } else { if (isCastingRequired(c, getterName)) { file.add(Import.of(SimpleType.create(c.getDatabaseType()))); sb.append("(").append(shortName(c.getDatabaseType())).append(") ");
col.isNullable() ? optional(fuSupport.entityType()) : fuSupport.entityType() final String constant = getSupport().namer().javaStaticFieldName(col.getJavaName()); identifierEnum.add(EnumConstant.of(constant).add(Value.ofText(col.getId()))); fuSupport.entityName() + "." + fuSupport.namer().javaStaticFieldName( fu.getForeignColumn().getJavaName() if (col.getTypeMapper().isPresent()) { final String typeMapper = col.getTypeMapper().get(); intrf.add(Field.of(getSupport().namer().javaStaticFieldName(col.getJavaName()), ref.getType()) .final_() .set(Value.ofInvocation(
@Override public String getDescription() { return "The referenced element " + col.getId() + ", is not enabled. Disabled elements will " + "not be generated. Thus, referencing a disabled element " + "will result in broken code.\n" + "This might be a result of the element in question not being enabled, " + "or that an ancestor of the element is not enabled. \n" + "To fix this issue, make sure the element " + targetName + " is enabled."; }
).toMap(); this.generatedFieldSupports = columnsByFields.entrySet().stream().filter(e -> e.getValue().isAutoIncrement()) .map(e -> new GeneratedFieldSupport<>( e.getKey(), e.getValue(), resultSetMapperComponent.apply(e.getValue().findDatabaseType()) )).collect(toList());
column.mutator().setId(columnName); column.mutator().setName(columnName); column.mutator().setOrdinalPosition(md.getOrdinalPosition()); column.mutator().setNullable(nullable); + "Fallback to JDBC-type %s", table.getId(), column.getId(), md.getTypeName(), md.getDataType(), column.mutator().setDatabaseType(selectedJdbcClass); || selectedJdbcClass == Character.class || selectedJdbcClass == Boolean.class) { column.mutator().setTypeMapper(TypeMapper.primitive().getClass()); final Dbms dbms = schema.getParentOrThrow(); final List<String> constants = enumConstantsOf(dbms, table, columnName); column.mutator().setEnumConstants(constants.stream().collect(joining(",")));
/** * Returns the full name used in the database for the specified * {@link Column}. This is typically constructed by combining the table and * the column name with a separator, but that might be different in * different implementations. * * @param column the column to retrieve the name of * @return the full name */ default String fullNameOf(Column column) { final Table table = column.getParentOrThrow(); final Schema schema = table.getParentOrThrow(); return fullNameOf(schema.getName(), table.getName(), column.getName()); }
@Override public Type getJavaType(Column column) { return column.findDatabaseType(); }
.filter(c -> c.getTypeMapper() .filter(tm -> !"".equals(tm)) .filter(tm -> !tm.equals(TypeMapper.identity().getClass().getName())) .add(tmsName + " = " + SqlTypeMapperHelper.class.getSimpleName() + ".create(project, " + getSupport().entityName() + "." + getSupport().namer().javaStaticFieldName(col.getJavaName()) + ", " + getSupport().entityName() + ".class);" );
column.getJavaName().replace("_", "") )) final Class<?>[] params = m.getParameterTypes(); return params.length == 1 && params[0] == column.findDatabaseType(); })
col.isNullable() ? DefaultType.optional(fuSupport.entityType()) : fuSupport.entityType()) ))) .add( col.isNullable() ? "if (" + GETTER_METHOD_PREFIX + getSupport().namer().javaTypeName(col.getJavaName()) + "()" + isPresentName + ") " + block( "return foreignManager.stream().filter(" + fuSupport.entityName() + "." + fuSupport.namer().javaStaticFieldName(fu.getForeignColumn().getJavaName()) + ".equal(" + GETTER_METHOD_PREFIX + getSupport().namer().javaTypeName(col.getJavaName()) + "()" + getterName + ")).findAny();" ) + " else " + block( "." + fuSupport.namer().javaStaticFieldName(fu.getForeignColumn().getJavaName()) + ".equal(" + GETTER_METHOD_PREFIX + getSupport().namer().javaTypeName(col.getJavaName()) + "()" + getterName + ")).findAny().orElse(null);"
@Override public Type getJavaType(Column column) { if (column.isNullable()) { return Boolean.class; } else { return boolean.class; } }
/** * Debug method used to track type mappers of a project. May be of future use if one perhaps would venture to * investigate why we get several copies of the dbms from the database */ private void printTypeMappers(String heading, Project p) { System.out.println(heading); p.dbmses().map(d -> (Dbms) d).forEach(dbms -> { dbms.schemas().map(s -> (Schema) s).forEach(schema -> { schema.tables().map(t -> (Table) t).forEach(table -> { table.columns().map(c -> (Column) c).filter(c -> c.getTypeMapper().isPresent()).forEach(column -> { String mapperName = column.getTypeMapper().get(); if (mapperName.endsWith("PrimitiveTypeMapper")) { mapperName = "Primitive"; } System.out.println(" - " + dbms.getName() + ":" + schema.getName() + "/" + table.getName() + "." + column.getName() + " mapped by " + mapperName); }); }); }); }); }
@Override public Type getJavaType(Column column) { final String type = column.getDatabaseType(); switch (type) { case "java.lang.Byte" : return byte.class; case "java.lang.Short" : return short.class; case "java.lang.Integer" : return int.class; case "java.lang.Long" : return long.class; case "java.lang.Float" : return float.class; case "java.lang.Double" : return double.class; case "java.lang.Boolean" : return boolean.class; case "java.lang.Character" : return char.class; default : throw new UnsupportedOperationException( "Type " + type + " is not a wrapper for a primitive type." ); } }
.filter(col -> col.getAsString(ORIGINAL_TYPE).filter("INTEGER"::equalsIgnoreCase).isPresent()) .noneMatch(col -> table.primaryKeyColumns().anyMatch(pkc -> DocumentDbUtil.isSame(pkc.findColumn().get(), col))) && table.columns().map(Column::getId).noneMatch("rowid"::equalsIgnoreCase)) { final Column column = table.mutator().addNewColumn(); column.mutator().setId("rowid"); column.mutator().setName("rowid"); column.mutator().setOrdinalPosition(0); column.mutator().setDatabaseType(Long.class); column.mutator().setAutoIncrement(true); column.mutator().setNullable(false); column.mutator().setTypeMapper(PrimitiveTypeMapper.class); } else { table.columns() .filter(col -> col.getAsString(ORIGINAL_TYPE).filter("INTEGER"::equalsIgnoreCase).isPresent()) .filter(col -> table.primaryKeyColumns().anyMatch(pkc -> DocumentDbUtil.isSame(pkc.findColumn().get(), col))) .forEach(col -> col.mutator().setAutoIncrement(true)); col.getData().remove(ORIGINAL_TYPE); });
/** * Returns the full name of the enum that will be generated for * the specified column. * * @param column the column that should be implemented as an enum * @param injector the injector used in the platform * @return full name for the enum */ public static String enumNameOf(Column column, Injector injector) { final TranslatorSupport<Table> support = new TranslatorSupport<>(injector, column.getParentOrThrow()); final String shortName = support.namer().javaTypeName(column.getJavaName()); return support.generatedEntityType().getTypeName() + "." + shortName; }