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
e4bd3b59
Commit
e4bd3b59
authored
May 07, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3378 from yiisoft/db-integrity-exception
Database IntegrityException
parents
b4914412
11cded9b
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
124 additions
and
14 deletions
+124
-14
Schema.php
extensions/sphinx/Schema.php
+19
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
Command.php
framework/db/Command.php
+2
-14
IntegrityException.php
framework/db/IntegrityException.php
+25
-0
Schema.php
framework/db/Schema.php
+33
-0
Schema.php
framework/db/cubrid/Schema.php
+8
-0
CommandTest.php
tests/unit/framework/db/CommandTest.php
+12
-0
PostgreSQLCommandTest.php
tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php
+24
-0
No files found.
extensions/sphinx/Schema.php
View file @
e4bd3b59
...
...
@@ -11,6 +11,7 @@ use yii\base\Object;
use
yii\caching\Cache
;
use
Yii
;
use
yii\caching\GroupDependency
;
use
yii\db\Exception
;
/**
* Schema represents the Sphinx schema information.
...
...
@@ -499,4 +500,22 @@ class Schema extends Object
return
$column
;
}
/**
* Handles database error
*
* @param \Exception $e
* @param string $rawSql SQL that produced exception
* @throws Exception
*/
public
function
handleException
(
\Exception
$e
,
$rawSql
)
{
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
);
}
}
}
framework/CHANGELOG.md
View file @
e4bd3b59
...
...
@@ -27,6 +27,7 @@ Yii Framework 2 Change Log
-
Bug #3368: Fix for comparing numeric attributes in JavaScript (technixp)
-
Bug: Fixed inconsistent return of
`\yii\console\Application::runAction()`
(samdark)
-
Enh #2264:
`CookieCollection::has()`
will return false for expired or removed cookies (qiangxue)
-
Enh #2435:
`yii\db\IntegrityException`
is now thrown on database integrity errors instead of general
`yii\db\Exception`
(samdark)
-
Enh #2837: Error page now shows arguments in stack trace method calls (samdark)
-
Enh #2906: Added support for using conditional comments for js and css files registered through asset bundles and Html helper (exromany, qiangxue)
-
Enh #2942: Added truncate and truncateWord methods (Alex-Code, samdark)
...
...
framework/db/Command.php
View file @
e4bd3b59
...
...
@@ -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
->
db
->
getSchema
()
->
handleException
(
$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
->
db
->
getSchema
()
->
handleException
(
$e
,
$rawSql
);
}
}
...
...
framework/db/IntegrityException.php
0 → 100644
View file @
e4bd3b59
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\db
;
/**
* Exception represents an exception that is caused by violation of DB constraints.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class
IntegrityException
extends
Exception
{
/**
* @return string the user-friendly name of this exception
*/
public
function
getName
()
{
return
'Integrity constraint violation'
;
}
}
framework/db/Schema.php
View file @
e4bd3b59
...
...
@@ -73,6 +73,14 @@ abstract class Schema extends Object
private
$_builder
;
/**
* @var array map of DB errors and corresponding exceptions
* If left part is found in DB error message exception class from the right part is used.
*/
public
$exceptionMap
=
[
'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.
...
...
@@ -470,4 +478,29 @@ abstract class Schema extends Object
return
'string'
;
}
}
/**
* Handles database error
*
* @param \Exception $e
* @param string $rawSql SQL that produced exception
* @throws Exception
*/
public
function
handleException
(
\Exception
$e
,
$rawSql
)
{
if
(
$e
instanceof
Exception
)
{
throw
$e
;
}
else
{
$exceptionClass
=
'\yii\db\Exception'
;
foreach
(
$this
->
exceptionMap
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/cubrid/Schema.php
View file @
e4bd3b59
...
...
@@ -65,6 +65,14 @@ class Schema extends \yii\db\Schema
];
/**
* @var array map of DB errors and corresponding exceptions
* If left part is found in DB error message exception class from the right part is used.
*/
public
$exceptionMap
=
[
'Operation would have caused one or more unique constraint violations'
=>
'yii\db\IntegrityException'
,
];
/**
* @inheritdoc
*/
public
function
releaseSavepoint
(
$name
)
...
...
tests/unit/framework/db/CommandTest.php
View file @
e4bd3b59
...
...
@@ -287,4 +287,16 @@ class CommandTest extends DatabaseTestCase
public
function
testDropIndex
()
{
}
public
function
testIntegrityViolation
()
{
$this
->
setExpectedException
(
'\yii\db\IntegrityException'
);
$db
=
$this
->
getConnection
();
$sql
=
'INSERT INTO profile(id, description) VALUES (123, \'duplicate\')'
;
$command
=
$db
->
createCommand
(
$sql
);
$command
->
execute
();
$command
->
execute
();
}
}
tests/unit/framework/db/pgsql/PostgreSQLCommandTest.php
0 → 100644
View file @
e4bd3b59
<?php
namespace
yii\tests\unit\framework\db\pgsql
;
use
yiiunit\framework\db\CommandTest
;
class
PostgreSQLCommandTest
extends
CommandTest
{
public
$driverName
=
'pgsql'
;
public
function
testAutoQuoting
()
{
$db
=
$this
->
getConnection
(
false
);
$sql
=
'SELECT [[id]], [[t.name]] FROM {{customer}} t'
;
$command
=
$db
->
createCommand
(
$sql
);
$this
->
assertEquals
(
'SELECT "id", "t"."name" FROM "customer" t'
,
$command
->
sql
);
}
public
function
testBindParamValue
()
{
$this
->
markTestIncomplete
(
'TODO: impement it'
);
}
}
\ 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