Add IBM_DB2 support

This commit is contained in:
Deon George 2021-09-06 15:28:20 +10:00
parent fed6ccfc27
commit b4407e6bc1
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
4 changed files with 215 additions and 181 deletions

View File

@ -1,5 +1,5 @@
{ {
"name": "cooperl/laravel-db2", "name": "leenooks/laravel-db2",
"description": "laravel-db2 is a simple DB2 service provider for Laravel. It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.", "description": "laravel-db2 is a simple DB2 service provider for Laravel. It provides DB2 Connection by extending the Illuminate Database component of the laravel framework.",
"keywords": [ "keywords": [
"laravel", "laravel",
@ -16,7 +16,7 @@
} }
], ],
"require": { "require": {
"php": "^7.3", "php": "^7.4|^8.0",
"illuminate/database": "^8.0" "illuminate/database": "^8.0"
}, },
"require-dev": { "require-dev": {

View File

@ -19,108 +19,111 @@ use Illuminate\Support\ServiceProvider;
*/ */
class DB2ServiceProvider extends ServiceProvider class DB2ServiceProvider extends ServiceProvider
{ {
/** /**
* Indicates if loading of the provider is deferred. * Indicates if loading of the provider is deferred.
* *
* @var bool * @var bool
*/ */
protected $defer = false; protected $defer = false;
/** private const drivers = [
* Bootstrap the application events. 'pdo_ibm',
* 'db2_ibmi_odbc',
* @return void 'db2_zos_odbc',
*/ 'db2_expressc_odbc',
public function boot() ];
{
$configPath = __DIR__ . '/config/db2.php';
$this->publishes([$configPath => $this->getConfigPath()], 'config');
}
/** /**
* Register the service provider. * Bootstrap the application events.
* *
* @return void * @return void
*/ */
public function register() public function boot()
{ {
// get the configs $configPath = __DIR__ . '/config/db2.php';
$conns = is_array(config('db2.connections')) ? config('db2.connections') : []; $this->publishes([$configPath => $this->getConfigPath()], 'config');
}
// Add my database configurations to the default set of configurations /**
config(['database.connections' => array_merge($conns, config('database.connections'))]); * Register the service provider.
*
* @return void
*/
public function register()
{
// get the configs
$conns = is_array(config('db2.connections')) ? config('db2.connections') : [];
// Extend the connections with pdo_odbc and pdo_ibm drivers // Add my database configurations to the default set of configurations
foreach (config('database.connections') as $conn => $config) { config(['database.connections' => array_merge($conns, config('database.connections'))]);
// Only use configurations that feature a "odbc", "ibm" or "odbczos" driver
if (!isset($config['driver']) || !in_array($config['driver'], [
'db2_ibmi_odbc',
'db2_ibmi_ibm',
'db2_zos_odbc',
'db2_expressc_odbc',
])
) {
continue;
}
// Create a connector // Extend the connections with pdo_odbc and pdo_ibm drivers
$this->app['db']->extend($conn, function($config, $name) { foreach (config('database.connections') as $conn => $config) {
$config['name'] = $name;
switch ($config['driver']) {
case 'db2_expressc_odbc':
case 'db2_ibmi_odbc':
$connector = new ODBCConnector();
break;
case 'db2_zos_odbc': // Only use configurations that feature a "odbc", "ibm" or "odbczos" driver
$connector = new ODBCZOSConnector(); if (! isset($config['driver']) || ! in_array($config['driver'],self::drivers))
break; continue;
case 'db2_ibmi_ibm': // Create a connector
default: $this->app['db']->extend($conn,function($config,$name) {
$connector = new IBMConnector(); $config = array_merge($config,config('db2.drivers.'.$config['driver']));
break; $config['name'] = $name;
} switch ($config['driver']) {
case 'db2_expressc_odbc':
case 'db2_ibmi_odbc':
$connector = new ODBCConnector();
break;
$db2Connection = $connector->connect($config); case 'db2_zos_odbc':
$connector = new ODBCZOSConnector();
break;
return new DB2Connection($db2Connection, $config["database"], $config["prefix"], $config); case 'db2_ibmi_ibm':
}); default:
} $connector = new IBMConnector();
break;
}
$this->app->extend( $db2Connection = $connector->connect($config);
'queue',
function (QueueManager $queueManager) {
$queueManager->addConnector('db2_odbc', function () {
return new DB2Connector($this->app['db']);
});
return $queueManager; return new DB2Connection($db2Connection,$config['database'],$config['prefix'],$config);
} });
); }
}
/** $this->app->extend(
* Get the config path 'queue',
* function (QueueManager $queueManager) {
* @return string $queueManager->addConnector('db2_odbc', function () {
*/ return new DB2Connector($this->app['db']);
protected function getConfigPath() });
{
if ($this->app instanceof LaravelApplication) {
return config_path('db2.php');
} elseif ($this->app instanceof LumenApplication) {
return base_path('config/db2.php');
}
}
/** return $queueManager;
* Get the services provided by the provider. }
* );
* @return array }
*/
public function provides() /**
{ * Get the config path
return []; *
} * @return string
*/
protected function getConfigPath()
{
if ($this->app instanceof LaravelApplication) {
return config_path('db2.php');
} elseif ($this->app instanceof LumenApplication) {
return base_path('config/db2.php');
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [];
}
} }

View File

@ -9,15 +9,19 @@ namespace Cooperl\DB2\Database\Connectors;
*/ */
class IBMConnector extends DB2Connector class IBMConnector extends DB2Connector
{ {
/** /**
* @param array $config * @param array $config
* *
* @return string * @return string
*/ */
protected function getDsn(array $config) protected function getDsn(array $config)
{ {
$dsn = "ibm:DRIVER={$config['driverName']};DATABASE={$config['database']};HOSTNAME={$config['host']};PORT={$config['port']};PROTOCOL=TCPIP;"; return sprintf('ibm:DRIVER=%s;DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;%s',
$config['driverName'],
return $dsn; $config['database'],
} $config['host'],
$config['port'],
$config['ssl'] ? 'SECURITY=SSL;' : '',
);
}
} }

View File

@ -73,84 +73,111 @@ PDO::I5_TXN_NO_COMMIT
return [ return [
'connections' => [ 'connections' => [
'db2' => [
'driver' => 'pdo_ibm',
// or '{iSeries Access ODBC Driver}' '{IBM i Access ODBC Driver 64-bit}'
'host' => 'server',
'username' => '',
'password' => '',
'database' => 'WRKRDBDIRE entry',
'prefix' => '',
'schema' => 'default schema',
'port' => 50000,
'date_format' => 'Y-m-d H:i:s',
'ssl' => FALSE,
],
],
'ibmi' => [ 'drivers' => [
'driver' => 'db2_ibmi_odbc', 'pdo_ibm' => [
// or 'db2_ibmi_ibm' / 'db2_zos_odbc' / 'db2_expressc_odbc 'driverName' => '{IBM DB2 ODBC DRIVER}',
'driverName' => '{IBM i Access ODBC Driver}', 'options' => [
// or '{iSeries Access ODBC Driver}' '{IBM i Access ODBC Driver 64-bit}' PDO::ATTR_CASE => PDO::CASE_LOWER,
'host' => 'server', PDO::ATTR_PERSISTENT => false
'username' => '', ]
'password' => '', + (defined('PDO::I5_ATTR_DBC_SYS_NAMING') ? [PDO::I5_ATTI5_ATTR_DBC_SYS_NAMINGR_COMMIT => false] : [])
'database' => 'WRKRDBDIRE entry', + (defined('PDO::I5_ATTR_COMMIT') ? [PDO::I5_ATTR_COMMIT => PDO::I5_TXN_NO_COMMIT] : [])
'prefix' => '', + (defined('PDO::I5_ATTR_JOB_SORT') ? [PDO::I5_ATTR_JOB_SORT => false] : [])
'schema' => 'default schema', + (defined('PDO::I5_ATTR_DBC_LIBL') ? [PDO::I5_ATTR_DBC_LIBL => ''] : [])
'port' => 50000, + (defined('PDO::I5_ATTR_DBC_CURLIB') ? [PDO::I5_ATTR_DBC_CURLIB => ''] : [])
'date_format' => 'Y-m-d H:i:s', ],
'odbc_keywords' => [
'SIGNON' => 3,
'SSL' => 0,
'CommitMode' => 2,
'ConnectionType' => 0,
'DefaultLibraries' => '',
'Naming' => 0,
'UNICODESQL' => 0,
'DateFormat' => 5,
'DateSeperator' => 0,
'Decimal' => 0,
'TimeFormat' => 0,
'TimeSeparator' => 0,
'TimestampFormat' => 0,
'ConvertDateTimeToChar' => 0,
'BLOCKFETCH' => 1,
'BlockSizeKB' => 32,
'AllowDataCompression' => 1,
'CONCURRENCY' => 0,
'LAZYCLOSE' => 0,
'MaxFieldLength' => 15360,
'PREFETCH' => 0,
'QUERYTIMEOUT' => 1,
'DefaultPkgLibrary' => 'QGPL',
'DefaultPackage' => 'A /DEFAULT(IBM),2,0,1,0',
'ExtendedDynamic' => 0,
'QAQQINILibrary' => '',
'SQDIAGCODE' => '',
'LANGUAGEID' => 'ENU',
'SORTTABLE' => '',
'SortSequence' => 0,
'SORTWEIGHT' => 0,
'AllowUnsupportedChar' => 0,
'CCSID' => 819,
'GRAPHIC' => 0,
'ForceTranslation' => 0,
'ALLOWPROCCALLS' => 0,
'DB2SQLSTATES' => 0,
'DEBUG' => 0,
'TRUEAUTOCOMMIT' => 0,
'CATALOGOPTIONS' => 3,
'LibraryView' => 0,
'ODBCRemarks' => 0,
'SEARCHPATTERN' => 1,
'TranslationDLL' => '',
'TranslationOption' => 0,
'MAXTRACESIZE' => 0,
'MultipleTraceFiles' => 1,
'TRACE' => 0,
'TRACEFILENAME' => '',
'ExtendedColInfo' => 0,
],
'options' => [
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_PERSISTENT => false
]
+ (defined('PDO::I5_ATTR_DBC_SYS_NAMING') ? [PDO::I5_ATTI5_ATTR_DBC_SYS_NAMINGR_COMMIT => false] : [])
+ (defined('PDO::I5_ATTR_COMMIT') ? [PDO::I5_ATTR_COMMIT => PDO::I5_TXN_NO_COMMIT] : [])
+ (defined('PDO::I5_ATTR_JOB_SORT') ? [PDO::I5_ATTR_JOB_SORT => false] : [])
+ (defined('PDO::I5_ATTR_DBC_LIBL') ? [PDO::I5_ATTR_DBC_LIBL => ''] : [])
+ (defined('PDO::I5_ATTR_DBC_CURLIB') ? [PDO::I5_ATTR_DBC_CURLIB => ''] : [])
],
], 'odbc_db2' => [
'driver' => 'db2_ibmi_odbc',
// or 'db2_ibmi_ibm' / 'db2_zos_odbc' / 'db2_expressc_odbc
'driverName' => '{IBM i Access ODBC Driver}',
// or '{iSeries Access ODBC Driver}' '{IBM i Access ODBC Driver 64-bit}'
'host' => 'server',
'username' => '',
'password' => '',
'database' => 'WRKRDBDIRE entry',
'prefix' => '',
'schema' => 'default schema',
'port' => 50000,
'date_format' => 'Y-m-d H:i:s',
'odbc_keywords' => [
'SIGNON' => 3,
'SSL' => 0,
'CommitMode' => 2,
'ConnectionType' => 0,
'DefaultLibraries' => '',
'Naming' => 0,
'UNICODESQL' => 0,
'DateFormat' => 5,
'DateSeperator' => 0,
'Decimal' => 0,
'TimeFormat' => 0,
'TimeSeparator' => 0,
'TimestampFormat' => 0,
'ConvertDateTimeToChar' => 0,
'BLOCKFETCH' => 1,
'BlockSizeKB' => 32,
'AllowDataCompression' => 1,
'CONCURRENCY' => 0,
'LAZYCLOSE' => 0,
'MaxFieldLength' => 15360,
'PREFETCH' => 0,
'QUERYTIMEOUT' => 1,
'DefaultPkgLibrary' => 'QGPL',
'DefaultPackage' => 'A /DEFAULT(IBM),2,0,1,0',
'ExtendedDynamic' => 0,
'QAQQINILibrary' => '',
'SQDIAGCODE' => '',
'LANGUAGEID' => 'ENU',
'SORTTABLE' => '',
'SortSequence' => 0,
'SORTWEIGHT' => 0,
'AllowUnsupportedChar' => 0,
'CCSID' => 819,
'GRAPHIC' => 0,
'ForceTranslation' => 0,
'ALLOWPROCCALLS' => 0,
'DB2SQLSTATES' => 0,
'DEBUG' => 0,
'TRUEAUTOCOMMIT' => 0,
'CATALOGOPTIONS' => 3,
'LibraryView' => 0,
'ODBCRemarks' => 0,
'SEARCHPATTERN' => 1,
'TranslationDLL' => '',
'TranslationOption' => 0,
'MAXTRACESIZE' => 0,
'MultipleTraceFiles' => 1,
'TRACE' => 0,
'TRACEFILENAME' => '',
'ExtendedColInfo' => 0,
],
'options' => [
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_PERSISTENT => false
]
+ (defined('PDO::I5_ATTR_DBC_SYS_NAMING') ? [PDO::I5_ATTI5_ATTR_DBC_SYS_NAMINGR_COMMIT => false] : [])
+ (defined('PDO::I5_ATTR_COMMIT') ? [PDO::I5_ATTR_COMMIT => PDO::I5_TXN_NO_COMMIT] : [])
+ (defined('PDO::I5_ATTR_JOB_SORT') ? [PDO::I5_ATTR_JOB_SORT => false] : [])
+ (defined('PDO::I5_ATTR_DBC_LIBL') ? [PDO::I5_ATTR_DBC_LIBL => ''] : [])
+ (defined('PDO::I5_ATTR_DBC_CURLIB') ? [PDO::I5_ATTR_DBC_CURLIB => ''] : [])
],
],
]; ];