PageRenderTime 53ms CodeModel.GetById 11ms RepoModel.GetById 15ms app.codeStats 0ms

/lib/php/DB/QueryTool/EasyJoin.php

https://bitbucket.org/adarshj/convenient_website
PHP | 135 lines | 45 code | 13 blank | 77 comment | 9 complexity | bb22c576f9745c7a23d530c26b5a7d42 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains the DB_QueryTool_EasyJoin class
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Database
  15. * @package DB_QueryTool
  16. * @author Wolfram Kriesing <wk@visionp.de>
  17. * @author Paolo Panto <wk@visionp.de>
  18. * @copyright 2003-2005 Wolfram Kriesing, Paolo Panto
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  20. * @version CVS: $Id: EasyJoin.php,v 1.9 2005/02/27 17:15:05 quipo Exp $
  21. * @link http://pear.php.net/package/DB_QueryTool
  22. */
  23. /**
  24. * require the DB_QueryTool_Query class
  25. */
  26. require_once 'DB/QueryTool/Query.php';
  27. /**
  28. * DB_QueryTool_EasyJoin class
  29. *
  30. * @category Database
  31. * @package DB_QueryTool
  32. * @author Wolfram Kriesing <wk@visionp.de>
  33. * @copyright 2003-2005 Wolfram Kriesing
  34. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  35. * @link http://pear.php.net/package/DB_QueryTool
  36. */
  37. class DB_QueryTool_EasyJoin extends DB_QueryTool_Query
  38. {
  39. // {{{ class vars
  40. /**
  41. * This is the regular expression that shall be used to find a table's shortName
  42. * in a column name, the string found by using this regular expression will be removed
  43. * from the column name and it will be checked if it is a table name
  44. * i.e. the default '/_id$/' would find the table name 'user' from the column name 'user_id'
  45. *
  46. * @var string regexp
  47. */
  48. var $_tableNamePreg = '/_id$/';
  49. /**
  50. * This is to find the column name that is referred by it, so the default find
  51. * from 'user_id' the column 'id' which will be used to refer to the 'user' table
  52. *
  53. * @var string regexp
  54. */
  55. var $_columnNamePreg = '/^.*_/';
  56. // }}}
  57. // {{{ __construct()
  58. /**
  59. * call parent constructor
  60. * @param mixed $dsn DSN string, DSN array or DB object
  61. * @param array $options
  62. */
  63. function __construct($dsn=false, $options=array())
  64. {
  65. parent::DB_QueryTool_Query($dsn, $options);
  66. }
  67. // }}}
  68. // {{{ autoJoin()
  69. /**
  70. * Join the given tables, using the column names, to find out how to join the tables;
  71. * i.e., if table1 has a column named &quot;table2_id&quot;, this method will join
  72. * &quot;WHERE table1.table2_id=table2.id&quot;.
  73. * All joins made here are only concatenated via AND.
  74. * @param array $tables
  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. $shortNameIndexed = $this->getTableSpec(true, $tables);
  84. $nameIndexed = $this->getTableSpec(false, $tables);
  85. //print_r($shortNameIndexed);
  86. //print_r($tables); print '<br><br>';
  87. if (sizeof($shortNameIndexed) != sizeof($tables)) {
  88. $this->_errorLog("autoJoin-ERROR: not all the tables are in the tableSpec!<br />");
  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'
  107. // this would be quite stupid, but it used to be :-(
  108. if ($shortNameIndexed[$possibleTableShortName]['name'] != $aTable ||
  109. $possibleColumnName != $aCol
  110. ) {
  111. $where = $shortNameIndexed[$possibleTableShortName]['name'].".$possibleColumnName=$aTable.$aCol";
  112. $this->addJoin($nameIndexed[$aTable]['name'], $where);
  113. $this->addJoin($shortNameIndexed[$possibleTableShortName]['name'], $where);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. // }}}
  121. }
  122. ?>