Source for file MysqlConnector.php

Documentation is available at MysqlConnector.php

  1. <?php
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /**
  5.  * IConnector implementation for the PHP MySQL database extension.
  6.  *
  7.  * System requirements:
  8.  * <ul>
  9.  * <li>PHP 5</li>
  10.  * <li>The {@link PHP_MANUAL#book.mysql MySQL database extension}</li>
  11.  * </ul>
  12.  *
  13.  * This library is free software: you can redistribute it and/or modify
  14.  * it under the terms of the GNU Lesser General Public License as published by
  15.  * the Free Software Foundation, either version 3 of the License, or
  16.  * (at your option) any later version.
  17.  * The Connector library is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20.  * {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
  21.  * for more details.
  22.  *
  23.  * @author Per Egil Roksvaag
  24.  * @copyright 2009 Per Egil Roksvaag
  25.  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  26.  * @package connector
  27.  * @version 2.0.0
  28.  */
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////
  31. /**
  32.  * Includes the parent class.
  33.  */
  34.  
  35. require_once("BaseConnector.php");
  36.  
  37. ///////////////////////////////////////////////////////////////////////////////
  38. /**
  39.  * IConnector implementation for the PHP MySQL database extension.
  40.  * @package connector
  41.  */
  42.  
  43. class MysqlConnector extends BaseConnector implements IConnector
  44. {
  45.     ///////////////////////////////////////////////////////////////////////////
  46.     /**
  47.      * List of additional escape characters for MySQL string parameters.
  48.      */
  49.  
  50.     const PARAM_ADD_SLASHES = "paramAddSlashes";
  51.  
  52.     ///////////////////////////////////////////////////////////////////////////
  53.     /**
  54.      * Open a connection to a MySQL Server and set global options.
  55.      *
  56.      * @param array $connection An associated array of connection settings, like host and user name.
  57.      * @param array $options An associated array of global options for the resulting instance.
  58.      * @return MysqlConnector 
  59.      */
  60.  
  61.     public function MysqlConnector($connection$options array())
  62.     {
  63.         if($this->open($connection))
  64.         {
  65.             $this->options[self::PARAM_ADD_SLASHES"()+-/?@;";
  66.             unset($this->options[self::PARAM_PREFIX]);
  67.             unset($this->options[self::PARAM_QUERIES]);
  68.             parent::BaseConnector($options);
  69.         }
  70.         else if($this->lookup("throwException"$options))
  71.         {
  72.             throw new Exception("MysqlConnector error: Connection failed.");
  73.         }
  74.         else if($this->lookup(self::LOG_ERROR$options))
  75.         {
  76.             $log "MysqlConnector error: Connection failed in ";
  77.             error_log($log.$_SERVER["SCRIPT_FILENAME"]);
  78.         }
  79.     }
  80.  
  81.     ///////////////////////////////////////////////////////////////////////////
  82.     /**
  83.      * Open a database connection.
  84.      * @param array $connection An associated array of connection settings, like host and user name.
  85.      * @return bool <var>true</var>, if a database connection was successfully opened, <var>false</var> otherwise.
  86.      */
  87.  
  88.     protected function open($connection)
  89.     {
  90.         $hostname $this->lookup(self::CONN_HOSTNAME$connection);
  91.         $database $this->lookup(self::CONN_DATABASE$connection);
  92.         $username $this->lookup(self::CONN_USERNAME$connection);
  93.         $password $this->lookup(self::CONN_PASSWORD$connection);
  94.  
  95.         $native $this->lookup(self::CONN_NATIVE$connection0);
  96.         $charset $this->lookup(self::CONN_CHARSET$connection"utf8");
  97.         $pool $this->lookup(self::CONN_POOL$connectiontrue);
  98.         $port $this->lookup(self::CONN_PORT$connection);
  99.         $port && $hostname.= ":".$port;
  100.         
  101.         $this->conn = $this->lookup(self::CONN_PERSISTENT$connection)
  102.             ? mysql_pconnect($hostname$username$password$native)
  103.             : mysql_connect($hostname$username$password!$pool$native);
  104.  
  105.         if($this->conn)
  106.         {
  107.             $database && mysql_select_db($database);
  108.             $charset && mysql_query("SET NAMES '{$charset}'");
  109.         }
  110.         return is_resource($this->conn);
  111.     }
  112.  
  113.     ///////////////////////////////////////////////////////////////////////////
  114.     /**
  115.      * Send a SQL SELECT query to the database and get the query result.
  116.      *
  117.      * @see IConnector::select().
  118.      * @param string $query A SQL query to execute on a database.
  119.      * @param array $param An associated array of values to be used in the $query.
  120.      * @param array $map An array of type definitions for the <var>$param</var> values.
  121.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  122.      * @throws Exception when $param doesn't match the type definition $map.
  123.      * @return array The query result as a table (array of associated arrays).
  124.      */
  125.  
  126.     public function select($query$param array()$map array()$options array())
  127.     {
  128.         $param TypeValidator::check($param$map);
  129.         $query $this->bind($query$param$stack$options);
  130.         $query $this->build($query$options);
  131.         $hash $this->getHash($query$stack$options);
  132.  
  133.         if($this->lookup(self::LOG_DEBUG$options))
  134.         {
  135.             $log "MysqlConnector debug: Select query in ";
  136.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  137.         }
  138.         if($this->getCache($hash$table$options))
  139.         {
  140.             return $table;
  141.         }
  142.         if($stmt mysql_query($query$this->conn))
  143.         {
  144.             $table $this->fetch($stmt$options);
  145.             mysql_free_result($stmt);
  146.             $this->setCache($hash$table$options);
  147.             return $table;
  148.         }
  149.         if($this->lookup(self::LOG_ERROR$options))
  150.         {
  151.             $log "MysqlConnector error: Select query in ";
  152.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  153.             error_log("MysqlConnector error: ".mysql_error($this->conn));
  154.         }
  155.         return false;
  156.     }
  157.  
  158.     ///////////////////////////////////////////////////////////////////////////
  159.     /**
  160.      * Send a SQL INSERT query to the database and get the IDENTITY ID
  161.      * generated from the last INSERT operation (if any).
  162.      *
  163.      * @see IConnector::insert().
  164.      * @param string $query A SQL query to execute on a database.
  165.      * @param array $param An associated array of values to be used in the $query.
  166.      * @param array $map An array of type definitions for the <var>$param</var> values.
  167.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  168.      * @throws Exception when $param doesn't match the type definition $map.
  169.      * @return int The IDENTITY ID of the last inserted row.
  170.      */
  171.  
  172.     public function insert($query$param array()$map array()$options array())
  173.     {
  174.         $param TypeValidator::check($param$map);
  175.         $query $this->bind($query$param$stack$options);
  176.         $query rtrim($query";");
  177.  
  178.         if($this->lookup(self::LOG_DEBUG$options))
  179.         {
  180.             $log "MysqlConnector debug: Insert query in ";
  181.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  182.         }
  183.         if(mysql_query($query$this->conn))
  184.         {
  185.             $key mysql_insert_id($this->conn);
  186.             return ($key !== 0$key null;
  187.          }
  188.         else if($this->lookup(self::LOG_ERROR$options))
  189.         {
  190.             $log "MysqlConnector error: Insert query in ";
  191.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  192.             error_log("MysqlConnector error: ".mysql_error($this->conn));
  193.         }
  194.         return false;
  195.     }
  196.  
  197.     ///////////////////////////////////////////////////////////////////////////
  198.     /**
  199.      * Send a SQL UPDATE query to the database and get the number of rows
  200.      * updates by the query.
  201.      *
  202.      * @see IConnector::update().
  203.      * @param string $query A SQL query to execute on a database.
  204.      * @param array $param An associated array of values to be used in the $query.
  205.      * @param array $map An array of type definitions for the <var>$param</var> values.
  206.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  207.      * @throws Exception when $param doesn't match the type definition $map.
  208.      * @return int The number of rows updates by the query.
  209.      */
  210.  
  211.     public function update($query$param array()$map array()$options array())
  212.     {
  213.         $param TypeValidator::check($param$map);
  214.         $query $this->bind($query$param$stack$options);
  215.         $query rtrim($query";");
  216.  
  217.         if($this->lookup(self::LOG_DEBUG$options))
  218.         {
  219.             $log "MysqlConnector debug: Update query in ";
  220.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  221.         }
  222.         if(mysql_query($query$this->conn))
  223.         {
  224.             return mysql_affected_rows($this->conn);
  225.          }
  226.         else if($this->lookup(self::LOG_ERROR$options))
  227.         {
  228.             $log "MysqlConnector error: Update query in ";
  229.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  230.             error_log("MysqlConnector error: ".mysql_error($this->conn));
  231.         }
  232.         return false;
  233.     }
  234.  
  235.     ///////////////////////////////////////////////////////////////////////////
  236.     /**
  237.      * Send a SQL DELETE query to the database and get the number of rows
  238.      * deleted by the query.
  239.      *
  240.      * @see IConnector::delete().
  241.      * @param string $query A SQL query to execute on a database.
  242.      * @param array $param An associated array of values to be used in the $query.
  243.      * @param array $map An array of type definitions for the <var>$param</var> values.
  244.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  245.      * @throws Exception when $param doesn't match the type definition $map.
  246.      * @return int The number of rows deleted by the query.
  247.      */
  248.  
  249.     public function delete($query$param array()$map array()$options array())
  250.     {
  251.         $param TypeValidator::check($param$map);
  252.         $query $this->bind($query$param$stack$options);
  253.         $query rtrim($query";");
  254.  
  255.         if($this->lookup(self::LOG_DEBUG$options))
  256.         {
  257.             $log "MysqlConnector debug: Delete query in ";
  258.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  259.         }
  260.         if(mysql_query($query$this->conn))
  261.         {
  262.             return mysql_affected_rows($this->conn);
  263.          }
  264.         else if($this->lookup(self::LOG_ERROR$options))
  265.         {
  266.             $log "MysqlConnector error: Delete query in ";
  267.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  268.             error_log("MysqlConnector error: ".mysql_error($this->conn));
  269.         }
  270.         return false;
  271.     }
  272.  
  273.     ///////////////////////////////////////////////////////////////////////////
  274.     /**
  275.      * Escape special characters in a string parameter.
  276.      *
  277.      * @param string $value The string to be escaped.
  278.      * @param array $options An associated array of options.
  279.      * @return string The escaped string.
  280.      */
  281.  
  282.     protected function strEscape($value$options array())
  283.     {
  284.         $slash $this->lookup(self::PARAM_ADD_SLASHES$options);
  285.         $value mysql_real_escape_string($value$this->conn);
  286.         return $slash addcslashes($value$slash$value;
  287.     }
  288.  
  289.     ///////////////////////////////////////////////////////////////////////////
  290.     /**
  291.      * Add a LIMIT x y statement to a SQL SELECT query when the
  292.      * {@link IConnector::RESULT_LENGTH} or {@link IConnector::RESULT_OFFSET}
  293.      * is set.
  294.      *
  295.      * @param string $query A SQL query to execute on a database.
  296.      * @param array $options An associated array of options.
  297.      * @return string A modified SQL query.
  298.      */
  299.  
  300.     protected function build($query$options array())
  301.     {
  302.         $query rtrim($query";");
  303.         $offset $this->lookup(self::RESULT_OFFSET$options0);
  304.         $length $this->lookup(self::RESULT_LENGTH$options);
  305.                 
  306.         if($offset || is_int($length))
  307.         {
  308.             if(is_null($length)) $length PHP_INT_MAX;
  309.             $query.= LB."LIMIT  {$offset}, {$length}";
  310.         }
  311.         return $query;
  312.     }
  313.  
  314.     ///////////////////////////////////////////////////////////////////////////
  315.     /**
  316.      * Fetch multiple rows of a query result.
  317.      *
  318.      * If the {@link IConnector::RESULT_LENGTH} or {@link IConnector::RESULT_OFFSET}
  319.      * options are set, some rows are omitted from the beginning and/or the end
  320.      * of the query result.
  321.      * If the {@link IConnector::RESULT_KEY_FIELD} option is set, the
  322.      * resulting table is an <b>associated</b> array of rows.
  323.      *
  324.      * @param resource $stmt A statement resource corresponding to an executed statement.
  325.      * @param array $options An associated array of options.
  326.      * @return array The query result as a table (array of associated arrays).
  327.      */
  328.  
  329.     protected function fetch($stmt$options array())
  330.     {
  331.         $key $this->lookup(self::RESULT_KEY_FIELD$options);
  332.         $table array();
  333.  
  334.         while($row mysql_fetch_array($stmtMYSQL_ASSOC))
  335.         {
  336.             $row $this->strDecode($row);
  337.             $key $table[$row[$key]] $row $table[$row;
  338.         }
  339.         return $table;
  340.     }
  341.  
  342.     ///////////////////////////////////////////////////////////////////////////
  343. }
  344.  
  345. ?>

Documentation generated on Wed, 03 Jun 2009 12:41:52 +0200 by phpDocumentor 1.4.1