PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/class/sqlManager.Class.php

https://github.com/myszor/sew
PHP | 210 lines | 138 code | 23 blank | 49 comment | 24 complexity | 2f9f76a3eacf504278efb8655ed823d4 MD5 | raw file
  1. <?php
  2. require_once $_SERVER['DOCUMENT_ROOT'].'/configs/config.php';
  3. require_once("Exceptions_handler_lib.php");
  4. /**
  5. * sql manager class
  6. * @author jacek(at)jacoor.net
  7. *
  8. */
  9. final class sqlManager extends mysqli { //don't suppose that i have to extend this
  10. private static $instance;
  11. public static function GetInstance()
  12. {
  13. if ( !isset(self::$instance))
  14. {
  15. self::$instance = new sqlManager();
  16. }
  17. return self::$instance;
  18. }
  19. /**
  20. * private constructor
  21. *
  22. */
  23. private function __construct(){
  24. //this is singletone :-)
  25. self::I_hate_fucking_magic_quotes(); //clear magic quotes
  26. try{
  27. @parent::__construct(config::hostname(),config::username(),config::pass(), config::dbname(), config::port());
  28. if (mysqli_connect_errno()!==0)
  29. throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
  30. $result=@$this->query("SET NAMES 'utf8'");
  31. $result=@$this->set_charset("utf8");
  32. if ($result===FALSE)
  33. {
  34. $error = $this->error;
  35. $errcode = $this->errno;
  36. $this->close();
  37. throw new Exception($error, $errcode);
  38. }
  39. }
  40. catch (Exception $dberror)
  41. {
  42. throw new MySQLIConnectException($dberror->getMessage(),$dberror->getCode());
  43. }
  44. }
  45. /**
  46. * fetch rows with desired criteria
  47. * @param $tableName
  48. * @param $search associative array with data to search (key => val)
  49. * @param $get indexed array of columns name to get , if null all are fetched
  50. * @param $order order of records to return (withoud 'order by' part)
  51. * @return associative of fetched row(s)
  52. * @todo implement limit
  53. */
  54. public function getRows ($tableName, $search = null, $get = null, $order = null) {
  55. if ($tableName===null)
  56. trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
  57. if (is_array($search)){
  58. $where = "WHERE ";
  59. foreach ($search as $key => $val){
  60. $where = $where . addslashes($key) .' = \''.addslashes($val).'\' AND ';
  61. }
  62. }
  63. if ($get === null)
  64. $get = ' * ';
  65. else{
  66. foreach ($get as $key => &$val)
  67. $get[$key] = addslashes($val);
  68. $get = implode(array_values($get),',');
  69. }
  70. if ($order != null ) $order = "ORDER BY ".addslashes($order);
  71. $where = substr($where,0,-5);//remove last char
  72. $query = "
  73. SELECT $get FROM `$tableName`
  74. $where
  75. $order
  76. ;";
  77. $res = $this->query($query);
  78. if (!$this->error){
  79. while ($row = $res->fetch_assoc())
  80. $result[] = $row;
  81. }
  82. else trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR);
  83. return ($result);
  84. }
  85. /**
  86. * inserts single row to table
  87. * @param $tableName
  88. * @param $fields array of fields to be inserted $key => $val
  89. * @return boolean
  90. * @exception throws some exception on mysqli error
  91. */
  92. public function insertRow ($tableName, array $fields){
  93. if ($tableName === null) trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
  94. if (!is_array($fields)) trigger_error("Niepoprawna wartość fields", E_USER_ERROR);
  95. $cols = implode(',', array_keys($fields));
  96. foreach ($fields as $key => &$val){
  97. $fields[$key] = addslashes($val);
  98. }
  99. $vals = implode($fields,'\',\'');
  100. $vals = '\''.$vals.'\'';
  101. $query = "
  102. INSERT INTO `$tableName`
  103. ($cols)
  104. VALUES($vals);";
  105. $this->query($query);
  106. if ($this->error)
  107. trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR);
  108. return $this->insert_id;
  109. }
  110. /**
  111. * fetch rows with desired criteria
  112. * @param $tableName
  113. * @param $update associative array with data to update (key => val)
  114. * @param $search associative array with data to search (key => val)
  115. * @return mysqli_affected_rows
  116. */
  117. public function updateRow ($tableName, array $update, array $search) {
  118. if ($tableName===null) trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
  119. if (!is_array($search)) trigger_error("Niepoprawna wartość search", E_USER_ERROR);
  120. if (!is_array($update)) trigger_error("Niepoprawna wartość update", E_USER_ERROR);
  121. $set = "SET ";
  122. foreach ($update as $key => $val)
  123. $set = $set . addslashes($key) ." = '".addslashes($val)."', ";
  124. $set = substr($set,0,-2);//remove last char
  125. $where = "WHERE ";
  126. foreach ($search as $key => $val)
  127. $where = $where . addslashes($key) ." = '".addslashes($val)."' AND ";
  128. $where = substr($where,0,-5);//remove last char
  129. $query = "
  130. UPDATE `$tableName`
  131. $set
  132. $where
  133. ;";
  134. $this->query($query);
  135. if ($this->error)
  136. trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR);
  137. return $this->affected_rows;
  138. }
  139. /**
  140. * deletes rows with desired criteria. Does not allow to delete all.
  141. * @param $tableName
  142. * @param $search associative array with data to search (key => val)
  143. * @return myqli_affected_rows
  144. * @exception MysqliQueryException
  145. */
  146. public function deleteRows ($tableName, $search) {
  147. if ($tableName===null) trigger_error("Niepoprawna wartość tableName", E_USER_ERROR);
  148. if (!is_array($search)) trigger_error("Niepoprawna wartość search", E_USER_ERROR);
  149. $where = "WHERE ";
  150. foreach ($search as $key => $val){
  151. $where = $where . addslashes($key) .' = '.addslashes($val).' AND ';
  152. }
  153. $where = substr($where,0,-5);//remove last char
  154. $query = "
  155. DELETE FROM `$tableName`
  156. $where
  157. ;";
  158. $this->query($query);
  159. if ($this->error)
  160. trigger_error("Bład zapytania mysqli" .$this->error , E_USER_ERROR);
  161. return $this->affected_rows;
  162. }
  163. /**
  164. * I really hate magic_quotes_gpc - this stuff reverts changes of magic quotes
  165. * @return unknown_type
  166. */
  167. private static function I_hate_fucking_magic_quotes(){
  168. if (ini_get ('magic_quotes_sybase') != false)
  169. die('Yo! I will not work untill you disable magic_quotes_sybase in your php.ini');
  170. if (get_magic_quotes_gpc()){
  171. array_walk_recursive(&$_REQUEST, 'sqlManager::clearslashes');
  172. array_walk_recursive(&$_POST, 'sqlManager::clearslashes');
  173. array_walk_recursive(&$_GET, 'sqlManager::clearslashes');
  174. array_walk_recursive(&$_COOKIE, 'sqlManager::clearslashes');
  175. }
  176. }
  177. /**
  178. * clears slashes from magic quotes
  179. * @param $input
  180. * @return modified $input
  181. */
  182. private static function clearslashes(&$input){
  183. $input = stripslashes($input);
  184. return true;
  185. }
  186. }
  187. ?>