/Data_Grid/class.eyemysqladap.inc.php

https://bitbucket.org/evinw/lib · PHP · 410 lines · 156 code · 56 blank · 198 comment · 21 complexity · df926118d31b69fa83b9976ae0be910c MD5 · raw file

  1. <?php
  2. /**
  3. * EyeMySQLAdap
  4. * MySQL database adapter
  5. *
  6. * LICENSE: This source file is subject to the BSD license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.eyesis.ca/license.txt. If you did not receive a copy of
  9. * the BSD License and are unable to obtain it through the web, please
  10. * send a note to mike@eyesis.ca so I can send you a copy immediately.
  11. *
  12. * @author Micheal Frank <mike@eyesis.ca>
  13. * @copyright 2008 Eyesis
  14. * @license http://www.eyesis.ca/license.txt BSD License
  15. * @version v1.0.1 11/26/2008 9:41:46 AM
  16. */
  17. class EyeMySQLAdap {
  18. private $host, $user, $pass, $db_name;
  19. /**
  20. * MySQL connection information
  21. *
  22. * @var resource
  23. */
  24. private $link;
  25. /**
  26. * Result of last query
  27. *
  28. * @var resource
  29. */
  30. private $result;
  31. /**
  32. * Date and time
  33. *
  34. */
  35. const DATETIME = 'Y-m-d H:i:s';
  36. /**
  37. * Date
  38. *
  39. */
  40. const DATE = 'Y-m-d';
  41. /**
  42. * Constructor
  43. *
  44. * @param string $host MySQL host address
  45. * @param string $user Database user
  46. * @param string $password Database password
  47. * @param string $db Database name
  48. * @param boolean $persistant Is persistant connection
  49. * @param boolean $connect_now Connect now
  50. * @return void
  51. */
  52. public function __construct($host, $user, $password, $db, $persistant = true, $connect_now = true) {
  53. $this->host = $host; // Host address
  54. $this->user = $user; // User
  55. $this->pass = $password; // Password
  56. $this->db_name = $db; // Database
  57. if ($connect_now)
  58. $this->connect($persistant);
  59. return;
  60. }
  61. /**
  62. * Destructor
  63. *
  64. * @return void
  65. */
  66. public function __destruct() {
  67. $this->close();
  68. }
  69. /**
  70. * Connect to the database
  71. *
  72. * @param boolean $persist Is persistant connection
  73. * @return boolean
  74. */
  75. public function connect($persist = true) {
  76. if ($persist)
  77. $link = mysql_pconnect($this->host, $this->user, $this->pass);
  78. else
  79. $link = mysql_connect($this->host, $this->user, $this->pass);
  80. if (!$link)
  81. trigger_error('Could not connect to the database.', E_USER_ERROR);
  82. if ($link) {
  83. $this->link = $link;
  84. if (mysql_select_db($this->db_name, $link))
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. * Query the database
  91. *
  92. * @param string $query SQL query string
  93. * @return resource MySQL result set
  94. */
  95. public function query($query) {
  96. $result = mysql_query($query, $this->link);
  97. $this->result = $result;
  98. if ($result == false)
  99. trigger_error('Uncovered an error in your SQL query script: "' . $this->error() . '"');
  100. return $this->result;
  101. }
  102. /**
  103. * Update the database
  104. *
  105. * @param array $values 3D array of fields and values to be updated
  106. * @param string $table Table to update
  107. * @param string $where Where condition
  108. * @param string $limit Limit condition
  109. * @return boolean Result
  110. */
  111. public function update(array $values, $table, $where = false, $limit = false) {
  112. if (count($values) < 0)
  113. return false;
  114. $fields = array();
  115. foreach ($values as $field => $val)
  116. $fields[] = "`" . $field . "` = '" . $this->escapeString($val) . "'";
  117. $where = ($where) ? " WHERE " . $where : '';
  118. $limit = ($limit) ? " LIMIT " . $limit : '';
  119. if ($this->query("UPDATE `" . $table . "` SET " . implode($fields, ", ") . $where . $limit))
  120. return true;
  121. else
  122. return false;
  123. }
  124. /**
  125. * Insert one new row
  126. *
  127. * @param array $values 3D array of fields and values to be inserted
  128. * @param string $table Table to insert
  129. * @return boolean Result
  130. */
  131. public function insert(array $values, $table) {
  132. if (count($values) < 0)
  133. return false;
  134. foreach ($values as $field => $val)
  135. $values[$field] = $this->escapeString($val);
  136. if ($this->query("INSERT INTO `" . $table . "`(`" . implode(array_keys($values), "`, `") . "`) VALUES ('" . implode($values, "', '") . "')"))
  137. return true;
  138. else
  139. return false;
  140. }
  141. /**
  142. * Select
  143. *
  144. * @param mixed $fields Array or string of fields to retrieve
  145. * @param string $table Table to retrieve from
  146. * @param string $where Where condition
  147. * @param string $orderby Order by clause
  148. * @param string $limit Limit condition
  149. * @return array Array of rows
  150. */
  151. public function select($fields, $table, $where = false, $orderby = false, $limit = false) {
  152. if (is_array($fields))
  153. $fields = "`" . implode($fields, "`, `") . "`";
  154. $orderby = ($orderby) ? " ORDER BY " . $orderby : '';
  155. $where = ($where) ? " WHERE " . $where : '';
  156. $limit = ($limit) ? " LIMIT " . $limit : '';
  157. $this->query("SELECT " . $fields . " FROM `" . $table . "`" . $where . $orderby . $limit);
  158. if ($this->countRows() > 0) {
  159. $rows = array();
  160. while ($r = $this->fetchAssoc())
  161. $rows[] = $r;
  162. return $rows;
  163. } else
  164. return false;
  165. }
  166. /**
  167. * Selects one row
  168. *
  169. * @param mixed $fields Array or string of fields to retrieve
  170. * @param string $table Table to retrieve from
  171. * @param string $where Where condition
  172. * @param string $orderby Order by clause
  173. * @return array Row values
  174. */
  175. public function selectOne($fields, $table, $where = false, $orderby = false) {
  176. $result = $this->select($fields, $table, $where, $orderby, '1');
  177. return $result[0];
  178. }
  179. /**
  180. * Selects one value from one row
  181. *
  182. * @param mixed $field Name of field to retrieve
  183. * @param string $table Table to retrieve from
  184. * @param string $where Where condition
  185. * @param string $orderby Order by clause
  186. * @return array Field value
  187. */
  188. public function selectOneValue($field, $table, $where = false, $orderby = false) {
  189. $result = $this->selectOne($field, $table, $where, $orderby);
  190. return $result[$field];
  191. }
  192. /**
  193. * Delete rows
  194. *
  195. * @param string $table Table to delete from
  196. * @param string $where Where condition
  197. * @param string $limit Limit condition
  198. * @return boolean Result
  199. */
  200. public function delete($table, $where = false, $limit = 1) {
  201. $where = ($where) ? " WHERE " . $where : '';
  202. $limit = ($limit) ? " LIMIT " . $limit : '';
  203. if ($this->query("DELETE FROM `" . $table . "`" . $where . $limit))
  204. return true;
  205. else
  206. return false;
  207. }
  208. /**
  209. * Fetch results by associative array
  210. *
  211. * @param mixed $query Select query or MySQL result
  212. * @return array Row
  213. */
  214. public function fetchAssoc($query = false) {
  215. $this->resCalc($query);
  216. return mysql_fetch_assoc($query);
  217. }
  218. /**
  219. * Fetch results by enumerated array
  220. *
  221. * @param mixed $query Select query or MySQL result
  222. * @return array Row
  223. */
  224. public function fetchRow($query = false) {
  225. $this->resCalc($query);
  226. return mysql_fetch_row($query);
  227. }
  228. /**
  229. * Fetch one row
  230. *
  231. * @param mixed $query Select query or MySQL result
  232. * @return array
  233. */
  234. public function fetchOne($query = false) {
  235. list($result) = $this->fetchRow($query);
  236. return $result;
  237. }
  238. /**
  239. * Fetch a field name in a result
  240. *
  241. * @param mixed $query Select query or MySQL result
  242. * @param int $offset Field offset
  243. * @return string Field name
  244. */
  245. public function fieldName($query = false, $offset) {
  246. $this->resCalc($query);
  247. return mysql_field_name($query, $offset);
  248. }
  249. /**
  250. * Fetch all field names in a result
  251. *
  252. * @param mixed $query Select query or MySQL result
  253. * @return array Field names
  254. */
  255. public function fieldNameArray($query = false) {
  256. $names = array();
  257. $field = $this->countFields($query);
  258. for ($i = 0; $i < $field; $i++)
  259. $names[] = $this->fieldName($query, $i);
  260. return $names;
  261. }
  262. /**
  263. * Free result memory
  264. *
  265. * @return boolean
  266. */
  267. public function freeResult() {
  268. return mysql_free_result($this->result);
  269. }
  270. /**
  271. * Add escape characters for importing data
  272. *
  273. * @param string $str String to parse
  274. * @return string
  275. */
  276. public function escapeString($str) {
  277. return mysql_real_escape_string($str, $this->link);
  278. }
  279. /**
  280. * Count number of rows in a result
  281. *
  282. * @param mixed $result Select query or MySQL result
  283. * @return int Number of rows
  284. */
  285. public function countRows($result = false) {
  286. $this->resCalc($result);
  287. return (int)mysql_num_rows($result);
  288. }
  289. /**
  290. * Count number of fields in a result
  291. *
  292. * @param mixed $result Select query or MySQL result
  293. * @return int Number of fields
  294. */
  295. public function countFields($result = false) {
  296. $this->resCalc($result);
  297. return (int)mysql_num_fields($result);
  298. }
  299. /**
  300. * Get last inserted id of the last query
  301. *
  302. * @return int Inserted in
  303. */
  304. public function insertId() {
  305. return (int)mysql_insert_id($this->link);
  306. }
  307. /**
  308. * Get number of affected rows of the last query
  309. *
  310. * @return int Affected rows
  311. */
  312. public function affectedRows() {
  313. return (int)mysql_affected_rows($this->link);
  314. }
  315. /**
  316. * Get the error description from the last query
  317. *
  318. * @return string
  319. */
  320. public function error() {
  321. return mysql_error($this->link);
  322. }
  323. /**
  324. * Dump MySQL info to page
  325. *
  326. * @return void
  327. */
  328. public function dumpInfo() {
  329. echo mysql_info($this->link);
  330. }
  331. /**
  332. * Close the link connection
  333. *
  334. * @return boolean
  335. */
  336. public function close() {
  337. return mysql_close($this->link);
  338. }
  339. /**
  340. * Determine the data type of a query
  341. *
  342. * @param mixed $result Query string or MySQL result set
  343. * @return void
  344. */
  345. private function resCalc(&$result) {
  346. if ($result == false)
  347. $result = $this->result;
  348. else {
  349. if (gettype($result) != 'resource')
  350. $result = $this->query($result);
  351. }
  352. return;
  353. }
  354. }