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
97fc7ffd
Unverified
Commit
97fc7ffd
authored
Jun 13, 2020
by
Sergei Morozov
Committed by
GitHub
Jun 13, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4072 from morozov/driver-connect-exceptions
Driver::connect() should throw only driver-level exceptions
parents
b134e4af
ae8c7208
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
86 additions
and
110 deletions
+86
-110
Connection.php
src/Connection.php
+11
-3
PrimaryReadReplicaConnection.php
src/Connections/PrimaryReadReplicaConnection.php
+7
-1
Driver.php
src/Driver.php
+3
-0
Driver.php
src/Driver/Mysqli/Driver.php
+1
-6
Driver.php
src/Driver/OCI8/Driver.php
+8
-13
Driver.php
src/Driver/PDOMySql/Driver.php
+6
-14
Driver.php
src/Driver/PDOOracle/Driver.php
+6
-12
Driver.php
src/Driver/PDOPgSql/Driver.php
+24
-30
Driver.php
src/Driver/PDOSqlite/Driver.php
+6
-12
Driver.php
src/Driver/SQLAnywhere/Driver.php
+12
-16
ConnectionTest.php
tests/ConnectionTest.php
+2
-3
No files found.
src/Connection.php
View file @
97fc7ffd
...
...
@@ -10,6 +10,7 @@ use Doctrine\DBAL\Cache\CacheException;
use
Doctrine\DBAL\Cache\CachingResult
;
use
Doctrine\DBAL\Cache\QueryCacheProfile
;
use
Doctrine\DBAL\Driver\Connection
as
DriverConnection
;
use
Doctrine\DBAL\Driver\DriverException
;
use
Doctrine\DBAL\Driver\PingableConnection
;
use
Doctrine\DBAL\Driver\Result
as
DriverResult
;
use
Doctrine\DBAL\Driver\ServerInfoAwareConnection
;
...
...
@@ -342,6 +343,8 @@ class Connection implements DriverConnection
*
* @return bool TRUE if the connection was successfully established, FALSE if
* the connection is already open.
*
* @throws DBALException
*/
public
function
connect
()
{
...
...
@@ -353,7 +356,12 @@ class Connection implements DriverConnection
$user
=
$this
->
params
[
'user'
]
??
null
;
$password
=
$this
->
params
[
'password'
]
??
null
;
$this
->
_conn
=
$this
->
_driver
->
connect
(
$this
->
params
,
$user
,
$password
,
$driverOptions
);
try
{
$this
->
_conn
=
$this
->
_driver
->
connect
(
$this
->
params
,
$user
,
$password
,
$driverOptions
);
}
catch
(
DriverException
$e
)
{
throw
DBALException
::
driverException
(
$this
->
_driver
,
$e
);
}
$this
->
isConnected
=
true
;
$this
->
transactionNestingLevel
=
0
;
...
...
@@ -420,7 +428,7 @@ class Connection implements DriverConnection
if
(
$this
->
_conn
===
null
)
{
try
{
$this
->
connect
();
}
catch
(
Throwable
$originalException
)
{
}
catch
(
DBALException
$originalException
)
{
if
(
!
isset
(
$this
->
params
[
'dbname'
]))
{
throw
$originalException
;
}
...
...
@@ -432,7 +440,7 @@ class Connection implements DriverConnection
try
{
$this
->
connect
();
}
catch
(
Throwable
$fallbackException
)
{
}
catch
(
DBALException
$fallbackException
)
{
// Either the platform does not support database-less connections
// or something else went wrong.
// Reset connection parameters and rethrow the original exception.
...
...
src/Connections/PrimaryReadReplicaConnection.php
View file @
97fc7ffd
...
...
@@ -5,8 +5,10 @@ namespace Doctrine\DBAL\Connections;
use
Doctrine\Common\EventManager
;
use
Doctrine\DBAL\Configuration
;
use
Doctrine\DBAL\Connection
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Driver
;
use
Doctrine\DBAL\Driver\Connection
as
DriverConnection
;
use
Doctrine\DBAL\Driver\DriverException
;
use
Doctrine\DBAL\Driver\Result
;
use
Doctrine\DBAL\Driver\Statement
;
use
Doctrine\DBAL\Event\ConnectionEventArgs
;
...
...
@@ -230,7 +232,11 @@ class PrimaryReadReplicaConnection extends Connection
$user
=
$connectionParams
[
'user'
]
??
null
;
$password
=
$connectionParams
[
'password'
]
??
null
;
return
$this
->
_driver
->
connect
(
$connectionParams
,
$user
,
$password
,
$driverOptions
);
try
{
return
$this
->
_driver
->
connect
(
$connectionParams
,
$user
,
$password
,
$driverOptions
);
}
catch
(
DriverException
$e
)
{
throw
DBALException
::
driverException
(
$this
->
_driver
,
$e
);
}
}
/**
...
...
src/Driver.php
View file @
97fc7ffd
...
...
@@ -2,6 +2,7 @@
namespace
Doctrine\DBAL
;
use
Doctrine\DBAL\Driver\DriverException
;
use
Doctrine\DBAL\Platforms\AbstractPlatform
;
use
Doctrine\DBAL\Schema\AbstractSchemaManager
;
...
...
@@ -22,6 +23,8 @@ interface Driver
* @param mixed[] $driverOptions The driver options to use when connecting.
*
* @return \Doctrine\DBAL\Driver\Connection The database connection.
*
* @throws DriverException
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
[]);
...
...
src/Driver/Mysqli/Driver.php
View file @
97fc7ffd
...
...
@@ -2,7 +2,6 @@
namespace
Doctrine\DBAL\Driver\Mysqli
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Driver\AbstractMySQLDriver
;
class
Driver
extends
AbstractMySQLDriver
...
...
@@ -12,11 +11,7 @@ class Driver extends AbstractMySQLDriver
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
[])
{
try
{
return
new
MysqliConnection
(
$params
,
(
string
)
$username
,
(
string
)
$password
,
$driverOptions
);
}
catch
(
MysqliException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
return
new
MysqliConnection
(
$params
,
(
string
)
$username
,
(
string
)
$password
,
$driverOptions
);
}
/**
...
...
src/Driver/OCI8/Driver.php
View file @
97fc7ffd
...
...
@@ -2,7 +2,6 @@
namespace
Doctrine\DBAL\Driver\OCI8
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Driver\AbstractOracleDriver
;
use
const
OCI_NO_AUTO_COMMIT
;
...
...
@@ -17,18 +16,14 @@ class Driver extends AbstractOracleDriver
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
[])
{
try
{
return
new
OCI8Connection
(
(
string
)
$username
,
(
string
)
$password
,
$this
->
_constructDsn
(
$params
),
$params
[
'charset'
]
??
''
,
$params
[
'sessionMode'
]
??
OCI_NO_AUTO_COMMIT
,
$params
[
'persistent'
]
??
false
);
}
catch
(
OCI8Exception
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
return
new
OCI8Connection
(
(
string
)
$username
,
(
string
)
$password
,
$this
->
_constructDsn
(
$params
),
$params
[
'charset'
]
??
''
,
$params
[
'sessionMode'
]
??
OCI_NO_AUTO_COMMIT
,
$params
[
'persistent'
]
??
false
);
}
/**
...
...
src/Driver/PDOMySql/Driver.php
View file @
97fc7ffd
...
...
@@ -2,11 +2,9 @@
namespace
Doctrine\DBAL\Driver\PDOMySql
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Driver\AbstractMySQLDriver
;
use
Doctrine\DBAL\Driver\PDOConnection
;
use
PDO
;
use
PDOException
;
/**
* PDO MySql driver.
...
...
@@ -22,18 +20,12 @@ class Driver extends AbstractMySQLDriver
$driverOptions
[
PDO
::
ATTR_PERSISTENT
]
=
true
;
}
try
{
$conn
=
new
PDOConnection
(
$this
->
constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
catch
(
PDOException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
return
$conn
;
return
new
PDOConnection
(
$this
->
constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
/**
...
...
src/Driver/PDOOracle/Driver.php
View file @
97fc7ffd
...
...
@@ -2,11 +2,9 @@
namespace
Doctrine\DBAL\Driver\PDOOracle
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Driver\AbstractOracleDriver
;
use
Doctrine\DBAL\Driver\PDOConnection
;
use
PDO
;
use
PDOException
;
/**
* PDO Oracle driver.
...
...
@@ -27,16 +25,12 @@ class Driver extends AbstractOracleDriver
$driverOptions
[
PDO
::
ATTR_PERSISTENT
]
=
true
;
}
try
{
return
new
PDOConnection
(
$this
->
constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
catch
(
PDOException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
return
new
PDOConnection
(
$this
->
constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
/**
...
...
src/Driver/PDOPgSql/Driver.php
View file @
97fc7ffd
...
...
@@ -2,11 +2,9 @@
namespace
Doctrine\DBAL\Driver\PDOPgSql
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Driver\AbstractPostgreSQLDriver
;
use
Doctrine\DBAL\Driver\PDOConnection
;
use
PDO
;
use
PDOException
;
use
function
defined
;
...
...
@@ -24,35 +22,31 @@ class Driver extends AbstractPostgreSQLDriver
$driverOptions
[
PDO
::
ATTR_PERSISTENT
]
=
true
;
}
try
{
$connection
=
new
PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
if
(
defined
(
'PDO::PGSQL_ATTR_DISABLE_PREPARES'
)
&&
(
!
isset
(
$driverOptions
[
PDO
::
PGSQL_ATTR_DISABLE_PREPARES
])
||
$driverOptions
[
PDO
::
PGSQL_ATTR_DISABLE_PREPARES
]
===
true
)
)
{
$connection
->
getWrappedConnection
()
->
setAttribute
(
PDO
::
PGSQL_ATTR_DISABLE_PREPARES
,
true
);
}
/* defining client_encoding via SET NAMES to avoid inconsistent DSN support
* - the 'client_encoding' connection param only works with postgres >= 9.1
* - passing client_encoding via the 'options' param breaks pgbouncer support
*/
if
(
isset
(
$params
[
'charset'
]))
{
$connection
->
exec
(
'SET NAMES \''
.
$params
[
'charset'
]
.
'\''
);
}
return
$connection
;
}
catch
(
PDOException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
$connection
=
new
PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
if
(
defined
(
'PDO::PGSQL_ATTR_DISABLE_PREPARES'
)
&&
(
!
isset
(
$driverOptions
[
PDO
::
PGSQL_ATTR_DISABLE_PREPARES
])
||
$driverOptions
[
PDO
::
PGSQL_ATTR_DISABLE_PREPARES
]
===
true
)
)
{
$connection
->
getWrappedConnection
()
->
setAttribute
(
PDO
::
PGSQL_ATTR_DISABLE_PREPARES
,
true
);
}
/* defining client_encoding via SET NAMES to avoid inconsistent DSN support
* - the 'client_encoding' connection param only works with postgres >= 9.1
* - passing client_encoding via the 'options' param breaks pgbouncer support
*/
if
(
isset
(
$params
[
'charset'
]))
{
$connection
->
exec
(
'SET NAMES \''
.
$params
[
'charset'
]
.
'\''
);
}
return
$connection
;
}
/**
...
...
src/Driver/PDOSqlite/Driver.php
View file @
97fc7ffd
...
...
@@ -2,11 +2,9 @@
namespace
Doctrine\DBAL\Driver\PDOSqlite
;
use
Doctrine\DBAL\DBALException
;
use
Doctrine\DBAL\Driver\AbstractSQLiteDriver
;
use
Doctrine\DBAL\Driver\PDOConnection
;
use
Doctrine\DBAL\Platforms\SqlitePlatform
;
use
PDOException
;
use
function
array_merge
;
...
...
@@ -35,16 +33,12 @@ class Driver extends AbstractSQLiteDriver
unset
(
$driverOptions
[
'userDefinedFunctions'
]);
}
try
{
$connection
=
new
PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
}
catch
(
PDOException
$ex
)
{
throw
DBALException
::
driverException
(
$this
,
$ex
);
}
$connection
=
new
PDOConnection
(
$this
->
_constructPdoDsn
(
$params
),
$username
,
$password
,
$driverOptions
);
$pdo
=
$connection
->
getWrappedConnection
();
...
...
src/Driver/SQLAnywhere/Driver.php
View file @
97fc7ffd
...
...
@@ -21,22 +21,18 @@ class Driver extends AbstractSQLAnywhereDriver
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
[])
{
try
{
return
new
SQLAnywhereConnection
(
$this
->
buildDsn
(
$params
[
'host'
]
??
null
,
$params
[
'port'
]
??
null
,
$params
[
'server'
]
??
null
,
$params
[
'dbname'
]
??
null
,
$username
,
$password
,
$driverOptions
),
$params
[
'persistent'
]
??
false
);
}
catch
(
SQLAnywhereException
$e
)
{
throw
DBALException
::
driverException
(
$this
,
$e
);
}
return
new
SQLAnywhereConnection
(
$this
->
buildDsn
(
$params
[
'host'
]
??
null
,
$params
[
'port'
]
??
null
,
$params
[
'server'
]
??
null
,
$params
[
'dbname'
]
??
null
,
$username
,
$password
,
$driverOptions
),
$params
[
'persistent'
]
??
false
);
}
/**
...
...
tests/ConnectionTest.php
View file @
97fc7ffd
...
...
@@ -20,7 +20,6 @@ use Doctrine\DBAL\Logging\DebugStack;
use
Doctrine\DBAL\ParameterType
;
use
Doctrine\DBAL\Platforms\AbstractPlatform
;
use
Doctrine\DBAL\VersionAwarePlatformDriver
;
use
Exception
;
use
PHPUnit\Framework\MockObject\MockObject
;
use
PHPUnit\Framework\TestCase
;
use
stdClass
;
...
...
@@ -788,8 +787,8 @@ class ConnectionTest extends TestCase
$driverMock
=
$this
->
createMock
(
VersionAwarePlatformDriver
::
class
);
$connection
=
new
Connection
([
'dbname'
=>
'foo'
],
$driverMock
);
$originalException
=
new
Exception
(
'Original exception'
);
$fallbackException
=
new
Exception
(
'Fallback exception'
);
$originalException
=
new
DBAL
Exception
(
'Original exception'
);
$fallbackException
=
new
DBAL
Exception
(
'Fallback exception'
);
$driverMock
->
expects
(
self
::
at
(
0
))
->
method
(
'connect'
)
...
...
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