如何使用 sqlbuilder 构建 SELECT 查询?
How to build SELECT query with sqlbuilder?
我正在使用来自 http://openhms.sourceforge.net/sqlbuilder/ 的 Java 和 SQLBuilder,并且正在尝试动态构建 SQL 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 对象。
有什么方法可以用正确的引号创建查询吗?
BinaryCondition.like() 采用 Column Object
对象,然后在内部使用 Converter.toColumnSqlObject(Object)
将其转换为 SqlObject
。 Class DbTable
和 Class 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"));
我找到了解决办法。我必须创建扩展 CustomSql
的新 class Column
并将我的列名作为参数传递:
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"));
或者不创建自己的 class:
SelectQuery sql = new SelectQuery();
sql.addAllColumns().addCustomFromTable("table1");
sql.addCondition(BinaryCondition.like(new CustomSql("column1"), "A"));
它创建以下 SQL 查询,工作正常:
SELECT * FROM table1 WHERE (column1 LIKE 'A')
请检查工作示例并重构您自己的查询
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();
看看这个库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;
}
}
我正在使用来自 http://openhms.sourceforge.net/sqlbuilder/ 的 Java 和 SQLBuilder,并且正在尝试动态构建 SQL 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 对象。
有什么方法可以用正确的引号创建查询吗?
BinaryCondition.like() 采用 Column Object
对象,然后在内部使用 Converter.toColumnSqlObject(Object)
将其转换为 SqlObject
。 Class DbTable
和 Class 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"));
我找到了解决办法。我必须创建扩展 CustomSql
的新 class Column
并将我的列名作为参数传递:
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"));
或者不创建自己的 class:
SelectQuery sql = new SelectQuery();
sql.addAllColumns().addCustomFromTable("table1");
sql.addCondition(BinaryCondition.like(new CustomSql("column1"), "A"));
它创建以下 SQL 查询,工作正常:
SELECT * FROM table1 WHERE (column1 LIKE 'A')
请检查工作示例并重构您自己的查询
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();
看看这个库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;
}
}