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

/ xvweb/core/libraries/pdo/PDOStatement_mysql.class.php

http://xvweb.googlecode.com/
PHP | 352 lines | 197 code | 21 blank | 134 comment | 24 complexity | 5906103d67f96254ef1187a16aa89a0e MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-3.0
  1. <?php
  2. /** File PDOStatement_mysql.class.php *
  3. *(C) Andrea Giammarchi [2005/10/13] */
  4. /**
  5. * Class PDOStatement_mysql
  6. * This class is used from class PDO_mysql to manage a MySQL database.
  7. * Look at PDO.clas.php file comments to know more about MySQL connection.
  8. * ---------------------------------------------
  9. * @Compatibility >= PHP 4
  10. * @Dependencies PDO.class.php
  11. * PDO_mysql.class.php
  12. * @Author Andrea Giammarchi
  13. * @Site http://www.devpro.it/
  14. * @Mail andrea [ at ] 3site [ dot ] it
  15. * @Date 2005/10/13
  16. * @LastModified 2006/01/29 09:30 [fixed execute bug]
  17. * @Version 0.1b - tested
  18. */
  19. class PDOStatement_mysql {
  20. /**
  21. * 'Private' variables:
  22. * __connection:Resource Database connection
  23. * __dbinfo:Array Array with 4 elements used to manage connection
  24. * __persistent:Boolean Connection mode, is true on persistent, false on normal (deafult) connection
  25. * __query:String Last query used
  26. * __result:Resource Last result from last query
  27. * __fetchmode:Integer constant PDO_FETCH_* result mode
  28. * __errorCode:String Last error string code
  29. * __errorInfo:Array Last error informations, code, number, details
  30. * __boundParams:Array Stored bindParam
  31. */
  32. var $__connection;
  33. var $__dbinfo;
  34. var $__persistent = false;
  35. var $__query = '';
  36. var $__result = null;
  37. var $__fetchmode = PDO::FETCH_BOTH;
  38. var $__errorCode = '';
  39. var $__errorInfo = Array('');
  40. var $__boundParams = Array();
  41. /**
  42. * Public constructor:
  43. * Called from PDO to create a PDOStatement for this database
  44. * new PDOStatement_sqlite( &$__query:String, &$__connection:Resource, $__dbinfo:Array )
  45. * @Param String query to prepare
  46. * @Param Resource database connection
  47. * @Param Array 4 elements array to manage connection
  48. */
  49. function PDOStatement_mysql(&$__query, &$__connection, &$__dbinfo) {
  50. $this->__query = &$__query;
  51. $this->__connection = &$__connection;
  52. $this->__dbinfo = &$__dbinfo;
  53. }
  54. /**
  55. * Public method:
  56. * Replace ? or :named values to execute prepared query
  57. * this->bindParam( $mixed:Mixed, &$variable:Mixed, $type:Integer, $lenght:Integer ):Void
  58. * @Param Mixed Integer or String to replace prepared value
  59. * @Param Mixed variable to replace
  60. * @Param Integer this variable is not used but respects PDO original accepted parameters
  61. * @Param Integer this variable is not used but respects PDO original accepted parameters
  62. */
  63. function bindParam($mixed, &$variable, $type = null, $lenght = null) {
  64. if(is_string($mixed))
  65. $this->__boundParams[$mixed] = $variable;
  66. else
  67. array_push($this->__boundParams, $variable);
  68. }
  69. /**
  70. * Public method:
  71. * Checks if query was valid and returns how may fields returns
  72. * this->columnCount( void ):Void
  73. */
  74. function columnCount() {
  75. $result = 0;
  76. if(!is_null($this->__result))
  77. $result = mysql_num_fields($this->__result);
  78. return $result;
  79. }
  80. /**
  81. * Public method:
  82. * Returns a code rappresentation of an error
  83. * this->errorCode( void ):String
  84. * @Return String String rappresentation of the error
  85. */
  86. function errorCode() {
  87. return $this->__errorCode;
  88. }
  89. /**
  90. * Public method:
  91. * Returns an array with error informations
  92. * this->errorInfo( void ):Array
  93. * @Return Array Array with 3 keys:
  94. * 0 => error code
  95. * 1 => error number
  96. * 2 => error string
  97. */
  98. function errorInfo() {
  99. return $this->__errorInfo;
  100. }
  101. /**
  102. * Public method:
  103. * Excecutes a query and returns true on success or false.
  104. * this->exec( $array:Array ):Boolean
  105. * @Param Array If present, it should contain all replacements for prepared query
  106. * @Return Boolean true if query has been done without errors, false otherwise
  107. */
  108. function execute($array = Array()) {
  109. if(count($this->__boundParams) > 0)
  110. $array = &$this->__boundParams;
  111. $__query = $this->__query;
  112. if(count($array) > 0) {
  113. foreach($array as $k => $v) {
  114. if(!is_int($k) || substr($k, 0, 1) === ':') {
  115. if(!isset($tempf))
  116. $tempf = $tempr = array();
  117. array_push($tempf, $k);
  118. array_push($tempr, '"'.mysql_escape_string($v).'"');
  119. }
  120. else {
  121. $parse = create_function('$v', 'return \'"\'.mysql_escape_string($v).\'"\';');
  122. $__query = preg_replace("/(\?)/e", '$parse($array[$k++]);', $__query);
  123. break;
  124. }
  125. }
  126. if(isset($tempf))
  127. $__query = str_replace($tempf, $tempr, $__query);
  128. }
  129. if(is_null($this->__result = &$this->__uquery($__query)))
  130. $keyvars = false;
  131. else
  132. $keyvars = true;
  133. $this->__boundParams = array();
  134. return $keyvars;
  135. }
  136. /**
  137. * Public method:
  138. * Returns, if present, next row of executed query or false.
  139. * this->fetch( $mode:Integer, $cursor:Integer, $offset:Integer ):Mixed
  140. * @Param Integer PDO_FETCH_* constant to know how to read next row, default PDO_FETCH_BOTH
  141. * NOTE: if $mode is omitted is used default setted mode, PDO_FETCH_BOTH
  142. * @Param Integer this variable is not used but respects PDO original accepted parameters
  143. * @Param Integer this variable is not used but respects PDO original accepted parameters
  144. * @Return Mixed Next row of executed query or false if there is nomore.
  145. */
  146. function fetch($mode = PDO_FETCH_BOTH, $cursor = null, $offset = null) {
  147. if(func_num_args() == 0)
  148. $mode = &$this->__fetchmode;
  149. $result = false;
  150. if(!is_null($this->__result)) {
  151. switch($mode) {
  152. case PDO_FETCH_NUM:
  153. $result = mysql_fetch_row($this->__result);
  154. break;
  155. case PDO_FETCH_ASSOC:
  156. $result = mysql_fetch_assoc($this->__result);
  157. break;
  158. case PDO_FETCH_OBJ:
  159. $result = mysql_fetch_object($this->__result);
  160. break;
  161. case PDO_FETCH_BOTH:
  162. default:
  163. $result = mysql_fetch_array($this->__result);
  164. break;
  165. }
  166. }
  167. if(!$result)
  168. $this->__result = null;
  169. return $result;
  170. }
  171. /**
  172. * Public method:
  173. * Returns an array with all rows of executed query.
  174. * this->fetchAll( $mode:Integer ):Array
  175. * @Param Integer PDO_FETCH_* constant to know how to read all rows, default PDO_FETCH_BOTH
  176. * NOTE: this doesn't work as fetch method, then it will use always PDO_FETCH_BOTH
  177. * if this param is omitted
  178. * @Return Array An array with all fetched rows
  179. */
  180. function fetchAll($mode = PDO_FETCH_BOTH) {
  181. $result = array();
  182. if(!is_null($this->__result)) {
  183. switch($mode) {
  184. case PDO_FETCH_NUM:
  185. while($r = mysql_fetch_row($this->__result))
  186. array_push($result, $r);
  187. break;
  188. case PDO_FETCH_ASSOC:
  189. while($r = mysql_fetch_assoc($this->__result))
  190. array_push($result, $r);
  191. break;
  192. case PDO_FETCH_OBJ:
  193. while($r = mysql_fetch_object($this->__result))
  194. array_push($result, $r);
  195. break;
  196. case PDO_FETCH_BOTH:
  197. default:
  198. while($r = mysql_fetch_array($this->__result))
  199. array_push($result, $r);
  200. break;
  201. }
  202. }
  203. $this->__result = null;
  204. return $result;
  205. }
  206. /**
  207. * Public method:
  208. * Returns, if present, first column of next row of executed query
  209. * this->fetchSingle( void ):Mixed
  210. * @Return Mixed Null or next row's first column
  211. */
  212. function fetchSingle() {
  213. $result = null;
  214. if(!is_null($this->__result)) {
  215. $result = @mysql_fetch_row($this->__result);
  216. if($result)
  217. $result = $result[0];
  218. else
  219. $this->__result = null;
  220. }
  221. return $result;
  222. }
  223. /**
  224. * Public method:
  225. * Returns number of last affected database rows
  226. * this->rowCount( void ):Integer
  227. * @Return Integer number of last affected rows
  228. * NOTE: works with INSERT, UPDATE and DELETE query type
  229. */
  230. function rowCount() {
  231. return mysql_affected_rows($this->__connection);
  232. }
  233. // NOT TOTALLY SUPPORTED PUBLIC METHODS
  234. /**
  235. * Public method:
  236. * Quotes correctly a string for this database
  237. * this->getAttribute( $attribute:Integer ):Mixed
  238. * @Param Integer a constant [ PDO_ATTR_SERVER_INFO,
  239. * PDO_ATTR_SERVER_VERSION,
  240. * PDO_ATTR_CLIENT_VERSION,
  241. * PDO_ATTR_PERSISTENT ]
  242. * @Return Mixed correct information or false
  243. */
  244. function getAttribute($attribute) {
  245. $result = false;
  246. switch($attribute) {
  247. case PDO_ATTR_SERVER_INFO:
  248. $result = mysql_get_host_info($this->__connection);
  249. break;
  250. case PDO_ATTR_SERVER_VERSION:
  251. $result = mysql_get_server_info($this->__connection);
  252. break;
  253. case PDO_ATTR_CLIENT_VERSION:
  254. $result = mysql_get_client_info();
  255. break;
  256. case PDO_ATTR_PERSISTENT:
  257. $result = $this->__persistent;
  258. break;
  259. }
  260. return $result;
  261. }
  262. /**
  263. * Public method:
  264. * Sets database attributes, in this version only connection mode.
  265. * this->setAttribute( $attribute:Integer, $mixed:Mixed ):Boolean
  266. * @Param Integer PDO_* constant, in this case only PDO_ATTR_PERSISTENT
  267. * @Param Mixed value for PDO_* constant, in this case a Boolean value
  268. * true for permanent connection, false for default not permament connection
  269. * @Return Boolean true on change, false otherwise
  270. */
  271. function setAttribute($attribute, $mixed) {
  272. $result = false;
  273. if($attribute === PDO_ATTR_PERSISTENT && $mixed != $this->__persistent) {
  274. $result = true;
  275. $this->__persistent = (boolean) $mixed;
  276. mysql_close($this->__connection);
  277. if($this->__persistent === true)
  278. $this->__connection = &mysql_pconnect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  279. else
  280. $this->__connection = &mysql_connect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  281. mysql_select_db($this->__dbinfo[3], $this->__connection);
  282. }
  283. return $result;
  284. }
  285. /**
  286. * Public method:
  287. * Sets default fetch mode to use with this->fetch() method.
  288. * this->setFetchMode( $mode:Integer ):Boolean
  289. * @Param Integer PDO_FETCH_* constant to use while reading an execute query with fetch() method.
  290. * NOTE: PDO_FETCH_LAZY and PDO_FETCH_BOUND are not supported
  291. * @Return Boolean true on change, false otherwise
  292. */
  293. function setFetchMode($mode) {
  294. $result = false;
  295. switch($mode) {
  296. case PDO_FETCH_NUM:
  297. case PDO_FETCH_ASSOC:
  298. case PDO_FETCH_OBJ:
  299. case PDO_FETCH_BOTH:
  300. $result = true;
  301. $this->__fetchmode = &$mode;
  302. break;
  303. }
  304. return $result;
  305. }
  306. // UNSUPPORTED PUBLIC METHODS
  307. function bindColumn($mixewd, &$param, $type = null, $max_length = null, $driver_option = null) {
  308. return false;
  309. }
  310. function __setErrors($er) {
  311. if(!is_resource($this->__connection)) {
  312. $errno = mysql_errno();
  313. $errst = mysql_error();
  314. }
  315. else {
  316. $errno = mysql_errno($this->__connection);
  317. $errst = mysql_error($this->__connection);
  318. }
  319. $this->__errorCode = &$er;
  320. $this->__errorInfo = Array($this->__errorCode, $errno, $errst);
  321. $this->__result = null;
  322. }
  323. function __uquery(&$query) {
  324. if(!@$query = mysql_query($query, $this->__connection)) {
  325. $this->__setErrors('SQLER');
  326. $query = null;
  327. }
  328. return $query;
  329. }
  330. }
  331. ?>