pdo_dblib throws incorrect PDOException for UNIQUE constraint violation
未关闭
还没有人认领这个 Issue。
Bug
Extension: pdo_dblib
Status: Needs Triage
- 主要语言
- C
- 星标
- 40.4k
- 派生
- 8.2k
- 平均合并
- 2 天 13 小时
- 30 天内合并 PR
- 96
描述
Description
The following code:
<?php
$pdo = new PDO("dblib:host=myserver.example.org");
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$pdo->exec('DROP TABLE IF EXISTS tempdb.dbo.myTable;');
$pdo->exec('CREATE TABLE tempdb.dbo.myTable (id int UNIQUE);');
$i = $pdo->prepare("INSERT INTO tempdb.dbo.myTable (id) VALUES (:id);");
$i->execute([':id' => 1]);
$i->execute([':id' => 1]); // should throw exception for UNIQUE violation
exit(0);
Resulted in this output with a PDOException thrown having the SQLSTATE error code set to HY000 [General error]:
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 20018 Violation of UNIQUE KEY constraint 'UQ__myTable__3213E83EB59C83C0'. Cannot insert duplicate key in object 'dbo.myTable'. The duplicate key value is (1). [20018] (severity 14) [INSERT INTO tempdb.dbo.myTable (id) VALUES ('1');] in ~/mybug.php:9
Stack trace:
#0 ~/mybug.php(9): PDOStatement->execute()
#1 {main}
thrown in ~/mybug.php on line 9
But I expected this output instead with a PDOException thrown having the SQLSTATE error code set to 23000 [Integrity constraint violation]):
PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: Violation of UNIQUE KEY constraint 'UQ__myTable__3213E83EB59C83C0'. Cannot insert duplicate key in object 'dbo.myTable'. The duplicate key value is (1). (severity 14) [INSERT INTO tempdb.dbo.myTable (id) VALUES ('1');] in ~/mybug.php:9
Stack trace:
#0 ~/mybug.php(9): PDOStatement->execute()
#1 {main}
thrown in ~/mybug.php on line 9
Ref for SQLSTATEs: https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/appendix-a-odbc-error-codes
Note that if you use tsql from freetds/libtds directly (without pdo_dblib), you get this output:
$ tsql -H myserver.example.org -p 1433
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> DROP TABLE IF EXISTS tempdb.dbo.myTable;
2> CREATE TABLE tempdb.dbo.myTable (id int UNIQUE);
3> INSERT INTO tempdb.dbo.myTable (id) VALUES (1);
4> GO
1> INSERT INTO tempdb.dbo.myTable (id) VALUES (1);
2> GO
Msg 2627 (severity 14, state 1) from MYSERVER Line 1:
"Violation of UNIQUE KEY constraint 'UQ__myTable__3213E83E6D65FB8A'. Cannot insert duplicate key in object 'dbo.myTable'. The duplicate key value is (1)."
Msg 3621 (severity 0, state 0) from MYSERVER Line 1:
"The statement has been terminated."
1> quit
$
PHP Version
PHP 8.1.7
Operating System
Debian unstable/testing/bookworm
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 PHP 示例,通过 pdo_dblib、FreeTDS 和 SQL Server 重现重复键情况,然后将其 SQLSTATE 与直接的 tsql 输出进行比较。跟踪 PDO 驱动程序如何处理报告的数据库错误;完成标准是将 UNIQUE 违反公开为 SQLSTATE 23000,同时保留现有的错误详细信息。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, php
- 领域
- backend, database
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100