reactiveData/sys/sql_bridge.php
Frederico Falcao 14f96e78f9 IMPROVEMENT: bring Python trigger support to parity with PHP.
Add SQL bridge, support functions, cron jobs, error handling, and PyPI version pinning so Python triggers match PHP capabilities.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-28 00:13:18 +03:00

55 lines
1.8 KiB
PHP

#!/usr/bin/env php
<?php
/**
* CLI bridge so sandboxed Python can call the same SQL helpers as PHP (sys.php).
* Reads JSON from stdin, writes JSON to stdout, errors to stderr.
*/
if (php_sapi_name() !== 'cli') {
fwrite(STDERR, "CLI only\n");
exit(1);
}
require_once dirname(__DIR__) . '/.env.php';
require_once __DIR__ . '/sys.php';
$input = json_decode(file_get_contents('php://stdin'), true);
if (!is_array($input) || empty($input['action'])) {
fwrite(STDERR, json_encode(['error' => 'Invalid input']));
exit(1);
}
$dbHost = $input['db_host'] ?? null;
$dbUser = $input['db_user'] ?? null;
$dbPass = $input['db_pass'] ?? null;
$dbName = $input['db_name'] ?? null;
try {
switch ($input['action']) {
case 'sql':
$result = sql($input['query'], $dbHost, $dbUser, $dbPass, $dbName);
echo json_encode(['result' => $result]);
break;
case 'sql_read':
$result = sql_read($input['query'], $dbHost, $dbUser, $dbPass, $dbName);
echo json_encode(['result' => $result]);
break;
case 'sql_write':
$result = sql_write($input['query'], $dbHost, $dbUser, $dbPass, $dbName);
echo json_encode(['result' => $result]);
break;
case 'sql_read_and_hydrate':
$result = sql_read_and_hydrate($input['query']);
echo json_encode(['result' => $result]);
break;
case 'get_tbl_primary_key_col_name':
$result = getTblPrimaryKeyColName($input['table']);
echo json_encode(['result' => $result]);
break;
default:
throw new Exception('Unknown action: ' . $input['action']);
}
} catch (Throwable $e) {
fwrite(STDERR, json_encode(['error' => $e->getMessage()]));
exit(1);
}