Skip to content

Commit

Permalink
Make use of the new id field in documents
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Mar 8, 2017
1 parent fc36f48 commit 5ebfbec
Show file tree
Hide file tree
Showing 30 changed files with 177 additions and 157 deletions.
Expand Up @@ -85,11 +85,11 @@ public void simpleModelTestSetUp() {
project = speedment.getOrThrow(ProjectComponent.class).getProject(); project = speedment.getOrThrow(ProjectComponent.class).getProject();
dbms = project.dbmses().findAny().get(); dbms = project.dbmses().findAny().get();
schema = dbms.schemas().findAny().get(); schema = dbms.schemas().findAny().get();
table = schema.tables().filter(t -> TABLE_NAME.equals(t.getName())).findAny().get(); table = schema.tables().filter(t -> TABLE_NAME.equals(t.getId())).findAny().get();
column = table.columns().findAny().get(); column = table.columns().findAny().get();
pkColumn = table.primaryKeyColumns().findAny().get(); pkColumn = table.primaryKeyColumns().findAny().get();


table2 = schema.tables().filter(t -> TABLE_NAME2.equals(t.getName())).findAny().get(); table2 = schema.tables().filter(t -> TABLE_NAME2.equals(t.getId())).findAny().get();
column2 = table2.columns().findAny().get(); column2 = table2.columns().findAny().get();
} }


Expand Down
Expand Up @@ -56,9 +56,9 @@ public void testPreview() {


final JavaLanguageNamer javaLanguageNamer = speedment.getOrThrow(JavaLanguageNamer.class); final JavaLanguageNamer javaLanguageNamer = speedment.getOrThrow(JavaLanguageNamer.class);


assertTrue(code.contains(javaLanguageNamer.javaVariableName(table.getName()))); assertTrue(code.contains(javaLanguageNamer.javaVariableName(table.getId())));
assertTrue(code.contains(javaLanguageNamer.javaTypeName(table.getName()))); assertTrue(code.contains(javaLanguageNamer.javaTypeName(table.getId())));
assertTrue(code.contains(javaLanguageNamer.javaVariableName(column.getName()))); assertTrue(code.contains(javaLanguageNamer.javaVariableName(column.getId())));
assertTrue(code.contains(javaLanguageNamer.javaTypeName(column.getName()))); assertTrue(code.contains(javaLanguageNamer.javaTypeName(column.getId())));
} }
} }
Expand Up @@ -46,6 +46,6 @@ protected String getClassOrInterfaceName() {
@Override @Override
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "The main interface for entities of the {@code " + return "The main interface for entities of the {@code " +
getDocument().getName() + "}-table in the database."; getDocument().getId() + "}-table in the database.";
} }
} }
Expand Up @@ -101,15 +101,15 @@ protected Interface makeCodeGenModel(File file) {
) )
.add(Method.of("getDbmsName", String.class).public_() .add(Method.of("getDbmsName", String.class).public_()
.add(OVERRIDE) .add(OVERRIDE)
.add(returnString(getSupport().dbmsOrThrow().getName())) .add(returnString(getSupport().dbmsOrThrow().getId()))
) )
.add(Method.of("getSchemaName", String.class).public_() .add(Method.of("getSchemaName", String.class).public_()
.add(OVERRIDE) .add(OVERRIDE)
.add(returnString(getSupport().schemaOrThrow().getName())) .add(returnString(getSupport().schemaOrThrow().getId()))
) )
.add(Method.of("getTableName", String.class).public_() .add(Method.of("getTableName", String.class).public_()
.add(OVERRIDE) .add(OVERRIDE)
.add(returnString(getSupport().tableOrThrow().getName())) .add(returnString(getSupport().tableOrThrow().getId()))
) )
.add(Method.of("getColumnName", String.class).public_() .add(Method.of("getColumnName", String.class).public_()
.add(OVERRIDE) .add(OVERRIDE)
Expand Down Expand Up @@ -213,7 +213,7 @@ file, getSupport().tableOrThrow(), col, getSupport().entityType(), injector
file.add(Import.of(entityType)); file.add(Import.of(entityType));


final String constant = getSupport().namer().javaStaticFieldName(col.getJavaName()); final String constant = getSupport().namer().javaStaticFieldName(col.getJavaName());
identifierEnum.add(EnumConstant.of(constant).add(Value.ofText(col.getName()))); identifierEnum.add(EnumConstant.of(constant).add(Value.ofText(col.getId())));


// Begin building the field value parameters. // Begin building the field value parameters.
final Stream.Builder<String> fieldParams = Stream.builder(); final Stream.Builder<String> fieldParams = Stream.builder();
Expand Down Expand Up @@ -283,7 +283,7 @@ protected String getJavadocRepresentText() {
return "The generated base for the {@link " return "The generated base for the {@link "
+ getSupport().entityType().getTypeName() + getSupport().entityType().getTypeName()
+ "}-interface representing entities of the {@code " + "}-interface representing entities of the {@code "
+ getDocument().getName() + "}-table in the database."; + getDocument().getId() + "}-table in the database.";
} }


@Override @Override
Expand Down
Expand Up @@ -92,15 +92,15 @@ public JavaClassTranslator<Table, Interface> getForeignEmt() {


private IllegalStateException noEnabledForeignKeyException() { private IllegalStateException noEnabledForeignKeyException() {
return new IllegalStateException( return new IllegalStateException(
"FK " + fk.getName() + " does not have an enabled ForeignKeyColumn" "FK " + fk.getId() + " does not have an enabled ForeignKeyColumn"
); );
} }


private SpeedmentException couldNotFindLocalColumnException() { private SpeedmentException couldNotFindLocalColumnException() {
return new SpeedmentException( return new SpeedmentException(
"Could not find referenced local column '" + "Could not find referenced local column '" +
fkc.getName() + "' in table '" + fkc.getId() + "' in table '" +
fkc.getParent().flatMap(ForeignKey::getParent).get().getName() + "'." fkc.getParent().flatMap(ForeignKey::getParent).get().getId() + "'."
); );
} }


Expand Down
Expand Up @@ -54,7 +54,7 @@ protected Class makeCodeGenModel(File file) {
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "The default {@link " + ApplicationBuilder.class.getName() + return "The default {@link " + ApplicationBuilder.class.getName() +
"} implementation class for the {@link " + Project.class.getName() + "} implementation class for the {@link " + Project.class.getName() +
"} named " + getSupport().projectOrThrow().getName() + "."; "} named " + getSupport().projectOrThrow().getId() + ".";
} }


private Type generatedBuilderType() { private Type generatedBuilderType() {
Expand Down
Expand Up @@ -55,7 +55,7 @@ protected Class makeCodeGenModel(File file) {
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "The default {@link " + Speedment.class.getName() + return "The default {@link " + Speedment.class.getName() +
"} implementation class for the {@link " + Project.class.getName() + "} implementation class for the {@link " + Project.class.getName() +
"} named " + getSupport().projectOrThrow().getName() + "."; "} named " + getSupport().projectOrThrow().getId() + ".";
} }


private Type applicationType() { private Type applicationType() {
Expand Down
Expand Up @@ -54,7 +54,7 @@ protected Interface makeCodeGenModel(File file) {
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "An {@link " + ApplicationBuilder.class.getName() + return "An {@link " + ApplicationBuilder.class.getName() +
"} interface for the {@link " + Project.class.getName() + "} interface for the {@link " + Project.class.getName() +
"} named " + getSupport().projectOrThrow().getName() + "."; "} named " + getSupport().projectOrThrow().getId() + ".";
} }


private Type generatedType() { private Type generatedType() {
Expand Down
Expand Up @@ -79,7 +79,7 @@ protected Class makeCodeGenModel(File file) {


final Map<String, List<Table>> nameMap = traverseOver(project, Table.class) final Map<String, List<Table>> nameMap = traverseOver(project, Table.class)
.filter(HasEnabled::test) .filter(HasEnabled::test)
.collect(Collectors.groupingBy(Table::getName)); .collect(Collectors.groupingBy(Table::getId));


final Set<String> ambigousNames = MapStream.of(nameMap) final Set<String> ambigousNames = MapStream.of(nameMap)
.filterValue(l -> l.size() > 1) .filterValue(l -> l.size() > 1)
Expand All @@ -96,7 +96,7 @@ protected Class makeCodeGenModel(File file) {
final Type managerImplType = support.managerImplType(); final Type managerImplType = support.managerImplType();
final Type sqlAdapterType = support.sqlAdapterType(); final Type sqlAdapterType = support.sqlAdapterType();


if (ambigousNames.contains(t.getName())) { if (ambigousNames.contains(t.getId())) {
managerImpls.add(managerImplType.getTypeName()); managerImpls.add(managerImplType.getTypeName());
sqlAdapters.add(sqlAdapterType.getTypeName()); sqlAdapters.add(sqlAdapterType.getTypeName());
} else { } else {
Expand Down Expand Up @@ -167,7 +167,7 @@ protected Javadoc getJavaDoc() {
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "A generated base {@link " + AbstractApplicationBuilder.class.getName() return "A generated base {@link " + AbstractApplicationBuilder.class.getName()
+ "} class for the {@link " + Project.class.getName() + "} class for the {@link " + Project.class.getName()
+ "} named " + getSupport().projectOrThrow().getName() + "."; + "} named " + getSupport().projectOrThrow().getId() + ".";
} }


@Override @Override
Expand Down
Expand Up @@ -61,7 +61,7 @@ protected Class makeCodeGenModel(File file) {
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "The generated {@link " + AbstractApplicationBuilder.class.getName() + return "The generated {@link " + AbstractApplicationBuilder.class.getName() +
"} implementation class for the {@link " + Project.class.getName() + "} implementation class for the {@link " + Project.class.getName() +
"} named " + getSupport().projectOrThrow().getName() + "."; "} named " + getSupport().projectOrThrow().getId() + ".";
} }


private Type generatedType() { private Type generatedType() {
Expand Down
Expand Up @@ -56,6 +56,6 @@ protected Interface makeCodeGenModel(File file) {
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "The generated {@link " + AbstractApplicationBuilder.class.getName() + return "The generated {@link " + AbstractApplicationBuilder.class.getName() +
"} application interface for the {@link " + Project.class.getName() + "} application interface for the {@link " + Project.class.getName() +
"} named " + getSupport().projectOrThrow().getName() + "."; "} named " + getSupport().projectOrThrow().getId() + ".";
} }
} }
Expand Up @@ -147,7 +147,7 @@ protected Javadoc getJavaDoc() {
protected String getJavadocRepresentText() { protected String getJavadocRepresentText() {
return "A {@link " + ApplicationMetadata.class.getName() return "A {@link " + ApplicationMetadata.class.getName()
+ "} class for the {@link " + Project.class.getName() + "} class for the {@link " + Project.class.getName()
+ "} named " + getSupport().projectOrThrow().getName() + "." + "} named " + getSupport().projectOrThrow().getId() + "."
+ " This class contains the meta data present at code generation time."; + " This class contains the meta data present at code generation time.";
} }


Expand Down
Expand Up @@ -70,7 +70,7 @@ protected Class makeCodeGenModel(File file) {
.add(Field.of("tableIdentifier", SimpleParameterizedType.create(TableIdentifier.class, getSupport().entityType())).private_().final_()) .add(Field.of("tableIdentifier", SimpleParameterizedType.create(TableIdentifier.class, getSupport().entityType())).private_().final_())
.add(Constructor.of().protected_() .add(Constructor.of().protected_()
.add("this.tableIdentifier = " + TableIdentifier.class.getSimpleName() + ".of(" .add("this.tableIdentifier = " + TableIdentifier.class.getSimpleName() + ".of("
+ Stream.of(getSupport().dbmsOrThrow().getName(), getSupport().schemaOrThrow().getName(), getSupport().tableOrThrow().getName()) + Stream.of(getSupport().dbmsOrThrow().getId(), getSupport().schemaOrThrow().getId(), getSupport().tableOrThrow().getId())
.map(s -> "\"" + s + "\"").collect(joining(", ")) .map(s -> "\"" + s + "\"").collect(joining(", "))
+ ");") + ");")
) )
Expand Down Expand Up @@ -108,6 +108,6 @@ public Type getImplType() {
} }


private static boolean isPrimaryKey(Column column) { private static boolean isPrimaryKey(Column column) {
return column.getParentOrThrow().findPrimaryKeyColumn(column.getName()).isPresent(); return column.getParentOrThrow().findPrimaryKeyColumn(column.getId()).isPresent();
} }
} }
Expand Up @@ -108,9 +108,9 @@ protected Class makeCodeGenModel(File file) {
.add("this.tableIdentifier = " .add("this.tableIdentifier = "
+ TableIdentifier.class.getSimpleName() + ".of(" + TableIdentifier.class.getSimpleName() + ".of("
+ Stream.of( + Stream.of(
getSupport().dbmsOrThrow().getName(), getSupport().dbmsOrThrow().getId(),
getSupport().schemaOrThrow().getName(), getSupport().schemaOrThrow().getId(),
getSupport().tableOrThrow().getName() getSupport().tableOrThrow().getId()
).map(s -> "\"" + s + "\"").collect(joining(", ")) ).map(s -> "\"" + s + "\"").collect(joining(", "))
+ ");") + ");")
) )
Expand Down
Expand Up @@ -55,7 +55,7 @@ protected Type typeOfPK() {


final Column firstColumn = columnsFromPks() final Column firstColumn = columnsFromPks()
.findFirst().orElseThrow(() -> new SpeedmentTranslatorException( .findFirst().orElseThrow(() -> new SpeedmentTranslatorException(
"Table '" + table().get().getName() + "Table '" + table().get().getId() +
"' did not contain any primary key columns." "' did not contain any primary key columns."
)); ));


Expand All @@ -82,7 +82,7 @@ private Stream<Column> columnsFromPks() {
return pk.findColumn().get(); return pk.findColumn().get();
} catch (final NoSuchElementException ex) { } catch (final NoSuchElementException ex) {
throw new SpeedmentTranslatorException( throw new SpeedmentTranslatorException(
"Could not find any column belonging to primary key '" + pk.getName() + "'.", ex "Could not find any column belonging to primary key '" + pk.getId() + "'.", ex
); );
} }
}); });
Expand Down
Expand Up @@ -38,6 +38,7 @@
import com.speedment.runtime.config.*; import com.speedment.runtime.config.*;
import com.speedment.runtime.config.internal.*; import com.speedment.runtime.config.internal.*;
import com.speedment.runtime.config.trait.HasEnabled; import com.speedment.runtime.config.trait.HasEnabled;
import com.speedment.runtime.config.trait.HasId;
import com.speedment.runtime.config.trait.HasMainInterface; import com.speedment.runtime.config.trait.HasMainInterface;
import com.speedment.runtime.config.trait.HasName; import com.speedment.runtime.config.trait.HasName;
import com.speedment.runtime.core.annotation.GeneratedCode; import com.speedment.runtime.core.annotation.GeneratedCode;
Expand All @@ -58,7 +59,7 @@
* *
* @author Per Minborg * @author Per Minborg
*/ */
public abstract class AbstractJavaClassTranslator<DOC extends Document & HasName & HasEnabled & HasMainInterface, T extends ClassOrInterface<T>> public abstract class AbstractJavaClassTranslator<DOC extends Document & HasId & HasName & HasEnabled & HasMainInterface, T extends ClassOrInterface<T>>
implements JavaClassTranslator<DOC, T> { implements JavaClassTranslator<DOC, T> {


public static final String public static final String
Expand Down Expand Up @@ -358,7 +359,7 @@ public T build() {
.flatMap(t -> t.foreignKeys()) .flatMap(t -> t.foreignKeys())
.filter(HasEnabled::test) .filter(HasEnabled::test)
.filter(fk -> fk.foreignKeyColumns() .filter(fk -> fk.foreignKeyColumns()
.filter(fkc -> fkc.getForeignTableName().equals(getDocument().getName())) .filter(fkc -> fkc.getForeignTableName().equals(getDocument().getId()))
.filter(HasEnabled::test) .filter(HasEnabled::test)
.filter(fkc -> fkc.findForeignColumn().map(HasEnabled::test).orElse(false)) .filter(fkc -> fkc.findForeignColumn().map(HasEnabled::test).orElse(false))
.findFirst() .findFirst()
Expand Down
Expand Up @@ -63,8 +63,8 @@ public static String enumNameOf(Column column, Injector injector) {
public static List<String> enumConstantsOf(Column column) { public static List<String> enumConstantsOf(Column column) {
return Stream.of(column.getEnumConstants() return Stream.of(column.getEnumConstants()
.orElseThrow(() -> new RuntimeException( .orElseThrow(() -> new RuntimeException(
"Column '" + column.getName() + "Column '" + column.getId() +
"' in table '" + column.getParentOrThrow().getName() + "' in table '" + column.getParentOrThrow().getId() +
"' was marked as an enum but no enum constants was specified." "' was marked as an enum but no enum constants was specified."
)) ))
.split(",") .split(",")
Expand Down
Expand Up @@ -104,7 +104,7 @@ default Optional<? extends Table> findForeignTable() {
.findFirst(); .findFirst();


return schema.flatMap(s -> s.tables() return schema.flatMap(s -> s.tables()
.filter(tab -> tab.getName().equals(getForeignTableName())) .filter(tab -> tab.getId().equals(getForeignTableName()))
.findAny() .findAny()
); );
} }
Expand All @@ -118,7 +118,7 @@ default Optional<? extends Table> findForeignTable() {
default Optional<? extends Column> findForeignColumn() { default Optional<? extends Column> findForeignColumn() {
return findForeignTable() return findForeignTable()
.flatMap(table -> table.columns() .flatMap(table -> table.columns()
.filter(col -> col.getName().equals(getForeignColumnName())) .filter(col -> col.getId().equals(getForeignColumnName()))
.findAny() .findAny()
); );
} }
Expand Down
Expand Up @@ -136,20 +136,20 @@ default Table findTableByName(String fullName) {
); );
} }


final String dbmsName = parts[0], final String dbmsId = parts[0],
schemaName = parts[1], schemaId = parts[1],
tableName = parts[2]; tableId = parts[2];


return dbmses() return dbmses()
.filter(d -> dbmsName.equals(d.getName())) .filter(d -> dbmsId.equals(d.getId()))
.findAny() .findAny()
.orElseThrow(() -> new IllegalArgumentException( .orElseThrow(() -> new IllegalArgumentException(
"Could not find dbms: '" + dbmsName + "'.")) "Could not find dbms: '" + dbmsId + "'."))
.schemas().filter(s -> schemaName.equals(s.getName())).findAny() .schemas().filter(s -> schemaId.equals(s.getId())).findAny()
.orElseThrow(() -> new IllegalArgumentException( .orElseThrow(() -> new IllegalArgumentException(
"Could not find schema: '" + schemaName + "'.")) "Could not find schema: '" + schemaId + "'."))
.tables().filter(t -> tableName.equals(t.getName())).findAny() .tables().filter(t -> tableId.equals(t.getId())).findAny()
.orElseThrow(() -> new IllegalArgumentException( .orElseThrow(() -> new IllegalArgumentException(
"Could not find table: '" + tableName + "'.")); "Could not find table: '" + tableId + "'."));
} }
} }
Expand Up @@ -78,20 +78,20 @@ public interface Table extends
*/ */
Stream<? extends PrimaryKeyColumn> primaryKeyColumns(); Stream<? extends PrimaryKeyColumn> primaryKeyColumns();


default Optional<? extends Column> findColumn(String name) { default Optional<? extends Column> findColumn(String id) {
return columns().filter(child -> child.getName().equals(name)).findAny(); return columns().filter(child -> child.getId().equals(id)).findAny();
} }


default Optional<? extends Index> findIndex(String name) { default Optional<? extends Index> findIndex(String id) {
return indexes().filter(child -> child.getName().equals(name)).findAny(); return indexes().filter(child -> child.getId().equals(id)).findAny();
} }


default Optional<? extends ForeignKey> findForeignKey(String name) { default Optional<? extends ForeignKey> findForeignKey(String id) {
return foreignKeys().filter(child -> child.getName().equals(name)).findAny(); return foreignKeys().filter(child -> child.getId().equals(id)).findAny();
} }


default Optional<? extends PrimaryKeyColumn> findPrimaryKeyColumn(String name) { default Optional<? extends PrimaryKeyColumn> findPrimaryKeyColumn(String id) {
return primaryKeyColumns().filter(child -> child.getName().equals(name)).findAny(); return primaryKeyColumns().filter(child -> child.getId().equals(id)).findAny();
} }


@Override @Override
Expand Down
Expand Up @@ -34,7 +34,7 @@
* @author Emil Forslund * @author Emil Forslund
* @since 2.3.0 * @since 2.3.0
*/ */
public interface HasColumn extends Document, HasName { public interface HasColumn extends Document, HasId, HasName {


/** /**
* Locates and returns the column referenced by the {@link #getName()} * Locates and returns the column referenced by the {@link #getName()}
Expand All @@ -49,7 +49,7 @@ default Optional<? extends Column> findColumn() {
.findFirst() .findFirst()
.flatMap(table -> table .flatMap(table -> table
.columns() .columns()
.filter(col -> col.getName().equals(getName())) .filter(col -> col.getId().equals(getId()))
.findAny() .findAny()
); );
} }
Expand Down

0 comments on commit 5ebfbec

Please sign in to comment.