PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/core/libraries/database.php

http://rapyd-framework.googlecode.com/
PHP | 324 lines | 202 code | 28 blank | 94 comment | 30 complexity | d34b9298b44c66511c3493f6d869760b MD5 | raw file
  1. <?php
  2. if (!defined('RAPYD_PATH'))
  3. exit('No direct script access allowed');
  4. /**
  5. * Database library
  6. *
  7. * @package Core
  8. * @author Felice Ostuni
  9. * @copyright (c) 2011 Rapyd Team
  10. * @license http://www.rapyd.com/license
  11. */
  12. class rpd_database_library
  13. {
  14. public $database;
  15. public $hostname;
  16. public $username;
  17. public $password;
  18. public $dbdriver;
  19. public $dbprefix = '';
  20. public $port = '';
  21. public $conn_id = FALSE;
  22. public $result_id = FALSE;
  23. public $db_debug = FALSE;
  24. public $result_array = array();
  25. public $result_object = array();
  26. public $last_query;
  27. public $queries = array();
  28. /**
  29. * when rapyd is used as "library" it can use a valid connection resource link
  30. * if parent framework already instanced a connection
  31. *
  32. * @param object $conn_id
  33. */
  34. public function __construct($conn_id=FALSE)
  35. {
  36. $this->conn_id = $conn_id;
  37. }
  38. /**
  39. * prepp a value using gettype how to escape
  40. *
  41. * @param mixed $str the value to prepp
  42. * @return mixed escaped value
  43. */
  44. public function escape($str)
  45. {
  46. switch (gettype($str))
  47. {
  48. case 'string' : $str = "'" . $this->escape_str($str) . "'";
  49. break;
  50. case 'boolean' : $str = ($str === FALSE) ? 0 : 1;
  51. break;
  52. default : $str = ($str === NULL) ? 'NULL' : $str;
  53. break;
  54. }
  55. return $str;
  56. }
  57. /**
  58. * execute a query and return result_id object
  59. * show error only if db_debug == TRUE
  60. *
  61. * @param string $sql
  62. * @return object
  63. */
  64. public function query($sql)
  65. {
  66. $this->queries[] = $sql;
  67. if (FALSE === ($this->result_id = $this->execute($sql)))
  68. {
  69. if ($this->db_debug)
  70. {
  71. return $this->show_error(array($this->error_number($this->conn_id), $this->error_message($this->conn_id), $sql));
  72. }
  73. return $this->show_error('DATABASE ERROR');
  74. }
  75. $this->last_query = $sql;
  76. return $this->result_id;
  77. }
  78. /**
  79. * return entire resultset as multi-dimensional associative array
  80. *
  81. * @return array the resultset
  82. */
  83. public function result_array($field='')
  84. {
  85. if (count($this->result_array) > 0)
  86. {
  87. return $this->result_array;
  88. }
  89. if ($this->result_id === FALSE OR $this->num_rows() == 0)
  90. {
  91. return array();
  92. }
  93. $this->data_seek(0);
  94. while ($row = $this->fetch_assoc())
  95. {
  96. if ($field!='')
  97. {
  98. $this->result_array[$row[$field]] = $row;
  99. } else {
  100. $this->result_array[] = $row;
  101. }
  102. }
  103. return $this->result_array;
  104. }
  105. /**
  106. * return current row as associative array
  107. *
  108. * @return array current row
  109. */
  110. public function row_array()
  111. {
  112. return $this->fetch_assoc();
  113. }
  114. /**
  115. * return current row as object
  116. *
  117. * @return object
  118. */
  119. public function row_object()
  120. {
  121. return $this->fetch_object();
  122. }
  123. /**
  124. * same as result_array() but using first result column as index and second as value
  125. * used to get data ready for dropdowns, checkboxgroups, or custom "menu" structures
  126. *
  127. * @return array
  128. */
  129. public function options_array()
  130. {
  131. if (count($this->result_array) > 0)
  132. {
  133. return $this->result_array;
  134. }
  135. if ($this->result_id === FALSE OR $this->num_rows() == 0)
  136. {
  137. return array();
  138. }
  139. $this->data_seek(0);
  140. while ($row = $this->fetch_row())
  141. {
  142. switch (count($row))
  143. {
  144. case 2:
  145. $data[$row[0]] = $row[1];
  146. break;
  147. case 3:
  148. $data[$row[0]][$row[1]] = $row[2];
  149. break;
  150. default: return array();
  151. }
  152. }
  153. $this->result_array = $data;
  154. return $this->result_array;
  155. }
  156. /**
  157. * return entire resultset as multi-dimensional array containing row-objects
  158. *
  159. * @return array of objects
  160. */
  161. public function result_object()
  162. {
  163. if (count($this->result_object) > 0)
  164. {
  165. return $this->result_object;
  166. }
  167. if ($this->result_id === FALSE OR $this->num_rows() == 0)
  168. {
  169. return array();
  170. }
  171. $this->data_seek(0);
  172. while ($row = $this->fetch_object())
  173. {
  174. $this->result_object[] = $row;
  175. }
  176. return $this->result_object;
  177. }
  178. /**
  179. * cont all records in a table
  180. *
  181. * @todo check if "escape_table" works then remove "if"
  182. * @param string $table
  183. * @return int record number
  184. */
  185. public function count_all($table)
  186. {
  187. if ($this->dbdriver == 'pgsql')
  188. {
  189. $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix . $table);
  190. } else
  191. {
  192. $this->query("SELECT COUNT(*) AS numrows FROM `" . $this->dbprefix . $table . "`");
  193. }
  194. if ($this->num_rows() == 0)
  195. return '0';
  196. $row = $this->result_object();
  197. return $row[0]->numrows;
  198. }
  199. /**
  200. * show all tables in current db
  201. *
  202. * @todo check if it's mysql-specific or standard ansi
  203. * @todo check if "escape_table" works with all drivers, then use it instead plain backtrics
  204. * @return array tables list
  205. */
  206. public function list_tables()
  207. {
  208. $retval = array();
  209. $this->query("SHOW TABLES FROM `" . $this->database . "`");
  210. if ($this->num_rows() > 0)
  211. {
  212. foreach ($this->result_array() as $row)
  213. {
  214. if (isset($row['TABLE_NAME']))
  215. {
  216. $retval[] = $row['TABLE_NAME'];
  217. } else
  218. {
  219. $retval[] = array_shift($row);
  220. }
  221. }
  222. }
  223. return $retval;
  224. }
  225. /**
  226. * show all fields of given table
  227. *
  228. * @todo check if it's mysql-specific or standard ansi
  229. * @param string $table
  230. * @return array field list of given table
  231. */
  232. public function list_fields($table = '')
  233. {
  234. $retval = array();
  235. $this->query("SHOW COLUMNS FROM " . self::escape_table($table));
  236. foreach ($this->result_array() as $row)
  237. {
  238. if (isset($row['COLUMN_NAME']))
  239. {
  240. $retval[] = $row['COLUMN_NAME'];
  241. } else
  242. {
  243. $retval[] = current($row);
  244. }
  245. }
  246. return $retval;
  247. }
  248. /**
  249. * show field structure of given table
  250. *
  251. * @todo try to move conditional dbdriver-if to correct driver
  252. * @param string $table
  253. * @return array
  254. */
  255. public function field_data($table)
  256. {
  257. $retval = array();
  258. $this->query("SELECT * FROM " . self::escape_table($this->dbprefix . $table) . " LIMIT 1");
  259. while ($field = $this->fetch_field())
  260. {
  261. if ($this->dbdriver == 'pgsql')
  262. {
  263. $retval = $this->fetch_field();
  264. } else
  265. {
  266. $retval[] = $field;
  267. }
  268. }
  269. return $retval;
  270. }
  271. /**
  272. * prepp table name inside queries
  273. *
  274. * @todo check if it works for all drivers
  275. * @param string $table
  276. * @return string
  277. */
  278. protected static function escape_table($table)
  279. {
  280. if (stristr($table, '.'))
  281. {
  282. $table = preg_replace("/\./", "`.`", $table);
  283. }
  284. return $table;
  285. }
  286. /**
  287. * internal shortcut to show an application error
  288. *
  289. * @todo test&debug
  290. * @param string $message
  291. */
  292. protected function show_error($message)
  293. {
  294. rpd::error(implode(', ', (!is_array($message)) ? array($message) : $message));
  295. }
  296. }