Documentation is available at db-mysql.php
- <?php
- /* ******************************************************************** */
- /* CATALYST PHP Source Code */
- /* -------------------------------------------------------------------- */
- /* This program is free software; you can redistribute it and/or modify */
- /* it under the terms of the GNU General Public License as published by */
- /* the Free Software Foundation; either version 2 of the License, or */
- /* (at your option) any later version. */
- /* */
- /* This program is distributed in the hope that it will be useful, */
- /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
- /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
- /* GNU General Public License for more details. */
- /* */
- /* You should have received a copy of the GNU General Public License */
- /* along with this program; if not, write to: */
- /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
- /* Boston, MA 02111-1307 USA */
- /* -------------------------------------------------------------------- */
- /* */
- /* Filename: db-mysql.php */
- /* Author: Paul Waite */
- /* Description: Definitions for MySQL database access. */
- /* */
- /* ******************************************************************** */
- /** @package database */* MYSQL database interface
- * This is a database interface class. It is an impedance-matcher
- * between the high-level Axyl functions for accessing data, and
- * the specific functions suplpied by Php to access a particular
- * flavour of databse such as Postgres, MS-SQL Server, Sybase etc.
- * @package database
- * @access private
- */
- class db_mysql extends database {
- /** Constructor */
- function db_mysql($name="", $user="", $passwd="", $host="", $port=0) {
- $this->database($name, $user, $passwd, $host, $port);
- $this->type = "mysql";
- }
- // ....................................................................
- /**
- * Connect to the database.
- * @param boolean $persistent Whether to connect persistently or not
- * @return boolean Status true if connected successfully
- */
- function connect($persistent==NOT_PERSISTENT) {
- if (!$this->connected) {
- $hoststr = $this->host;
- if ($this->port != 0) $hoststr .= ":" . $this->port;
- if ($persistent)
- $this->dbid = mysql_pconnect($hoststr, $this->user, $this->passwd);
- else
- $this->dbid = mysql_connect($hoststr, $this->user, $this->passwd);
- if ($this->dbid) {
- if (mysql_select_db($this->name, $this->dbid)) {
- $this->connected = TRUE;
- }
- }
- }
- return $this->connected;
- }
- // ....................................................................
- /** Disconnect from the database, if connected. */
- function disconnect() {
- if (mysql_close($this->dbid)) {
- $this->connected = false;
- }
- }
- // ....................................................................
- /**
- * Execute a query on the connected database.
- * @param string $sql The SQL query to execute on the database
- * @return resource A database query resource ID, or false if query failed
- */
- function query($sql) {
- $sql = $this->convert_boolean_syntax($sql);
- $this->timer->restart();
- $rid = mysql_query($sql, $this->dbid);
- $this->timer->stop();
- $this->executable_sql = $sql;
- $this->rid = $rid;
- $this->query_report();
- return $rid;
- }
- // ....................................................................
- /**
- * Return the number of rows returned by a SELECT query.
- * @param resource $rid The resource ID for the executed query
- * @return integer The number of rows returned by the query
- */
- function numrows($rid) {
- return mysql_num_rows($rid);
- }
- // ....................................................................
- /**
- * Return the number of rows affected by a query.
- * @param resource $rid The resource ID for the executed query
- * @return integer The number of rows affected by the query
- */
- function affectedrows($rid) {
- return mysql_affected_rows($rid);
- }
- // ....................................................................
- /**
- * Free a resource.
- * @param resource $rid The resource ID for the executed query
- */
- function freeresult($rid) {
- mysql_free_result($rid);
- }
- // ....................................................................
- /**
- * Return the last error message.
- * @return string The last error message which was generated
- */
- function errormessage() {
- return mysql_error($this->dbid);
- }
- // ....................................................................
- /**
- * Return the specified row, as a standard (enumerated) array of
- * field values.
- * @param resource $rid The resource ID for the executed query
- * @param integer $rowno Row number (zero-based) of row to return
- * @return array Enumerated array of field values
- */
- function fetch_row($rid, $rowno) {
- if (mysql_data_seek($rid, $rowno)) {
- return mysql_fetch_row($rid);
- }
- else {
- return false;
- }
- }
- // ....................................................................
- /**
- * Return the specified row, as an associative array of fields
- * in a fieldname => value format.
- * @param resource $rid The resource ID for the executed query
- * @param integer $rowno Row number (zero-based) of row to return
- * @return array Associative array of field values
- */
- function fetch_array($rid, $rowno) {
- if (mysql_data_seek($rid, $rowno)) {
- return mysql_fetch_assoc($rid);
- }
- else {
- return false;
- }
- }
- // ....................................................................
- /**
- * Given an Axyl SQL query object, build the SQL string from it
- * in suitable format for the currently connected database server.
- * @param pointer $sqlquery Pointer to an Axyl query object
- * @return string The SQL string built from the query object
- */
- function SQL(&$sqlquery) {
- $sql = "";
- switch (strtoupper($sqlquery->type)) {
- case "SELECT":
- $sql .= "SELECT ";
- if ($sqlquery->fields->total == 0) $sql .= "*";
- else $sql .= $sqlquery->fields->listed();
- $sql .= " FROM ";
- $sql .= $sqlquery->tables->listed();
- if ($sqlquery->where->total > 0) {
- $sql .= " WHERE ";
- $sql .= $sqlquery->where->listed(" ");
- }
- if ($sqlquery->groupby->total > 0) {
- $sql .= " GROUP BY ";
- $sql .= $sqlquery->groupby->listed();
- }
- if ($sqlquery->orderby->total > 0) {
- $sql .= " ORDER BY ";
- $sql .= $sqlquery->orderby->listed();
- }
- if ($sqlquery->limit > 0 || $sqlquery->offset > 0) {
- if ($sqlquery->limit > 0) {
- $sql .= " LIMIT ";
- if ($sqlquery->offset > 0) {
- $sql .= $sqlquery->offset . "," . $sqlquery->limit;
- }
- else {
- $sql .= $sqlquery->limit;
- }
- }
- }
- break;
- case "INSERT":
- $sql .= "INSERT INTO ";
- $sql .= $sqlquery->tables->listed();
- if ($sqlquery->fields->total > 0) {
- $sql .= " (" . $sqlquery->fields->listed() . ")";
- }
- $sql .= " VALUES ";
- $sql .= "(" . $sqlquery->fields->values() . ")";
- break;
- case "DELETE":
- $sql .= "DELETE FROM ";
- $sql .= $sqlquery->tables->listed();
- if ($sqlquery->where->total > 0) {
- $sql .= " WHERE ";
- $sql .= $sqlquery->where->listed(" ");
- }
- break;
- case "UPDATE":
- $sql .= "UPDATE ";
- $sql .= $sqlquery->tables->listed();
- $sql .= " SET ";
- $sql .= $sqlquery->fields->equated();
- if ($sqlquery->where->total > 0) {
- $sql .= " WHERE ";
- $sql .= $sqlquery->where->listed(" ");
- }
- break;
- }
- // Render any NULL values..
- $sql = str_replace("'".NULLVALUE."'", "NULL", $sql);
- // Return SQL we have built..
- return $sql;
- }
- }
- // ----------------------------------------------------------------------
- // Ensure MySQL Php module is present..
- if (!extension_loaded("mysql")) {
- if (!dl("mysql.so")) {
- exit;
- }
- }
- // ----------------------------------------------------------------------
- ?>
Documentation generated by phpDocumentor 1.3.0RC3