| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Nip\Database\Adapters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Nip\Database\Adapters\Profiler\Profiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class AbstractAdapter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package Nip\Database\Adapters | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | abstract class AbstractAdapter | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * @var null|Profiler | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     protected $_profiler = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * Executes SQL query | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @param string $sql | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     public function execute($sql) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         if ($this->hasProfiler()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             if ($profile = $this->getProfiler()->start()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |                 $profile->setName($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                 $profile->setAdapter($this); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $result = $this->query($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         if ($this->hasProfiler() && $profile !== null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             $this->getProfiler()->end($profile); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         if ($result !== false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             trigger_error($this->error()." [$sql]", E_USER_WARNING); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public function hasProfiler() | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return is_object($this->_profiler); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return Profiler|null | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function getProfiler() | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return $this->_profiler; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param Profiler $profiler | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function setProfiler($profiler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |         $this->_profiler = $profiler; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * @param string $sql | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     abstract public function query($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     abstract public function error(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function newProfiler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $profiler = new Profiler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |         return $profiler; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 86 |  |  |     abstract public function quote($value); | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     abstract public function cleanData($data); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     abstract public function connect( | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         $host = false, | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         $user = false, | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         $password = false, | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         $database = false, | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         $newLink = false | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |     ); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |      * @param string $table | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |     abstract public function describeTable($table); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |     abstract public function disconnect(); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |     abstract public function lastInsertID(); | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |     abstract public function affectedRows(); | 
            
                                                        
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |