Source for file BaseConnector.php

Documentation is available at BaseConnector.php

  1. <?php
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /**
  5.  * This file contains the abstract BaseConnector class.
  6.  *
  7.  * System requirements:
  8.  * <ul>
  9.  * <li>PHP 5</li>
  10.  * </ul>
  11.  *
  12.  * This library is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU Lesser General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  * The Connector library is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19.  * {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
  20.  * for more details.
  21.  *
  22.  * @author Per Egil Roksvaag
  23.  * @copyright 2009 Per Egil Roksvaag
  24.  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  25.  * @package connector
  26.  * @version 2.0.0
  27.  */
  28.  
  29. ///////////////////////////////////////////////////////////////////////////////
  30. /**
  31.  * Include the {@link IConnector} interface.
  32.  */
  33.  
  34. require_once("IConnector.php");
  35.  
  36. ///////////////////////////////////////////////////////////////////////////////
  37. /**
  38.  * Include the {@link TypeValidator} class and the global function
  39.  * {@link is_date()}.
  40.  */
  41.  
  42. require_once("TypeValidator.php");
  43.  
  44. ///////////////////////////////////////////////////////////////////////////////
  45. /**
  46.  * The BaseConnector implements common tasks for all IConnector
  47.  * classes in this package.
  48.  *
  49.  * You can't create an instance of this class (it's abstract).
  50.  * <b>Extend</b> it to create <b>new IConnector classes</b>,
  51.  * e.g. for PHP database extensions not contained in this package.
  52.  *
  53.  * @tutorial connector.pkg#extend
  54.  * @package connector
  55.  */
  56.  
  57. abstract class BaseConnector implements IConnector
  58. {
  59.     ///////////////////////////////////////////////////////////////////////////
  60.     /**
  61.      * @var resource The PHP database link identifier.
  62.      */
  63.  
  64.     protected $conn;
  65.  
  66.     ///////////////////////////////////////////////////////////////////////////
  67.     /**
  68.      * @var array An associated array of cached query results.
  69.      */
  70.  
  71.     protected $cache = array();
  72.  
  73.     ///////////////////////////////////////////////////////////////////////////
  74.     /**
  75.      * @var array Default values for all global options.
  76.      * @tutorial connector.pkg#options.element
  77.      */
  78.  
  79.     protected $options = array
  80.     (
  81.         self::CHAR_APPLICATION => "UTF-8",
  82.         self::CHAR_DATABASE => null,
  83.         self::LOG_DEBUG => false,
  84.         self::LOG_ERROR => true,
  85.         self::PARAM_CAST_NUMERIC => true,
  86.         self::PARAM_NAMED => false,
  87.         self::PARAM_PREFIX => "?",
  88.         self::PARAM_QUERIES => false,
  89.         self::PARAM_STRIP_TAGS => false,
  90.         self::PARAM_STRIP_MAGIC => false,
  91.         self::PARAM_TRIM => false,
  92.         self::RESULT_CACHE => true
  93.     //    self::RESULT_LENGTH only allowed as a local select() option.
  94.     //    self::RESULT_OFFSET only allowed as a local select() option.
  95.     //    self::RESULT_KEY_FIELD only allowed as a local select() option.
  96.     );
  97.  
  98.     ///////////////////////////////////////////////////////////////////////////
  99.     /**
  100.      * Call this method from descendent constructors to set global options.
  101.      *
  102.      * You can't create an instance of this class (it's abstract).
  103.      * @param array $options An associative array of options, see the {@tutorial connector.pkg#options.element}.
  104.      * @return BaseConnector 
  105.      */
  106.  
  107.     public function BaseConnector($options array())
  108.     {
  109.         $this->options[self::PARAM_STRIP_MAGIC= (bool)get_magic_quotes_gpc();
  110.         $this->setOptions($options);
  111.     }
  112.  
  113.     ///////////////////////////////////////////////////////////////////////////
  114.     /**
  115.      * Begins a transaction on the current connection.
  116.      * The current transaction includes all statements on the connection that
  117.      * were executed after the call to transaction() and before any calls
  118.      * to rollback() or commit().
  119.      *
  120.      * @see IConnector::transaction().
  121.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  122.      * @return bool true if the transaction was successfully begun, false otherwise.
  123.      */
  124.     
  125.     public function transaction($options array())
  126.     {
  127.         return true;
  128.     }
  129.     
  130.     ///////////////////////////////////////////////////////////////////////////
  131.     /**
  132.      * Commits the current transaction on the current connection.
  133.      * The current transaction includes all statements on the connection that
  134.      * were executed after the call to transaction() and before any calls
  135.      * to rollback() or commit().
  136.      *
  137.      * @see IConnector::commit().
  138.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  139.      * @return bool true if the transaction was successfully committed, false otherwise.
  140.      */
  141.     
  142.     public function commit($options array())
  143.     {
  144.         return true;
  145.     }
  146.     
  147.     ///////////////////////////////////////////////////////////////////////////
  148.     /**
  149.      * Rolls back the current transaction on the current connection.
  150.      * The current transaction includes all statements on the connection that
  151.      * were executed after the call to transaction() and before any calls
  152.      * to rollback() or commit().
  153.      *
  154.      * @see IConnector::rollback().
  155.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  156.      * @return bool true if the transaction was successfully rolled back, false otherwise.
  157.      */
  158.     
  159.     public function rollback($options array())
  160.     {
  161.         return true;
  162.     }
  163.     
  164.     ///////////////////////////////////////////////////////////////////////////
  165.     /**
  166.      * Get the database link identifier used by the IConnector instance.
  167.      * @return resource A PHP database link identifier.
  168.      */
  169.  
  170.     public function getLink()
  171.     {
  172.         return $this->conn;
  173.     }
  174.  
  175.     ///////////////////////////////////////////////////////////////////////////
  176.     /**
  177.      * Get an array of all global options or the value of the global
  178.      * option defined in the optional $element parameter.
  179.      *
  180.      * @param string $element An option constant, see the {@tutorial connector.pkg#options.element}.
  181.      * @return mixed All options as an associated array or the value of a single option.
  182.      */
  183.  
  184.     public function getOptions($element null)
  185.     {
  186.         return $element $this->lookup($element$this->options;
  187.     }
  188.  
  189.     ///////////////////////////////////////////////////////////////////////////
  190.     /**
  191.      * Set one or more global options.
  192.      * Some options are only valid as constructor or function parameters.
  193.      *
  194.      * @param mixed $options Multiple options as an associated array or the value of a single option.
  195.      * @param string $element An option constant, see the {@tutorial connector.pkg#options.element}.
  196.      * @return mixed The previous option value(s).
  197.      */
  198.  
  199.     public function setOptions($options$element null)
  200.     {
  201.         $result $this->getOptions($element);
  202.  
  203.         if($element)
  204.         {
  205.             if(in_array($elementarray_keys($this->options)))
  206.             {
  207.                 $this->options[$element$options;
  208.             }
  209.         }
  210.         else if(is_array($options))
  211.         {
  212.             foreach($options as $element => $value)
  213.             {
  214.                 $this->setOptions($value$element);
  215.             }
  216.         }
  217.         return $result;
  218.     }
  219.  
  220.     ///////////////////////////////////////////////////////////////////////////
  221.     /**
  222.      * Calculate the md5 hash value of the query string and - if necessary -
  223.      * the parameters of a parameterized query.
  224.      *
  225.      * @param string $query The modified SQL query string after binding.
  226.      * @param array $stack An array of parameters to be used in a parameterized query.
  227.      * @param array $options An associated array of options.
  228.      * @return string The md5 hash value.
  229.      */
  230.  
  231.     protected function getHash($query$stack array()$options array())
  232.     {
  233.         return $stack && $this->lookup(self::PARAM_QUERIES)
  234.             ? md5($query.serialize($stack))
  235.             : md5($query);
  236.     }
  237.  
  238.     ///////////////////////////////////////////////////////////////////////////
  239.     /**
  240.      * Check if a maching query result exists in the result cache.
  241.      *
  242.      * If true, set the <var>$table</var> argument to the cached query result,
  243.      * otherwise <var>$table</var> is set to an empty array.
  244.      *
  245.      * @param string $hash A hash value of a query, see {@link getHash()}.
  246.      * @param array &$table If found, return the cached query result, otherwise an empty array.
  247.      * @param array $options An associated array of options.
  248.      * @return bool true if a maching query result is found, false otherwise.
  249.      */
  250.  
  251.     protected function getCache($hash&$table$options array())
  252.     {
  253.         if($this->lookup(self::RESULT_CACHE$options))
  254.         {
  255.             if(array_key_exists($hash$this->cache))
  256.             {
  257.                 $table $this->cache[$hash];
  258.                 return true;
  259.             }
  260.         }
  261.         $table array();
  262.         return false;
  263.     }
  264.  
  265.     ///////////////////////////////////////////////////////////////////////////
  266.     /**
  267.      * Add a new query result to the result cache.
  268.      *
  269.      * @param string $hash A hash value of a query, see {@link getHash()}.
  270.      * @param array $table The query result to cache.
  271.      * @param array $options An associated array of options.
  272.      * @return int The number of cached query results.
  273.      */
  274.  
  275.     protected function setCache($hash$table$options array())
  276.     {
  277.         if($this->lookup(self::RESULT_CACHE$options))
  278.         {
  279.             $this->cache[$hash$table;
  280.         }
  281.         return count($this->cache);
  282.     }
  283.  
  284.     ///////////////////////////////////////////////////////////////////////////
  285.     /**
  286.      * Get an option value. Any options in the local $options array
  287.      * overrides the global options.
  288.      *
  289.      * @param string $element An option constant, see the {@tutorial connector.pkg#options.element}.
  290.      * @param array $options An associated array of options.
  291.      * @param mixed $default The default value to return if no global or local options are set.
  292.      * @return mixed The value of a single option.
  293.      */
  294.  
  295.     protected function lookup($element$options array()$default null)
  296.     {
  297.         if($options && array_key_exists($element$options))
  298.         {
  299.             return $options[$element];
  300.         }
  301.         else if(array_key_exists($element$this->options))
  302.         {
  303.             return $this->options[$element];
  304.         }
  305.         else
  306.         {
  307.             return $default;
  308.         }
  309.     }
  310.  
  311.     ///////////////////////////////////////////////////////////////////////////
  312.     /**
  313.      * Replace the query placeholders with matching parameter values or - if
  314.      * parameterized queries are enabled - the database specific placeholder.
  315.      *
  316.      * Parameter values are converted, stripped, escaped, encoded and quoted
  317.      * according to the option values. The $stack returns an array of values
  318.      * for each placeholder to be used in parameterized queries.
  319.      *
  320.      * @param string $query A SQL query string.
  321.      * @param array $param An associated array of values to replace the $query placeholders.
  322.      * @param array &$stack Returns an array of values for use in parameterized queries.
  323.      * @param array $options An associated array of options.
  324.      * @return string The modified SQL query.
  325.      */
  326.  
  327.     protected function bind($query$param&$stack array()$options array())
  328.     {
  329.         $result "";
  330.         $pattern "/\B:(\w+)/";
  331.         $query trim($query);
  332.         $split preg_split($pattern$query);
  333.         $count preg_match_all($pattern$query$matchesPREG_SET_ORDER);
  334.  
  335.         for($i 0$i $count$i++)
  336.         {
  337.             list($match$name$matches[$i];
  338.             $sql $this->cast($param[$name]$name$stack$options);
  339.             $result.= $split[$i];
  340.             $result.= $sql !== null $sql $match;
  341.         }
  342.         $result.= end($split);
  343.         return $result;
  344.     }
  345.  
  346.     ///////////////////////////////////////////////////////////////////////////
  347.     /**
  348.      * Push a parameter on the stack and return a query placeholder.
  349.      *
  350.      * @param mixed $value The parameter to replace the query placeholder.
  351.      * @param string $name The name of the query placeholder.
  352.      * @param array &$stack Push parameters on this array for use in parameterized queries.
  353.      * @param array $options An associated array of options.
  354.      * @return string A valid SQL placeholder.
  355.      */
  356.  
  357.     protected function push($value$name&$stack$options array())
  358.     {
  359.         if($this->lookup(self::PARAM_NAMED$options))
  360.         {
  361.             $prefix $this->lookup(self::PARAM_PREFIX$options);
  362.             $stack[$name$value;
  363.             return $prefix.$name;
  364.         }
  365.         else
  366.         {
  367.             $prefix $this->lookup(self::PARAM_PREFIX$options);
  368.             $stack[$value;
  369.             return $prefix;
  370.         }
  371.     }
  372.  
  373.     ///////////////////////////////////////////////////////////////////////////
  374.     /**
  375.      * Cast a parameter to a valid SQL string or placeholder. The parameter is
  376.      * also pushed on the $stack - and if necessary converted - to be used in
  377.      * parameterized queries.
  378.      *
  379.      * @param mixed $value The parameter to replace the query placeholder.
  380.      * @param string $name The name of the query placeholder.
  381.      * @param array &$stack Push parameters on this array for use in parameterized queries.
  382.      * @param array $options An associated array of options.
  383.      * @return string A valid SQL string or placeholder.
  384.      */
  385.  
  386.     protected function cast($value$name&$stack$options array())
  387.     {
  388.         if(is_null($value))        return $this->castNull($value$name$stack$options);
  389.         if(is_bool($value))        return $this->castBool($value$name$stack$options);
  390.         if(is_int($value))        return $this->castInt($value$name$stack$options);
  391.         if(is_float($value))    return $this->castFloat($value$name$stack$options);
  392.         if(is_date($value))        return $this->castDate($value$name$stack$options);
  393.         if(is_numeric($value))    return $this->castNumeric($value$name$stack$options);
  394.         if(is_string($value))    return $this->castString($value$name$stack$options);
  395.         if(is_object($value))    return $this->castObject($value$name$stack$options);
  396.         if(is_array($value))    return $this->castArray($value$name$stack$options);
  397.         return null;
  398.     }
  399.  
  400.     ///////////////////////////////////////////////////////////////////////////
  401.     /**
  402.      * Cast null to a valid SQL string.
  403.      *
  404.      * @param null $value The parameter to replace the query placeholder.
  405.      * @param string $name The name of the query placeholder.
  406.      * @param array &$stack Push parameters on this array for use in parameterized queries.
  407.      * @param array $options An associated array of options.
  408.      * @return string A valid SQL string.
  409.      */
  410.  
  411.     protected function castNull($value$name&$stack$options array())
  412.     {
  413.         return "NULL";
  414.     }
  415.  
  416.     ///////////////////////////////////////////////////////////////////////////
  417.     /**
  418.      * Cast a boolean parameter to a valid SQL string or placeholder.
  419.      * The parameter is also pushed on the $stack and converted
  420.      * to be used in parameterized queries.
  421.      *
  422.      * @param bool $value The parameter to replace the query placeholder.
  423.      * @param string $name The name of the query placeholder.
  424.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  425.      * @param array $options An associated array of options.
  426.      * @return string A valid SQL string or placeholder.
  427.      */
  428.  
  429.     ///////////////////////////////////////////////////////////////////////////
  430.  
  431.     protected function castBool($value$name&$stack$options array())
  432.     {
  433.         return $this->lookup(self::PARAM_QUERIES$options)
  434.             ? $this->push((int)$value$name$stack$options)
  435.             : (string)(int)$value;
  436.     }
  437.  
  438.     ///////////////////////////////////////////////////////////////////////////
  439.     /**
  440.      * Cast an integer parameter to a valid SQL string or placeholder.
  441.      * The parameter is also pushed on the $stack to be used in parameterized
  442.      * queries.
  443.      *
  444.      * @param int $value The parameter to replace the query placeholder.
  445.      * @param string $name The name of the query placeholder.
  446.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  447.      * @param array $options An associated array of options.
  448.      * @return string A valid SQL string or placeholder.
  449.      */
  450.  
  451.     protected function castInt($value$name&$stack$options array())
  452.     {
  453.         return $this->lookup(self::PARAM_QUERIES$options)
  454.             ? $this->push($value$name$stack$options)
  455.             : (string)$value;
  456.     }
  457.  
  458.     ///////////////////////////////////////////////////////////////////////////
  459.     /**
  460.      * Cast a float parameter to a valid SQL string or placeholder.
  461.      * The parameter is also pushed on the $stack to be used in parameterized
  462.      * queries.
  463.      *
  464.      * @param float $value The parameter to replace the query placeholder.
  465.      * @param string $name The name of the query placeholder.
  466.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  467.      * @param array $options An associated array of options.
  468.      * @return string A valid SQL string or placeholder.
  469.      */
  470.  
  471.     protected function castFloat($value$name&$stack$options array())
  472.     {
  473.         return $this->lookup(self::PARAM_QUERIES$options)
  474.             ? $this->push($value$name$stack$options)
  475.             : (string)$value;
  476.     }
  477.  
  478.     ///////////////////////////////////////////////////////////////////////////
  479.     /**
  480.      * Cast a DateTime parameter to a valid SQL string or placeholder.
  481.      * The parameter is also pushed on the $stack to be used in parameterized
  482.      * queries.
  483.      *
  484.      * @param DateTime $value The parameter to replace the query placeholder.
  485.      * @param string $name The name of the query placeholder.
  486.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  487.      * @param array $options An associated array of options.
  488.      * @return string A valid SQL string or placeholder.
  489.      */
  490.  
  491.     protected function castDate($value$name&$stack$options array())
  492.     {
  493.         if($this->lookup(self::PARAM_QUERIES$options))
  494.         {
  495.             return $this->push($value$name$stack$options);
  496.         }
  497.         else
  498.         {
  499.             $sql date_format($value"Y-m-d H:i:s");
  500.             return $this->strQuote($sql$options);
  501.         }
  502.     }
  503.  
  504.     ///////////////////////////////////////////////////////////////////////////
  505.     /**
  506.      * Cast a numeric string parameter to a valid SQL string or placeholder.
  507.      * The parameter is also pushed on the $stack and converted to be used in
  508.      * parameterized queries.
  509.      *
  510.      * @param string $value The parameter to replace the query placeholder.
  511.      * @param string $name The name of the query placeholder.
  512.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  513.      * @param array $options An associated array of options.
  514.      * @return string A valid SQL string or placeholder.
  515.      */
  516.  
  517.     protected function castNumeric($value$name&$stack$options array())
  518.     {
  519.         if($this->lookup(self::PARAM_CAST_NUMERIC$options))
  520.         {
  521.             $int = (int)$value;
  522.             $float = (float)$value;
  523.             
  524.             if((string)$int === $value)
  525.             {
  526.                 return $this->castInt($int$name$stack$options);
  527.             }
  528.             if((string)$float === $value)
  529.             {
  530.                 return $this->castFloat($float$name$stack$options);
  531.             }
  532.         }
  533.         return $this->castString($value$name$stack$options);
  534.     }
  535.  
  536.     ///////////////////////////////////////////////////////////////////////////
  537.     /**
  538.      * Cast a string parameter to a valid SQL string or placeholder.
  539.      * The parameter is also pushed on the $stack and modified to be used in
  540.      * parameterized queries.
  541.      *
  542.      * String values are converted, stripped, escaped, encoded and quoted
  543.      * according to the option values.
  544.      *
  545.      * @param string $value The parameter to replace the query placeholder.
  546.      * @param string $name The name of the query placeholder.
  547.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  548.      * @param array $options An associated array of options.
  549.      * @return string A valid SQL string or placeholder.
  550.      */
  551.  
  552.     protected function castString($value$name&$stack$options array())
  553.     {
  554.         $value $this->strStrip($value$options);
  555.  
  556.         if($this->lookup(self::PARAM_QUERIES$options))
  557.         {
  558.             $value $this->strEncode($value$options);
  559.             return $this->push($value$name$stack$options);
  560.         }
  561.         else
  562.         {
  563.             $value $this->strEscape($value$options);
  564.             $value $this->strEncode($value$options);
  565.             return $this->strQuote($value$options);
  566.         }
  567.     }
  568.  
  569.     ///////////////////////////////////////////////////////////////////////////
  570.     /**
  571.      * With the exception of DataTime values, objects are not supported as
  572.      * parameters. Override this method to serialize your objects as
  573.      * SQL query strings.
  574.      *
  575.      * @param object $value The parameter to replace the query placeholder.
  576.      * @param string $name The name of the query placeholder.
  577.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  578.      * @param array $options An associated array of options.
  579.      * @return string A valid SQL string or placeholder.
  580.      */
  581.  
  582.     protected function castObject($value$name&$stack$options array())
  583.     {
  584.         return null;
  585.     }
  586.  
  587.     ///////////////////////////////////////////////////////////////////////////
  588.     /**
  589.      * Arrays are converted to a comma separated list of values.
  590.      *
  591.      * Strings are escaped and quoted. No placeholders are returend.
  592.      * Any arrays within the array are ignored. If the array is empty,
  593.      * the string "NULL" is returned.
  594.      *
  595.      * @param array $list An array of parameter to replace the query placeholder.
  596.      * @param string $name The name of the query placeholder.
  597.      * @param array &$stack Parameters are pushed on this array for use in parameterized queries.
  598.      * @param array $options An associated array of options.
  599.      * @return string A valid SQL string or placeholder.
  600.      */
  601.  
  602.     protected function castArray($list$name&$stack$options array())
  603.     {
  604.         $sql array();
  605.         $options[self::PARAM_QUERIESfalse;
  606.  
  607.         foreach($list as $value)
  608.         {
  609.             if(is_null($value))            $sql[$this->castNull($value$name$stack$options);
  610.             else if(is_bool($value))    $sql[$this->castBool($value$name$stack$options);
  611.             else if(is_int($value))        $sql[$this->castInt($value$name$stack$options);
  612.             else if(is_float($value))    $sql[$this->castFloat($value$name$stack$options);
  613.             else if(is_date($value))    $sql[$this->castDate($value$name$stack$options);
  614.             else if(is_numeric($value))    $sql[$this->castNumeric($value$name$stack$options);
  615.             else if(is_string($value))    $sql[$this->castString($value$name$stack$options);
  616.             else if(is_object($value))    $sql[$this->castObject($value$name$stack$options);
  617.         }
  618.         return count($sqlimplode(","$sql"NULL";
  619.     }
  620.  
  621.     ///////////////////////////////////////////////////////////////////////////
  622.     /**
  623.      * Strip spare or insecure parts from a string parameter
  624.      * according to the current options.
  625.      *
  626.      * @param string $value The string that will be stripped.
  627.      * @param array $options An associated array of options.
  628.      * @return string The stripped string.
  629.      */
  630.  
  631.     protected function strStrip($value$options array())
  632.     {
  633.         if($this->lookup(self::PARAM_STRIP_MAGIC$options))
  634.         {
  635.             $value stripslashes($value);
  636.         }
  637.         if($this->lookup(self::PARAM_STRIP_TAGS$options))
  638.         {
  639.             $value strip_tags($value);
  640.         }
  641.         if($this->lookup(self::PARAM_TRIM$options))
  642.         {
  643.             $value trim($value);
  644.         }
  645.         return $value;
  646.     }
  647.  
  648.     ///////////////////////////////////////////////////////////////////////////
  649.     /**
  650.      * Escape special characters in a string parameter.
  651.      *
  652.      * @param string $value The string to be escaped.
  653.      * @param array $options An associated array of options.
  654.      * @return string The escaped string.
  655.      */
  656.  
  657.     protected function strEscape($value$options array())
  658.     {
  659.         return str_replace("'""''"$value);
  660.     }
  661.  
  662.     ///////////////////////////////////////////////////////////////////////////
  663.     /**
  664.      * Add surrounding quotes to a string parameter.
  665.      *
  666.      * @param string $value The string to be quoted.
  667.      * @param array $options An associated array of options.
  668.      * @return string The quoted string.
  669.      */
  670.  
  671.     protected function strQuote($value$options array())
  672.     {
  673.         return "'".$value."'";
  674.     }
  675.  
  676.     ///////////////////////////////////////////////////////////////////////////
  677.     /**
  678.      * Converts character encoding of string parameters to the character
  679.      * set of the database according to the current options.
  680.      *
  681.      * @param string $value The string that will be converted.
  682.      * @param array $options An associated array of options.
  683.      * @return string The converted string.
  684.      */
  685.  
  686.     protected function strEncode($value$options array())
  687.     {
  688.         $db $this->lookup(self::CHAR_DATABASE$options);
  689.         $app $this->lookup(self::CHAR_APPLICATION$options);
  690.  
  691.         if($db && $app && $db != $app)
  692.         {
  693.             if(function_exists("mb_convert_variables"))
  694.             {
  695.                 mb_convert_variables($db$app$value);
  696.             }
  697.             else if(function_exists("iconv"))
  698.             {
  699.                 if(is_string($value))
  700.                 {
  701.                     return iconv($app$db$value);
  702.                 }
  703.                 else if(is_array($value))
  704.                 {
  705.                     foreach($value as $key => $cell)
  706.                     {
  707.                         if(is_string($cell))
  708.                         {
  709.                             $value[$keyiconv($app$db$cell);
  710.                         }
  711.                     }
  712.                 }
  713.             }
  714.         }
  715.         return $value;
  716.     }
  717.  
  718.     ///////////////////////////////////////////////////////////////////////////
  719.     /**
  720.      * Converts character encoding of from the character
  721.      * set of the database according to the current options.
  722.      *
  723.      * @param string $value The string that will be converted.
  724.      * @param array $options An associated array of options.
  725.      * @return string The converted string.
  726.      */
  727.  
  728.     protected function strDecode($value$options array())
  729.     {
  730.         $db $this->lookup(self::CHAR_DATABASE$options);
  731.         $app $this->lookup(self::CHAR_APPLICATION$options);
  732.  
  733.         if($db && $app && $db != $app)
  734.         {
  735.             if(function_exists("mb_convert_variables"))
  736.             {
  737.                 mb_convert_variables($app$db$value);
  738.             }
  739.             else if(function_exists("iconv"))
  740.             {
  741.                 if(is_string($value))
  742.                 {
  743.                     return iconv($db$app$value);
  744.                 }
  745.                 else if(is_array($value))
  746.                 {
  747.                     foreach($value as $key => $cell)
  748.                     {
  749.                         if(is_string($cell))
  750.                         {
  751.                             $value[$keyiconv($db$app$cell);
  752.                         }
  753.                     }
  754.                 }
  755.             }
  756.         }
  757.         return $value;
  758.     }
  759.  
  760.     ///////////////////////////////////////////////////////////////////////////
  761. }
  762.  
  763. ?>

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