1515
1616use CodeIgniter \Database \BaseConnection ;
1717use CodeIgniter \Database \Exceptions \DatabaseException ;
18- use CodeIgniter \Database \Exceptions \UniqueConstraintViolationException ;
1918use CodeIgniter \Database \TableName ;
2019use stdClass ;
2120
@@ -90,6 +89,44 @@ class Connection extends BaseConnection
9089 */
9190 protected $ _reserved_identifiers = ['* ' ];
9291
92+ /**
93+ * Checks whether the native database error represents a unique constraint violation.
94+ */
95+ protected function isUniqueConstraintViolation (int |string $ code , string $ message ): bool
96+ {
97+ $ errors = sqlsrv_errors (SQLSRV_ERR_ERRORS );
98+ if (! is_array ($ errors )) {
99+ return false ;
100+ }
101+
102+ foreach ($ errors as $ error ) {
103+ // SQLSTATE 23000 (integrity constraint violation) with SQL Server error
104+ // 2627 (UNIQUE CONSTRAINT or PRIMARY KEY violation) or 2601 (UNIQUE INDEX violation).
105+ if (($ error ['SQLSTATE ' ] ?? '' ) === '23000 '
106+ && in_array ($ error ['code ' ] ?? 0 , [2627 , 2601 ], true )) {
107+ return true ;
108+ }
109+ }
110+
111+ return false ;
112+ }
113+
114+ /**
115+ * Checks whether the native database code represents a retryable transaction failure.
116+ */
117+ protected function isRetryableTransactionErrorCode (int |string $ code ): bool
118+ {
119+ $ vendorCode = (string ) (is_string ($ code ) && str_contains ($ code , '/ ' )
120+ ? substr ($ code , strrpos ($ code , '/ ' ) + 1 )
121+ : $ code );
122+
123+ if (preg_match ('/^\d+$/ ' , $ vendorCode ) !== 1 ) {
124+ return false ;
125+ }
126+
127+ return in_array ((int ) $ vendorCode , [1205 , 3960 ], true );
128+ }
129+
93130 /**
94131 * Class constructor
95132 */
@@ -538,9 +575,7 @@ protected function execute(string $sql)
538575 ]);
539576
540577 $ error = $ this ->error ();
541- $ exception = $ this ->isUniqueConstraintViolation ()
542- ? new UniqueConstraintViolationException ($ message , $ error ['code ' ])
543- : new DatabaseException ($ message , $ error ['code ' ]);
578+ $ exception = $ this ->createDatabaseException ($ message , $ error ['code ' ]);
544579
545580 if ($ this ->DBDebug ) {
546581 throw $ exception ;
@@ -552,25 +587,6 @@ protected function execute(string $sql)
552587 return $ stmt ;
553588 }
554589
555- private function isUniqueConstraintViolation (): bool
556- {
557- $ errors = sqlsrv_errors (SQLSRV_ERR_ERRORS );
558- if (! is_array ($ errors )) {
559- return false ;
560- }
561-
562- foreach ($ errors as $ error ) {
563- // SQLSTATE 23000 (integrity constraint violation) with SQL Server error
564- // 2627 (UNIQUE CONSTRAINT or PRIMARY KEY violation) or 2601 (UNIQUE INDEX violation).
565- if (($ error ['SQLSTATE ' ] ?? '' ) === '23000 '
566- && in_array ($ error ['code ' ] ?? 0 , [2627 , 2601 ], true )) {
567- return true ;
568- }
569- }
570-
571- return false ;
572- }
573-
574590 /**
575591 * The name of the platform in use (MySQLi, mssql, etc)
576592 */
0 commit comments