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
d4580237
Commit
d4580237
authored
Mar 09, 2017
by
Steve Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixes #990: try platform detection without database name as fallback
parent
c48effb6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
1 deletion
+95
-1
Connection.php
lib/Doctrine/DBAL/Connection.php
+43
-1
ConnectionTest.php
tests/Doctrine/Tests/DBAL/ConnectionTest.php
+26
-0
ConnectionTest.php
tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
+26
-0
No files found.
lib/Doctrine/DBAL/Connection.php
View file @
d4580237
...
...
@@ -425,9 +425,51 @@ class Connection implements DriverConnection
// If not connected, we need to connect now to determine the platform version.
if
(
null
===
$this
->
_conn
)
{
try
{
$this
->
connect
();
}
catch
(
\Exception
$originalException
)
{
if
(
empty
(
$this
->
_params
[
'dbname'
]))
{
throw
$originalException
;
}
// The database to connect to might not yet exist.
// Retry detection without database name connection parameter.
$databaseName
=
$this
->
_params
[
'dbname'
];
$this
->
_params
[
'dbname'
]
=
null
;
try
{
$this
->
connect
();
}
catch
(
\Exception
$fallbackException
)
{
// Either the platform does not support database-less connections
// or something else went wrong.
// Reset connection parameters and rethrow the original exception.
$this
->
_params
[
'dbname'
]
=
$databaseName
;
throw
$originalException
;
}
// Reset connection parameters.
$this
->
_params
[
'dbname'
]
=
$databaseName
;
$serverVersion
=
$this
->
getServerVersion
();
// Close "temporary" connection to allow connecting to the real database again.
$this
->
close
();
return
$serverVersion
;
}
}
return
$this
->
getServerVersion
();
}
/**
* Returns the database server version if the underlying driver supports it.
*
* @return string|null
*/
private
function
getServerVersion
()
{
// Automatic platform version detection.
if
(
$this
->
_conn
instanceof
ServerInfoAwareConnection
&&
!
$this
->
_conn
->
requiresQueryForServerVersion
()
...
...
tests/Doctrine/Tests/DBAL/ConnectionTest.php
View file @
d4580237
...
...
@@ -12,6 +12,7 @@ use Doctrine\DBAL\Events;
use
Doctrine\Tests\Mocks\DriverConnectionMock
;
use
Doctrine\Tests\Mocks\DriverMock
;
use
Doctrine\DBAL\Cache\ArrayStatement
;
use
Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock
;
class
ConnectionTest
extends
\Doctrine\Tests\DbalTestCase
{
...
...
@@ -712,4 +713,29 @@ class ConnectionTest extends \Doctrine\Tests\DbalTestCase
(
new
Connection
(
$this
->
params
,
$driver
))
->
executeCacheQuery
(
$query
,
$params
,
$types
,
$queryCacheProfileMock
)
);
}
/**
* @group DBAL-990
*/
public
function
testRethrowsOriginalExceptionOnDeterminingPlatformWhenConnectingToNonExistentDatabase
()
{
/** @var \Doctrine\Tests\Mocks\VersionAwarePlatformDriverMock|\PHPUnit_Framework_MockObject_MockObject $driverMock */
$driverMock
=
$this
->
createMock
(
VersionAwarePlatformDriverMock
::
class
);
$connection
=
new
Connection
(
array
(
'dbname'
=>
'foo'
),
$driverMock
);
$originalException
=
new
\Exception
(
'Original exception'
);
$fallbackException
=
new
\Exception
(
'Fallback exception'
);
$driverMock
->
expects
(
$this
->
at
(
0
))
->
method
(
'connect'
)
->
willThrowException
(
$originalException
);
$driverMock
->
expects
(
$this
->
at
(
1
))
->
method
(
'connect'
)
->
willThrowException
(
$fallbackException
);
$this
->
expectExceptionMessage
(
$originalException
->
getMessage
());
$connection
->
getDatabasePlatform
();
}
}
tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
View file @
d4580237
...
...
@@ -4,6 +4,7 @@ namespace Doctrine\Tests\DBAL\Functional;
use
Doctrine\DBAL\ConnectionException
;
use
Doctrine\DBAL\DriverManager
;
use
Doctrine\DBAL\Platforms\AbstractPlatform
;
use
Doctrine\DBAL\Types\Type
;
class
ConnectionTest
extends
\Doctrine\Tests\DbalFunctionalTestCase
...
...
@@ -272,4 +273,29 @@ class ConnectionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$connection
->
close
();
}
/**
* @group DBAL-990
*/
public
function
testDeterminesDatabasePlatformWhenConnectingToNonExistentDatabase
()
{
if
(
in_array
(
$this
->
_conn
->
getDatabasePlatform
()
->
getName
(),
[
'oracle'
,
'db2'
],
true
))
{
$this
->
markTestSkipped
(
'Platform does not support connecting without database name.'
);
}
$params
=
$this
->
_conn
->
getParams
();
$params
[
'dbname'
]
=
'foo_bar'
;
$connection
=
DriverManager
::
getConnection
(
$params
,
$this
->
_conn
->
getConfiguration
(),
$this
->
_conn
->
getEventManager
()
);
$this
->
assertInstanceOf
(
AbstractPlatform
::
class
,
$connection
->
getDatabasePlatform
());
$this
->
assertFalse
(
$connection
->
isConnected
());
$this
->
assertSame
(
$params
,
$connection
->
getParams
());
$connection
->
close
();
}
}
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