From 364d18f166ab1a61f4b5169f0072ad26a640b8f0 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Thu, 3 Apr 2025 17:43:27 +0200 Subject: [PATCH] AdminerSqlGemini: Display errors --- externals/jush | 2 +- plugins/sql-gemini.php | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/externals/jush b/externals/jush index 182e9506..071536a7 160000 --- a/externals/jush +++ b/externals/jush @@ -1 +1 @@ -Subproject commit 182e95066ca8d2a4561fe3336952b3ae1c2cabaa +Subproject commit 071536a7c95eecf19aeb176ac32da16afbb7afc1 diff --git a/plugins/sql-gemini.php b/plugins/sql-gemini.php index 1dd53cb0..b1f50e6a 100644 --- a/plugins/sql-gemini.php +++ b/plugins/sql-gemini.php @@ -28,23 +28,22 @@ class AdminerSqlGemini { foreach (Adminer\tables_list() as $table => $type) { $prompt .= Adminer\create_sql($table, false, "CREATE") . ";\n\n"; } - $prompt .= "Prefer returning more columns including primary key.\n\n"; + $prompt .= "Prefer returning relevant columns including primary key.\n\n"; $prompt .= "Give me this SQL query and nothing else:\n\n$_POST[gemini]\n\n"; //~ echo $prompt; exit; $context = stream_context_create(array("http" => array( "method" => "POST", "header" => array("User-Agent: AdminerSqlGemini", "Content-Type: application/json"), "content" => '{"contents": [{"parts":[{"text": ' . json_encode($prompt) . '}]}]}', + "ignore_errors" => true, ))); $response = json_decode(file_get_contents("https://generativelanguage.googleapis.com/v1beta/models/$this->model:generateContent?key=$this->apiKey", false, $context)); - $text = $response->candidates[0]->content->parts[0]->text; - $in_code = false; - foreach (preg_split('~(^|\n)```(sql)?(\n|$)~', $text) as $part) { - $part = trim($part); - if ($part) { - echo ($in_code ? $part : "/*\n$part\n*/") . "\n\n"; - } - $in_code = !$in_code; + if (isset($response->error)) { + echo "-- " . $response->error->message; + } else { + $text = $response->candidates[0]->content->parts[0]->text; + $text = preg_replace('~(\n|^)```sql\n(.+)\n```(\n|$)~sU', "*/\n\n\\2\n\n/*", "/*\n$text*/\n"); + echo preg_replace('~/\*\s*\*/\n*~', '', $text); } exit; }