高度なカスタマイズ > サービスおよびインフラストラクチャのカスタマイズ > アドバンス照会機能 > SearchCondition > 複合照会
  
複合照会
複合照会は、集合演算子を使用し、複数のコンポーネント照会を単一の SQL ステートメントに結合する SQL ステートメントです。集合演算子には、UNION、UNION ALL、INTERSECT、および MINUS があります。複合照会は、集合演算子を指定し、コンポーネント照会を追加して作成します。コンポーネント照会は StatementSpec オブジェクトなので、複合照会のネストもサポートされます。
以下の例は、特定の PartMaster 番号およびその代替の番号すべてを返す複合照会です。番号のみが選択され、完全なオブジェクトは選択されません。すべてのサブクラスを考慮する場合、複合照会ステートメントにはすべてのサブクラステーブルが含まれるので、このような選択が必要です。これらのサブクラステーブルには、ほかのコンポーネントステートメントとは互換性のないステートメントごとに選択リストを作成する追加のコラムが含まれる場合があります。SQL では、複合ステートメントの各コンポーネント照会が選択リストで同一番号を持ち、対応するタイプであることが必要です。以下に、この照会の SQL を示します。
SELECT A0.number
FROM WTPartMaster A0
WHERE (A0.name = ‘ENGINE')
UNION
SELECT A2.number
FROM WTPartMaster A0,WTPartAlternateLink A1,WTPartMaster A2
WHERE (A0.name = ‘ENGINE') AND
(A0.idA2A2 = A1.idA3A5) AND (A2.idA2A2 = A1.idA3B5)
以下のコードで、照会の仕様を作成します。最初に作成する選択は、ENGINE という名前の PartMasters 用です。
QuerySpec partSelect = new QuerySpec();
int partIndex = partSelect.appendClassList(wt.part.WTPartMaster.class, false);
partSelect.appendWhere(new SearchCondition(wt.part.WTPartMaster.class,
WTPartMaster.NAME, SearchCondition.EQUAL, "ENGINE"), new int[]
{ partIndex });
次に作成する選択は、返される PartMaster の代替用です。代替は、WTPartAlternateLink クラスを使用して表します。これは、PartMasters 間の多対多の関係です。オリジナルの部品とその代替のこの関係では、結合を指定する必要があります。
QuerySpec altSelect = new QuerySpec();
partIndex = altSelect.appendClassList(wt.part.WTPartMaster.class, false);
int altIndex = altSelect.appendClassList(W wt.part.WTPartAlternateLink.class,
false);
int altPartIndex = altSelect.appendClassList(wt.part.WTPartMaster.class,
false);
altSelect.appendSelect(new ClassAttribute(
wt.part.WTPartMaster.class, wt.part.WTPartMaster.NUMBER),
new int[] { altPartIndex }, false);
altSelect.appendWhere(new
SearchCondition(wt.part.WTPartMaster.class,
WTPartMaster.NAME, SearchCondition.EQUAL, "ENGINE"), new int[]
{ partIndex });
altSelect.appendJoin(altIndex, wt.part.WTPartAlternateLink.ALTERNATES_ROLE,
partIndex);
altSelect.appendJoin(altIndex, wt.part.WTPartAlternateLink.ALTERNATE_FOR_ROLE,
altPartIndex);
最後に、前の 2 つの照会および UNION 集合演算子を使用して複合ステートメントを作成します。
CompoundQuerySpec compound = new CompoundQuerySpec();
compound.setSetOperator(SetOperator.UNION);
compound.addComponent(partSelect);
compound.addComponent(altSelect);