databases
[ class tree: databases ] [ index: databases ] [ all elements ]

Source for file Join.class.php

Documentation is available at Join.class.php

  1. <?php
  2. /**
  3.  * @package databases
  4.  */
  5.  
  6. /**
  7.  * @package   databases
  8.  * @version   0.1.31
  9.  * @author    Daniel Plücken <daniel@debakel.net>
  10.  * @license   http://www.gnu.org/copyleft/lesser.html
  11.  *             GNU Lesser General Public License
  12.  * @copyright Copyright (C) 2004 Daniel Plücken <daniel@debakel.net>
  13.  *
  14.  *  This library is free software; you can redistribute it and/or
  15.  *  modify it under the terms of the GNU Lesser General Public
  16.  *  License as published by the Free Software Foundation; either
  17.  *  version 2.1 of the License.
  18.  *
  19.  *  This library is distributed in the hope that it will be useful,
  20.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22.  *  GNU Lesser General Public License for more details.
  23.  *
  24.  *  You should have received a copy of the GNU Lesser General
  25.  *  Public License along with this library; if not, write to the
  26.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  27.  *  Boston, MA 02111-1307 USA
  28.  */
  29. class Join
  30. {
  31.   /**
  32.    * The left databasetable's object of this join.
  33.    *
  34.    * @var    object $dbt_left 
  35.    * @access public
  36.    */
  37.   var $dbt_left = null;
  38.   /**
  39.    * The right databasetable's object of this join.
  40.    *
  41.    * @var    object $dbt_right 
  42.    * @access public
  43.    */
  44.   var $dbt_right = null;
  45.   /**
  46.    * Carries the left fieldnames of this join.
  47.    * Note that fieldnames in a join could also be values. It isn't necessary to
  48.    * join one fieldname to another.
  49.    *
  50.    * @var    array   $left_field 
  51.    * @access private
  52.    */
  53.   var $left_field = "";
  54.   /**
  55.    * Carries the right fieldnames of this join.
  56.    * Note that fieldnames in a join could also be values. It isn't necessary to
  57.    * join one fieldname to another.
  58.    *
  59.    * @var    array   $right_field 
  60.    * @access private
  61.    */
  62.   var $right_field = "";
  63.   /**
  64.    * Carries an alias of the tablename of $this->dbt_left.
  65.    *
  66.    * @var    array   $alias_left 
  67.    * @access private
  68.    */
  69.   var $alias_left = "";
  70.   /**
  71.    * Carries an alias of the tablename of $this->dbt_right.
  72.    *
  73.    * @var    array  $alias_right 
  74.    * @access public
  75.    */
  76.   var $alias_right = "";
  77.   /**
  78.    * Carries the relational operators between the fields in the "ON"-Clause of
  79.    * the join.
  80.    *
  81.    * @var    array  $join_operator 
  82.    * @access public
  83.    */
  84.   var $relational_operator = array();
  85.   /**
  86.    * Carries the logical operators between the relational statements in the
  87.    * "ON"-Clause of the join.
  88.    *
  89.    * @var    array  $join_operator 
  90.    * @access public
  91.    */
  92.   var $logical_operator = array();
  93.   /**
  94.    * Carries the type of join. Can be INNER, CROSS, LEFT [OUTER], RIGHT [OUTER]
  95.    * or NATURAL [LEFT | RIGTH [OUTER]]. The Standard is LEFT, because it was the
  96.    * only used join in the past.
  97.    *
  98.    * @var    string  $type 
  99.    * @access public
  100.    */
  101.   var $type = "LEFT";
  102.  
  103.  
  104.   /**
  105.    * Constructor
  106.    *
  107.    * @version 1.2
  108.    * @since   0.1.0
  109.    * @author  Daniel Plücken <daniel@debakel.net>
  110.    * @access  public
  111.    * @param   object $dbt_left 
  112.    * @param   object $dbt_right 
  113.    * @param   mixed  $left_field    A fieldname of $dbt_left which should get
  114.    *                                 a join or a condition for $right_field.
  115.    * @param   mixed  $right_field   A fieldname that should join to $left_field.
  116.    * @param   string $alias_left*   An alias for the tablename of $dbt_left.
  117.    * @param   string $alias_right*  An alias for the tablename of $dbt_right.
  118.    * @return  Join 
  119.    */
  120.   function Join(
  121.                  &$dbt_left,       &$dbt_right,
  122.                  $left_field,       $right_field,
  123.                  $alias_left "l"$alias_right "r",
  124.                  $logical_operator array(),
  125.                  $relational_operator array()
  126.                )
  127.   {
  128.     if(
  129.         is_a$dbt_left"ABSTDatabaseTable" )
  130.      || is_subclass_of$dbt_left"ABSTDatabaseTable" )
  131.       )
  132.       $this->dbt_left = &$dbt_left;
  133.     else
  134.       die(
  135.            "<h3>The parameter dbt_left does not "
  136.               ."refer to a valid database's object!</h3>"
  137.          );
  138.  
  139.     if(
  140.         is_a$dbt_right"ABSTDatabaseTable" )
  141.      || is_subclass_of$dbt_right"ABSTDatabaseTable" )
  142.       )
  143.       $this->dbt_right = &$dbt_right;
  144.     else
  145.       die(
  146.            "<h3>The parameter dbt_right does not "
  147.               ."refer to a valid database's object!</h3>"
  148.          );
  149.  
  150.  
  151.     ifis_array$left_field ) )
  152.       $this->left_field = $left_field;
  153.     else
  154.       $this->left_field[$left_field;
  155.  
  156.     ifis_array$right_field ) )
  157.       $this->right_field = $right_field;
  158.     else
  159.       $this->right_field[$right_field;
  160.  
  161.     ifis_array$logical_operator ) )
  162.       $this->logical_operator = $logical_operator;
  163.     else
  164.       $this->logical_operator[$logical_operator;
  165.  
  166.     ifis_array$relational_operator ) )
  167.       $this->relational_operator = $relational_operator;
  168.     else
  169.       $this->relational_operator[$relational_operator;
  170.  
  171.  
  172.     $this->alias_left = $alias_left;
  173.     $this->alias_right = $alias_right;
  174.   }
  175.  
  176.  
  177.  
  178.   /**
  179.    * Sets the type of join. Has to be INNER, CROSS, LEFT [OUTER], RIGHT [OUTER]
  180.    * or NATURAL [LEFT | RIGTH [OUTER]]
  181.    *
  182.    * @version 1.01
  183.    * @since   0.1.3
  184.    * @author  Daniel Plücken <daniel@debakel.net>
  185.    * @access  public
  186.    * @param   string  $left_field   A fieldname of $dbt_left which should get a
  187.    *                                 join or a condition for $right_field.
  188.    * @param   string  $right_field  A fieldname that should join to $left_field.
  189.    * @return  void 
  190.    */
  191.   function setType$string )
  192.   {
  193.      if (
  194.           $string == ""
  195.        || preg_match(
  196.                       "!^(?:"
  197.                           ."(?:LEFT|RIGHT)(?:\s+OUTER)?"
  198.                           ."|INNER|CROSS"
  199.                           ."|NATURAL(?:\s+(?:LEFT|RIGHT)(?:\s+OUTER)?)?"
  200.                        .")$!i"$string
  201.                     )
  202.         )
  203.      {   $this->type = strtoupper$string )}
  204.   }
  205.  
  206.  
  207.  
  208.   /**
  209.    * Adds Fields to this join.
  210.    *
  211.    * @version 1.0
  212.    * @since   0.1.1
  213.    * @author  Daniel Plücken ( daniel@debakel.net )
  214.    * @access  public
  215.    * @param   string  $left_field   A fieldname of $dbt_left which should get a
  216.    *                                 join or a condition for $right_field.
  217.    * @param   string  $right_field  A fieldname that should join to $left_field.
  218.    * @return  void 
  219.    */
  220.   function addFields()
  221.   {
  222.     $param func_get_args();
  223.  
  224.     ifcount$param == )
  225.       array_pop$param );
  226.  
  227.     for$i 0$i count$param )$i++)
  228.     {
  229.       if(
  230.           $i == 0
  231.        && !empty$param[$i)
  232.         )
  233.         $this->left_field[$param[$i];
  234.       if(
  235.           $i == 1
  236.        && !empty$param[$i)
  237.         )
  238.         $this->right_field[$param[$i];
  239.     }
  240.   }
  241.  
  242.  
  243.  
  244.   /**
  245.    * Swaps the left-right definition. The reference from the left table moves
  246.    * to the reference of the right database table and the other way around.
  247.    * This is useful for some complications with the join generation.
  248.    *
  249.    * @version 1.0
  250.    * @since   0.1.2
  251.    * @author  Daniel Plücken <daniel@debakel.net>
  252.    * @access  public
  253.    * @return  void 
  254.    */
  255.   function swapLeftRight()
  256.   {
  257.     $help_ref &$this->alias_left;
  258.     unset$this->alias_left );
  259.     $this->alias_left =$this->alias_right;
  260.     unset$this->alias_right );
  261.     $this->alias_right =$help_ref;
  262.     unset$help_ref );
  263.  
  264.     $help_ref &$this->left_field;
  265.     unset$this->left_field );
  266.     $this->left_field =$this->right_field;
  267.     unset$this->right_field );
  268.     $this->right_field =$help_ref;
  269.     unset$help_ref );
  270.  
  271.     $help_ref &$this->dbt_left;
  272.     unset$this->dbt_left );
  273.     $this->dbt_left =$this->dbt_right;
  274.     unset$this->dbt_right );
  275.     $this->dbt_right =$help_ref;
  276.     unset$help_ref );
  277.   }
  278. // End of class Join
  279. ?>

Documentation generated on Thu, 05 Jun 2008 19:13:41 +0200 by phpDocumentor 1.4.1