Advertisement
pbowers

UserSpice patch - improvements on DB.php

Aug 28th, 2016
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.20 KB | None | 0 0
  1. *** ../UserSpice41/users/classes/DB.php 2016-08-15 15:51:50.857641600 +0200
  2. --- ./users/classes/DB.php  2016-08-28 17:38:12.822601400 +0200
  3. ***************
  4. *** 19,34 ****
  5.   */
  6.   class DB {
  7.     private static $_instance = null;
  8. !   private $_pdo, $_query, $_error = false, $_results, $_resultsArray, $_count = 0, $_lastId, $_queryCount=0;
  9.  
  10.     private function __construct(){
  11.         try{
  12.             $this->_pdo = new PDO('mysql:host=' .
  13.                 Config::get('mysql/host') .';dbname='.
  14.                 Config::get('mysql/db'),
  15.                 Config::get('mysql/username'),
  16.                 Config::get('mysql/password'),
  17. !               array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION sql_mode = ''"));
  18.         } catch(PDOException $e){
  19.             die($e->getMessage());
  20.         }
  21. --- 19,36 ----
  22.   */
  23.   class DB {
  24.     private static $_instance = null;
  25. !   private $_pdo, $_query, $_error = false, $_errorInfo, $_results, $_resultsArray, $_count = 0, $_lastId, $_queryCount=0;
  26.  
  27.     private function __construct(){
  28. +       if (!$opts = Config::get('mysql/options'))
  29. +           $opts = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION sql_mode = ''");
  30.         try{
  31.             $this->_pdo = new PDO('mysql:host=' .
  32.                 Config::get('mysql/host') .';dbname='.
  33.                 Config::get('mysql/db'),
  34.                 Config::get('mysql/username'),
  35.                 Config::get('mysql/password'),
  36. !               $opts);
  37.         } catch(PDOException $e){
  38.             die($e->getMessage());
  39.         }
  40. ***************
  41. *** 42,49 ****
  42. --- 44,53 ----
  43.     }
  44.  
  45.     public function query($sql, $params = array()){
  46. +       #echo "DEBUG: query(sql=$sql, params=".print_r($params,true).")<br />\n";
  47.         $this->_queryCount++;
  48.         $this->_error = false;
  49. +       $this->_errorInfo = array(0, null, null);
  50.         if ($this->_query = $this->_pdo->prepare($sql)) {
  51.             $x = 1;
  52.             if (count($params)) {
  53. ***************
  54. *** 54,65 ****
  55. --- 58,72 ----
  56.             }
  57.  
  58.             if ($this->_query->execute()) {
  59. +               if ($this->_query->columnCount() > 0) {
  60.                     $this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);
  61.                     $this->_resultsArray = json_decode(json_encode($this->_results),true);
  62. +               }
  63.                 $this->_count = $this->_query->rowCount();
  64.                 $this->_lastId = $this->_pdo->lastInsertId();
  65.             } else{
  66.                 $this->_error = true;
  67. +               $this->_errorInfo = $this->_query->errorInfo();
  68.             }
  69.         }
  70.         return $this;
  71. ***************
  72. *** 75,96 ****
  73.  
  74.     public function action($action, $table, $where = array()){
  75.         $sql = "{$action} FROM {$table}";
  76. !       $value = '';
  77. !       if (count($where) === 3) {
  78. !           $operators = array('=', '>', '<', '>=', '<=');
  79. !
  80. !           $field = $where[0];
  81. !           $operator = $where[1];
  82. !           $value = $where[2];
  83.  
  84. !           if(in_array($operator, $operators)){
  85. !               $sql .= " WHERE {$field} {$operator} ?";
  86.             }
  87.         }
  88. !       if (!$this->query($sql, array($value))->error()) {
  89. !           return $this;
  90.         }
  91. !       return false;
  92.     }
  93.  
  94.     public function get($table, $where){
  95. --- 82,151 ----
  96.  
  97.     public function action($action, $table, $where = array()){
  98.         $sql = "{$action} FROM {$table}";
  99. !       $values = array();
  100. !     $where_text = $this->_calcWhere($where, $values);
  101. !     if ($where_text) $sql .= " WHERE $where_text";
  102. !       if (!$this->query($sql, $values)->error()) {
  103. !           return $this;
  104. !       }
  105. !       return false;
  106. !   }
  107.  
  108. !   private function _calcWhere($w, &$vals, $comboparg='and') {
  109. !     #echo "DEBUG: Entering _calcwhere(w=".print_r($w,true).",...)<br />\n";
  110. !     if (is_array($w)) {
  111. !               #echo "DEBUG: is_array - check<br />\n";
  112. !         $comb_ops = ['and', 'or'];
  113. !         $valid_ops = ['=', '<', '>', '<=', '>=', '<>', '!='];
  114. !         # believe it or not, this appears to be the fastest way to check
  115. !         # sequential vs associative. Particularly with our expected short
  116. !         # arrays it shouldn't impact memory usage
  117. !         # https://gist.github.com/Thinkscape/1965669
  118. !         if (array_values($w) === $w) { // sequential array
  119. !                       #echo "DEBUG: Sequential array - check!<br />\n";
  120. !             if (in_array(strtolower($w[0]), $comb_ops)) {
  121. !                               #echo "DEBUG: w=".print_r($w,true)."<br />\n";
  122. !                 $sql = '';
  123. !                 $combop = '';
  124. !                 for ($i = 1; $i < count($w); $i++) {
  125. !                     $sql .= ' '. $combop . ' ' . $this->_calcWhere($w[$i], $vals);
  126. !                     $combop = $w[0];
  127. !                 }
  128. !                 return '('.$sql.')';
  129. !             } elseif (count($w) == 3 && in_array($w[1], $valid_ops)) {
  130. !                 #echo "DEBUG: normal condition w=".print_r($w,true)."<br />\n";
  131. !                 $vals[] = $w[2];
  132. !                 return $w[0] . ' ' . $w[1] . ' ?';
  133. !             } else {
  134. !                 echo "ERROR: w=".print_r($w,true)."<br />\n";
  135. !             }
  136. !         } else { // associative array ['field' => 'value']
  137. !             #echo "DEBUG: Associative<br />\n";
  138. !             $sql = '';
  139. !             $combop = '';
  140. !             foreach ($w as $k=>$v) {
  141. !                 if (in_array(strtolower($k), $comb_ops)) {
  142. !                     #echo "DEBUG: A<br />\n";
  143. !                     #echo "A: k=$k, v=".print_r($v,true)."<br />\n";
  144. !                     $sql .= $combop . ' (' . $this->calcWhere($v, $vals, $k) . ') ';
  145. !                     $combop = $comboparg;
  146. !                 } else {
  147. !                     #echo "DEBUG: B<br />\n";
  148. !                     #echo "B: k=$k, v=".print_r($v,true)."<br />\n";
  149. !                     $vals[] = $v;
  150. !                     if (in_array(substr($k,-1,1), array('=', '<', '>'))) // 'field !='=>'value'
  151. !                         $sql .= $combop . ' ' . $k . ' ? ';
  152. !                     else // 'field'=>'value'
  153. !                         $sql .= $combop . ' ' . $k . ' = ? ';
  154. !                     $combop = $comboparg;
  155.                   }
  156.               }
  157. !             return ' ('.$sql.') ';
  158.           }
  159. !     } else {
  160. !         echo "ERROR: No array in $w<br />\n";
  161. !     }
  162. !
  163.     }
  164.  
  165.     public function get($table, $where){
  166. ***************
  167. *** 163,168 ****
  168. --- 218,231 ----
  169.         return $this->_error;
  170.     }
  171.  
  172. +   public function errorInfo() {
  173. +       return $this->_errorInfo;
  174. +   }
  175. +
  176. +   public function errorString() {
  177. +       return 'ERROR #'.$this->_errorInfo[0].': '.$this->_errorInfo[2];
  178. +   }
  179. +
  180.     public function lastId(){
  181.         return $this->_lastId;
  182.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement