| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class DbSqlAware { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |    * @var db_mysql $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |   protected $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |    * DbSqlAware constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |    * @param db_mysql|null $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 2 |  |   public function __construct($db = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 2 |  |     $this->db = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 2 |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |    * @param db_mysql|null $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |    * @return static | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 2 |  |   public static function build($db = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 2 |  |     return new static($db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |    * @return db_mysql|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |   public function getDb() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     return $this->db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |    * @param db_mysql|null $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |   public function setDb($db) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |     $this->db = $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    * Just escapes string used DB string escape method - or a drop-in replacement addslashes() if not DB specified | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |    * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 7 |  |    * @return mixed|string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 | 7 |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 7 |  |   protected function escapeString($string) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 4 |  |     return | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 4 |  |       method_exists($this->db, 'db_escape') | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 7 |  |         ? $this->db->db_escape($string) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         : addslashes($string); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |    * Quotes string by ref as DDL ID (i.e. field or table name) - except '*' which not quoted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |    * @param string &$string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |   protected function quoteFieldSimpleByRef(&$string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     $string = $this->escapeString($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 5 |  |     if ((string)$string && '*' != $string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |       $string = '`' . $string . '`'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 5 |  |    * Escapes field - include fully qualified field like table.field, table.* and mask '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |    * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |   protected function quoteField($string) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     // Checking if string is just a '*' - to skip some code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     if ($string != '*') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |       $temp = explode('.', $string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |       array_walk($temp, array($this, 'quoteFieldSimpleByRef')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |       $string = implode('.', $temp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |    * Making and alias from fully qualified field name prepending with function name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |    * @param string $functionName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |    * @param string $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |   protected function aliasFromField($functionName, $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     $alias = strtolower($functionName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     // Checking if string is just a '*' - to skip some code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     if ($field != '*') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |       $temp = explode('.', $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |       array_walk($temp, 'DbSqlHelper::UCFirstByRef'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |       if (!empty($temp[1]) && $temp[1] == '*') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         unset($temp[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |       $temp = implode('', $temp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |       $alias .= ucfirst($temp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |       $alias .= 'Value'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     return $alias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |