commit a0025b38ea67ca0d9eb93d7c320e708eeecfb60a
Author: Filip Jani <jsem@filek.cz>
Date:   Tue Jun 11 18:23:00 2019 +0200

    Optimalizace SQL dotazu

diff --git a/app/model/repository/TransliterationRepository.php b/app/model/repository/TransliterationRepository.php
index 482f410..835e3fb 100644
--- a/app/model/repository/TransliterationRepository.php
+++ b/app/model/repository/TransliterationRepository.php
@@ -36,9 +36,10 @@ class TransliterationRepository extends Repository
      * @param ArrayHash $queryParams objekt s podmínkami pro hledaný text
      * @param $offset null|int
      * @param $limit null|int
+     * @param bool $countOnly : flag pokud chceme vrátit pouze počet výsledků
      * @return \Nette\Database\ResultSet
      */
-    public function transliterationsFulltextSearch(ArrayHash $queryParams, int $offset = null, int $limit = null)
+    public function transliterationsFulltextSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, bool $countOnly = false)
     {
         $where = '';
         $whereArgs = [];
@@ -118,20 +119,30 @@ class TransliterationRepository extends Repository
             $whereArgs[] = $this->prepareQueryArgByOperator($queryParams['date'], $queryParams['date_condition']);
         }
 
-        $query = "SELECT 
-                    t.id_transliteration as id,
-                    s.id_surface as surface_id, 
-                    b.book_abrev, 
-                    t.chapter, 
-                    l.transliteration, 
-                    l.id_line as line_id,
-                    l.line_number 
-                  FROM transliteration t
+        if ($countOnly)
+        {
+            $query = "SELECT COUNT(t.id_transliteration) as count ";
+        } else
+        {
+            $query = "SELECT t.id_transliteration as id,
+                             s.id_surface as surface_id, 
+                             b.book_abrev, 
+                             t.chapter, 
+                             l.transliteration, 
+                             l.id_line as line_id,
+                             l.line_number ";
+        }
+
+        $query .= "FROM transliteration t
                   LEFT JOIN surface s ON s.id_transliteration = t.id_transliteration
                   LEFT JOIN line l ON l.id_surface = s.id_surface
                   LEFT JOIN book b ON t.id_book = b.id_book
-                  WHERE " . $where .
-                 " ORDER BY id DESC ";
+                  WHERE " . $where;
+
+        if (!$countOnly)
+        {
+            $query .= " ORDER BY id DESC ";
+        }
 
         if($offset !== null && $limit !== null)
         {
@@ -145,7 +156,7 @@ class TransliterationRepository extends Repository
 
     public function getTransliterationsFulltextSearchTotalCount($queryParams)
     {
-        return $this->transliterationsFulltextSearch($queryParams, null, null)->getRowCount();
+        return $this->transliterationsFulltextSearch($queryParams, null, null, true)->fetch()->count;
     }
 
     /**
@@ -225,9 +236,10 @@ class TransliterationRepository extends Repository
      * @param ArrayHash $queryParams : parametry hledání
      * @param int|null $offset
      * @param int|null $limit
+     * @param bool $countOnly : flag pokud chceme vrátit pouze počet výsledků
      * @return \Nette\Database\ResultSet
      */
-    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null)
+    public function transliterationCatalogueSearch(ArrayHash $queryParams, int $offset = null, int $limit = null, bool $countOnly = false)
     {
         /**
          * @var $mc string : udává jestli je mezi vyhledávanými parametry logické AND nebo OR
@@ -303,12 +315,18 @@ class TransliterationRepository extends Repository
             $where = substr($where, strlen($mc));
         }
 
-        $query = "SELECT 
-                    t.id_transliteration as id,
-                    b.book_abrev, 
-                    t.chapter, 
-                    t.id_museum
-                  FROM transliteration t
+        if ($countOnly)
+        {
+            $query = "SELECT COUNT(t.id_transliteration) as count ";
+        } else
+        {
+            $query = "SELECT t.id_transliteration as id,
+                             b.book_abrev, 
+                             t.chapter, 
+                             t.id_museum ";
+        }
+
+        $query .= "FROM transliteration t
                   LEFT JOIN book b ON t.id_book = b.id_book ";
 
         if (!empty($where))
@@ -316,12 +334,16 @@ class TransliterationRepository extends Repository
             $query .= "WHERE " . $where;
         }
 
-        $query .= " GROUP BY b.book_abrev, t.chapter, t.id_transliteration ORDER BY t.chapter ";
+        if(!$countOnly)
+        {
+            $query .= " GROUP BY b.book_abrev, t.chapter, t.id_transliteration ORDER BY t.chapter ";
 
-        if($offset !== NULL && $limit !== NULL){
-            $query .= ' LIMIT ?, ?';
-            $whereArgs[] = (int) $offset;
-            $whereArgs[] = (int) $limit;
+            if ($offset !== NULL && $limit !== NULL)
+            {
+                $query .= ' LIMIT ?, ?';
+                $whereArgs[] = (int)$offset;
+                $whereArgs[] = (int)$limit;
+            }
         }
 
 
@@ -340,6 +362,6 @@ class TransliterationRepository extends Repository
      */
     public function getTransliterationCatalogueSearchTotalCount(ArrayHash $queryParams, int $offset = null, int $limit = null): int
     {
-        return $this->transliterationCatalogueSearch($queryParams, $offset, $limit)->getRowCount();
+        return $this->transliterationCatalogueSearch($queryParams, $offset, $limit, true)->fetch()->count;
     }
 }
\ No newline at end of file
