2016-09-25 132 views
3

我使用Java和從http://openhms.sourceforge.net/sqlbuilder/ SQLBuilder,我試圖構建SQL SELECT查詢動態地:如何用sqlbuilder建立SELECT查詢?

SelectQuery sql = new SelectQuery(); 
sql.addAllColumns().addCustomFromTable("table1"); 
sql.addCondition(BinaryCondition.like("column1", "A")); 

然而,它創建的字符串是這樣的:

SELECT * FROM table1 WHERE ('column1' LIKE 'A') 

由於錯誤引號('column1')它無法正常工作。我想它預計在.like()方法中的某個Column對象。 有沒有什麼辦法用適當的引號創建查詢?

+1

您應該使用列對象類型而不是「column1」字符串 –

+0

@SergeiPodlipaev是的,我已經解決了它。無論如何感謝 –

回答

0

我找到了解決辦法。我必須創建新類Column擴展CustomSql,並通過我的列名作爲參數:

public class Column extends CustomSql { 
    public Column(String str) { 
     super(str); 
    } 
} 

然後:

SelectQuery sql = new SelectQuery(); 
sql.addAllColumns().addCustomFromTable("table1"); 
sql.addCondition(BinaryCondition.like(new Column("column1"), "A")); 

或者沒有創建自己的類:

SelectQuery sql = new SelectQuery(); 
sql.addAllColumns().addCustomFromTable("table1"); 
sql.addCondition(BinaryCondition.like(new CustomSql("column1"), "A")); 

它創建下面的SQL查詢,它工作正常:

SELECT * FROM table1 WHERE (column1 LIKE 'A') 
0

BinaryCondition.like()將Object取爲Column Object,然後在內部使用Converter.toColumnSqlObject(Object)將其轉換爲SqlObject。在Class DbTableClass DbSchema中分別有一個名爲findColumn(String columnName)findSchema(String tableName)的方法,您可以在其中傳遞一個簡單的字符串對象。試試這個會解決你的問題:

DbTable table1= schema.findSchema("table1"); 
DbColumn column1 = table1.findColumn("column1"); 

SelectQuery sql = new SelectQuery(); 
sql.addAllColumns().addCustomFromTable(table1); 
sql.addCondition(BinaryCondition.like(column1, "A")); 
+0

我沒有使用'DbSchema'來創建我的數據庫。我只需要構建一個查詢。可能嗎? –

0

請檢查工作示例和重構自己的查詢

String query3 = 
     new SelectQuery() 
     .addCustomColumns(
      custNameCol, 
      FunctionCall.sum().addColumnParams(orderTotalCol)) 
     .addJoins(SelectQuery.JoinType.INNER, custOrderJoin) 
     .addCondition(BinaryCondition.like(custNameCol, "%bob%")) 
     .addCondition(BinaryCondition.greaterThan(
         orderDateCol, 
         JdbcEscape.date(new Date(108, 0, 1)), true)) 
     .addGroupings(custNameCol) 
     .addHaving(BinaryCondition.greaterThan(
        FunctionCall.sum().addColumnParams(orderTotalCol), 
        100, false)) 
     .validate().toString(); 
+0

這個例子假設我已經使用模式對象創建了數據庫,但是我只需要構建一個查詢。看到我的答案 –

0

看看這個庫JDSQL(它需要Java 8):

JQuery jquery = new JQuery(); 
    Collection<Map<String, Object>> result = jquery.select("tbl1::column1", "tbl2::column2") //Select column list 
               .from("Table1" , "TB1") // Specifiy main table entry, and you can add alias 
               .join("Table2::tb2") // Provide your join table, and another way to provide alias name 
               .on("tbl1.key1", "tbl2.key1") // your on statement will be based on the passed 2 values equaliy 
               .join("Table3", "tbl3", true) // Join another table with a flag to enable/disable the join (Lazy Joining) 
               .on("tbl2.key2", "tbl3.key1", (st-> {st.and("tbl3.condition = true"); return st;})) 
               .where("tbl1.condition", true, "!=") // Start your where statment and it also support enable/disable flags 
               .and("tbl2.condition = true", (st-> {st.or("tbl.cond2", 9000, "="); return st;})) // And statment that is grouping an or inside parentheses to group conditions 
               .and("tbl3.cond3=5", false) // And statment with a flag to enable/disable the condition 
               .get((String sql, Map<String, Object> parameters)-> getData(sql, parameters)); // Passing the hybrid getter. 
                   //You can also assign the getter at the jqueryobject itself by calling setGetter. 
} 

private static Collection<Map<String, Object>> getData(String sql, Map<String, Object> parameters){ 



    return null; 

} 

}