Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Tomáš Trávníček
doctrine-dbal
Commits
46863217
Commit
46863217
authored
Nov 13, 2013
by
Benjamin Eberlei
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'DBAL-407'
parents
d79ce18e
4a5be979
Changes
22
Hide whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
620 additions
and
31 deletions
+620
-31
.gitignore
.gitignore
+1
-0
Connection.php
lib/Doctrine/DBAL/Connection.php
+5
-5
DBALException.php
lib/Doctrine/DBAL/DBALException.php
+28
-2
Driver.php
lib/Doctrine/DBAL/Driver.php
+8
-0
Driver.php
lib/Doctrine/DBAL/Driver/DrizzlePDOMySql/Driver.php
+8
-0
DB2Driver.php
lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php
+8
-0
Driver.php
lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
+63
-1
MysqliConnection.php
lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
+8
-0
MysqliException.php
lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php
+3
-1
Driver.php
lib/Doctrine/DBAL/Driver/OCI8/Driver.php
+8
-0
Driver.php
lib/Doctrine/DBAL/Driver/PDOIbm/Driver.php
+8
-0
Driver.php
lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php
+61
-6
Driver.php
lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php
+8
-0
Driver.php
lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php
+60
-6
Driver.php
lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php
+61
-6
Driver.php
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php
+8
-0
Driver.php
lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php
+8
-0
Statement.php
lib/Doctrine/DBAL/Statement.php
+6
-1
DBALExceptionTest.php
tests/Doctrine/Tests/DBAL/DBALExceptionTest.php
+2
-1
DataAccessTest.php
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
+6
-2
ExceptionTest.php
tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
+247
-0
DriverMock.php
tests/Doctrine/Tests/Mocks/DriverMock.php
+5
-0
No files found.
.gitignore
View file @
46863217
...
...
@@ -5,3 +5,4 @@ dist/
download/
lib/Doctrine/Common/
vendor/
*.phpunit.xml
lib/Doctrine/DBAL/Connection.php
View file @
46863217
...
...
@@ -645,7 +645,7 @@ class Connection implements DriverConnection
try
{
$stmt
=
new
Statement
(
$statement
,
$this
);
}
catch
(
\Exception
$ex
)
{
throw
DBALException
::
driverExceptionDuringQuery
(
$ex
,
$statement
);
throw
DBALException
::
driverExceptionDuringQuery
(
$
this
->
_driver
,
$
ex
,
$statement
);
}
$stmt
->
setFetchMode
(
$this
->
defaultFetchMode
);
...
...
@@ -698,7 +698,7 @@ class Connection implements DriverConnection
$stmt
=
$this
->
_conn
->
query
(
$query
);
}
}
catch
(
\Exception
$ex
)
{
throw
DBALException
::
driverExceptionDuringQuery
(
$ex
,
$query
,
$this
->
resolveParams
(
$params
,
$types
));
throw
DBALException
::
driverExceptionDuringQuery
(
$
this
->
_driver
,
$
ex
,
$query
,
$this
->
resolveParams
(
$params
,
$types
));
}
$stmt
->
setFetchMode
(
$this
->
defaultFetchMode
);
...
...
@@ -807,7 +807,7 @@ class Connection implements DriverConnection
break
;
}
}
catch
(
\Exception
$ex
)
{
throw
DBALException
::
driverExceptionDuringQuery
(
$ex
,
$args
[
0
]);
throw
DBALException
::
driverExceptionDuringQuery
(
$
this
->
_driver
,
$
ex
,
$args
[
0
]);
}
$statement
->
setFetchMode
(
$this
->
defaultFetchMode
);
...
...
@@ -860,7 +860,7 @@ class Connection implements DriverConnection
$result
=
$this
->
_conn
->
exec
(
$query
);
}
}
catch
(
\Exception
$ex
)
{
throw
DBALException
::
driverExceptionDuringQuery
(
$ex
,
$query
,
$this
->
resolveParams
(
$params
,
$types
));
throw
DBALException
::
driverExceptionDuringQuery
(
$
this
->
_driver
,
$
ex
,
$query
,
$this
->
resolveParams
(
$params
,
$types
));
}
if
(
$logger
)
{
...
...
@@ -891,7 +891,7 @@ class Connection implements DriverConnection
try
{
$result
=
$this
->
_conn
->
exec
(
$statement
);
}
catch
(
\Exception
$ex
)
{
throw
DBALException
::
driverExceptionDuringQuery
(
$ex
,
$statement
);
throw
DBALException
::
driverExceptionDuringQuery
(
$
this
->
_driver
,
$
ex
,
$statement
);
}
if
(
$logger
)
{
...
...
lib/Doctrine/DBAL/DBALException.php
View file @
46863217
...
...
@@ -21,6 +21,18 @@ namespace Doctrine\DBAL;
class
DBALException
extends
\Exception
{
const
ERROR_DUPLICATE_KEY
=
1
;
const
ERROR_UNKNOWN_TABLE
=
2
;
const
ERROR_TABLE_ALREADY_EXISTS
=
3
;
const
ERROR_FOREIGN_KEY_CONSTRAINT
=
4
;
const
ERROR_NOT_NULL
=
5
;
const
ERROR_BAD_FIELD_NAME
=
6
;
const
ERROR_NON_UNIQUE_FIELD_NAME
=
7
;
const
ERROR_SYNTAX
=
9
;
const
ERROR_UNABLE_TO_OPEN
=
10
;
const
ERROR_WRITE_READONLY
=
11
;
const
ERROR_ACCESS_DENIED
=
12
;
/**
* @param string $method
*
...
...
@@ -74,13 +86,14 @@ class DBALException extends \Exception
}
/**
* @param \Doctrine\DBAL\Driver $driver
* @param \Exception $driverEx
* @param string $sql
* @param array $params
*
* @return \Doctrine\DBAL\DBALException
*/
public
static
function
driverExceptionDuringQuery
(
\Exception
$driverEx
,
$sql
,
array
$params
=
array
())
public
static
function
driverExceptionDuringQuery
(
Driver
$driver
,
\Exception
$driverEx
,
$sql
,
array
$params
=
array
())
{
$msg
=
"An exception occurred while executing '"
.
$sql
.
"'"
;
if
(
$params
)
{
...
...
@@ -88,7 +101,20 @@ class DBALException extends \Exception
}
$msg
.=
":
\n\n
"
.
$driverEx
->
getMessage
();
return
new
self
(
$msg
,
0
,
$driverEx
);
return
new
self
(
$msg
,
$driver
->
convertExceptionCode
(
$driverEx
),
$driverEx
);
}
/**
* @param \Doctrine\DBAL\Driver $driver
* @param \Exception $driverEx
*
* @return \Doctrine\DBAL\DBALException
*/
public
static
function
driverException
(
Driver
$driver
,
\Exception
$driverEx
)
{
$msg
=
"An exception occured in driver: "
.
$driverEx
->
getMessage
();
return
new
self
(
$msg
,
$driver
->
convertExceptionCode
(
$driverEx
),
$driverEx
);
}
/**
...
...
lib/Doctrine/DBAL/Driver.php
View file @
46863217
...
...
@@ -72,4 +72,12 @@ interface Driver
* @return string The name of the database.
*/
public
function
getDatabase
(
Connection
$conn
);
/**
* @param \Exception $exception
*
* @return int
*/
public
function
convertExceptionCode
(
\Exception
$exception
);
}
lib/Doctrine/DBAL/Driver/DrizzlePDOMySql/Driver.php
View file @
46863217
...
...
@@ -99,4 +99,12 @@ class Driver implements \Doctrine\DBAL\Driver
return
$params
[
'dbname'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php
View file @
46863217
...
...
@@ -91,4 +91,12 @@ class DB2Driver implements Driver
return
$params
[
'dbname'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
lib/Doctrine/DBAL/Driver/Mysqli/Driver.php
View file @
46863217
...
...
@@ -20,6 +20,8 @@
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\Driver
as
DriverInterface
;
use
Doctrine\DBAL\Driver\Mysqli\MysqliException
;
use
Doctrine\DBAL\DBALException
;
/**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
...
...
@@ -31,7 +33,11 @@ class Driver implements DriverInterface
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
{
return
new
MysqliConnection
(
$params
,
$username
,
$password
,
$driverOptions
);
try
{
return
new
MysqliConnection
(
$params
,
$username
,
$password
,
$driverOptions
);
}
catch
(
MysqliException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
}
/**
...
...
@@ -67,4 +73,60 @@ class Driver implements DriverInterface
return
$params
[
'dbname'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
if
(
strpos
(
$exception
->
getMessage
(),
'Table'
)
===
0
)
{
if
(
strpos
(
$exception
->
getMessage
(),
'doesn\'t exist'
)
!==
false
)
{
return
DBALException
::
ERROR_UNKNOWN_TABLE
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'already exists'
)
!==
false
)
{
return
DBALException
::
ERROR_TABLE_ALREADY_EXISTS
;
}
}
if
(
strpos
(
$exception
->
getMessage
(),
'Unknown column'
)
===
0
)
{
return
DBALException
::
ERROR_BAD_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Cannot delete or update a parent row: a foreign key constraint fails'
)
!==
false
)
{
return
DBALException
::
ERROR_FOREIGN_KEY_CONSTRAINT
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Duplicate entry'
)
!==
false
)
{
return
DBALException
::
ERROR_DUPLICATE_KEY
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Column not found: 1054 Unknown column'
)
!==
false
)
{
return
DBALException
::
ERROR_BAD_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'in field list is ambiguous'
)
!==
falsE
)
{
return
DBALException
::
ERROR_NON_UNIQUE_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'You have an error in your SQL syntax; check the manual'
)
!==
false
)
{
return
DBALException
::
ERROR_SYNTAX
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Access denied for user'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'getaddrinfo failed: Name or service not known'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
if
(
strpos
(
$exception
->
getMessage
(),
' cannot be null'
))
{
return
DBALException
::
ERROR_NOT_NULL
;
}
var_dump
(
$exception
->
geTcode
());
var_dump
(
$exception
->
getMEssage
());
return
0
;
}
}
lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
View file @
46863217
...
...
@@ -45,10 +45,18 @@ class MysqliConnection implements Connection
$socket
=
isset
(
$params
[
'unix_socket'
])
?
$params
[
'unix_socket'
]
:
ini_get
(
'mysqli.default_socket'
);
$this
->
_conn
=
mysqli_init
();
$previousHandler
=
set_error_handler
(
function
()
{
});
if
(
!
$this
->
_conn
->
real_connect
(
$params
[
'host'
],
$username
,
$password
,
$params
[
'dbname'
],
$port
,
$socket
))
{
set_error_handler
(
$previousHandler
);
throw
new
MysqliException
(
$this
->
_conn
->
connect_error
,
$this
->
_conn
->
connect_errno
);
}
set_error_handler
(
$previousHandler
);
if
(
isset
(
$params
[
'charset'
]))
{
$this
->
_conn
->
set_charset
(
$params
[
'charset'
]);
}
...
...
lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php
View file @
46863217
...
...
@@ -19,9 +19,11 @@
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\DBALException
;
/**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/
class
MysqliException
extends
\
Exception
class
MysqliException
extends
DBAL
Exception
{
}
lib/Doctrine/DBAL/Driver/OCI8/Driver.php
View file @
46863217
...
...
@@ -113,4 +113,12 @@ class Driver implements \Doctrine\DBAL\Driver
return
$params
[
'user'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
lib/Doctrine/DBAL/Driver/PDOIbm/Driver.php
View file @
46863217
...
...
@@ -105,4 +105,12 @@ class Driver implements \Doctrine\DBAL\Driver
return
$params
[
'dbname'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php
View file @
46863217
...
...
@@ -20,6 +20,8 @@
namespace
Doctrine\DBAL\Driver\PDOMySql
;
use
Doctrine\DBAL\Connection
;
use
Doctrine\DBAL\DBALException
;
use
PDOException
;
/**
* PDO MySql driver.
...
...
@@ -33,12 +35,16 @@ class Driver implements \Doctrine\DBAL\Driver
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
{
$conn
=
new
\Doctrine\DBAL\Driver\PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
try
{
$conn
=
new
\Doctrine\DBAL\Driver\PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
catch
(
PDOException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
return
$conn
;
}
...
...
@@ -108,4 +114,53 @@ class Driver implements \Doctrine\DBAL\Driver
}
return
$conn
->
query
(
'SELECT DATABASE()'
)
->
fetchColumn
();
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
switch
(
$exception
->
getCode
())
{
case
'42S02'
:
return
DBALException
::
ERROR_UNKNOWN_TABLE
;
case
'42S01'
:
return
DBALException
::
ERROR_TABLE_ALREADY_EXISTS
;
default
:
if
(
strpos
(
$exception
->
getMessage
(),
'Cannot delete or update a parent row: a foreign key constraint fails'
)
!==
false
)
{
return
DBALException
::
ERROR_FOREIGN_KEY_CONSTRAINT
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Duplicate entry'
)
!==
false
)
{
return
DBALException
::
ERROR_DUPLICATE_KEY
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Column not found: 1054 Unknown column'
)
!==
false
)
{
return
DBALException
::
ERROR_BAD_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'in field list is ambiguous'
)
!==
falsE
)
{
return
DBALException
::
ERROR_NON_UNIQUE_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'You have an error in your SQL syntax; check the manual'
)
!==
false
)
{
return
DBALException
::
ERROR_SYNTAX
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'Access denied for user'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'getaddrinfo failed: Name or service not known'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
if
(
strpos
(
$exception
->
getMessage
(),
' cannot be null'
))
{
return
DBALException
::
ERROR_NOT_NULL
;
}
}
return
0
;
}
}
lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php
View file @
46863217
...
...
@@ -113,4 +113,12 @@ class Driver implements \Doctrine\DBAL\Driver
return
$params
[
'user'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php
View file @
46863217
...
...
@@ -20,6 +20,8 @@
namespace
Doctrine\DBAL\Driver\PDOPgSql
;
use
Doctrine\DBAL\Platforms
;
use
Doctrine\DBAL\DBALException
;
use
PDOException
;
/**
* Driver that connects through pdo_pgsql.
...
...
@@ -33,12 +35,16 @@ class Driver implements \Doctrine\DBAL\Driver
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
array
())
{
return
new
\Doctrine\DBAL\Driver\PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
try
{
return
new
\Doctrine\DBAL\Driver\PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
catch
(
PDOException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
}
/**
...
...
@@ -99,5 +105,53 @@ class Driver implements \Doctrine\DBAL\Driver
?
$params
[
'dbname'
]
:
$conn
->
query
(
'SELECT CURRENT_DATABASE()'
)
->
fetchColumn
();
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
if
(
strpos
(
$exception
->
getMessage
(),
'duplicate key value violates unique constraint'
)
!==
false
)
{
return
DBALException
::
ERROR_DUPLICATE_KEY
;
}
if
(
$exception
->
getCode
()
===
"42P01"
)
{
return
DBALException
::
ERROR_UNKNOWN_TABLE
;
}
if
(
$exception
->
getCode
()
===
"42P07"
)
{
return
DBALException
::
ERROR_TABLE_ALREADY_EXISTS
;
}
if
(
$exception
->
getCode
()
===
"23503"
)
{
return
DBALException
::
ERROR_FOREIGN_KEY_CONSTRAINT
;
}
if
(
$exception
->
getCode
()
===
"23502"
)
{
return
DBALException
::
ERROR_NOT_NULL
;
}
if
(
$exception
->
getCode
()
===
"42703"
)
{
return
DBALException
::
ERROR_BAD_FIELD_NAME
;
}
if
(
$exception
->
getCode
()
===
"42702"
)
{
return
DBALException
::
ERROR_NON_UNIQUE_FIELD_NAME
;
}
if
(
$exception
->
getCode
()
===
"42601"
)
{
return
DBALException
::
ERROR_SYNTAX
;
}
if
(
stripos
(
$exception
->
getMessage
(),
'password authentication failed for user'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
if
(
stripos
(
$exception
->
getMessage
(),
'Name or service not known'
)
!==
false
)
{
return
DBALException
::
ERROR_ACCESS_DENIED
;
}
return
0
;
}
}
lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php
View file @
46863217
...
...
@@ -19,6 +19,9 @@
namespace
Doctrine\DBAL\Driver\PDOSqlite
;
use
Doctrine\DBAL\DBALException
;
use
PDOException
;
/**
* The PDO Sqlite driver.
*
...
...
@@ -46,12 +49,16 @@ class Driver implements \Doctrine\DBAL\Driver
unset
(
$driverOptions
[
'userDefinedFunctions'
]);
}
$pdo
=
new
\Doctrine\DBAL\Driver\PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
try
{
$pdo
=
new
\Doctrine\DBAL\Driver\PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
catch
(
PDOException
$ex
)
{
throw
DBALException
::
driverException
(
$this
,
$ex
);
}
foreach
(
$this
->
_userDefinedFunctions
as
$fn
=>
$data
)
{
$pdo
->
sqliteCreateFunction
(
$fn
,
$data
[
'callback'
],
$data
[
'numArgs'
]);
...
...
@@ -112,4 +119,52 @@ class Driver implements \Doctrine\DBAL\Driver
return
isset
(
$params
[
'path'
])
?
$params
[
'path'
]
:
null
;
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
if
(
strpos
(
$exception
->
getMessage
(),
'must be unique'
)
!==
false
)
{
return
DBALException
::
ERROR_DUPLICATE_KEY
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'may not be NULL'
)
!==
false
)
{
return
DBALException
::
ERROR_NOT_NULL
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'is not unique'
)
!==
false
)
{
return
DBALException
::
ERROR_DUPLICATE_KEY
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'no such table:'
)
!==
false
)
{
return
DBALException
::
ERROR_UNKNOWN_TABLE
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'already exists'
)
!==
false
)
{
return
DBALException
::
ERROR_TABLE_ALREADY_EXISTS
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'has no column named'
)
!==
false
)
{
return
DBALException
::
ERROR_BAD_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'ambiguous column name'
)
!==
false
)
{
return
DBALException
::
ERROR_NON_UNIQUE_FIELD_NAME
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'syntax error'
)
!==
false
)
{
return
DBALException
::
ERROR_SYNTAX
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'attempt to write a readonly database'
)
!==
false
)
{
return
DBALException
::
ERROR_WRITE_READONLY
;
}
if
(
strpos
(
$exception
->
getMessage
(),
'unable to open database file'
)
!==
false
)
{
return
DBALException
::
ERROR_UNABLE_TO_OPEN
;
}
return
0
;
}
}
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php
View file @
46863217
...
...
@@ -102,4 +102,12 @@ class Driver implements \Doctrine\DBAL\Driver
return
$params
[
'dbname'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
lib/Doctrine/DBAL/Driver/SQLSrv/Driver.php
View file @
46863217
...
...
@@ -83,4 +83,12 @@ class Driver implements \Doctrine\DBAL\Driver
$params
=
$conn
->
getParams
();
return
$params
[
'dbname'
];
}
/**
* {@inheritdoc}
*/
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
lib/Doctrine/DBAL/Statement.php
View file @
46863217
...
...
@@ -164,7 +164,12 @@ class Statement implements \IteratorAggregate, DriverStatement
try
{
$stmt
=
$this
->
stmt
->
execute
(
$params
);
}
catch
(
\Exception
$ex
)
{
throw
DBALException
::
driverExceptionDuringQuery
(
$ex
,
$this
->
sql
,
$this
->
conn
->
resolveParams
(
$this
->
params
,
$this
->
types
));
throw
DBALException
::
driverExceptionDuringQuery
(
$this
->
conn
->
getDriver
(),
$ex
,
$this
->
sql
,
$this
->
conn
->
resolveParams
(
$this
->
params
,
$this
->
types
)
);
}
if
(
$logger
)
{
...
...
tests/Doctrine/Tests/DBAL/DBALExceptionTest.php
View file @
46863217
...
...
@@ -8,7 +8,8 @@ class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase
{
public
function
testDriverExceptionDuringQueryAcceptsBinaryData
()
{
$e
=
DBALException
::
driverExceptionDuringQuery
(
new
\Exception
,
''
,
array
(
'ABC'
,
chr
(
128
)));
$driver
=
$this
->
getMock
(
'\Doctrine\DBAL\Driver'
);
$e
=
DBALException
::
driverExceptionDuringQuery
(
$driver
,
new
\Exception
,
''
,
array
(
'ABC'
,
chr
(
128
)));
$this
->
assertContains
(
'with params ["ABC", "\x80"]'
,
$e
->
getMessage
());
}
}
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
View file @
46863217
...
...
@@ -203,15 +203,19 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$row
=
array_change_key_case
(
$row
,
\CASE_LOWER
);
$this
->
assertEquals
(
1
,
$row
[
'test_int'
]);
$this
->
assertEquals
(
$datetimeString
,
$row
[
'test_datetime'
]);
$this
->
assertEquals
(
$datetimeString
,
$row
[
'test_datetime'
]);
}
/**
* @group DBAL-209
* @expectedException \Doctrine\DBAL\DBALException
*/
public
function
testFetchAllWithMissingTypes
()
{
if
(
$this
->
_conn
->
getDriver
()
instanceof
\Doctrine\DBAL\Driver\Mysqli\Driver
)
{
$this
->
markTestSkipped
(
'mysqli actually supports this'
);
}
$datetimeString
=
'2010-01-01 10:10:10'
;
$datetime
=
new
\DateTime
(
$datetimeString
);
$sql
=
"SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?"
;
...
...
tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php
0 → 100644
View file @
46863217
<?php
namespace
Doctrine\Tests\DBAL\Functional
;
use
Doctrine\DBAL\DBALException
;
class
ExceptionTest
extends
\Doctrine\Tests\DbalFunctionalTestCase
{
public
function
setUp
()
{
parent
::
setUp
();
$supportExceptions
=
array
(
'pdo_sqlite'
,
'pdo_mysql'
,
'pdo_pgsql'
,
'mysqli'
);
$params
=
$this
->
_conn
->
getParams
();
if
(
!
in_array
(
$params
[
'driver'
],
$supportExceptions
))
{
$this
->
markTestSkipped
(
'Driver does not support special exception handling.'
);
}
}
public
function
testDuplicateKeyException
()
{
$table
=
new
\Doctrine\DBAL\Schema\Table
(
"duplicatekey_table"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
());
$table
->
setPrimaryKey
(
array
(
'id'
));
foreach
(
$this
->
_conn
->
getDatabasePlatform
()
->
getCreateTableSQL
(
$table
)
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
$this
->
_conn
->
insert
(
"duplicatekey_table"
,
array
(
'id'
=>
1
));
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_DUPLICATE_KEY
);
$this
->
_conn
->
insert
(
"duplicatekey_table"
,
array
(
'id'
=>
1
));
}
public
function
testUnknownTableException
()
{
$sql
=
"SELECT * FROM unknown_table"
;
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_UNKNOWN_TABLE
);
$this
->
_conn
->
executeQuery
(
$sql
);
}
public
function
testTableAlreadyExistsException
()
{
$table
=
new
\Doctrine\DBAL\Schema\Table
(
"alreadyexist_table"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
());
$table
->
setPrimaryKey
(
array
(
'id'
));
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_TABLE_ALREADY_EXISTS
);
foreach
(
$this
->
_conn
->
getDatabasePlatform
()
->
getCreateTableSQL
(
$table
)
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
foreach
(
$this
->
_conn
->
getDatabasePlatform
()
->
getCreateTableSQL
(
$table
)
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
}
public
function
testForeignKeyContraintException
()
{
if
(
!
$this
->
_conn
->
getDatabasePlatform
()
->
supportsForeignKeyConstraints
())
{
$this
->
markTestSkipped
(
"Only fails on platforms with foreign key constraints."
);
}
$schema
=
new
\Doctrine\DBAL\Schema\Schema
();
$table
=
$schema
->
createTable
(
"constraint_error_table"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
());
$table
->
setPrimaryKey
(
array
(
'id'
));
$owningTable
=
$schema
->
createTable
(
"owning_table"
);
$owningTable
->
addColumn
(
'id'
,
'integer'
,
array
());
$owningTable
->
addColumn
(
'constraint_id'
,
'integer'
,
array
());
$owningTable
->
setPrimaryKey
(
array
(
'id'
));
$owningTable
->
addForeignKeyConstraint
(
$table
,
array
(
'constraint_id'
),
array
(
'id'
));
foreach
(
$schema
->
toSql
(
$this
->
_conn
->
getDatabasePlatform
())
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
$this
->
_conn
->
insert
(
"constraint_error_table"
,
array
(
'id'
=>
1
));
$this
->
_conn
->
insert
(
"owning_table"
,
array
(
'id'
=>
1
,
'constraint_id'
=>
1
));
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_FOREIGN_KEY_CONSTRAINT
);
$this
->
_conn
->
delete
(
'constraint_error_table'
,
array
(
'id'
=>
1
));
}
public
function
testNotNullException
()
{
$schema
=
new
\Doctrine\DBAL\Schema\Schema
();
$table
=
$schema
->
createTable
(
"notnull_table"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
());
$table
->
addColumn
(
'value'
,
'integer'
,
array
(
'notnull'
=>
true
));
$table
->
setPrimaryKey
(
array
(
'id'
));
foreach
(
$schema
->
toSql
(
$this
->
_conn
->
getDatabasePlatform
())
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_NOT_NULL
);
$this
->
_conn
->
insert
(
"notnull_table"
,
array
(
'id'
=>
1
,
'value'
=>
null
));
}
public
function
testBadFieldNameException
()
{
$schema
=
new
\Doctrine\DBAL\Schema\Schema
();
$table
=
$schema
->
createTable
(
"bad_fieldname_table"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
());
foreach
(
$schema
->
toSql
(
$this
->
_conn
->
getDatabasePlatform
())
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_BAD_FIELD_NAME
);
$this
->
_conn
->
insert
(
"bad_fieldname_table"
,
array
(
'name'
=>
5
));
}
public
function
testNonUniqueFieldNameException
()
{
$schema
=
new
\Doctrine\DBAL\Schema\Schema
();
$table
=
$schema
->
createTable
(
"ambiguous_list_table"
);
$table
->
addColumn
(
'id'
,
'integer'
);
$table2
=
$schema
->
createTable
(
"ambiguous_list_table_2"
);
$table2
->
addColumn
(
'id'
,
'integer'
);
foreach
(
$schema
->
toSql
(
$this
->
_conn
->
getDatabasePlatform
())
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
$sql
=
'SELECT id FROM ambiguous_list_table, ambiguous_list_table_2'
;
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_NON_UNIQUE_FIELD_NAME
);
$this
->
_conn
->
executeQuery
(
$sql
);
}
public
function
testNotUniqueException
()
{
$schema
=
new
\Doctrine\DBAL\Schema\Schema
();
$table
=
$schema
->
createTable
(
"unique_field_table"
);
$table
->
addColumn
(
'id'
,
'integer'
);
$table
->
addUniqueIndex
(
array
(
'id'
));
foreach
(
$schema
->
toSql
(
$this
->
_conn
->
getDatabasePlatform
())
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
$this
->
_conn
->
insert
(
"unique_field_table"
,
array
(
'id'
=>
5
));
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_DUPLICATE_KEY
);
$this
->
_conn
->
insert
(
"unique_field_table"
,
array
(
'id'
=>
5
));
}
public
function
testSyntaxErrorException
()
{
$table
=
new
\Doctrine\DBAL\Schema\Table
(
"syntax_error_table"
);
$table
->
addColumn
(
'id'
,
'integer'
,
array
());
$table
->
setPrimaryKey
(
array
(
'id'
));
foreach
(
$this
->
_conn
->
getDatabasePlatform
()
->
getCreateTableSQL
(
$table
)
AS
$sql
)
{
$this
->
_conn
->
executeQuery
(
$sql
);
}
$sql
=
'SELECT id FRO syntax_error_table'
;
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
DBALException
::
ERROR_SYNTAX
);
$this
->
_conn
->
executeQuery
(
$sql
);
}
/**
* @dataProvider getSqLiteOpenConnection
*/
public
function
testConnectionExceptionSqLite
(
$mode
,
$exceptionCode
)
{
if
(
$this
->
_conn
->
getDatabasePlatform
()
->
getName
()
!=
'sqlite'
)
{
$this
->
markTestSkipped
(
"Only fails this way on sqlite"
);
}
$filename
=
sprintf
(
'%s/%s'
,
sys_get_temp_dir
(),
'doctrine_failed_connection.db'
);
if
(
file_exists
(
$filename
))
{
unlink
(
$filename
);
}
touch
(
$filename
);
chmod
(
$filename
,
$mode
);
$params
=
array
(
'driver'
=>
'pdo_sqlite'
,
'path'
=>
$filename
,
);
$conn
=
\Doctrine\DBAL\DriverManager
::
getConnection
(
$params
);
$schema
=
new
\Doctrine\DBAL\Schema\Schema
();
$table
=
$schema
->
createTable
(
"no_connection"
);
$table
->
addColumn
(
'id'
,
'integer'
);
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
$exceptionCode
);
foreach
(
$schema
->
toSql
(
$conn
->
getDatabasePlatform
())
AS
$sql
)
{
$conn
->
executeQuery
(
$sql
);
}
}
public
function
getSqLiteOpenConnection
()
{
return
array
(
array
(
0000
,
DBALException
::
ERROR_UNABLE_TO_OPEN
),
array
(
0444
,
DBALException
::
ERROR_WRITE_READONLY
),
);
}
/**
* @dataProvider getConnectionParams
*/
public
function
testConnectionException
(
$params
,
$exceptionCode
)
{
if
(
$this
->
_conn
->
getDatabasePlatform
()
->
getName
()
==
'sqlite'
)
{
$this
->
markTestSkipped
(
"Only skipped if platform is not sqlite"
);
}
$defaultParams
=
$this
->
_conn
->
getParams
();
$params
=
array_merge
(
$defaultParams
,
$params
);
$conn
=
\Doctrine\DBAL\DriverManager
::
getConnection
(
$params
);
$schema
=
new
\Doctrine\DBAL\Schema\Schema
();
$table
=
$schema
->
createTable
(
"no_connection"
);
$table
->
addColumn
(
'id'
,
'integer'
);
$this
->
setExpectedException
(
'\Doctrine\DBAL\DBALException'
,
null
,
$exceptionCode
);
foreach
(
$schema
->
toSql
(
$conn
->
getDatabasePlatform
())
AS
$sql
)
{
$conn
->
executeQuery
(
$sql
);
}
}
public
function
getConnectionParams
()
{
return
array
(
array
(
array
(
'user'
=>
'not_existing'
),
DBALException
::
ERROR_ACCESS_DENIED
),
array
(
array
(
'password'
=>
'really_not'
),
DBALException
::
ERROR_ACCESS_DENIED
),
array
(
array
(
'host'
=>
'localnope'
),
DBALException
::
ERROR_ACCESS_DENIED
),
);
}
}
tests/Doctrine/Tests/Mocks/DriverMock.php
View file @
46863217
...
...
@@ -69,4 +69,9 @@ class DriverMock implements \Doctrine\DBAL\Driver
{
return
;
}
public
function
convertExceptionCode
(
\Exception
$exception
)
{
return
0
;
}
}
\ 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