100 lines
3.4 KiB
PHP
Executable File
100 lines
3.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\M3\Services;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
/**
|
|
* PostgreSQL replacement for the old IBM i Toolkit program-call transport.
|
|
*
|
|
* The legacy M3 code invokes IBM i programs after writing PostgreSQL-backed
|
|
* staging data. Core no longer connects to DB2. These requests are persisted
|
|
* so a PostgreSQL-native worker/integration can process them without making
|
|
* the web request depend on IBM i drivers.
|
|
*/
|
|
class PostgresProgramBridge
|
|
{
|
|
private string $errorCode = '';
|
|
|
|
private string $errorMessage = '';
|
|
|
|
public function setOptions(array $options): self
|
|
{
|
|
return $this;
|
|
}
|
|
|
|
public function AddParameterChar(string $io, int $size, string $comment = '', string $name = '', mixed $value = null, mixed ...$rest): array
|
|
{
|
|
return compact('io', 'size', 'comment', 'name', 'value');
|
|
}
|
|
|
|
public function AddParameterPackDec(string $io, int $length, int $scale, string $comment = '', string $name = '', mixed $value = null, mixed ...$rest): array
|
|
{
|
|
return compact('io', 'length', 'scale', 'comment', 'name', 'value');
|
|
}
|
|
|
|
public function AddParameterZoned(string $io, int $length, int $scale, string $comment = '', string $name = '', mixed $value = null, mixed ...$rest): array
|
|
{
|
|
return compact('io', 'length', 'scale', 'comment', 'name', 'value');
|
|
}
|
|
|
|
public function PgmCall(string $program, ?string $library = null, array $parameters = [], array $returnParameters = [], array $options = []): array|false
|
|
{
|
|
return $this->queue('program', $program, $library, [
|
|
'parameters' => $parameters,
|
|
'return_parameters' => $returnParameters,
|
|
'options' => $options,
|
|
]);
|
|
}
|
|
|
|
public function CLCommand(string $command): array|false
|
|
{
|
|
return $this->queue('command', $command, null, []);
|
|
}
|
|
|
|
public function getErrorCode(): string
|
|
{
|
|
return $this->errorCode;
|
|
}
|
|
|
|
public function getErrorMsg(): string
|
|
{
|
|
return $this->errorMessage;
|
|
}
|
|
|
|
private function queue(string $type, string $operation, ?string $library, array $payload): array|false
|
|
{
|
|
try {
|
|
$record = [
|
|
'type' => $type,
|
|
'operation' => $operation,
|
|
'library' => $library,
|
|
'payload' => json_encode($payload, JSON_THROW_ON_ERROR),
|
|
'requested_by' => function_exists('m3_user') ? m3_user() : null,
|
|
'status' => 'pending',
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
];
|
|
|
|
if (Schema::connection(config('m3.connection', 'pgsql'))->hasTable('m3_program_requests')) {
|
|
$id = DB::connection(config('m3.connection', 'pgsql'))
|
|
->table('m3_program_requests')
|
|
->insertGetId($record);
|
|
} else {
|
|
Log::warning('M3 PostgreSQL program bridge table is not migrated; request logged only.', $record);
|
|
$id = null;
|
|
}
|
|
|
|
return ['success' => true, 'queued' => true, 'request_id' => $id, 'io_param' => []];
|
|
} catch (\Throwable $e) {
|
|
$this->errorCode = (string) $e->getCode();
|
|
$this->errorMessage = $e->getMessage();
|
|
Log::error('Unable to queue M3 program request.', ['exception' => $e]);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|