指定行不存在
当使用主键或者唯一键操作指定的一条数据时,如果对应的数据不存在,自动返回错误码。
包括使用这些场景:
- 查单条
- 修改单条
- 删除单条
不适用场景
以下场景,查不到数据是正常业务逻辑,Luwak不会认定是错误,也不返回错误码:
- 查结果集(这种情况,查不到就返回空结果集)
兼容历史
当用主键和唯一键查询单条数据时,如果结果为空,是否返回错误码?
Luwak系统配置文件,有一个全局开关:
throw_error_when_row_not_found = true
- 如果开启了这个开关,当找不到指定行时,返回错误码(ITEM_NOT_FOUND)。
- 如果禁用了这个选项,当找不到指定行时,返回成功,但结果为空(这是为了兼容一些客户的历史习惯)
- 默认是开启的
示例
以员工表employees为例,为保证后续示例都是操作不存在的记录,在数据表结构后,增加了清除emp_no=20001的员工 数据语句。
--员工表
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` enum('M','F') NOT NULL,
`hire_date` date NOT NULL,
`salary` int(11) DEFAULT NULL,
PRIMARY KEY (`emp_no`)
);
--清除干扰测试的数据
delete from employees where emp_no = '20001';
示例1 查询不存在的记录
数据准备中,我们已经删除了工号为20001的数据,通过sql先查询验证一下
mysql> select * from employees where emp_no=20001;
Empty set (0.00 sec)
可见数据在数据表中确实不存在。
下面我们通过luwak,查询这个员工的信息,看看结果会返回什么。
请求报文
JSON-RPC的请求报文如下
{
"jsonrpc": "2.0",
"method": "hrm.employees.detail",
"params": 20001,
"id": "client-unique-request-id"
}
响应报文
系统提示指定数据不存在,给出错误信息。
{
"jsonrpc": "2.0",
"error": {
"code": -32600,
"message": "checking if the primary key exists: hrm.employees 20001 does not exist",
"data": {
"errorCode": "EU.Constraint.PK.NotFound",
"errorDetails": [
{
"field": "empNo",
"fieldValue": "20001",
"location": "hrm.employees",
"message": "hrm.employees.emp_no 20001 does not exist"
}
],
"errorMessage": "hrm.employees 20001 does not exist"
}
},
"id": "client-unique-request-id"
}
示例2 修改不存在的记录
还是以刚才验证不存在的数据为例,现在尝试对该数据进行修改,看看执行结果会是怎样的。
请求报文
JSON-RPC的请求报文如下
{
"jsonrpc": "2.0",
"method": "hrm.employees.update",
"params": {
"empNo": 20001,
"firstName": "Thomas"
},
"id": "client-unique-request-id"
}
响应报文
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
{
"jsonrpc": "2.0",
"error": {
"code": -32600,
"message": "checking if the primary key exists: hrm.employees map[empNo:20001] does not exist",
"data": {
"errorCode": "EU.Constraint.PK.NotFound",
"errorDetails": [
{
"constraint": "PrimaryKeyNotFound",
"field": "empNo",
"fieldValue": 20001,
"location": "hrm.employees",
"message": "primary key not found in hrm.employees.emp_no 20001"
}
],
"errorMessage": "hrm.employees map[empNo:20001] does not exist"
}
},
"id": "client-unique-request-id"
}
可见,对不存在的记录进行修改操作时,系统提示未找到对应的记录,拒绝操作。
示例3 删除不存在的记录
还是以上述不存在的数据为例,我们来尝试对它进行删除操作。
请求报文
JSON-RPC的请求报文如下
{
"jsonrpc": "2.0",
"method": "hrm.employees.delete",
"params": 20001,
"id": "client-unique-request-id"
}
响应报文
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
{
"jsonrpc": "2.0",
"error": {
"code": -32600,
"message": "checking if the primary key exists: hrm.employees map[empNo:20001] does not exist",
"data": {
"errorCode": "EU.Constraint.PK.NotFound",
"errorDetails": [
{
"constraint": "PrimaryKeyNotFound",
"field": "empNo",
"fieldValue": "20001",
"location": "hrm.employees",
"message": "primary key not found in hrm.employees.emp_no 20001"
}
],
"errorMessage": "hrm.employees map[empNo:20001] does not exist"
}
},
"id": "client-unique-request-id"
}
可见,对不存在的记录进行操作时,系统会提示未找到对应的记录,拒绝操作。
Tested on Luwak 1.12.9