TableReferenceInfoFactory { public static <T extends Table<T>> TableReferenceInfo<T> create( long classNameId, Column<T, Long> primaryKeyColumn, TableReferenceDefinition<T> tableReferenceDefinition) { ParentTableReferenceInfoBuilderImpl<T> parentTableReferenceInfoBuilderImpl = new ParentTableReferenceInfoBuilderImpl<>( tableReferenceDefinition, primaryKeyColumn); tableReferenceDefinition.defineParentTableReferences( parentTableReferenceInfoBuilderImpl); ChildTableReferenceInfoBuilderImpl<T> childTableReferenceInfoBuilderImpl = new ChildTableReferenceInfoBuilderImpl<>( tableReferenceDefinition, primaryKeyColumn); tableReferenceDefinition.defineChildTableReferences( childTableReferenceInfoBuilderImpl); TableReferenceAppenderRegistry.appendTableReferences( tableReferenceDefinition, primaryKeyColumn, parentTableReferenceInfoBuilderImpl, childTableReferenceInfoBuilderImpl); return new TableReferenceInfo<>( childTableReferenceInfoBuilderImpl.getTableJoinHoldersMap(), classNameId, parentTableReferenceInfoBuilderImpl.getTableJoinHoldersMap(), tableReferenceDefinition); }  static TableReferenceInfo<T> create(
		long classNameId, Column<T, Long> primaryKeyColumn,
		TableReferenceDefinition<T> tableReferenceDefinition);  }
@Test public void testChildInnerJoin() { Function<FromStep, JoinStep> childJoinFunction = fromStep -> fromStep.from( ReferenceExampleTable.INSTANCE ).innerJoinON( MainExampleTable.INSTANCE, MainExampleTable.INSTANCE.mainExampleId.eq( ReferenceExampleTable.INSTANCE.mainExampleId) ); TableReferenceDefinition<MainExampleTable> tableReferenceDefinition = new TestTableReferenceDefinition<MainExampleTable>( MainExampleTable.INSTANCE) { @Override public void defineChildTableReferences( ChildTableReferenceInfoBuilder<MainExampleTable> childTableReferenceInfoBuilder) { childTableReferenceInfoBuilder.referenceInnerJoin( childJoinFunction); } @Override public void defineParentTableReferences( ParentTableReferenceInfoBuilder<MainExampleTable> parentTableReferenceInfoBuilder) { } }; TableReferenceInfo<MainExampleTable> tableReferenceInfo = TableReferenceInfoFactory.create( MainExampleTable.CLASS_NAME_ID, MainExampleTable.INSTANCE.mainExampleId, tableReferenceDefinition); Assert.assertNotNull(tableReferenceInfo); Assert.assertSame( tableReferenceDefinition, tableReferenceInfo.getTableReferenceDefinition()); Map<Table<?>, List<TableJoinHolder>> childTableJoinHoldersMap = tableReferenceInfo.getChildTableJoinHoldersMap(); Assert.assertEquals( childTableJoinHoldersMap.toString(), 1, childTableJoinHoldersMap.size()); List<TableJoinHolder> childJoinHolders = childTableJoinHoldersMap.get( ReferenceExampleTable.INSTANCE); Assert.assertEquals( childJoinHolders.toString(), 1, childJoinHolders.size()); TableJoinHolder childJoinHolder = childJoinHolders.get(0); Assert.assertSame(childJoinFunction, childJoinHolder.getJoinFunction()); Map<Table<?>, List<TableJoinHolder>> parentTableJoinHoldersMap = tableReferenceInfo.getParentTableJoinHoldersMap(); Assert.assertTrue( parentTableJoinHoldersMap.toString(), parentTableJoinHoldersMap.isEmpty()); Assert.assertEquals( MainExampleTable.CLASS_NAME_ID, tableReferenceInfo.getClassNameId()); }
@Test public void testMissingRequirements() { TableReferenceInfo<ReferenceExampleTable> tableReferenceInfo = TableReferenceInfoFactory.create( ReferenceExampleTable.CLASS_NAME_ID, ReferenceExampleTable.INSTANCE.mainExampleId, new MissingRequirementsTableReferenceDefinition()); Map<Table<?>, List<TableJoinHolder>> parentTableJoinHoldersMap = tableReferenceInfo.getParentTableJoinHoldersMap(); Assert.assertEquals( parentTableJoinHoldersMap.toString(), 3, parentTableJoinHoldersMap.size()); _assertMissingRequirementsSQL( parentTableJoinHoldersMap.get(MainExampleTable.INSTANCE), DSLQueryFactoryUtil.select( ReferenceExampleTable.INSTANCE.mainExampleId, new Scalar<>(MainExampleTable.INSTANCE.getTableName()) ).from( ReferenceExampleTable.INSTANCE ).leftJoinOn( MainExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.referenceExampleId.eq( MainExampleTable.INSTANCE.classPK) ).leftJoinOn( ClassNameTable.INSTANCE, ClassNameTable.INSTANCE.classNameId.eq( MainExampleTable.INSTANCE.classNameId) ).where( () -> { Predicate predicate = MainExampleTable.INSTANCE.mainExampleId.isNull(); Scalar<String> key = new Scalar<>("key"); return predicate.and( key.eq(MainExampleTable.INSTANCE.name) ).and( MainExampleTable.INSTANCE.flag.eq(0) ).and( ClassNameTable.INSTANCE.value.eq( TableReferenceInfoFactoryTest.class.getName()) ).and( ReferenceExampleTable.INSTANCE.referenceExampleId. isNotNull() ).and( ReferenceExampleTable.INSTANCE.referenceExampleId.neq( 0L) ); } )); ReferenceExampleTable aliasReferenceExampleTable = ReferenceExampleTable.INSTANCE.as("aliasParentTable"); _assertMissingRequirementsSQL( parentTableJoinHoldersMap.get(ReferenceExampleTable.INSTANCE), DSLQueryFactoryUtil.select( ReferenceExampleTable.INSTANCE.mainExampleId, new Scalar<>(ReferenceExampleTable.INSTANCE.getTableName()) ).from( ReferenceExampleTable.INSTANCE ).leftJoinOn( aliasReferenceExampleTable, ReferenceExampleTable.INSTANCE.parentReferenceExampleId.eq( aliasReferenceExampleTable.referenceExampleId) ).where( () -> { Predicate predicate = aliasReferenceExampleTable.referenceExampleId.isNull(); return predicate.and( ReferenceExampleTable.INSTANCE.parentReferenceExampleId. isNotNull() ).and( ReferenceExampleTable.INSTANCE.parentReferenceExampleId. neq(0L) ); } )); _assertMissingRequirementsSQL( parentTableJoinHoldersMap.get(StringIntExampleTable.INSTANCE), DSLQueryFactoryUtil.select( ReferenceExampleTable.INSTANCE.mainExampleId, new Scalar<>(StringIntExampleTable.INSTANCE.getTableName()) ).from( ReferenceExampleTable.INSTANCE ).leftJoinOn( StringIntExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.stringKey.eq( StringIntExampleTable.INSTANCE.stringKey ).and( ReferenceExampleTable.INSTANCE.integerKey.eq( StringIntExampleTable.INSTANCE.integerKey) ) ).where( () -> { Predicate predicate = StringIntExampleTable.INSTANCE.id.isNull(); return predicate.and( ReferenceExampleTable.INSTANCE.stringKey.isNotNull() ).and( ReferenceExampleTable.INSTANCE.stringKey.neq("") ).and( ReferenceExampleTable.INSTANCE.integerKey.isNotNull() ); } )); }
@Test public void testParentInnerJoin() { Function<FromStep, JoinStep> parentJoinFunction = fromStep -> fromStep.from( MainExampleTable.INSTANCE ).innerJoinON( ReferenceExampleTable.INSTANCE, MainExampleTable.INSTANCE.mainExampleId.eq( ReferenceExampleTable.INSTANCE.mainExampleId) ); ReferenceExampleTable aliasReferenceExampleTable = ReferenceExampleTable.INSTANCE.as("aliasReferenceExampleTable"); Function<FromStep, JoinStep> selfJoinFunction1 = fromStep -> fromStep.from( aliasReferenceExampleTable ).innerJoinON( ReferenceExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.referenceExampleId.eq( aliasReferenceExampleTable.parentReferenceExampleId) ); Function<FromStep, JoinStep> selfJoinFunction2 = fromStep -> fromStep.from( aliasReferenceExampleTable ).innerJoinON( ReferenceExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.parentReferenceExampleId.eq( aliasReferenceExampleTable.referenceExampleId) ); TableReferenceDefinition<ReferenceExampleTable> tableReferenceDefinition = new TestTableReferenceDefinition<ReferenceExampleTable>( ReferenceExampleTable.INSTANCE) { @Override public void defineChildTableReferences( ChildTableReferenceInfoBuilder<ReferenceExampleTable> childTableReferenceInfoBuilder) { childTableReferenceInfoBuilder.referenceInnerJoin( selfJoinFunction1); } @Override public void defineParentTableReferences( ParentTableReferenceInfoBuilder<ReferenceExampleTable> parentTableReferenceInfoBuilder) { parentTableReferenceInfoBuilder.referenceInnerJoin( parentJoinFunction ).referenceInnerJoin( selfJoinFunction2 ); } }; TableReferenceInfo<ReferenceExampleTable> tableReferenceInfo = TableReferenceInfoFactory.create( ReferenceExampleTable.CLASS_NAME_ID, ReferenceExampleTable.INSTANCE.referenceExampleId, tableReferenceDefinition); Assert.assertNotNull(tableReferenceInfo); Assert.assertSame( tableReferenceDefinition, tableReferenceInfo.getTableReferenceDefinition()); Map<Table<?>, List<TableJoinHolder>> childTableJoinHoldersMap = tableReferenceInfo.getChildTableJoinHoldersMap(); Assert.assertEquals( childTableJoinHoldersMap.toString(), 1, childTableJoinHoldersMap.size()); List<TableJoinHolder> childTableJoinHolders = childTableJoinHoldersMap.get(ReferenceExampleTable.INSTANCE); Assert.assertEquals( childTableJoinHolders.toString(), 1, childTableJoinHolders.size()); TableJoinHolder childJoinHolder = childTableJoinHolders.get(0); Assert.assertSame(selfJoinFunction1, childJoinHolder.getJoinFunction()); Assert.assertSame( aliasReferenceExampleTable.referenceExampleId, childJoinHolder.getParentPKColumn()); Assert.assertSame( ReferenceExampleTable.INSTANCE.referenceExampleId, childJoinHolder.getChildPKColumn()); Map<Table<?>, List<TableJoinHolder>> parentTableJoinHoldersMap = tableReferenceInfo.getParentTableJoinHoldersMap(); Assert.assertEquals( parentTableJoinHoldersMap.toString(), 2, parentTableJoinHoldersMap.size()); List<TableJoinHolder> parentTableJoinHolders = parentTableJoinHoldersMap.get(MainExampleTable.INSTANCE); Assert.assertEquals( parentTableJoinHolders.toString(), 1, parentTableJoinHolders.size()); TableJoinHolder parentJoinHolder = parentTableJoinHolders.get(0); Assert.assertSame( parentJoinFunction, parentJoinHolder.getJoinFunction()); Assert.assertSame( MainExampleTable.INSTANCE.mainExampleId, parentJoinHolder.getParentPKColumn()); Assert.assertSame( ReferenceExampleTable.INSTANCE.referenceExampleId, parentJoinHolder.getChildPKColumn()); parentTableJoinHolders = parentTableJoinHoldersMap.get( ReferenceExampleTable.INSTANCE); Assert.assertEquals( parentTableJoinHolders.toString(), 1, parentTableJoinHolders.size()); childJoinHolder = parentTableJoinHolders.get(0); Assert.assertSame(selfJoinFunction2, childJoinHolder.getJoinFunction()); Assert.assertSame( aliasReferenceExampleTable.referenceExampleId, childJoinHolder.getParentPKColumn()); Assert.assertSame( ReferenceExampleTable.INSTANCE.referenceExampleId, childJoinHolder.getChildPKColumn()); Assert.assertEquals( ReferenceExampleTable.CLASS_NAME_ID, tableReferenceInfo.getClassNameId()); }
@Test public void testTableReferenceDefinitionInnerJoinValidation() { TableReferenceDefinition<MainExampleTable> tableReferenceDefinition = new TestTableReferenceDefinition<MainExampleTable>( MainExampleTable.INSTANCE) { @Override public void defineChildTableReferences( ChildTableReferenceInfoBuilder<MainExampleTable> childTableReferenceInfoBuilder) { try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> DSLQueryFactoryUtil.select( ).from( MainExampleTable.INSTANCE )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( "Missing join in \"select * from MainExample\"", illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> DSLQueryFactoryUtil.select( ).from( MainExampleTable.INSTANCE ).innerJoinON( MainExampleTable.INSTANCE, MainExampleTable.INSTANCE.mainExampleId.eq( MainExampleTable.INSTANCE.mainExampleId) )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "Join function must use provided from step ", "for join step \"select * from MainExample ", "inner join MainExample on ", "MainExample.mainExampleId = ", "MainExample.mainExampleId\""), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> fromStep.from( ReferenceExampleTable.INSTANCE ).innerJoinON( ReferenceExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.mainExampleId.eq( ReferenceExampleTable.INSTANCE. mainExampleId) )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "Required table \"MainExample\" is unused in ", "join step \"... from ReferenceExample inner ", "join ReferenceExample on ", "ReferenceExample.mainExampleId = ", "ReferenceExample.mainExampleId\""), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> fromStep.from( MainExampleTable.INSTANCE ).innerJoinON( MainExampleTable.INSTANCE, MainExampleTable.INSTANCE.mainExampleId.eq( MainExampleTable.INSTANCE.mainExampleId) )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "Invalid join for join step \"... from ", "MainExample inner join MainExample on ", "MainExample.mainExampleId = ", "MainExample.mainExampleId\", ensure table ", "alias is used for self joins"), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> fromStep.from( MainExampleTable.INSTANCE ).innerJoinON( ReferenceExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.mainExampleId.eq( MainExampleTable.INSTANCE.mainExampleId) )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "First join must be on table \"MainExample\" ", "for join step \"... from MainExample inner ", "join ReferenceExample on ", "ReferenceExample.mainExampleId = ", "MainExample.mainExampleId\""), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> fromStep.from( ReferenceExampleTable.INSTANCE ).leftJoinOn( MainExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.mainExampleId.eq( MainExampleTable.INSTANCE.mainExampleId) )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "Invalid join type \"left\" for join step ", "\"... from ReferenceExample left join ", "MainExample on ", "ReferenceExample.mainExampleId = ", "MainExample.mainExampleId\""), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> fromStep.from( ReferenceExampleTable.INSTANCE ).innerJoinON( MainExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.mainExampleId. neq( MainExampleTable.INSTANCE.mainExampleId ).and( MainExampleTable.INSTANCE.mainExampleId. gte( ReferenceExampleTable.INSTANCE. mainExampleId) ) )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "Invalid predicate operand \"!=\" for join ", "step \"... from ReferenceExample inner join ", "MainExample on ", "ReferenceExample.mainExampleId != ", "MainExample.mainExampleId and ", "MainExample.mainExampleId >= ", "ReferenceExample.mainExampleId\""), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> fromStep.from( MainExampleTable.INSTANCE.as("aliasMainExample") ).innerJoinON( MainExampleTable.INSTANCE, ReferenceExampleTable.INSTANCE.mainExampleId.eq( MainExampleTable.INSTANCE.mainExampleId) )); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "Predicate column tables [MainExample, ", "ReferenceExample] do not match join tables ", "[MainExample, MainExample aliasMainExample] ", "for join step \"... from MainExample ", "aliasMainExample inner join MainExample on ", "ReferenceExample.mainExampleId = ", "MainExample.mainExampleId\""), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> fromStep.from( InvalidTable.INSTANCE ).innerJoinON( MainExampleTable.INSTANCE, MainExampleTable.INSTANCE.mainExampleId.eq( InvalidTable.INSTANCE.mainExampleId) )); } catch (IllegalArgumentException illegalArgumentException) { Assert.assertEquals( StringBundler.concat( "No long type primary key column found for ", "table \"InvalidTable\" for join step \"... ", "from InvalidTable inner join MainExample on ", "MainExample.mainExampleId = ", "InvalidTable.mainExampleId\""), illegalArgumentException.getMessage()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> { fromStep.as("test"); return null; }); Assert.fail(); } catch (Exception exception) { Assert.assertSame( UnsupportedOperationException.class, exception.getClass()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> { fromStep.union(null); return null; }); Assert.fail(); } catch (Exception exception) { Assert.assertSame( UnsupportedOperationException.class, exception.getClass()); } try { childTableReferenceInfoBuilder.referenceInnerJoin( fromStep -> { fromStep.unionAll(null); return null; }); Assert.fail(); } catch (Exception exception) { Assert.assertSame( UnsupportedOperationException.class, exception.getClass()); } } @Override public void defineParentTableReferences( ParentTableReferenceInfoBuilder<MainExampleTable> parentTableReferenceInfoBuilder) { } }; TableReferenceInfo<MainExampleTable> tableReferenceInfo = TableReferenceInfoFactory.create( MainExampleTable.CLASS_NAME_ID, MainExampleTable.INSTANCE.mainExampleId, tableReferenceDefinition); Assert.assertNotNull(tableReferenceInfo); Assert.assertSame( tableReferenceDefinition, tableReferenceInfo.getTableReferenceDefinition()); Map<Table<?>, List<TableJoinHolder>> childTableJoinHoldersMap = tableReferenceInfo.getChildTableJoinHoldersMap(); Assert.assertTrue( childTableJoinHoldersMap.toString(), childTableJoinHoldersMap.isEmpty()); Map<Table<?>, List<TableJoinHolder>> parentTableJoinHoldersMap = tableReferenceInfo.getParentTableJoinHoldersMap(); Assert.assertTrue( parentTableJoinHoldersMap.toString(), parentTableJoinHoldersMap.isEmpty()); Assert.assertEquals( MainExampleTable.CLASS_NAME_ID, tableReferenceInfo.getClassNameId()); }