リクエストで受け取った条件ツリーに基づいてクエリを生成する API に取り組んでいます。ツリー形式は次のとおりです。
次のように SQL クエリで変換する必要があります。
WHERE (a>b OR c<d) AND (e>f OR g<h)
または、このように
WHERE ((a>b) OR (c<d)) AND ((e>f) OR (g<h))
事前注文トラバーサルを使用して、クエリを生成できます。ただし、括弧を追加することはできません。以下は私のinOrderトラバーサルコードです:
public void inorder(Filter n, StringBuilder builder) {
if (n != null) {
inorder(n.getLeft(), builder);
print(n, builder);
inorder(n.getRight(), builder);
}
}
private void print(Filter node, StringBuilder builder) {
if (null == node.getField() || null == node.getValue()) {
builder.append(node.getLogicOperator());
} else {
builder.append((node.getField() + " " + node.getComparisonOperator() + " " + node
.getValue()));
}
builder.append("\n");
}
ブラケットを挿入する場所を教えてください。前もって感謝します。
****アップデート****
今、同じ構造からmongoクエリを生成したいと思います。文字列を生成することはできますが、括弧を追加することはできません: 次のようなものが必要です:
$and : [
{ $or : [ { a: b}, { c: d } ] },
{ $or : [ { e: f}, { g: { $lt : h} } ] }
]
誰でも助けてもらえますか?