我正在使用来自http://openhms.sourceforge.net/sqlbuilder/的Java和SQLBuilder,并且正在尝试动态地构建SQL SELECT查询:
IEnumerator
但是,它会创建如下字符串:
SelectQuery sql = new SelectQuery();
sql.addAllColumns().addCustomFromTable("table1");
sql.addCondition(BinaryCondition.like("column1", "A"));
由于引号错误(SELECT * FROM table1 WHERE ('column1' LIKE 'A')
),它无法正常工作。我想它需要'column1'
方法中的一些Column对象。
有没有办法用适当的引号创建查询?
答案 0 :(得分:1)
我找到了解决方案。我必须创建扩展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')
答案 1 :(得分:0)
BinaryCondition.like()接受一个Column Object
的对象,然后在内部使用SqlObject
将其转换为Converter.toColumnSqlObject(Object)
。在findColumn(String columnName)
和findSchema(String tableName)
中分别有一个名为Class DbTable
和Class DbSchema
的方法,您可以在其中传递一个简单的String对象。试试这个可以解决你的问题:
DbTable table1= schema.findSchema("table1");
DbColumn column1 = table1.findColumn("column1");
SelectQuery sql = new SelectQuery();
sql.addAllColumns().addCustomFromTable(table1);
sql.addCondition(BinaryCondition.like(column1, "A"));
答案 2 :(得分: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();
答案 3 :(得分: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;
}
}