Source for file db-mysql.php

Documentation is available at db-mysql.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: db-mysql.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for MySQL database access. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package database */* MYSQL database interface
  27. * This is a database interface class. It is an impedance-matcher
  28. * between the high-level Axyl functions for accessing data, and
  29. * the specific functions suplpied by Php to access a particular
  30. * flavour of databse such as Postgres, MS-SQL Server, Sybase etc.
  31. * @package database
  32. * @access private
  33. */
  34. class db_mysql extends database {
  35. /** Constructor */
  36.  
  37. function db_mysql($name="", $user="", $passwd="", $host="", $port=0, $enc="", $datestyle="") {
  38. $this->database($name, $user, $passwd, $host, $port, $enc, $datestyle);
  39. $this->type = "mysql";
  40. }
  41. // ....................................................................
  42. /**
  43. * Connect to the database.
  44. * @param boolean $persistent Whether to connect persistently or not
  45. * @return boolean Status true if connected successfully
  46. */
  47. function connect($persistent=NOT_PERSISTENT) {
  48. if (!$this->connected) {
  49. $hoststr = $this->host;
  50. if ($this->port != 0) $hoststr .= ":" . $this->port;
  51. if ($persistent)
  52. $this->dbid = mysql_pconnect($hoststr, $this->user, $this->passwd);
  53. else
  54. $this->dbid = mysql_connect($hoststr, $this->user, $this->passwd);
  55. if ($this->dbid) {
  56. if (mysql_select_db($this->name, $this->dbid)) {
  57. $this->connected = TRUE;
  58. }
  59. }
  60. }
  61. return $this->connected;
  62. }
  63. // ....................................................................
  64. /** Disconnect from the database, if connected. */
  65.  
  66. function disconnect() {
  67. if (mysql_close($this->dbid)) {
  68. $this->connected = false;
  69. }
  70. }
  71. // ....................................................................
  72. /**
  73. * Execute a query on the connected database.
  74. * @param string $sql The SQL query to execute on the database
  75. * @return resource A database query resource ID, or false if query failed
  76. */
  77. function query($sql) {
  78. $sql = $this->convert_boolean_syntax($sql);
  79. $this->timer->restart();
  80. $rid = mysql_query($sql, $this->dbid);
  81. $this->timer->stop();
  82. $this->executable_sql = $sql;
  83. $this->rid = $rid;
  84. $this->query_report();
  85. return $rid;
  86. }
  87. // ....................................................................
  88. /**
  89. * Return the number of rows returned by a SELECT query.
  90. * @param resource $rid The resource ID for the executed query
  91. * @return integer The number of rows returned by the query
  92. */
  93. function numrows($rid) {
  94. return mysql_num_rows($rid);
  95. }
  96. // ....................................................................
  97. /**
  98. * Return the number of rows affected by a query.
  99. * @param resource $rid The resource ID for the executed query
  100. * @return integer The number of rows affected by the query
  101. */
  102. function affectedrows($rid) {
  103. return mysql_affected_rows($rid);
  104. }
  105. // ....................................................................
  106. /**
  107. * Free a resource.
  108. * @param resource $rid The resource ID for the executed query
  109. */
  110. function freeresult($rid) {
  111. mysql_free_result($rid);
  112. }
  113. // ....................................................................
  114. /**
  115. * Return the last error message.
  116. * @return string The last error message which was generated
  117. */
  118. function errormessage() {
  119. return mysql_error($this->dbid);
  120. }
  121. // ....................................................................
  122. /**
  123. * Return the specified row, as a standard (enumerated) array of
  124. * field values.
  125. * @param resource $rid The resource ID for the executed query
  126. * @param integer $rowno Row number (zero-based) of row to return
  127. * @return array Enumerated array of field values
  128. */
  129. function fetch_row($rid, $rowno) {
  130. if (mysql_data_seek($rid, $rowno)) {
  131. return mysql_fetch_row($rid);
  132. }
  133. else {
  134. return false;
  135. }
  136. }
  137. // ....................................................................
  138. /**
  139. * Return the specified row, as an associative array of fields
  140. * in a fieldname => value format.
  141. * @param resource $rid The resource ID for the executed query
  142. * @param integer $rowno Row number (zero-based) of row to return
  143. * @return array Associative array of field values
  144. */
  145. function fetch_array($rid, $rowno) {
  146. if (mysql_data_seek($rid, $rowno)) {
  147. return mysql_fetch_assoc($rid);
  148. }
  149. else {
  150. return false;
  151. }
  152. }
  153. // ....................................................................
  154. /**
  155. * Used to escape particular characters (typically the single quote) so
  156. * that they can form part of the database data, rather than being
  157. * interpreted as command syntax. Note that this implementation makes
  158. * sure the string has none of the deprecated backslashes added by the
  159. * old addslashes() function, before escaping the content properly.
  160. * @param string $str
  161. * @return string The same string with appropriate chars escaped.
  162. */
  163. function escape_string($str="") {
  164. if (is_string($str)) {
  165. $str = mysql_real_escape_string(stripslashes($str));
  166. }
  167. return $str;
  168. } // escape_string
  169. // ....................................................................
  170. /**
  171. * Given an Axyl SQL query object, build the SQL string from it
  172. * in suitable format for the currently connected database server.
  173. * @param pointer $sqlquery Pointer to an Axyl query object
  174. * @return string The SQL string built from the query object
  175. */
  176. function SQL(&$sqlquery) {
  177. $sql = "";
  178. switch (strtoupper($sqlquery->type)) {
  179. case "SELECT":
  180. $sql .= "SELECT ";
  181. if ($sqlquery->fields->total == 0) $sql .= "*";
  182. else $sql .= $sqlquery->fields->listed();
  183. $sql .= " FROM ";
  184. $sql .= $sqlquery->tables->listed();
  185. if ($sqlquery->where->total > 0) {
  186. $sql .= " WHERE ";
  187. $sql .= $sqlquery->where->listed(" ");
  188. }
  189. if ($sqlquery->groupby->total > 0) {
  190. $sql .= " GROUP BY ";
  191. $sql .= $sqlquery->groupby->listed();
  192. }
  193. if ($sqlquery->orderby->total > 0) {
  194. $sql .= " ORDER BY ";
  195. $sql .= $sqlquery->orderby->listed();
  196. }
  197. if ($sqlquery->limit > 0 || $sqlquery->offset > 0) {
  198. if ($sqlquery->limit > 0) {
  199. $sql .= " LIMIT ";
  200. if ($sqlquery->offset > 0) {
  201. $sql .= $sqlquery->offset . "," . $sqlquery->limit;
  202. }
  203. else {
  204. $sql .= $sqlquery->limit;
  205. }
  206. }
  207. }
  208. break;
  209.  
  210. case "INSERT":
  211. $sql .= "INSERT INTO ";
  212. $sql .= $sqlquery->tables->listed();
  213. if ($sqlquery->fields->total > 0) {
  214. $sql .= " (" . $sqlquery->fields->listed() . ")";
  215. }
  216. $sql .= " VALUES ";
  217. $sql .= "(" . $sqlquery->fields->values() . ")";
  218. break;
  219.  
  220. case "DELETE":
  221. $sql .= "DELETE FROM ";
  222. $sql .= $sqlquery->tables->listed();
  223. if ($sqlquery->where->total > 0) {
  224. $sql .= " WHERE ";
  225. $sql .= $sqlquery->where->listed(" ");
  226. }
  227. break;
  228.  
  229. case "UPDATE":
  230. $sql .= "UPDATE ";
  231. $sql .= $sqlquery->tables->listed();
  232. $sql .= " SET ";
  233. $sql .= $sqlquery->fields->equated();
  234. if ($sqlquery->where->total > 0) {
  235. $sql .= " WHERE ";
  236. $sql .= $sqlquery->where->listed(" ");
  237. }
  238. break;
  239. }
  240. // Render any NULL values..
  241. $sql = str_replace("'".NULLVALUE."'", "NULL", $sql);
  242.  
  243. // Return SQL we have built..
  244. return $sql;
  245. }
  246. }
  247.  
  248. // ----------------------------------------------------------------------
  249. // Ensure MySQL Php module is present..
  250.  
  251. if (!extension_loaded("mysql")) {
  252. if (!dl("mysql.so")) {
  253. exit;
  254. }
  255. }
  256. // ----------------------------------------------------------------------
  257. ?>

Documentation generated by phpDocumentor 1.3.0RC3