From b4407e6bc12a2b51ff5cd0526226b8c26ca18388 Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 6 Sep 2021 15:28:20 +1000 Subject: [PATCH] Add IBM_DB2 support --- composer.json | 4 +- src/DB2ServiceProvider.php | 185 ++++++++++++----------- src/Database/Connectors/IBMConnector.php | 26 ++-- src/config/db2.php | 181 ++++++++++++---------- 4 files changed, 215 insertions(+), 181 deletions(-) diff --git a/composer.json b/composer.json index 385c8a3..b0a37d9 100644 --- a/composer.json +++ b/composer.json @@ -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.", "keywords": [ "laravel", @@ -16,7 +16,7 @@ } ], "require": { - "php": "^7.3", + "php": "^7.4|^8.0", "illuminate/database": "^8.0" }, "require-dev": { diff --git a/src/DB2ServiceProvider.php b/src/DB2ServiceProvider.php index a3a8614..7c59ab1 100644 --- a/src/DB2ServiceProvider.php +++ b/src/DB2ServiceProvider.php @@ -19,108 +19,111 @@ use Illuminate\Support\ServiceProvider; */ class DB2ServiceProvider extends ServiceProvider { - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = false; + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; - /** - * Bootstrap the application events. - * - * @return void - */ - public function boot() - { - $configPath = __DIR__ . '/config/db2.php'; - $this->publishes([$configPath => $this->getConfigPath()], 'config'); - } + private const drivers = [ + 'pdo_ibm', + 'db2_ibmi_odbc', + 'db2_zos_odbc', + 'db2_expressc_odbc', + ]; - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - // get the configs - $conns = is_array(config('db2.connections')) ? config('db2.connections') : []; + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $configPath = __DIR__ . '/config/db2.php'; + $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 - foreach (config('database.connections') as $conn => $config) { - // 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; - } + // Add my database configurations to the default set of configurations + config(['database.connections' => array_merge($conns, config('database.connections'))]); - // Create a connector - $this->app['db']->extend($conn, function($config, $name) { - $config['name'] = $name; - switch ($config['driver']) { - case 'db2_expressc_odbc': - case 'db2_ibmi_odbc': - $connector = new ODBCConnector(); - break; + // Extend the connections with pdo_odbc and pdo_ibm drivers + foreach (config('database.connections') as $conn => $config) { - case 'db2_zos_odbc': - $connector = new ODBCZOSConnector(); - break; + // Only use configurations that feature a "odbc", "ibm" or "odbczos" driver + if (! isset($config['driver']) || ! in_array($config['driver'],self::drivers)) + continue; - case 'db2_ibmi_ibm': - default: - $connector = new IBMConnector(); - break; - } + // Create a connector + $this->app['db']->extend($conn,function($config,$name) { + $config = array_merge($config,config('db2.drivers.'.$config['driver'])); + $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( - 'queue', - function (QueueManager $queueManager) { - $queueManager->addConnector('db2_odbc', function () { - return new DB2Connector($this->app['db']); - }); + $db2Connection = $connector->connect($config); - return $queueManager; - } - ); - } + return new DB2Connection($db2Connection,$config['database'],$config['prefix'],$config); + }); + } - /** - * Get the config path - * - * @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'); - } - } + $this->app->extend( + 'queue', + function (QueueManager $queueManager) { + $queueManager->addConnector('db2_odbc', function () { + return new DB2Connector($this->app['db']); + }); - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return []; - } + return $queueManager; + } + ); + } + + /** + * Get the config path + * + * @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 []; + } } diff --git a/src/Database/Connectors/IBMConnector.php b/src/Database/Connectors/IBMConnector.php index b5df1d1..2584390 100644 --- a/src/Database/Connectors/IBMConnector.php +++ b/src/Database/Connectors/IBMConnector.php @@ -9,15 +9,19 @@ namespace Cooperl\DB2\Database\Connectors; */ class IBMConnector extends DB2Connector { - /** - * @param array $config - * - * @return string - */ - protected function getDsn(array $config) - { - $dsn = "ibm:DRIVER={$config['driverName']};DATABASE={$config['database']};HOSTNAME={$config['host']};PORT={$config['port']};PROTOCOL=TCPIP;"; - - return $dsn; - } + /** + * @param array $config + * + * @return string + */ + protected function getDsn(array $config) + { + return sprintf('ibm:DRIVER=%s;DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;%s', + $config['driverName'], + $config['database'], + $config['host'], + $config['port'], + $config['ssl'] ? 'SECURITY=SSL;' : '', + ); + } } diff --git a/src/config/db2.php b/src/config/db2.php index 95ff378..4d2e714 100644 --- a/src/config/db2.php +++ b/src/config/db2.php @@ -73,84 +73,111 @@ PDO::I5_TXN_NO_COMMIT 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' => [ - '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 => ''] : []) - ], + 'drivers' => [ + 'pdo_ibm' => [ + 'driverName' => '{IBM DB2 ODBC DRIVER}', + '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 => ''] : []) + ], + ], ];