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

/system/vendor/class.eyemysqladap.inc.php

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