Skip to content

Commit af2298f

Browse files
authored
Optimized the database exception to implement unique constraint error detection. (#7194)
1 parent 1455cc9 commit af2298f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/SQLiteConnection.php

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Closure;
1616
use Doctrine\DBAL\Driver\PDO\SQLite\Driver;
1717
use Doctrine\DBAL\Driver\PDO\SQLite\Driver as DoctrineDriver;
18+
use Exception;
1819
use Hyperf\Database\Connection;
1920
use Hyperf\Database\Query\Grammars\Grammar as HyperfQueryGrammar;
2021
use Hyperf\Database\Query\Processors\Processor;
@@ -65,6 +66,16 @@ public function getSchemaBuilder(): SchemaBuilder
6566
return new SQLiteBuilder($this);
6667
}
6768

69+
/**
70+
* Determine if the given database exception was caused by a unique constraint violation.
71+
*
72+
* @return bool
73+
*/
74+
protected function isUniqueConstraintError(Exception $exception)
75+
{
76+
return boolval(preg_match('#(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)#i', $exception->getMessage()));
77+
}
78+
6879
/**
6980
* Get the default query grammar instance.
7081
*

0 commit comments

Comments
 (0)