| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Soupmix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class SQLQueryBuilder extends AbstractQueryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     private $queryBuilder; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 | 2 |  |     public function run(){ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 | 2 |  |         $this->queryBuilder = $this->getQueryBuilder(); | 
            
                                                                        
                            
            
                                    
            
            
                | 13 | 2 |  |         $count = $this->getCount(); | 
            
                                                                        
                            
            
                                    
            
            
                | 14 | 2 |  |         if (!isset($count[0]['total']) || ($count[0]['total']==0)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |             return ['total' => 0, 'data' => null]; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 2 |  |         $numberOfRows = $count[0]['total']; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 2 |  |         $this->setSortOrders(); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 2 |  |         $this->setReturnFields(); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 2 |  |         $this->setOffsetAndLimit(); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 2 |  |         $stmt = $this->soupmix->getConnection()->executeQuery( | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 2 |  |             $this->queryBuilder->getSql(), | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 2 |  |             $this->queryBuilder->getParameters() | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 2 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 2 |  |         $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 2 |  |         if($this->distinctFieldName !== null){ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 1 |  |                 $numberOfRows = count($result); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 2 |  |         return ['total' => $numberOfRows, 'data' => $result]; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  |     private function getQueryBuilder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |         if ($this->orFilters !== null){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |             $this->andFilters[] = $this->orFilters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |         $this->filters      = $this->andFilters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |         return $this->soupmix->buildQuery($this->collection, $this->filters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |     private function getCount() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |         $queryBuilderCount = clone $this->queryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         $queryBuilderCount->select(" COUNT(*) AS total "); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |         $stmt = $this->soupmix->getConnection()->executeQuery($queryBuilderCount->getSql(), $queryBuilderCount->getParameters()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         return $stmt->fetchAll(\PDO::FETCH_ASSOC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |     private function setSortOrders() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         if ($this->sortFields !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             foreach ($this->sortFields as $sortKey => $sortDir) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $this->queryBuilder->addOrderBy($sortKey, $sortDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |     private function setReturnFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         if ($this->distinctFieldName === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |             $fieldNames = ($this->fieldNames === null) ? "*" : $this->fieldNames; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |             $this->queryBuilder->select($fieldNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         $this->queryBuilder->select('DISTINCT (`' . $this->distinctFieldName . '`)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |     private function setOffsetAndLimit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |         $this->queryBuilder->setFirstResult($this->offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |             ->setMaxResults($this->limit); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  | } |