-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from chukShirley/odbc-support
Support PDO ODBC connections
- Loading branch information
Showing
8 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace DoctrineDbalIbmi\Driver; | ||
|
||
use Doctrine\DBAL\VersionAwarePlatformDriver; | ||
use DoctrineDbalIbmi\Platform\DB2IBMiPlatform; | ||
use DoctrineDbalIbmi\Schema\DB2IBMiSchemaManager; | ||
|
||
class OdbcDriver extends AbstractDB2Driver implements VersionAwarePlatformDriver | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) | ||
{ | ||
$params['dsn'] = 'odbc:' . $params['dsn']; | ||
$username = (!is_null($username)) ? $username : $params['username']; | ||
$password = (!is_null($password)) ? $password : $params['password']; | ||
|
||
return new OdbcIBMiConnection($params, $username, $password, $driverOptions); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'odbc'; | ||
} | ||
|
||
public function createDatabasePlatformForVersion($version) | ||
{ | ||
return new DB2IBMiPlatform(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDatabase(\Doctrine\DBAL\Connection $conn) | ||
{ | ||
$params = $conn->getParams(); | ||
|
||
return $params['dbname']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDatabasePlatform() | ||
{ | ||
return new DB2IBMiPlatform(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSchemaManager(\Doctrine\DBAL\Connection $conn) | ||
{ | ||
return new DB2IBMiSchemaManager($conn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace DoctrineDbalIbmi\Driver; | ||
|
||
use Doctrine\DBAL\Driver\IBMDB2\DB2Connection; | ||
use Doctrine\DBAL\Driver\PDOConnection; | ||
|
||
/** | ||
* IBMi Db2 Connection. | ||
* More documentation about iSeries schema at https://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_72/db2/rbafzcatsqlcolumns.htm | ||
* | ||
* @author Cassiano Vailati <c.vailati@esconsulting.it> | ||
* @author James Titcumb <james@asgrim.com> | ||
*/ | ||
class OdbcIBMiConnection extends PDOConnection | ||
{ | ||
protected $driverOptions = array(); | ||
|
||
/** | ||
* @param array $params | ||
* @param string $username | ||
* @param string $password | ||
* @param array $driverOptions | ||
* | ||
* @throws \Doctrine\DBAL\Driver\IBMDB2\DB2Exception | ||
*/ | ||
public function __construct($params, $username, $password, $driverOptions = array()) | ||
{ | ||
$this->driverOptions = $driverOptions; | ||
$this->driverOptions[\PDO::ATTR_PERSISTENT] = false; | ||
if (isset($params['persistent'])) { | ||
$this->driverOptions[\PDO::ATTR_PERSISTENT] = $params['persistent']; | ||
} | ||
parent::__construct($params['dsn'], $username, $password, $this->driverOptions); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function lastInsertId($name = null) | ||
{ | ||
$sql = 'SELECT IDENTITY_VAL_LOCAL() AS VAL FROM QSYS2'.$this->getSchemaSeparatorSymbol().'QSQPTABL'; | ||
$stmt = $this->prepare($sql); | ||
$stmt->execute(); | ||
|
||
$res = $stmt->fetch(); | ||
|
||
return $res['VAL']; | ||
} | ||
|
||
/** | ||
* Returns the appropriate schema separation symbol for i5 systems. | ||
* Other systems can hardcode '.' but i5 may need '.' or '/' depending on the naming mode. | ||
* | ||
* @return string | ||
*/ | ||
public function getSchemaSeparatorSymbol() | ||
{ | ||
// if "i5 naming" is on, use '/' to separate schema and table. Otherwise use '.' | ||
if (array_key_exists('i5_naming', $this->driverOptions) && $this->driverOptions['i5_naming']) { | ||
|
||
// "i5 naming" mode requires a slash | ||
return '/'; | ||
|
||
} else { | ||
// SQL naming requires a dot | ||
return '.'; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* Retrieves ibm_db2 native resource handle. | ||
* | ||
* Could be used if part of your application is not using DBAL. | ||
* | ||
* @return resource | ||
*/ | ||
public function getWrappedResourceHandle() | ||
{ | ||
$connProperty = new \ReflectionProperty(DB2Connection::class, '_conn'); | ||
$connProperty->setAccessible(true); | ||
return $connProperty->getValue($this); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function requiresQueryForServerVersion() | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineDbalIbmiTest\Driver; | ||
|
||
use DoctrineDbalIbmi\Driver\DB2IBMiConnection; | ||
use DoctrineDbalIbmi\Driver\OdbcIBMiConnection; | ||
use DoctrineDbalIbmiTest\Bootstrap; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class OdbcIbmiConnectionTest extends TestCase | ||
{ | ||
public function testCorrectConnectionClassIsUsed() | ||
{ | ||
if (!extension_loaded('pdo')) { | ||
$this->markTestSkipped('pdo extension not loaded'); | ||
} | ||
$em = Bootstrap::getEntityManager(); | ||
|
||
$connection = $em->getConnection()->getWrappedConnection(); | ||
|
||
self::assertInstanceOf(OdbcIBMiConnection::class, $connection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters