php / php/php-src

pdo_dblib throws incorrect PDOException for UNIQUE constraint violation

Đang mở
#9,463 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Extension: pdo_dblib Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.2k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện trường hợp khóa trùng lặp với pdo_dblib, FreeTDS và SQL Server bằng ví dụ PHP, sau đó so sánh SQLSTATE của trường hợp này với đầu ra tsql trực tiếp. Theo dõi cách trình điều khiển PDO xử lý lỗi cơ sở dữ liệu được báo cáo; hoàn tất khi vi phạm UNIQUE được hiển thị dưới dạng SQLSTATE 23000 trong khi vẫn giữ nguyên các chi tiết lỗi hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, php
Lĩnh vực
backend, database
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.