| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Maphper\DataSource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | class DatabaseSelect { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     private $resultCache = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     private $idCache = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     private $selectBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     private $whereBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     private $adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $databaseModify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $defaultSort; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $this->adapter = $adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->databaseModify = $databaseModify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->table = $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function findById($id, $pk) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		if (!isset($this->idCache[$id])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 			try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 				$result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 // Don't issue an error if it cannot be found since we return null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			if (isset($result[0])) 	$this->idCache[$id] = $result[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			else return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		return $this->idCache[$id]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function findByField(array $fields, $options, $defaultSort) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$cacheId = md5(serialize(func_get_args())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		if (!isset($this->resultCache[$cacheId])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			$query = $this->whereBuilder->createSql($fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			if (!isset($options['order'])) $options['order'] = $defaultSort; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 				$this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 				$this->databaseModify->addIndex(array_keys($query['args'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 				$this->databaseModify->addIndex(explode(',', $options['order'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 				$this->errors[] = $e; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				$this->resultCache[$cacheId] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		return $this->resultCache[$cacheId]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		//Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		if (is_array($field)) $field = $field[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$query = $this->whereBuilder->createSql($criteria); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$this->databaseModify->addIndex(array_keys($query['args'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			$this->databaseModify->addIndex(explode(',', $group)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			return $this->determineAggregateResult($result, $group, $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			return $group ? [] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     private function determineAggregateResult($result, $group, $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($group != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $ret = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             foreach ($result as $res) $ret[$res->$field] = $res->val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         else if (isset($result[0])) return $result[0]->val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         else return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     private function selectQuery(\Maphper\Lib\Query $query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     public function clearResultCache() { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $this->resultCache = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function clearIDCache() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->idCache = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function updateCache($data, $pkValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		else $this->cache[$pkValue] = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function deleteIDFromCache($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         unset($this->idCache[$id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |