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) {
  38. $this->database($name, $user, $passwd, $host, $port);
  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. * Given an Axyl SQL query object, build the SQL string from it
  156. * in suitable format for the currently connected database server.
  157. * @param pointer $sqlquery Pointer to an Axyl query object
  158. * @return string The SQL string built from the query object
  159. */
  160. function SQL(&$sqlquery) {
  161. $sql = "";
  162. switch (strtoupper($sqlquery->type)) {
  163. case "SELECT":
  164. $sql .= "SELECT ";
  165. if ($sqlquery->fields->total == 0) $sql .= "*";
  166. else $sql .= $sqlquery->fields->listed();
  167. $sql .= " FROM ";
  168. $sql .= $sqlquery->tables->listed();
  169. if ($sqlquery->where->total > 0) {
  170. $sql .= " WHERE ";
  171. $sql .= $sqlquery->where->listed(" ");
  172. }
  173. if ($sqlquery->groupby->total > 0) {
  174. $sql .= " GROUP BY ";
  175. $sql .= $sqlquery->groupby->listed();
  176. }
  177. if ($sqlquery->orderby->total > 0) {
  178. $sql .= " ORDER BY ";
  179. $sql .= $sqlquery->orderby->listed();
  180. }
  181. if ($sqlquery->limit > 0 || $sqlquery->offset > 0) {
  182. if ($sqlquery->limit > 0) {
  183. $sql .= " LIMIT ";
  184. if ($sqlquery->offset > 0) {
  185. $sql .= $sqlquery->offset . "," . $sqlquery->limit;
  186. }
  187. else {
  188. $sql .= $sqlquery->limit;
  189. }
  190. }
  191. }
  192. break;
  193.  
  194. case "INSERT":
  195. $sql .= "INSERT INTO ";
  196. $sql .= $sqlquery->tables->listed();
  197. if ($sqlquery->fields->total > 0) {
  198. $sql .= " (" . $sqlquery->fields->listed() . ")";
  199. }
  200. $sql .= " VALUES ";
  201. $sql .= "(" . $sqlquery->fields->values() . ")";
  202. break;
  203.  
  204. case "DELETE":
  205. $sql .= "DELETE FROM ";
  206. $sql .= $sqlquery->tables->listed();
  207. if ($sqlquery->where->total > 0) {
  208. $sql .= " WHERE ";
  209. $sql .= $sqlquery->where->listed(" ");
  210. }
  211. break;
  212.  
  213. case "UPDATE":
  214. $sql .= "UPDATE ";
  215. $sql .= $sqlquery->tables->listed();
  216. $sql .= " SET ";
  217. $sql .= $sqlquery->fields->equated();
  218. if ($sqlquery->where->total > 0) {
  219. $sql .= " WHERE ";
  220. $sql .= $sqlquery->where->listed(" ");
  221. }
  222. break;
  223. }
  224. // Render any NULL values..
  225. $sql = str_replace("'".NULLVALUE."'", "NULL", $sql);
  226.  
  227. // Return SQL we have built..
  228. return $sql;
  229. }
  230. }
  231.  
  232. // ----------------------------------------------------------------------
  233. // Ensure MySQL Php module is present..
  234.  
  235. if (!extension_loaded("mysql")) {
  236. if (!dl("mysql.so")) {
  237. exit;
  238. }
  239. }
  240. // ----------------------------------------------------------------------
  241. ?>

Documentation generated by phpDocumentor 1.3.0RC3