PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/frog/libraries/do_lite_drivers/DoMysql.php

https://github.com/Doap/FrogCMS
PHP | 428 lines | 273 code | 69 blank | 86 comment | 40 complexity | f51948bc7146f64386930c38367ac65a MD5 | raw file
  1. <?php
  2. /**
  3. * Frog CMS - Content Management Simplified. <http://www.madebyfrog.com>
  4. * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
  5. *
  6. * This file is part of Frog CMS.
  7. *
  8. * Frog CMS is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Frog CMS is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Frog CMS. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Frog CMS has made an exception to the GNU General Public License for plugins.
  22. * See exception.txt for details and the full text.
  23. */
  24. /**
  25. * Light PDO adpater for MySQL.
  26. *
  27. * @package frog
  28. * @subpackage libraries.dolite
  29. *
  30. * @author Philippe Archambault <philippe.archambault@gmail.com>
  31. * @version 0.0.1
  32. * @since Frog version 0.9.0
  33. * @license http://www.gnu.org/licenses/gpl.html GPL License
  34. * @copyright Philippe Archambault, 2008
  35. */
  36. /**
  37. * PDO adpater for Mysql
  38. *
  39. * For more specification on methods of this class check the PDO class
  40. * can be find on the same directory at DoLite.php
  41. *
  42. * @author Philippe Archambault <philippe.archambault@gmail.com>
  43. */
  44. class DoMysql
  45. {
  46. public $errorCode = '';
  47. public $errorInfo = array();
  48. private $_connection;
  49. private $_dbinfo;
  50. private $_persistent = false;
  51. /**
  52. * Checks connection and database selection
  53. *
  54. * @param string $host host with or without port info
  55. * @param string $db database name
  56. * @param string $user database user
  57. * @param string $pass database password
  58. */
  59. public function __construct($host, $db, $user, $pass)
  60. {
  61. if ( ! $this->_connection = mysql_connect($host, $user, $pass)) {
  62. $this->_setErrors('DBCON');
  63. } else {
  64. if ( ! mysql_select_db($db, $this->_connection)) {
  65. $this->_setErrors('DBER');
  66. } else {
  67. $this->_dbinfo = array(
  68. 'host' => $host,
  69. 'user' => $user,
  70. 'pass' => $pass,
  71. 'name' => $db
  72. );
  73. }
  74. }
  75. }
  76. // -----------------------------------------------------------------------
  77. public function exec($query)
  78. {
  79. if (mysql_query($query, $this->_connection)) {
  80. return mysql_affected_rows($this->_connection);
  81. }
  82. // else
  83. $this->_setErrors('SQLER');
  84. return false;
  85. }
  86. // -----------------------------------------------------------------------
  87. public function lastInsertId()
  88. {
  89. return mysql_insert_id($this->_connection);
  90. }
  91. public function prepare($query)
  92. {
  93. return new DoLiteStatementMysql($query, $this->_connection, $this->_dbinfo);
  94. }
  95. // -----------------------------------------------------------------------
  96. public function query($query)
  97. {
  98. $result_set = mysql_query($query, $this->_connection); // before was unbuffered
  99. if ($result_set) {
  100. $result = array();
  101. while ($row = mysql_fetch_assoc($result_set))
  102. array_push($result, $row);
  103. } else {
  104. $result = false;
  105. $this->_setErrors('SQLER');
  106. }
  107. return $result;
  108. }
  109. // -----------------------------------------------------------------------
  110. public function quote($string)
  111. {
  112. return "'".mysql_real_escape_string($string)."'";
  113. }
  114. // -----------------------------------------------------------------------
  115. public function getAttribute($attribute)
  116. {
  117. switch($attribute) {
  118. case DoLite::ATTR_SERVER_INFO:
  119. return mysql_get_host_info($this->_connection);
  120. break;
  121. case DoLite::ATTR_SERVER_VERSION:
  122. return mysql_get_server_info($this->_connection);
  123. break;
  124. case DoLite::ATTR_CLIENT_VERSION:
  125. return mysql_get_client_info();
  126. break;
  127. case DoLite::ATTR_PERSISTENT:
  128. return $this->_persistent;
  129. break;
  130. case DoLite::ATTR_DRIVER_NAME:
  131. return 'mysql';
  132. break;
  133. }
  134. return null;
  135. }
  136. // -----------------------------------------------------------------------
  137. function setAttribute($attribute, $mixed)
  138. {
  139. if ($attribute === DoLite::ATTR_PERSISTENT && $mixed != $this->_persistent) {
  140. $this->_persistent = (bool) $mixed;
  141. mysql_close($this->_connection);
  142. if ($this->_persistent === true)
  143. $this->_connection = mysql_pconnect($this->_dbinfo['host'], $this->_dbinfo['user'], $this->_dbinfo['pass']);
  144. else
  145. $this->_connection = mysql_connect($this->_dbinfo['host'], $this->_dbinfo['user'], $this->_dbinfo['pass']);
  146. mysql_select_db($this->_dbinfo['name'], $this->_connection);
  147. return true;
  148. }
  149. return false;
  150. }
  151. // -----------------------------------------------------------------------
  152. function beginTransaction()
  153. {
  154. return (bool) mysql_query('BEGIN WORK');
  155. }
  156. // -----------------------------------------------------------------------
  157. function commit()
  158. {
  159. return (bool) mysql_query('COMMIT');
  160. }
  161. // -----------------------------------------------------------------------
  162. function rollBack()
  163. {
  164. return (bool) mysql_query('ROLLBACK');
  165. }
  166. // -----------------------------------------------------------------------
  167. //
  168. // private methods
  169. //
  170. // -----------------------------------------------------------------------
  171. private function _setErrors($error)
  172. {
  173. if (!is_resource($this->_connection)) {
  174. $errno = mysql_errno();
  175. $errst = mysql_error();
  176. } else {
  177. $errno = mysql_errno($this->_connection);
  178. $errst = mysql_error($this->_connection);
  179. }
  180. $this->errorCode = $error;
  181. $this->errorInfo = array($error, $errno, $errst);
  182. }
  183. } // End DoMysql class
  184. /**
  185. * Lite PDOStatement adpater for MySQL
  186. *
  187. * For more specification on methods of this class check the PDO class
  188. * can be find on the same directory at PDO.php
  189. *
  190. */
  191. class DoLiteStatementMysql extends DoLiteStatement
  192. {
  193. function __construct($query, $connection, $dbinfo)
  194. {
  195. $this->_query = $query;
  196. $this->_connection = $connection;
  197. $this->_dbinfo = $dbinfo;
  198. }
  199. // -----------------------------------------------------------------------
  200. public function bindParam($parameter, &$variable, $data_type=null, $length=null, $driver_options=null)
  201. {
  202. $escaped_var = "'".mysql_real_escape_string($variable)."'";
  203. if (is_int($parameter)) {
  204. $this->_bindParams[$parameter] = $escaped_var;
  205. } else {
  206. $this->_query = str_replace($parameter, $escaped_var, $this->_query);
  207. }
  208. }
  209. // -----------------------------------------------------------------------
  210. public function closeCursor()
  211. {
  212. return mysql_free_result($this->_result);
  213. }
  214. // -----------------------------------------------------------------------
  215. public function columnCount()
  216. {
  217. return $this->_result ? mysql_num_fields($this->_result): 0;
  218. }
  219. // -----------------------------------------------------------------------
  220. public function fetch($mode=null, $cursor=null, $offset=null)
  221. {
  222. if (is_null($mode)) $mode = &$this->_fetchmode;
  223. if ($this->_result) {
  224. switch($mode) {
  225. case DoLite::FETCH_NUM:
  226. return mysql_fetch_row($this->_result);
  227. break;
  228. case DoLite::FETCH_ASSOC:
  229. return mysql_fetch_assoc($this->_result);
  230. break;
  231. case DoLite::FETCH_OBJ:
  232. return $this->fetchObject();
  233. break;
  234. case DoLite::FETCH_BOTH:
  235. default:
  236. return mysql_fetch_array($this->_result);
  237. }
  238. }
  239. return false;
  240. }
  241. // -----------------------------------------------------------------------
  242. public function fetchObject($class_name=null , $ctor_args=null)
  243. {
  244. if (is_null($class_name)) {
  245. return mysql_fetch_object($this->_result);
  246. } else if (is_array($ctor_args)) {
  247. return mysql_fetch_object($this->_result, $class_name, $ctor_args);
  248. } else {
  249. return mysql_fetch_object($this->_result, $class_name);
  250. }
  251. }
  252. // -----------------------------------------------------------------------
  253. public function fetchAll($mode=null)
  254. {
  255. if (is_null($mode)) $mode = $this->_fetchmode;
  256. $result = array();
  257. if ($this->_result) {
  258. switch ($mode) {
  259. case DoLite::FETCH_NUM:
  260. while ($row = mysql_fetch_row($this->_result))
  261. array_push($result, $row);
  262. break;
  263. case DoLite::FETCH_ASSOC:
  264. while ($row = mysql_fetch_assoc($this->_result))
  265. array_push($result, $row);
  266. break;
  267. case DoLite::FETCH_OBJ:
  268. while ($row = $this->fetchObject())
  269. array_push($result, $row);
  270. break;
  271. case DoLite::FETCH_BOTH:
  272. default:
  273. while ($row = mysql_fetch_array($this->_result))
  274. array_push($result, $row);
  275. break;
  276. }
  277. }
  278. return $result;
  279. }
  280. // -----------------------------------------------------------------------
  281. public function fetchColumn($column=1)
  282. {
  283. if ($column < 1) $column = 1;
  284. if ($this->_result) {
  285. $result = mysql_fetch_row($this->_result);
  286. if ($result)
  287. return $result[$column-1];
  288. }
  289. return null;
  290. }
  291. // -----------------------------------------------------------------------
  292. public function quote($string)
  293. {
  294. return "'".mysql_real_escape_string($string)."'";
  295. }
  296. // -----------------------------------------------------------------------
  297. public function rowCount()
  298. {
  299. return mysql_affected_rows($this->_connection);
  300. }
  301. // -----------------------------------------------------------------------
  302. public function getAttribute($attribute)
  303. {
  304. switch ($attribute) {
  305. case DoLite::ATTR_SERVER_INFO:
  306. return mysql_get_host_info($this->_connection);
  307. break;
  308. case DoLite::ATTR_SERVER_VERSION:
  309. return mysql_get_server_info($this->_connection);
  310. break;
  311. case DoLite::ATTR_CLIENT_VERSION:
  312. return mysql_get_client_info();
  313. break;
  314. case DoLite::ATTR_PERSISTENT:
  315. return $this->_persistent;
  316. break;
  317. }
  318. return null;
  319. }
  320. // -----------------------------------------------------------------------
  321. public function setAttribute($attribute, $mixed)
  322. {
  323. if ($attribute === DoLite::ATTR_PERSISTENT && $mixed != $this->_persistent) {
  324. $this->_persistent = (bool) $mixed;
  325. mysql_close($this->_connection);
  326. if ($this->_persistent === true)
  327. $this->_connection = mysql_pconnect($this->_dbinfo['host'], $this->_dbinfo['user'], $this->_dbinfo['pass']);
  328. else
  329. $this->_connection = mysql_connect($this->_dbinfo['host'], $this->_dbinfo['user'], $this->_dbinfo['pass']);
  330. mysql_select_db($this->_dbinfo['name'], $this->_connection);
  331. return true;
  332. }
  333. return false;
  334. }
  335. //
  336. // private methods
  337. //
  338. protected function _setErrors($error)
  339. {
  340. if ( ! is_resource($this->_connection)) {
  341. $errno = mysql_errno();
  342. $errst = mysql_error();
  343. }
  344. else {
  345. $errno = mysql_errno($this->_connection);
  346. $errst = mysql_error($this->_connection);
  347. }
  348. $this->_errorCode = $error;
  349. $this->_errorInfo = array($error, $errno, $errst);
  350. }
  351. protected function _query($query)
  352. {
  353. if ( ! $query = mysql_query($query, $this->_connection)) {
  354. $this->_setErrors('SQLER');
  355. $query = null;
  356. }
  357. return $query;
  358. }
  359. } // End DoLiteStatementMysql class