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.",
"keywords": [
"laravel",
@ -16,7 +16,7 @@
}
],
"require": {
"php": "^7.3",
"php": "^7.4|^8.0",
"illuminate/database": "^8.0"
},
"require-dev": {

View File

@ -26,6 +26,13 @@ class DB2ServiceProvider extends ServiceProvider
*/
protected $defer = false;
private const drivers = [
'pdo_ibm',
'db2_ibmi_odbc',
'db2_zos_odbc',
'db2_expressc_odbc',
];
/**
* Bootstrap the application events.
*
@ -52,19 +59,14 @@ class DB2ServiceProvider extends ServiceProvider
// 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',
])
) {
if (! isset($config['driver']) || ! in_array($config['driver'],self::drivers))
continue;
}
// 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':
@ -84,7 +86,7 @@ class DB2ServiceProvider extends ServiceProvider
$db2Connection = $connector->connect($config);
return new DB2Connection($db2Connection, $config["database"], $config["prefix"], $config);
return new DB2Connection($db2Connection,$config['database'],$config['prefix'],$config);
});
}
@ -109,6 +111,7 @@ class DB2ServiceProvider extends ServiceProvider
{
if ($this->app instanceof LaravelApplication) {
return config_path('db2.php');
} elseif ($this->app instanceof LumenApplication) {
return base_path('config/db2.php');
}

View File

@ -16,8 +16,12 @@ class IBMConnector extends DB2Connector
*/
protected function getDsn(array $config)
{
$dsn = "ibm:DRIVER={$config['driverName']};DATABASE={$config['database']};HOSTNAME={$config['host']};PORT={$config['port']};PROTOCOL=TCPIP;";
return $dsn;
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;' : '',
);
}
}

View File

@ -74,8 +74,36 @@ PDO::I5_TXN_NO_COMMIT
return [
'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' => [
'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}',
@ -150,7 +178,6 @@ return [
+ (defined('PDO::I5_ATTR_DBC_LIBL') ? [PDO::I5_ATTR_DBC_LIBL => ''] : [])
+ (defined('PDO::I5_ATTR_DBC_CURLIB') ? [PDO::I5_ATTR_DBC_CURLIB => ''] : [])
],
],
];