Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
ca23cd8a
Commit
ca23cd8a
authored
May 06, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented throwing \yii\db\IntegrityException on DB integrity errors
parent
b86f5a14
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
14 deletions
+45
-14
Command.php
framework/db/Command.php
+28
-14
Schema.php
framework/db/Schema.php
+7
-0
PostgreSQLCommandTest.php
tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php
+10
-0
No files found.
framework/db/Command.php
View file @
ca23cd8a
...
...
@@ -284,13 +284,7 @@ class Command extends \yii\base\Component
return
$n
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
if
(
$e
instanceof
Exception
)
{
throw
$e
;
}
else
{
$message
=
$e
->
getMessage
()
.
"
\n
The SQL being executed was:
$rawSql
"
;
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
throw
new
Exception
(
$message
,
$errorInfo
,
(
int
)
$e
->
getCode
(),
$e
);
}
$this
->
handleError
(
$e
,
$rawSql
);
}
}
...
...
@@ -423,13 +417,7 @@ class Command extends \yii\base\Component
return
$result
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
'yii\db\Command::query'
);
if
(
$e
instanceof
Exception
)
{
throw
$e
;
}
else
{
$message
=
$e
->
getMessage
()
.
"
\n
The SQL being executed was:
$rawSql
"
;
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
throw
new
Exception
(
$message
,
$errorInfo
,
(
int
)
$e
->
getCode
(),
$e
);
}
$this
->
handleError
(
$e
,
$rawSql
);
}
}
...
...
@@ -779,4 +767,30 @@ class Command extends \yii\base\Component
return
$this
->
setSql
(
$sql
);
}
/**
* Handles database error
*
* @param \Exception $e
* @param string $rawSql SQL that produced exception
* @throws Exception
*/
protected
function
handleError
(
\Exception
$e
,
$rawSql
)
{
if
(
$e
instanceof
Exception
)
{
throw
$e
;
}
else
{
$exceptionClass
=
'\yii\db\Exception'
;
$errorMap
=
$this
->
db
->
getSchema
()
->
errorMap
;
foreach
(
$errorMap
as
$error
=>
$class
)
{
if
(
strpos
(
$e
->
getMessage
(),
$error
)
!==
false
)
{
$exceptionClass
=
$class
;
}
}
$message
=
$e
->
getMessage
()
.
"
\n
The SQL being executed was:
$rawSql
"
;
$errorInfo
=
$e
instanceof
\PDOException
?
$e
->
errorInfo
:
null
;
throw
new
$exceptionClass
(
$message
,
$errorInfo
,
(
int
)
$e
->
getCode
(),
$e
);
}
}
}
framework/db/Schema.php
View file @
ca23cd8a
...
...
@@ -73,6 +73,13 @@ abstract class Schema extends Object
private
$_builder
;
/**
* @var array map of DB errors and corresponding exceptions
*/
public
$errorMap
=
[
'SQLSTATE[23'
=>
'\yii\db\IntegrityException'
,
];
/**
* Loads the metadata for the specified table.
* @param string $name table name
* @return TableSchema DBMS-dependent table metadata, null if the table does not exist.
...
...
tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php
0 → 100644
View file @
ca23cd8a
<?php
namespace
yii\tests\unit\framework\db\pgsql
;
use
yiiunit\framework\db\CommandTest
;
class
PostgreSQLCommandTest
extends
CommandTest
{
public
$driverName
=
'pgsql'
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment