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
747b0420
Unverified
Commit
747b0420
authored
Jun 11, 2020
by
Sergei Morozov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce the IBM DB2 DSN value object
parent
27e57e41
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
130 additions
and
23 deletions
+130
-23
DB2Driver.php
lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php
+10
-23
DataSourceName.php
lib/Doctrine/DBAL/Driver/IBMDB2/DataSourceName.php
+75
-0
DataSourceNameTest.php
.../Doctrine/Tests/DBAL/Driver/IBMDB2/DataSourceNameTest.php
+45
-0
No files found.
lib/Doctrine/DBAL/Driver/IBMDB2/DB2Driver.php
View file @
747b0420
...
...
@@ -4,8 +4,6 @@ namespace Doctrine\DBAL\Driver\IBMDB2;
use
Doctrine\DBAL\Driver\AbstractDB2Driver
;
use
function
strpos
;
/**
* IBM DB2 Driver.
*/
...
...
@@ -16,27 +14,16 @@ class DB2Driver extends AbstractDB2Driver
*/
public
function
connect
(
array
$params
,
$username
=
null
,
$password
=
null
,
array
$driverOptions
=
[])
{
if
(
!
isset
(
$params
[
'protocol'
]))
{
$params
[
'protocol'
]
=
'TCPIP'
;
}
if
(
strpos
(
$params
[
'dbname'
],
'='
)
===
false
)
{
// if the host isn't localhost, use extended connection params
$params
[
'dbname'
]
=
'DRIVER={IBM DB2 ODBC DRIVER}'
.
';DATABASE='
.
$params
[
'dbname'
]
.
';HOSTNAME='
.
$params
[
'host'
]
.
';PROTOCOL='
.
$params
[
'protocol'
]
.
';UID='
.
$username
.
';PWD='
.
$password
.
';'
;
if
(
isset
(
$params
[
'port'
]))
{
$params
[
'dbname'
]
.=
'PORT='
.
$params
[
'port'
];
}
$username
=
null
;
$password
=
null
;
}
return
new
DB2Connection
(
$params
,
(
string
)
$username
,
(
string
)
$password
,
$driverOptions
);
$params
[
'user'
]
=
$username
;
$params
[
'password'
]
=
$password
;
$params
[
'dbname'
]
=
DataSourceName
::
fromConnectionParameters
(
$params
)
->
toString
();
return
new
DB2Connection
(
$params
,
(
string
)
$username
,
(
string
)
$password
,
$driverOptions
);
}
/**
...
...
lib/Doctrine/DBAL/Driver/IBMDB2/DataSourceName.php
0 → 100644
View file @
747b0420
<?php
declare
(
strict_types
=
1
);
namespace
Doctrine\DBAL\Driver\IBMDB2
;
use
function
implode
;
use
function
sprintf
;
use
function
strpos
;
/**
* IBM DB2 DSN
*/
final
class
DataSourceName
{
/** @var string */
private
$string
;
private
function
__construct
(
string
$string
)
{
$this
->
string
=
$string
;
}
public
function
toString
()
:
string
{
return
$this
->
string
;
}
/**
* Creates the object from an array representation
*
* @param array<string,mixed> $params
*/
public
static
function
fromArray
(
array
$params
)
:
self
{
$chunks
=
[];
foreach
(
$params
as
$key
=>
$value
)
{
$chunks
[]
=
sprintf
(
'%s=%s'
,
$key
,
$value
);
}
return
new
self
(
implode
(
';'
,
$chunks
));
}
/**
* Creates the object from the given DBAL connection parameters.
*
* @param array<string,mixed> $params
*/
public
static
function
fromConnectionParameters
(
array
$params
)
:
self
{
if
(
isset
(
$params
[
'dbname'
])
&&
strpos
(
$params
[
'dbname'
],
'='
)
!==
false
)
{
return
new
self
(
$params
[
'dbname'
]);
}
$dsnParams
=
[];
foreach
([
'host'
=>
'HOSTNAME'
,
'port'
=>
'PORT'
,
'protocol'
=>
'PROTOCOL'
,
'dbname'
=>
'DATABASE'
,
'user'
=>
'UID'
,
'password'
=>
'PWD'
,
]
as
$dbalParam
=>
$dsnParam
)
{
if
(
!
isset
(
$params
[
$dbalParam
]))
{
continue
;
}
$dsnParams
[
$dsnParam
]
=
$params
[
$dbalParam
];
}
return
self
::
fromArray
(
$dsnParams
);
}
}
tests/Doctrine/Tests/DBAL/Driver/IBMDB2/DataSourceNameTest.php
0 → 100644
View file @
747b0420
<?php
namespace
Doctrine\Tests\DBAL\Driver\IBMDB2
;
use
Doctrine\DBAL\Driver\IBMDB2\DataSourceName
;
use
PHPUnit\Framework\TestCase
;
class
DataSourceNameTest
extends
TestCase
{
/**
* @param mixed[] $params
*
* @dataProvider connectionParametersProvider
*/
public
function
testFromConnectionParameters
(
array
$params
,
string
$expected
)
:
void
{
$dsn
=
DataSourceName
::
fromConnectionParameters
(
$params
);
$this
->
assertSame
(
$expected
,
$dsn
->
toString
());
}
/**
* @return iterable<string,array<int,mixed>>
*/
public
static
function
connectionParametersProvider
()
:
iterable
{
yield
'empty-params'
=>
[[],
''
];
yield
'cataloged-database'
=>
[
[
'host'
=>
'localhost'
,
'port'
=>
50000
,
'dbname'
=>
'doctrine'
,
'user'
=>
'db2inst1'
,
'password'
=>
'Passw0rd'
,
],
'HOSTNAME=localhost;PORT=50000;DATABASE=doctrine;UID=db2inst1;PWD=Passw0rd'
,
];
yield
'uncataloged-database'
=>
[
[
'dbname'
=>
'HOSTNAME=localhost;PORT=50000;DATABASE=doctrine;UID=db2inst1;PWD=Passw0rd'
],
'HOSTNAME=localhost;PORT=50000;DATABASE=doctrine;UID=db2inst1;PWD=Passw0rd'
,
];
}
}
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