AliSQL 为了减少对 MySQL 语法兼容性的侵入,并支持 returning 功能, 采用了 native procedure 的方式,使用DBMS_TRANS package,统一使用 returning procedure 来支持 DML 语句返回 Resultset。
其中:
Field list : 代表期望的返回字段,以 “,” 进行分割,支持 * 号表达;
Statement :表示要执行的DML 语句, 支持 INSERT / UPDATE / DELETE;
INSERT Returning
针对 insert 语句, returning proc 返回插入到表中的记录内容;
`id` int(11) NOT NULL AUTO_INCREMENT,
`col1` int(11) NOT NULL DEFAULT '1',
`col2` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
) ENGINE=InnoDB;
mysql> call dbms_trans.returning("*", "insert into t(id) values(NULL),(NULL)");
| id | col1 | col2 |
+----+------+---------------------+
| 1 | 1 | 2019-09-03 10:39:05 |
| 2 | 1 | 2019-09-03 10:39:05 |
+----+------+---------------------+
如果没有填入任何 Fields, returning 将退化成 OK/ERR 报文:
注意: INSERT returning 只支持 insert values 形式的语法,类似create as, insert select 不支持, 例如:
mysql> call dbms_trans.returning("", "insert into t select * from t");
UPDATE Returning
针对 update 语句, returning 返回更新后的记录:
注意: UPDATE returning 不支持多表 update 语句。
DELETE Returning
针对 delete 语句, returning 返回删除的记录前映像:
例如:
mysql> call dbms_trans.returning("id, col1, col2", "delete from t where id < 3");
+----+------+---------------------+
| id | col1 | col2 |
+----+------+---------------------+
| 1 | 1 | 2019-09-03 10:40:55 |
| 2 | 1 | 2019-09-03 10:40:55 |
+----+------+---------------------+
2. 字段不支持计算 Field list 中,只支持表中原生的字段,或者 * 号, 不支持进行计算或者聚合等操作。