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

Source for file EasyJoin.php

Documentation is available at EasyJoin.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.02 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Lorenzo Alberton <l.alberton at quipo.it>                    |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: EasyJoin.php,v 1.8 2004/10/13 16:28:20 quipo Exp $
  19. //
  20. // This is just a port of DB_QueryTool, originally written by
  21. // Wolfram Kriesing and Paolo Panto, vision:produktion <wk@visionp.de>
  22. // All the praises go to them :)
  23. //
  24. /**
  25.  * Load MDB_QueryTool_Query class
  26.  */
  27. require_once 'MDB/QueryTool/Query.php';
  28.  
  29. /**
  30.  *
  31.  *   @package MDB_QueryTool
  32.  *   @author  Lorenzo Alberton <l.alberton@quipo.it>
  33.  *   @access  public
  34.  */
  35. {
  36.     // {{{ class vars
  37.  
  38.     /**
  39.      * this is the regular expression that shall be used to find a table's
  40.      * shortName in a column name, the string found by using this regular
  41.      * expression will be removed from the column name and it will be checked
  42.      * if it is a table name i.e. the default '/_id$/' would find the table name
  43.      * 'user' from the column name 'user_id'
  44.      */
  45.     var $_tableNamePreg '/_id$/';
  46.  
  47.     /**
  48.      * this is to find the column name that is refered by it, so the default
  49.      * find from 'user_id' the column 'id' which will be used to refer to the
  50.      * 'user' table
  51.      */
  52.     var $_columnNamePreg '/^.*_/';
  53.  
  54.     // }}}
  55.     // {{{ __construct()
  56.  
  57.     /**
  58.      * call parent constructor
  59.      * @param mixed $dsn DSN string, DSN array or MDB object
  60.      * @param array $options 
  61.      */
  62.     function __construct($dsn=false$options=array())
  63.     {
  64.         parent::MDB_QueryTool_Query($dsn$options);
  65.     }
  66.  
  67.     // }}}
  68.     // {{{ autoJoin()
  69.  
  70.     /**
  71.      * join the tables given, using the column names, to find out how to join
  72.      * the tables this is, if table1 has a column names table2_id this method
  73.      * will join WHERE table1.table2_id=table2.id
  74.      * all joins made here are only concatenated via AND
  75.      */
  76.     function autoJoin($tables)
  77.     {
  78. // FIXXME if $tables is empty, autoJoin all available tables that have a relation
  79. // to $this->table, starting to search in $this->table
  80.         settype($tables'array');
  81.         // add this->table to the tables array, so we go thru the current table first
  82.         $tables array_merge(array($this->table)$tables);
  83.  
  84.         $shortNameIndexed $this->getTableSpec(true,  $tables);
  85.         $nameIndexed      $this->getTableSpec(false$tables);
  86.  
  87. //print_r($shortNameIndexed);
  88. //print_r($tables);        print '<br /> <br />';
  89.         if (sizeof($shortNameIndexed!= sizeof($tables)) {
  90.             $this->_errorLog('autoJoin-ERROR: not all the tables are in the tableSpec!<br />');
  91.         }
  92.         $joinTables     = array();
  93.         $joinConditions = array();
  94.         foreach ($tables as $aTable{
  95.             // go through $this->table and all the given tables
  96.             if ($metadata $this->metadata($aTable)) {
  97.                 foreach ($metadata as $aCol => $x{
  98.                     // go through each row to check which might be related to $aTable
  99.                     $possibleTableShortName preg_replace($this->_tableNamePreg,  ''$aCol);
  100.                     $possibleColumnName     preg_replace($this->_columnNamePreg''$aCol);
  101. //print "$aTable.$aCol .... possibleTableShortName=$possibleTableShortName .... possibleColumnName=$possibleColumnName<br />";
  102.                     if (!empty($shortNameIndexed[$possibleTableShortName])) {
  103.                         // are the tables given in the tableSpec?
  104.                         if (!$shortNameIndexed[$possibleTableShortName]['name'||
  105.                            !$nameIndexed[$aTable]['name'])
  106.                         {
  107.                             // its an error of the developer, so log the error, dont show it to the end user
  108.                             $this->_errorLog("autoJoin-ERROR: '$aTable' is not given in the tableSpec!<br />");
  109.                         else {
  110.                             // do only join different table.col combination,
  111.                             // we should not join stuff like 'question.question=question.question' this would be quite stupid, but it used to be :-(
  112.                             if ($shortNameIndexed[$possibleTableShortName]['name'!= $aTable ||
  113.                                 $possibleColumnName != $aCol
  114.                             {
  115.                                 $where $shortNameIndexed[$possibleTableShortName]['name'].".$possibleColumnName=$aTable.$aCol";
  116.                                 $this->addJoin($nameIndexed[$aTable]['name'],                      $where);
  117.                                 $this->addJoin($shortNameIndexed[$possibleTableShortName]['name']$where);
  118.                             }
  119.                         }
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.     }
  125.  
  126.     // }}}
  127. }
  128. ?>

Documentation generated on Mon, 11 Mar 2019 13:57:13 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.