From fe641b10e89832587493c835f1382ec9ceabf015 Mon Sep 17 00:00:00 2001
From: Carsten  Rose <carsten.rose@math.uzh.ch>
Date: Tue, 31 Dec 2024 11:53:09 +0100
Subject: [PATCH] Fixes #20309 Multi DB typeahead SQL broken

---
 Documentation-develop/MULTI_DB.md             | 26 +++++++++++++++----
 extension/Classes/Core/Constants.php          |  2 +-
 .../Form/FormElement/InputFormElement.php     |  3 ++-
 extension/Classes/Core/Form/TypeAhead.php     |  4 +--
 4 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/Documentation-develop/MULTI_DB.md b/Documentation-develop/MULTI_DB.md
index 5d0e30b8b..38f0bffd7 100644
--- a/Documentation-develop/MULTI_DB.md
+++ b/Documentation-develop/MULTI_DB.md
@@ -26,8 +26,24 @@ quite flexible.
 SIP Variable: __dbIndexData
 ---------------------------
 
-* It's possible to explicit define `Form.parameter.dbIndex=...`.
-* During Formload, it's not known if such a parameter is given
-* If _dbIndexData is given, load data from that DB.
-* In general: it's a huge effort to fix a small problem - maybe there are better solutions than to extend every SIP link 
-  with this parameter.
\ No newline at end of file
+SIP link might contain a variable `__dbIndexData`.
+
+Form
+^^^^
+
+* Per QFQ config `database.indexData` data or per form `Form.parameter.dbIndex=...`, a custom data-database can be specified.
+* During form load, it's hard to get the dbIndex from the form definition itself (cause it's loaded at that moment).
+* Therefore `_dbIndexData` is defined on the SIP and can be used to load data from the required DB.
+
+TypeAheadSql
+^^^^^^^^^^^^
+
+* Typeahead should be fast. Therefore not much classes are instantiated. Esecially not STORE.
+* During form load, the required database is known, but not during a typeahed event.
+* Therefore `_dbIndexData` is defined on the SIP and can be used to load data from the required DB.
+* This is **independent** of Multi-DB setup - even on a Single-DB setup, the config `database.indexData` might be 
+  different from the default. In order **not** to need class STORE, the dbIndex have to be specified earlier.
+
+
+
+  
diff --git a/extension/Classes/Core/Constants.php b/extension/Classes/Core/Constants.php
index f60743e5d..b32f82b3f 100644
--- a/extension/Classes/Core/Constants.php
+++ b/extension/Classes/Core/Constants.php
@@ -1071,7 +1071,7 @@ const DATA_REQUIRED = 'data-required';
 const DATA_SIP = 'data-sip';
 
 const CLASS_TYPEAHEAD = 'qfq-typeahead';
-const DATA_TYPEAHEAD_SIP = 'data-typeahead-sip'; // Used for typeAhead
+const DATA_TYPEAHEAD_SIP = 'data-typeahead-sip';
 
 const CLASS_NOTE = 'qfq-note';
 
diff --git a/extension/Classes/Core/Form/FormElement/InputFormElement.php b/extension/Classes/Core/Form/FormElement/InputFormElement.php
index 5ee936e0a..0c0ef69b2 100644
--- a/extension/Classes/Core/Form/FormElement/InputFormElement.php
+++ b/extension/Classes/Core/Form/FormElement/InputFormElement.php
@@ -315,7 +315,8 @@ class InputFormElement extends AbstractFormElement {
 
             $arr = [
                 FE_TYPEAHEAD_SQL => $sql,
-                FE_TYPEAHEAD_SQL_PREFETCH => $this->attributes[FE_TYPEAHEAD_SQL_PREFETCH] ?? ''
+                FE_TYPEAHEAD_SQL_PREFETCH => $this->attributes[FE_TYPEAHEAD_SQL_PREFETCH] ?? '',
+                PARAM_DB_INDEX_DATA => $this->form->specFinal[F_DB_INDEX]
             ];
         } elseif (isset($this->attributes[FE_TYPEAHEAD_LDAP])) {
             $this->attributes[FE_LDAP_SERVER] = Support::setIfNotSet($this->attributes, FE_LDAP_SERVER);
diff --git a/extension/Classes/Core/Form/TypeAhead.php b/extension/Classes/Core/Form/TypeAhead.php
index a40204722..bc566b8db 100644
--- a/extension/Classes/Core/Form/TypeAhead.php
+++ b/extension/Classes/Core/Form/TypeAhead.php
@@ -37,7 +37,6 @@ class TypeAhead {
      */
     public function __construct($phpUnit = false) {
 
-        #TODO: rewrite $phpUnit to: "if (!defined('PHPUNIT_QFQ')) {...}"
         $this->vars[TYPEAHEAD_API_QUERY] = isset($_GET[TYPEAHEAD_API_QUERY]) ? $_GET[TYPEAHEAD_API_QUERY] : '';
         $this->vars[TYPEAHEAD_API_PREFETCH] = isset($_GET[TYPEAHEAD_API_PREFETCH]) ? $_GET[TYPEAHEAD_API_PREFETCH] : '';
         $this->vars[TYPEAHEAD_API_SIP] = isset($_GET[TYPEAHEAD_API_SIP]) ? $_GET[TYPEAHEAD_API_SIP] : '';
@@ -63,11 +62,11 @@ class TypeAhead {
     public function process() {
 
         $arr = array();
-        $dbIndex = DB_INDEX_DEFAULT;
 
         $sipClass = new Sip();
 
         $sipVars = $sipClass->getVarsFromSip($this->vars[TYPEAHEAD_API_SIP]);
+        $dbIndex = $sipVars[PARAM_DB_INDEX_DATA]??DB_INDEX_DEFAULT;
 
         // Check for an optional given dbIndex: '[<int>]SELECT ...'
         $sql = $sipVars[FE_TYPEAHEAD_SQL] ?? '';
@@ -77,7 +76,6 @@ class TypeAhead {
             $sipVars[FE_TYPEAHEAD_SQL] = substr($sql, $pos + 1);
         }
 
-
         $this->db = new Database($dbIndex);
 
         if (isset($sipVars[FE_TYPEAHEAD_SQL])) {
-- 
GitLab