home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Magazine / HomeAutomation / Apache / lib / php / DB / mysql.php < prev    next >
Encoding:
PHP Script  |  2000-07-11  |  9.3 KB  |  384 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // Database independent query interface definition for PHP's MySQL
  21. // extension.
  22. //
  23.  
  24. //
  25. // XXX legend:
  26. //
  27. // XXX ERRORMSG: The error message from the mysql function should
  28. //                 be registered here.
  29. //
  30.  
  31. include_once 'DB/common.php';
  32.  
  33. class DB_mysql extends DB_common {
  34.     // {{{ properties
  35.  
  36.     var $connection;
  37.     var $phptype, $dbsyntax;
  38.     var $prepare_tokens = array();
  39.     var $prepare_types = array();
  40.  
  41.     // }}}
  42.  
  43.     // {{{ constructor
  44.  
  45.     function DB_mysql() {
  46.         $this->phptype = 'mysql';
  47.         $this->dbsyntax = 'mysql';
  48.         $this->features = array(
  49.             'prepare' => false,
  50.             'pconnect' => true,
  51.             'transactions' => false
  52.         );
  53.         $this->errorcode_map = array(
  54.             1004 => DB_ERROR_CANNOT_CREATE,
  55.             1005 => DB_ERROR_CANNOT_CREATE,
  56.             1006 => DB_ERROR_CANNOT_CREATE,
  57.             1007 => DB_ERROR_ALREADY_EXISTS,
  58.             1008 => DB_ERROR_CANNOT_DROP,
  59.             1046 => DB_ERROR_NODBSELECTED,
  60.             1146 => DB_ERROR_NOSUCHTABLE,
  61.             1064 => DB_ERROR_SYNTAX,
  62.             1054 => DB_ERROR_NOSUCHFIELD,
  63.             1062 => DB_ERROR_ALREADY_EXISTS,
  64.             1051 => DB_ERROR_NOSUCHTABLE
  65.         );
  66.     }
  67.  
  68.     // }}}
  69.  
  70.     // {{{ connect()
  71.  
  72.     /**
  73.      * Connect to a database and log in as the specified user.
  74.      *
  75.      * @param $dsn the data source name (see DB::parseDSN for syntax)
  76.      * @param $persistent (optional) whether the connection should
  77.      *        be persistent
  78.      *
  79.      * @return int DB_OK on success, a DB error code on failure
  80.      */
  81.     function connect($dsn, $persistent = false) {
  82.         if (is_array($dsn)) {
  83.             $dsninfo = &$dsn;
  84.         } else {
  85.             $dsninfo = DB::parseDSN($dsn);
  86.         }
  87.         if (!$dsninfo || !$dsninfo['phptype']) {
  88.             return DB_ERROR; // XXX ERRORMSG
  89.         }
  90.         $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  91.         $user = $dsninfo['username'];
  92.         $pw = $dsninfo['password'];
  93.         $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
  94.         if ($dbhost && $user && $pw) {
  95.             $conn = $connect_function($dbhost, $user, $pw);
  96.         } elseif ($dbhost && $user) {
  97.             $conn = $connect_function($dbhost, $user);
  98.         } elseif ($dbhost) {
  99.             $conn = $connect_function($dbhost);
  100.         } else {
  101.             $conn = false;
  102.         }
  103.         if ($conn == false) {
  104.             return DB_ERROR; // XXX ERRORMSG
  105.         }
  106.         if ($dsninfo['database']) {
  107.             mysql_select_db($dsninfo['database'], $conn);
  108.         }
  109.         $this->connection = $conn;
  110.         return DB_OK;
  111.     }
  112.  
  113.     // }}}
  114.     // {{{ disconnect()
  115.  
  116.     /**
  117.      * Log out and disconnect from the database.
  118.      *
  119.      * @return bool TRUE on success, FALSE if not connected.
  120.      */
  121.     function disconnect() {
  122.         return mysql_close($this->connection); // XXX ERRORMSG
  123.     }
  124.  
  125.     // }}}
  126.     // {{{ query()
  127.  
  128.  
  129.     /**
  130.      * Send a query to MySQL and return the results as a DB_result object.
  131.      *
  132.      * @param $query the SQL query
  133.      *
  134.      * @return object a DB_result object on success, a DB error code
  135.      * on failure
  136.      */
  137.     function &query($query) {
  138.         $result = mysql_query($query, $this->connection);
  139.         if (!$result) {
  140.             return $this->errorCode(mysql_errno($this->connection));
  141.         }
  142.         // Determine which queries that should return data, and which
  143.         // should return an error code only.
  144.         if (preg_match('/(SELECT|SHOW)/i', $query)) {
  145.             $resultObj = new DB_result($this, $result);
  146.             return $resultObj;
  147.         } else {
  148.             return DB_OK;
  149.         }
  150.     }
  151.  
  152.     // }}}
  153.     // {{{ simpleQuery()
  154.  
  155.     /**
  156.      * Send a query to MySQL and return the results as a MySQL resource
  157.      * identifier.
  158.      *
  159.      * @param $query the SQL query
  160.      *
  161.      * @return int returns a valid MySQL result for successful SELECT
  162.      * queries, DB_OK for other successful queries.  A DB error code
  163.      * is returned on failure.
  164.      */
  165.     function simpleQuery($query) {
  166.         $result = mysql_query($query, $this->connection);
  167.         if (!$result) {
  168.             return $this->errorCode(mysql_errno($this->connection));
  169.         }
  170.         // Determine which queries that should return data, and which
  171.         // should return an error code only.
  172.         if (preg_match('/(SELECT|SHOW)/i', $query)) {
  173.             return $result;
  174.         } else {
  175.             return DB_OK;
  176.         }
  177.     }
  178.  
  179.     // }}}
  180.     // {{{ fetchRow()
  181.  
  182.     /**
  183.      * Fetch a row and return as array.
  184.      *
  185.      * @param $result MySQL result identifier
  186.      * @param $getmode how the resulting array should be indexed
  187.      *
  188.      * @return int an array on success, a DB error code on failure, NULL
  189.      *             if there is no more data
  190.      */
  191.     function &fetchRow($result, $getmode = DB_GETMODE_DEFAULT) {
  192.         if ($getmode & DB_GETMODE_ASSOC) {
  193.             $row = mysql_fetch_array($result, MYSQL_ASSOC);
  194.         } else {
  195.             $row = mysql_fetch_row($result);
  196.         }
  197.         if (!$row) {
  198.             $errno = mysql_errno($this->connection);
  199.             if (!$errno) {
  200.                 return NULL;
  201.             }
  202.             return $this->errorCode($errno);
  203.         }
  204.         return $row;
  205.     }
  206.  
  207.     // }}}
  208.     // {{{ fetchInto()
  209.  
  210.     /**
  211.      * Fetch a row and insert the data into an existing array.
  212.      *
  213.      * @param $result MySQL result identifier
  214.      * @param $arr (reference) array where data from the row is stored
  215.      * @param $getmode how the array data should be indexed
  216.      *
  217.      * @return int DB_OK on success, a DB error code on failure
  218.      */
  219.     function fetchInto($result, &$arr, $getmode = DB_GETMODE_DEFAULT) {
  220.         if ($getmode & DB_GETMODE_ASSOC) {
  221.             $arr = mysql_fetch_array($result, MYSQL_ASSOC);
  222.         } else {
  223.             $arr = mysql_fetch_row($result);
  224.         }
  225.         if (!$arr) {
  226.             $errno = mysql_errno($this->connection);
  227.             if (!$errno) {
  228.                 return NULL;
  229.             }
  230.             return $this->errorCode($errno);
  231.         }
  232.         return DB_OK;
  233.     }
  234.  
  235.     // }}}
  236.     // {{{ freeResult()
  237.  
  238.     /**
  239.      * Free the internal resources associated with $result.
  240.      *
  241.      * @param $result MySQL result identifier or DB statement identifier
  242.      *
  243.      * @return bool TRUE on success, FALSE if $result is invalid
  244.      */
  245.     function freeResult($result) {
  246.         if (is_resource($result)) {
  247.             return mysql_free_result($result);
  248.         }
  249.         if (!isset($this->prepare_tokens[$result])) {
  250.             return false;
  251.         }
  252.         unset($this->prepare_tokens[$result]);
  253.         unset($this->prepare_types[$result]);
  254.         return true; 
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ numCols()
  259.  
  260.     /**
  261.      * Get the number of columns in a result set.
  262.      *
  263.      * @param $result MySQL result identifier
  264.      *
  265.      * @return int the number of columns per row in $result
  266.      */
  267.     function numCols($result) {
  268.         $cols = mysql_num_fields($result);
  269.         if (!$cols) {
  270.             return $this->errorCode(mysql_errno($this->connection));
  271.         }
  272.         return $cols;
  273.     }
  274.  
  275.     // }}}
  276.     // {{{ errorNative()
  277.  
  278.     /**
  279.      * Get the native error code of the last error (if any) that
  280.      * occured on the current connection.
  281.      *
  282.      * @return int native MySQL error code
  283.      */
  284.     function errorNative() {
  285.         return mysql_errno($this->connection);
  286.     }
  287.  
  288.     // }}}
  289.     // {{{ prepare()
  290.  
  291.     /**
  292.      * Prepares a query for multiple execution with execute().  With
  293.      * MySQL, this is emulated.
  294.      */
  295.     function prepare($query) {
  296.         $tokens = split('[\&\?]', $query);
  297.         $token = 0;
  298.         $types = array();
  299.         for ($i = 0; $i < strlen($query); $i++) {
  300.             switch ($query[$i]) {
  301.                 case '?':
  302.                     $types[$token++] = DB_PARAM_SCALAR;
  303.                     break;
  304.                 case '&':
  305.                     $types[$token++] = DB_PARAM_OPAQUE;
  306.                     break;
  307.             }
  308.         }
  309.         $this->prepare_tokens[] = &$tokens;
  310.         end($this->prepare_tokens);
  311.         $k = key($this->prepare_tokens);
  312.         $this->prepare_types[$k] = $types;
  313.         return $k;
  314.     }
  315.  
  316.     // }}}
  317.     // {{{ execute()
  318.  
  319.     /**
  320.      * @return int returns a MySQL result resource for successful
  321.      * SELECT queries, DB_OK for other successful queries.  A DB error
  322.      * code is returned on failure.
  323.      */
  324.     function execute($stmt, $data = false) {
  325.         $realquery = $this->execute_emulate_query($stmt, $data);
  326.         print "<!-- realquery=$realquery -->\n";
  327.         $result = mysql_query($realquery, $this->connection);
  328.         if (!$result) {
  329.             return $this->errorCode(mysql_errno($this->connection));
  330.         }
  331.         if (preg_match('/(SELECT|SHOW)/i', $realquery)) {
  332.             return $result;
  333.         } else {
  334.             return DB_OK;
  335.         }
  336.     }
  337.  
  338.     // }}}
  339.     // {{{ autoCommit()
  340.  
  341.     /**
  342.      * Enable/disable automatic commits [not supported by MySQL]
  343.      */
  344.     function autoCommit($onoff = false) {
  345.         return DB_ERROR_NOT_CAPABLE;
  346.     }
  347.  
  348.     // }}}
  349.     // {{{ commit()
  350.  
  351.     /**
  352.      * Commit transactions on the current connection [not supported by MySQL]
  353.      */
  354.     function commit() {
  355.         return DB_ERROR_NOT_CAPABLE;
  356.     }
  357.  
  358.     // }}}
  359.     // {{{ rollback()
  360.  
  361.     /**
  362.      * Roll back all uncommitted transactions on the current connection.
  363.      * [not supported by MySQL]
  364.      */
  365.     function rollback() {
  366.         return DB_ERROR_NOT_CAPABLE;
  367.     }
  368.  
  369.     // }}}
  370.  
  371.     // TODO/wishlist:
  372.     // simpleFetch
  373.     // simpleGet
  374.     // affectedRows
  375.     // longReadlen
  376.     // binmode
  377. }
  378.  
  379. // Local variables:
  380. // tab-width: 4
  381. // c-basic-offset: 4
  382. // End:
  383. ?>
  384.