From ec6d8033f416e836f39f8b4a969c593526f7bf2c Mon Sep 17 00:00:00 2001 From: git Date: Sat, 2 Aug 2025 10:31:39 +0100 Subject: [PATCH] handling VEC_ vector embeddings for LLM compatibility --- plt/index.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/plt/index.php b/plt/index.php index 8f7e3c3..37d2a68 100755 --- a/plt/index.php +++ b/plt/index.php @@ -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('') + 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] . "'"; @@ -198,11 +223,10 @@ function updateDatabaseRow($tableName, $originalRow, $newRowValue) { } $sql_instruction = "UPDATE $tableName SET " . implode(", ", $setStatements) . " WHERE " . implode(" AND ", $whereStatements); - try {sql($sql_instruction);} + try { sql($sql_instruction); } catch (Exception $e) { - echo redText("ERROR: while trying $sql_instruction\n\n");print_r($e->getMessage()); + echo redText("ERROR: while trying $sql_instruction\n\n"); print_r($e->getMessage()); } - } function updateTriggerError($tableName, $error) {