db2 driver updates

This commit is contained in:
Mark Gregory
2026-11-11 13:15:01 +00:00
parent f5ddf38abc
commit 76037ff72b
9 changed files with 656 additions and 13 deletions
+26 -11
View File
@@ -11,7 +11,7 @@ class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
DB::extend('odbc', function (array $config, string $name) {
DB::extend('odbc', function (array $config, string $name): Db2Connection {
$pdo = new PDO(
'odbc:' . ($config['dsn'] ?? 'MEDIUK'),
$config['username'] ?? '',
@@ -22,16 +22,7 @@ class AppServiceProvider extends ServiceProvider
]
);
$schema = $config['schema'] ?? null;
if ($schema) {
if (!preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/', $schema)) {
throw new \InvalidArgumentException('Invalid Db2 schema');
}
$pdo->exec('SET CURRENT SCHEMA ' . $schema);
}
$config['schema'] = $this->resolveDb2Schema($pdo, $config['schema'] ?? null);
$config['name'] = $name;
return new Db2Connection(
@@ -43,6 +34,30 @@ class AppServiceProvider extends ServiceProvider
});
}
/**
* Apply an explicit schema and read the effective schema for catalog queries.
*/
private function resolveDb2Schema(PDO $pdo, ?string $configuredSchema): string
{
$schema = trim($configuredSchema ?? '');
if ($schema !== '') {
if (! preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/D', $schema)) {
throw new \InvalidArgumentException('Invalid Db2 schema');
}
$pdo->exec('SET CURRENT SCHEMA '.$schema);
return strtoupper($schema);
}
$currentSchema = $pdo->query('VALUES CURRENT SCHEMA')->fetchColumn();
if (! is_string($currentSchema) || trim($currentSchema) === '') {
throw new \RuntimeException('Unable to resolve the current Db2 schema.');
}
return rtrim($currentSchema);
}
public function boot(): void
{
}
@@ -0,0 +1,49 @@
<?php
namespace App\Providers;
use App\Database\Db2\Db2Connection;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\DB;
use PDO;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
DB::extend('odbc', function (array $config, string $name) {
$pdo = new PDO(
'odbc:' . ($config['dsn'] ?? 'MEDIUK'),
$config['username'] ?? '',
$config['password'] ?? '',
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
]
);
$schema = $config['schema'] ?? null;
if ($schema) {
if (!preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/', $schema)) {
throw new \InvalidArgumentException('Invalid Db2 schema');
}
$pdo->exec('SET CURRENT SCHEMA ' . $schema);
}
$config['name'] = $name;
return new Db2Connection(
$pdo,
$config['database'] ?? '',
$config['prefix'] ?? '',
$config
);
});
}
public function boot(): void
{
}
}