handling VEC_ vector embeddings for LLM compatibility

This commit is contained in:
git 2025-08-02 10:31:39 +01:00
parent 58540955e6
commit ec6d8033f4

View File

@ -182,15 +182,40 @@ function runJavascriptCodeTrigger($functionName, $activeTable, $row) {
function updateDatabaseRow($tableName, $originalRow, $newRowValue) {
if (empty($originalRow) || empty($newRowValue)) return;
$pkColsName = getTblPrimaryKeyColName($tableName);
if (empty($pkColsName)) {echo redText("ERROR: No PRIMARY KEY foudn for table: $tableName\n"); return;}
if (empty($pkColsName)) { echo redText("ERROR: No PRIMARY KEY found for table: $tableName\n"); return; }
$pkColsValues = array_map(fn($cName) => $originalRow[$cName], $pkColsName);
$setStatements = [];
foreach ($newRowValue as $k => $v) {
$value = is_numeric($v) ? $v : "'" . str_replace("'", "''", is_string($v)?$v:json_encode($v)) . "'";
// Skip unchanged values (strict for scalars; JSON-compare for arrays)
if (array_key_exists($k, $originalRow)) {
$old = $originalRow[$k];
$equal = (is_array($v) || is_array($old))
? json_encode($v) === json_encode($old)
: $v === $old;
if ($equal) continue;
}
// Special case: columns ending in `_VEC` -> VEC_FromText('<json>')
if (function_exists('str_ends_with') ? str_ends_with($k, '_VEC') : preg_match('/_VEC$/', $k)) {
// Accept arrays/objects or already-JSON strings
$jsonText = is_string($v) ? $v : json_encode($v, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$jsonText = str_replace("'", "''", $jsonText);
$value = "VEC_FromText('{$jsonText}')";
} else {
// Default quoting/encoding
$value = is_numeric($v)
? $v
: "'" . str_replace("'", "''", is_string($v) ? $v : json_encode($v)) . "'";
}
$setStatements[] = "$k = $value";
}
// Nothing changed -> no UPDATE
if (empty($setStatements)) return;
$whereStatements = [];
foreach ($pkColsName as $k) {
$val = is_numeric($originalRow[$k]) ? $originalRow[$k] : "'" . $originalRow[$k] . "'";
@ -202,7 +227,6 @@ function updateDatabaseRow($tableName, $originalRow, $newRowValue) {
catch (Exception $e) {
echo redText("ERROR: while trying $sql_instruction\n\n"); print_r($e->getMessage());
}
}
function updateTriggerError($tableName, $error) {