initial Commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?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
|
||||
{
|
||||
\DB::connection('db2_local')->beforeExecuting(
|
||||
function ($sql, $bindings, $connection) {
|
||||
logger()->debug('DB2 BEFORE EXECUTE', [
|
||||
'sql' => $sql,
|
||||
'bindings_count' => count($bindings),
|
||||
]);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user