DB_QueryTool
[ class tree: DB_QueryTool ] [ index: DB_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:  Wolfram Kriesing, Paolo Panto, vision:produktion <wk@visionp.de>
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: EasyJoin.php,v 1.7 2004/10/13 16:24:47 quipo Exp $
  19. //
  20. /**
  21.  * Load DB_QueryTool_Query class
  22.  */
  23. require_once 'DB/QueryTool/Query.php';
  24.  
  25. /**
  26.  *
  27.  * @package    DB_QueryTool
  28.  * @access     public
  29.  * @author     Wolfram Kriesing <wolfram@kriesing.de>
  30.  */
  31. {
  32.     // {{{ class vars
  33.  
  34.     /**
  35.      * this is the regular expression that shall be used to find a table's shortName
  36.      * in a column name, the string found by using this regular expression will be removed
  37.      * from the column name and it will be checked if it is a table name
  38.      * i.e. the default '/_id$/' would find the table name 'user' from the column name 'user_id'
  39.      *
  40.      * @var string regexp
  41.      */
  42.     var $_tableNamePreg '/_id$/';
  43.  
  44.     /**
  45.      * this is to find the column name that is refered by it, so the default find
  46.      * from 'user_id' the column 'id' which will be used to refer to the 'user' table
  47.      *
  48.      * @var string regexp
  49.      */
  50.     var $_columnNamePreg '/^.*_/';
  51.  
  52.     // }}}
  53.     // {{{ __construct()
  54.  
  55.     /**
  56.      * call parent constructor
  57.      * @param mixed $dsn DSN string, DSN array or DB object
  58.      * @param array $options 
  59.      */
  60.     function __construct($dsn=false$options=array())
  61.     {
  62.         parent::DB_QueryTool_Query($dsn$options);
  63.     }
  64.  
  65.     // }}}
  66.     // {{{ autoJoin()
  67.  
  68.     /**
  69.      * join the tables given, using the column names, to find out how to join the tables
  70.      * this is, if table1 has a column names table2_id this method will join
  71.      * WHERE table1.table2_id=table2.id
  72.      * all joins made here are only concatenated via AND
  73.      */
  74.     function autoJoin($tables)
  75.     {
  76. // FIXXME if $tables is empty autoJoin all available tables that have a relation to $this->table, starting to search in $this->table
  77.         settype($tables'array');
  78.         // add this->table to the tables array, so we go thru the current table first
  79.         $tables array_merge(array($this->table)$tables);
  80.  
  81.         $shortNameIndexed $this->getTableSpec(true$tables);
  82.         $nameIndexed $this->getTableSpec(false$tables);
  83.  
  84. //print_r($shortNameIndexed);
  85. //print_r($tables);        print '<br><br>';
  86.         if (sizeof($shortNameIndexed!= sizeof($tables)) {
  87.             $this->_errorLog("autoJoin-ERROR: not all the tables are in the tableSpec!<br />");
  88.         }
  89.  
  90.         $joinTables = array();
  91.         $joinConditions = array();
  92.         foreach ($tables as $aTable{   // go through $this->table and all the given tables
  93.             if ($metadata $this->metadata($aTable))
  94.             foreach ($metadata as $aCol => $x{   // go through each row to check which might be related to $aTable
  95.                 $possibleTableShortName preg_replace($this->_tableNamePreg,  '' $aCol);
  96.                 $possibleColumnName     preg_replace($this->_columnNamePreg'' $aCol);
  97. //print "$aTable.$aCol .... possibleTableShortName=$possibleTableShortName .... possibleColumnName=$possibleColumnName<br>";
  98.                 if (isset($shortNameIndexed[$possibleTableShortName])) {
  99.                     // are the tables given in the tableSpec?
  100.                     if (!$shortNameIndexed[$possibleTableShortName]['name'||
  101.                         !$nameIndexed[$aTable]['name']{
  102.                         // its an error of the developer, so log the error, dont show it to the end user
  103.                         $this->_errorLog("autoJoin-ERROR: '$aTable' is not given in the tableSpec!<br />");
  104.                     else {
  105.                         // do only join different table.col combination,
  106.                         // we should not join stuff like 'question.question=question.question' this would be quite stupid, but it used to be :-(
  107.                         if ($shortNameIndexed[$possibleTableShortName]['name'!= $aTable ||
  108.                             $possibleColumnName != $aCol
  109.                         {
  110.                             $where $shortNameIndexed[$possibleTableShortName]['name'].".$possibleColumnName=$aTable.$aCol";
  111.                             $this->addJoin($nameIndexed[$aTable]['name'],                      $where);
  112.                             $this->addJoin($shortNameIndexed[$possibleTableShortName]['name']$where);
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.     }
  119.  
  120.     // }}}
  121. }
  122. ?>

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