/system/database/db.php

https://github.com/jpvf/boilerplate · PHP · 295 lines · 236 code · 59 blank · 0 comment · 19 complexity · e48d91fe5a1f77fcce990a5d2d003361 MD5 · raw file

  1. <?php if ( ! defined('BASE')) exit('Acceso directo prohibido');
  2. class db extends Active_Record
  3. {
  4. public $query;
  5. public $queries = array();
  6. public $counter = 0;
  7. public $time = 0;
  8. public $result = null;
  9. private $_database;
  10. private $_connection;
  11. private static $_current_databases;
  12. private static $instance;
  13. function __construct($db = null)
  14. {
  15. self::$instance = $this;
  16. $this->_connect();
  17. }
  18. function __destruct()
  19. {
  20. $this->_close();
  21. }
  22. public static function getInstance()
  23. {
  24. if ( ! self::$instance)
  25. {
  26. self::$instance = new db();
  27. }
  28. return self::$instance;
  29. }
  30. private function _list_tables()
  31. {
  32. $query = $this->query('SHOW TABLES FROM '.$this->_database);
  33. $tables = array();
  34. if ($query->num_rows() > 0)
  35. {
  36. foreach ($query->result('array') as $key => $val)
  37. {
  38. $tables[] = array_shift($val);
  39. }
  40. }
  41. return $tables;
  42. }
  43. function table_exists($table = '')
  44. {
  45. if (empty($table))
  46. {
  47. return FALSE;
  48. }
  49. $tables = $this->_list_tables();
  50. if ( in_array($table, $tables))
  51. {
  52. return TRUE;
  53. }
  54. return FALSE;
  55. }
  56. function query($query = '', $return_id = FALSE)
  57. {
  58. $query = empty($query) ? $this->get_sql() : $query;
  59. $res = new results();
  60. $res->conn_id = $this->_connection;
  61. $res->results_id = $this->_execute($query);
  62. if ($return_id !== FALSE)
  63. {
  64. return $res->results_id;
  65. }
  66. return $res;
  67. }
  68. function get_queries()
  69. {
  70. return $this->queries;
  71. }
  72. function count_queries()
  73. {
  74. return $this->counter;
  75. }
  76. function time_queries()
  77. {
  78. return $this->time;
  79. }
  80. function resource()
  81. {
  82. return $this->rows;
  83. }
  84. function last_id()
  85. {
  86. return mysql_insert_id();
  87. }
  88. function affected_rows()
  89. {
  90. return mysql_affected_rows();
  91. }
  92. private function _execute($query){
  93. $this->counter += 1;
  94. $start_time = microtime();
  95. $rows = mysql_query($query);
  96. $end_time = microtime();
  97. $this->queries[] = $query;
  98. $this->time += $end_time - $start_time;
  99. $this->query_time = $end_time - $start_time;
  100. $this->_query_error();
  101. return $rows;
  102. }
  103. function total_time()
  104. {
  105. return $this->time;
  106. }
  107. protected function _connect()
  108. {
  109. if ($this->_connection)
  110. {
  111. return;
  112. }
  113. Config::load('db');
  114. $db = Config::get_group('db');
  115. $this->_database = $db['database'];
  116. $this->_connection = mysql_connect($db['hostname'], $db['username'], $db['password'], TRUE);
  117. if ( ! $this->_connection)
  118. {
  119. throw new Database_Exception(mysql_error($this->_connection), mysql_errno($this->_connection));
  120. }
  121. $this->_connection_id = sha1($db['hostname'].'_'.$db['username'].'_'.$db['password']);
  122. $this->_set_charset('utf8');
  123. $this->_select_db($db['database']);
  124. return $this;
  125. }
  126. protected function _select_db($database)
  127. {
  128. if ( ! mysql_select_db($database, $this->_connection))
  129. {
  130. throw new Database_Exception(mysql_error($this->_connection), mysql_errno($this->_connection));
  131. }
  132. static::$_current_databases[$this->_connection_id] = $database;
  133. }
  134. protected function _set_charset($charset)
  135. {
  136. $status = mysql_set_charset($charset, $this->_connection);
  137. if ($status === FALSE)
  138. {
  139. throw new Database_Exception(mysql_error($this->_connection), mysql_errno($this->_connection));
  140. }
  141. }
  142. private function _close()
  143. {
  144. mysql_close($this->_connection);
  145. }
  146. private function _query_error()
  147. {
  148. $error = $this->_error();
  149. if ($error AND item('dbdebug'))
  150. {
  151. $error_html = '<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;background:#fff;width:600px;">
  152. <h3>Error de MySQL</h3>
  153. <p>Query: <br />' . nl2br(end($this->queries)) .'</p>
  154. <p>Error: ' . $error. '</p>
  155. <p>Tiempo: ' . $this->query_time . '</p>
  156. </div>';
  157. echo backtrace();
  158. echo $error_html;
  159. }
  160. return $this;
  161. }
  162. function last_query($formatted = TRUE)
  163. {
  164. if ($formatted !== FALSE)
  165. {
  166. return end($this->queries);
  167. }
  168. return '<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;background:#fff;width:600px;">
  169. <h3>Último Query</h3>
  170. <p>Query: <br />' . nl2br(end($this->queries)).'</p>
  171. <p>Tiempo: ' . $this->query_time . '</p>
  172. </div>';
  173. }
  174. private function _error()
  175. {
  176. return mysql_error();
  177. }
  178. function escape_str($str, $dashes = FALSE)
  179. {
  180. if (is_array($str))
  181. {
  182. foreach ($str as $key => $val)
  183. {
  184. $str[$key] = $this->escape_str($val, FALSE);
  185. }
  186. return $str;
  187. }
  188. if (function_exists('mysql_real_escape_string') AND is_resource($this->_connection))
  189. {
  190. $str = mysql_real_escape_string($str, $this->_connection);
  191. }
  192. elseif (function_exists('mysql_escape_string'))
  193. {
  194. $str = mysql_escape_string($str);
  195. }
  196. else
  197. {
  198. $str = addslashes($str);
  199. }
  200. if ($dashes === TRUE)
  201. {
  202. if (substr_count($str,'--') > 0)
  203. {
  204. $str = str_replace('--', '', $str);
  205. }
  206. if (substr_count($str, ';') > 0)
  207. {
  208. $str = str_replace(';', '', $str);
  209. }
  210. }
  211. return $str;
  212. }
  213. function field_data($table = '')
  214. {
  215. if ( ! empty($table))
  216. {
  217. $i = 0;
  218. $fields = array();
  219. $query = $this->query('SELECT * FROM '.$table);
  220. while ($i < mysql_num_fields($query->results_id))
  221. {
  222. $fields[] = mysql_fetch_field($query->results_id, $i);
  223. $i++;
  224. }
  225. return $fields;
  226. }
  227. return mysql_fetch_field($query->results_id);
  228. }
  229. }