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
931d9a56
Unverified
Commit
931d9a56
authored
Feb 28, 2018
by
Aleksey Shmelev
Committed by
Sergei Morozov
Jun 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for connection options for the PDOSqlsrv driver
parent
041d444f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
123 additions
and
4 deletions
+123
-4
Driver.php
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php
+49
-4
DriverTest.php
...ine/Tests/DBAL/Functional/Driver/PDOSqlsrv/DriverTest.php
+74
-0
No files found.
lib/Doctrine/DBAL/Driver/PDOSqlsrv/Driver.php
View file @
931d9a56
...
...
@@ -20,6 +20,8 @@
namespace
Doctrine\DBAL\Driver\PDOSqlsrv
;
use
Doctrine\DBAL\Driver\AbstractSQLServerDriver
;
use
function
is_int
;
use
function
sprintf
;
/**
* The PDO-based Sqlsrv driver.
...
...
@@ -33,8 +35,10 @@ class Driver extends AbstractSQLServerDriver
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
[])
{
[
$driverOptions
,
$connectionOptions
]
=
$this
->
splitOptions
(
$driverOptions
);
return
new
Connection
(
$this
->
_constructPdoDsn
(
$params
),
$this
->
_constructPdoDsn
(
$params
,
$connectionOptions
),
$username
,
$password
,
$driverOptions
...
...
@@ -45,10 +49,11 @@ class Driver extends AbstractSQLServerDriver
* Constructs the Sqlsrv PDO DSN.
*
* @param array $params
* @param string[] $connectionOptions
*
* @return string The DSN.
*/
private
function
_constructPdoDsn
(
array
$params
)
private
function
_constructPdoDsn
(
array
$params
,
array
$connectionOptions
)
{
$dsn
=
'sqlsrv:server='
;
...
...
@@ -61,16 +66,56 @@ class Driver extends AbstractSQLServerDriver
}
if
(
isset
(
$params
[
'dbname'
]))
{
$
dsn
.=
';Database='
.
$params
[
'dbname'
];
$
connectionOptions
[
'Database'
]
=
$params
[
'dbname'
];
}
if
(
isset
(
$params
[
'MultipleActiveResultSets'
]))
{
$
dsn
.=
'; MultipleActiveResultSets='
.
(
$params
[
'MultipleActiveResultSets'
]
?
'true'
:
'false'
)
;
$
connectionOptions
[
'MultipleActiveResultSets'
]
=
$params
[
'MultipleActiveResultSets'
]
?
'true'
:
'false'
;
}
$dsn
.=
$this
->
getConnectionOptionsDsn
(
$connectionOptions
);
return
$dsn
;
}
/**
* Separates a connection options from a driver options
*
* @param int[]|string[] $options
* @return int[][]|string[][]
*/
private
function
splitOptions
(
array
$options
)
:
array
{
$driverOptions
=
[];
$connectionOptions
=
[];
foreach
(
$options
as
$optionKey
=>
$optionValue
)
{
if
(
is_int
(
$optionKey
))
{
$driverOptions
[
$optionKey
]
=
$optionValue
;
}
else
{
$connectionOptions
[
$optionKey
]
=
$optionValue
;
}
}
return
[
$driverOptions
,
$connectionOptions
];
}
/**
* Converts a connection options array to the DSN
*
* @param string[] $connectionOptions
*/
private
function
getConnectionOptionsDsn
(
array
$connectionOptions
)
:
string
{
$connectionOptionsDsn
=
''
;
foreach
(
$connectionOptions
as
$paramName
=>
$paramValue
)
{
$connectionOptionsDsn
.=
sprintf
(
';%s=%s'
,
$paramName
,
$paramValue
);
}
return
$connectionOptionsDsn
;
}
/**
* {@inheritdoc}
*/
...
...
tests/Doctrine/Tests/DBAL/Functional/Driver/PDOSqlsrv/Driver.php
→
tests/Doctrine/Tests/DBAL/Functional/Driver/PDOSqlsrv/Driver
Test
.php
View file @
931d9a56
...
...
@@ -2,8 +2,10 @@
namespace
Doctrine\Tests\DBAL\Functional\Driver\PDOSqlsrv
;
use
Doctrine\DBAL\Driver\Connection
as
Connection
;
use
Doctrine\DBAL\Driver\PDOSqlsrv\Driver
;
use
Doctrine\Tests\DBAL\Functional\Driver\AbstractDriverTest
;
use
PDO
;
use
function
extension_loaded
;
class
DriverTest
extends
AbstractDriverTest
...
...
@@ -16,9 +18,11 @@ class DriverTest extends AbstractDriverTest
parent
::
setUp
();
if
(
!
$this
->
_conn
->
getDriver
()
instanceof
Driver
)
{
$this
->
markTestSkipped
(
'pdo_sqlsrv only test.'
)
;
if
(
$this
->
_conn
->
getDriver
()
instanceof
Driver
)
{
return
;
}
$this
->
markTestSkipped
(
'pdo_sqlsrv only test.'
);
}
/**
...
...
@@ -36,4 +40,35 @@ class DriverTest extends AbstractDriverTest
{
return
'master'
;
}
/**
* @param int[]|string[] $driverOptions
*/
protected
function
getConnection
(
array
$driverOptions
)
:
Connection
{
return
$this
->
_conn
->
getDriver
()
->
connect
(
[
'host'
=>
$GLOBALS
[
'db_host'
],
'port'
=>
$GLOBALS
[
'db_port'
],
],
$GLOBALS
[
'db_username'
],
$GLOBALS
[
'db_password'
],
$driverOptions
);
}
public
function
testConnectionOptions
()
:
void
{
$connection
=
$this
->
getConnection
([
'APP'
=>
'APP_NAME'
]);
$result
=
$connection
->
query
(
'SELECT APP_NAME()'
)
->
fetchColumn
();
self
::
assertSame
(
'APP_NAME'
,
$result
);
}
public
function
testDriverOptions
()
:
void
{
$connection
=
$this
->
getConnection
([
PDO
::
ATTR_CASE
=>
PDO
::
CASE_UPPER
]);
self
::
assertSame
(
PDO
::
CASE_UPPER
,
$connection
->
getAttribute
(
PDO
::
ATTR_CASE
));
}
}
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