0
我正在開發一個API,其中,我根據請求中收到的條件樹生成一個查詢。 以下是樹格式:Java Tree遍歷
應該得到翻譯在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");
}
任何人都可以請指導我在何處插入括號? 在此先感謝。
**** ****更新
現在,我想生成相同的結構蒙戈查詢。我能夠生成字符串,但無法附加括號:我想要這樣的:
$and : [
{ $or : [ { a: b}, { c: d } ] },
{ $or : [ { e: f}, { g: { $lt : h} } ] }
]
任何人都可以請幫忙嗎?
點上!在考慮情景之後,我做了類似的事情。如果節點是根節點,我還插入了不附加括號的條件。乾杯! – user3968762 2014-09-26 12:59:03
你好,你能提供一些用於括號生成字符串的指針嗎? – user3968762 2014-09-26 14:17:04