ObjectiveSQL 第一个正式版本提供了自动生成代码的特性,替代了简单SQL 的编程的编码工作,紧接着推出复杂SQL的解决方案。
ObjectiveSQL 通过修改Java 编译器,实现了运算符重载,可以实现Java 的运算符直接映射为SQL 的运算符,可以实现表达式的一致性,从而实现SQL 的过程化编程,使得复杂SQL 的编程变得简洁、易懂,具体请参考官方文档。
Github: https://github.com/braisdom/ObjectiveSql
Gitee: https://gitee.com/mirrors/ObjectiveSql
IntelliJ IDEA 插件(由于1.2.7 版本还未上线,请参考文档安装):http://www.objsql.com/docs/basic/intellij
- 过程化SQL 编程
// Calculating the member related order data
Member.Table memberTable = Member.asTable();
Order.Table orderTable = Order.asTable();
Select select = new Select();
select.from(orderTable, memberTable);
select.where(orderTable.memberId.eq(memberTable.id));
select.project(memberTable.no, memberTable.name, memberTable.mobile);
select.project(countDistinct(orderTable.no).as("order_count"))
.project(sum(orderTable.quantity).as("total_quantity"))
.project(sum(orderTable.amount).as("total_amount"))
.project(min(orderTable.salesAt).as("first_shopping"))
.project(max(orderTable.salesAt).as("last_shopping"));
select.groupBy(memberTable.no, memberTable.name, memberTable.mobile);
上述代码生成的SQL 如下:
SELECT
`T0`.`no` ,
`T0`.`name` ,
`T0`.`mobile` ,
COUNT(DISTINCT `T1`.`no` ) AS `order_count`,
SUM(`T1`.`quantity` ) AS `total_quantity`,
SUM(`T1`.`amount` ) AS `total_amount`,
MIN(`T1`.`sales_at` ) AS `first_shopping`,
MAX(`T1`.`sales_at` ) AS `last_shopping`
FROM
`orders` AS `T1`,
`members` AS `T0`
WHERE
(`T1`.`member_id` = `T0`.`id` )
GROUP BY
`T0`.`no` ,
`T0`.`name` ,
`T0`.`mobile`
2. 等价表达式(Java 计算表达式可以映射为SQL 语句表达式)
Order.Table orderTable = Order.asTable();
Select select = new Select();
select.project(sum(orderTable.amount) / sum(orderTable.quantity) * $(100) )
.from(orderTable)
.groupBy(orderTable.productId);
上述代码生成的SQL 如下:
SELECT SUM(order.amount) / SUM(order.quantity) * 100
FROM orders AS order GROUP BY order.produc_id
注意:本文归作者所有,未经作者允许,不得转载