Skip to content

Commit

Permalink
Merge pull request #15 from lucasmezencio/master
Browse files Browse the repository at this point in the history
Add IBM Mainframe z/OS support
  • Loading branch information
cooperl22 authored Aug 30, 2016
2 parents ed43908 + 1d08883 commit 4374796
Show file tree
Hide file tree
Showing 13 changed files with 594 additions and 377 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Simply add this code at the end of your ``app/config/database.php`` file:
*/

'ibmi' => [
'driver' => 'odbc' / 'ibm',
'driver' => 'odbc' / 'ibm' / 'odbczos',
'driverName' => '{IBM i Access ODBC Driver}' / '{iSeries Access ODBC Driver}',
// General settings
'host' => 'server',
Expand All @@ -67,6 +67,7 @@ Simply add this code at the end of your ``app/config/database.php`` file:
'database' => 'WRKRDBDIRE entry',
'prefix' => '',
'schema' => 'default schema',
'port' => 50000,
'signon' => 3,
'ssl' => 0,
'commitMode' => 2,
Expand Down
65 changes: 42 additions & 23 deletions src/Connectors/IBMConnector.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,103 @@
<?php

namespace Cooperl\Database\DB2\Connectors;

use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface;

use PDO;

/**
* Class IBMConnector
*
* @package Cooperl\Database\DB2\Connectors
*/
class IBMConnector extends Connector implements ConnectorInterface
{

/**
* @param array $config
*
* @return \PDO
*/
public function connect(array $config)
{
$dsn = $this->getDsn($config);

$options = [
PDO::I5_ATTR_DBC_SYS_NAMING => false,
PDO::I5_ATTR_COMMIT => PDO::I5_TXN_NO_COMMIT,
PDO::I5_ATTR_JOB_SORT => false
\PDO::I5_ATTR_DBC_SYS_NAMING => false,
\PDO::I5_ATTR_COMMIT => \PDO::I5_TXN_NO_COMMIT,
\PDO::I5_ATTR_JOB_SORT => false,
];

// Naming mode
switch ($config['naming']) {
case 1:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = true;
$options[\PDO::I5_ATTR_DBC_SYS_NAMING] = true;

break;
case 0:
default:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = false;
$options[\PDO::I5_ATTR_DBC_SYS_NAMING] = false;

break;
}

// Isolation mode
switch ($config['commitMode']) {
case 1:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_READ_COMMITTED;
$options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_READ_COMMITTED;

break;
case 2:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_READ_UNCOMMITTED;
$options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_READ_UNCOMMITTED;

break;
case 3:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_REPEATABLE_READ;
$options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_REPEATABLE_READ;

break;
case 4:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_SERIALIZABLE;
$options[\PDO::I5_ATTR_COMMIT] = \PDO::I5_TXN_SERIALIZABLE;

break;
case 0:
default:
$options[PDO::I5_ATTR_COMMIT] = PDO::I5_TXN_NO_COMMIT;

break;
}

// Job sort mode
switch ($config['jobSort']) {
case 1:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = true;
$options[\PDO::I5_ATTR_DBC_SYS_NAMING] = true;

break;
case 0:
default:
$options[PDO::I5_ATTR_DBC_SYS_NAMING] = false;
$options[\PDO::I5_ATTR_DBC_SYS_NAMING] = false;

break;
}

$options = $this->getOptions($config) + $options;

$connection = $this->createConnection($dsn, $config, $options);

if (isset($config['schema']))
{
if (isset($config['schema'])) {
$schema = $config['schema'];

$connection->prepare("set schema $schema")->execute();
$connection->prepare("set schema $schema")->execute();
}

return $connection;
}

protected function getDsn(array $config) {
extract($config);
$dsn = "ibm:$database";
/**
* @param array $config
*
* @return string
*/
protected function getDsn(array $config)
{
$dsn = "ibm:{$config['database']}";

return $dsn;
}

}
133 changes: 60 additions & 73 deletions src/Connectors/ODBCConnector.php
Original file line number Diff line number Diff line change
@@ -1,99 +1,86 @@
<?php

namespace Cooperl\Database\DB2\Connectors;

use Illuminate\Database\Connectors\Connector;
use Illuminate\Database\Connectors\ConnectorInterface;

/**
* Class ODBCConnector
*
* @package Cooperl\Database\DB2\Connectors
*/
class ODBCConnector extends Connector implements ConnectorInterface
{

/**
* @param array $config
*
* @return \PDO
*/
public function connect(array $config)
{
$dsn = $this->getDsn($config);

$options = $this->getOptions($config);

$connection = $this->createConnection($dsn, $config, $options);

if (isset($config['schema']))
{
if (isset($config['schema'])) {
$schema = $config['schema'];

$connection->prepare("set schema $schema")->execute();
$connection->prepare('set schema '.$schema)->execute();
}

return $connection;
}

protected function getDsn(array $config) {
extract($config);
/**
* @param array $config
*
* @return string
*/
protected function getDsn(array $config)
{
$dsnParts = [
'odbc:DRIVER=%s', 'SYSTEM=%s', 'UserID=%s', 'Password=%s', 'DATABASE=%s', 'SIGNON=%s', 'SSL=%s',
'CommitMode=%s', 'ConnectionType=%s', 'DefaultLibraries=%s', 'Naming=%s', 'UNICODESQL=%s', 'DateFormat=%s',
'DateSeperator=%s', 'Decimal=%s', 'TimeFormat=%s', 'TimeSeparator=%s', 'BLOCKFETCH=%s', 'BlockSizeKB=%s',
'AllowDataCompression=%s', 'CONCURRENCY=%s', 'LAZYCLOSE=%s', 'MaxFieldLength=%s', 'PREFETCH=%s',
'QUERYTIMEOUT=%s', 'DefaultPkgLibrary=%s', 'DefaultPackage=%s', 'ExtendedDynamic=%s', 'QAQQINILibrary=%s',
'SQDIAGCODE=%s', 'LANGUAGEID=%s', 'SORTTABLE=%s', 'SortSequence=%s', 'SORTWEIGHT=%s',
'AllowUnsupportedChar=%s', 'CCSID=%s', 'GRAPHIC=%s', 'ForceTranslation=%s', 'ALLOWPROCCALLS=%s',
'DB2SQLSTATES=%s', 'DEBUG=%s', 'TRUEAUTOCOMMIT=%s', 'CATALOGOPTIONS=%s', 'LibraryView=%s', 'ODBCRemarks=%s',
'SEARCHPATTERN=%s', 'TranslationDLL=%s', 'TranslationOption=%s', 'MAXTRACESIZE=%s', 'MultipleTraceFiles=%s',
'TRACE=%s', 'TRACEFILENAME=%s', 'ExtendedColInfo=%s',
'', // Just to add a semicolon to the end of string
];

$dsn = "odbc:"
// General settings
. "DRIVER=$driverName;"
. "SYSTEM=$host;"
. "UserID=$username;"
. "Password=$password;"
//Server settings
. "DATABASE=$database;"
. "SIGNON=$signon;"
. "SSL=$ssl;"
. "CommitMode=$commitMode;"
. "ConnectionType=$connectionType;"
. "DefaultLibraries=$defaultLibraries;"
. "Naming=$naming;"
. "UNICODESQL=$unicodeSql;"
// Format settings
. "DateFormat=$dateFormat;"
. "DateSeperator=$dateSeperator;"
. "Decimal=$decimal;"
. "TimeFormat=$timeFormat;"
. "TimeSeparator=$timeSeparator;"
// Performances settings
. "BLOCKFETCH=$blockFetch;"
. "BlockSizeKB=$blockSizeKB;"
. "AllowDataCompression=$allowDataCompression;"
. "CONCURRENCY=$concurrency;"
. "LAZYCLOSE=$lazyClose;"
. "MaxFieldLength=$maxFieldLength;"
. "PREFETCH=$prefetch;"
. "QUERYTIMEOUT=$queryTimeout;"
// Modules settings
. "DefaultPkgLibrary=$defaultPkgLibrary;"
. "DefaultPackage=$defaultPackage;"
. "ExtendedDynamic=$extendedDynamic;"
// Diagnostic settings
. "QAQQINILibrary=$QAQQINILibrary;"
. "SQDIAGCODE=$sqDiagCode;"
// Sort settings
. "LANGUAGEID=$languageId;"
. "SORTTABLE=$sortTable;"
. "SortSequence=$sortSequence;"
. "SORTWEIGHT=$sortWeight;"
// Conversion settings
. "AllowUnsupportedChar=$allowUnsupportedChar;"
. "CCSID=$ccsid;"
. "GRAPHIC=$graphic;"
. "ForceTranslation=$forceTranslation;"
// Other settings
. "ALLOWPROCCALLS=$allowProcCalls;"
. "DB2SQLSTATES=$DB2SqlStates;"
. "DEBUG=$debug;"
. "TRUEAUTOCOMMIT=$trueAutoCommit;"
. "CATALOGOPTIONS=$catalogOptions;"
. "LibraryView=$libraryView;"
. "ODBCRemarks=$ODBCRemarks;"
. "SEARCHPATTERN=$searchPattern;"
. "TranslationDLL=$translationDLL;"
. "TranslationOption=$translationOption;"
. "MAXTRACESIZE=$maxTraceSize;"
. "MultipleTraceFiles=$multipleTraceFiles;"
. "TRACE=$trace;"
. "TRACEFILENAME=$traceFilename;"
. "ExtendedColInfo=$extendedColInfo;"
;
$dsnConfig = [
// General settings
$config['driverName'], $config['host'], $config['username'], $config['password'],
//Server settings
$config['database'], $config['signon'], $config['ssl'], $config['commitMode'], $config['connectionType'],
$config['defaultLibraries'], $config['naming'], $config['unicodeSql'],
// Format settings
$config['dateFormat'], $config['dateSeperator'], $config['decimal'], $config['timeFormat'],
$config['timeSeparator'],
// Performances settings
$config['blockFetch'], $config['blockSizeKB'], $config['allowDataCompression'], $config['concurrency'],
$config['lazyClose'], $config['maxFieldLength'], $config['prefetch'], $config['queryTimeout'],
// Modules settings
$config['defaultPkgLibrary'], $config['defaultPackage'], $config['extendedDynamic'],
// Diagnostic settings
$config['QAQQINILibrary'], $config['sqDiagCode'],
// Sort settings
$config['languageId'], $config['sortTable'], $config['sortSequence'], $config['sortWeight'],
// Conversion settings
$config['allowUnsupportedChar'], $config['ccsid'], $config['graphic'], $config['forceTranslation'],
// Other settings
$config['allowProcCalls'], $config['DB2SqlStates'], $config['debug'], $config['trueAutoCommit'],
$config['catalogOptions'], $config['libraryView'], $config['ODBCRemarks'], $config['searchPattern'],
$config['translationDLL'], $config['translationOption'], $config['maxTraceSize'],
$config['multipleTraceFiles'], $config['trace'], $config['traceFilename'], $config['extendedColInfo'],
];

return $dsn;
return sprintf(implode(';', $dsnParts), ...$dsnConfig);
}

}
40 changes: 40 additions & 0 deletions src/Connectors/ODBCZOSConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Cooperl\Database\DB2\Connectors;

/**
* Class ODBCZOSConnector
*
* @package Cooperl\Database\DB2\Connectors
*/
class ODBCZOSConnector extends ODBCConnector
{
/**
* @param array $config
*
* @return string
*/
protected function getDsn(array $config)
{
$dsnParts = [
'odbc:DRIVER={IBM DB2 ODBC DRIVER}',
'Database=%s',
'Hostname=%s',
'Port=%s',
'Protocol=TCPIP',
'Uid=%s',
'Pwd=%s',
'', // Just to add a semicolon to the end of string
];

$dsnConfig = [
$config['database'],
$config['host'],
$config['port'],
$config['username'],
$config['password'],
];

return sprintf(implode(';', $dsnParts), ...$dsnConfig);
}
}
Loading

0 comments on commit 4374796

Please sign in to comment.