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
Hide 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
...
@@ -284,13 +284,7 @@ class Command extends \yii\base\Component
return
$n
;
return
$n
;
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
if
(
$e
instanceof
Exception
)
{
$this
->
handleError
(
$e
,
$rawSql
);
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
);
}
}
}
}
}
...
@@ -423,13 +417,7 @@ class Command extends \yii\base\Component
...
@@ -423,13 +417,7 @@ class Command extends \yii\base\Component
return
$result
;
return
$result
;
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
'yii\db\Command::query'
);
Yii
::
endProfile
(
$token
,
'yii\db\Command::query'
);
if
(
$e
instanceof
Exception
)
{
$this
->
handleError
(
$e
,
$rawSql
);
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
);
}
}
}
}
}
...
@@ -779,4 +767,30 @@ class Command extends \yii\base\Component
...
@@ -779,4 +767,30 @@ class Command extends \yii\base\Component
return
$this
->
setSql
(
$sql
);
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
...
@@ -73,6 +73,13 @@ abstract class Schema extends Object
private
$_builder
;
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.
* Loads the metadata for the specified table.
* @param string $name table name
* @param string $name table name
* @return TableSchema DBMS-dependent table metadata, null if the table does not exist.
* @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