PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/MDB/QueryTool/EasyJoin.php

https://bitbucket.org/adarshj/convenient_website
PHP | 133 lines | 47 code | 12 blank | 74 comment | 9 complexity | b4e7a32b47e5392bc5cb3006b0366a7d 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 MDB_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 MDB_QueryTool
  16. * @author Lorenzo Alberton <l dot alberton at quipo dot it>
  17. * @copyright 2003-2005 Lorenzo Alberton
  18. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  19. * @version CVS: $Id: EasyJoin.php,v 1.10 2005/02/25 16:37:57 quipo Exp $
  20. * @link http://pear.php.net/package/MDB_QueryTool
  21. */
  22. /**
  23. * require the MDB_QueryTool_Query class
  24. */
  25. require_once 'MDB/QueryTool/Query.php';
  26. /**
  27. * MDB_QueryTool_EasyJoin class
  28. *
  29. * @category Database
  30. * @package MDB_QueryTool
  31. * @author Lorenzo Alberton <l dot alberton at quipo dot it>
  32. * @copyright 2003-2005 Lorenzo Alberton
  33. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  34. * @link http://pear.php.net/package/MDB_QueryTool
  35. */
  36. class MDB_QueryTool_EasyJoin extends MDB_QueryTool_Query
  37. {
  38. // {{{ class vars
  39. /**
  40. * this is the regular expression that shall be used to find a table's
  41. * shortName in a column name, the string found by using this regular
  42. * expression will be removed from the column name and it will be checked
  43. * if it is a table name i.e. the default '/_id$/' would find the table name
  44. * 'user' from the column name 'user_id'
  45. */
  46. var $_tableNamePreg = '/_id$/';
  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. // {{{ __construct()
  55. /**
  56. * call parent constructor
  57. * @param mixed $dsn DSN string, DSN array or MDB object
  58. * @param array $options
  59. */
  60. function __construct($dsn=false, $options=array())
  61. {
  62. parent::MDB_QueryTool_Query($dsn, $options);
  63. }
  64. // }}}
  65. // {{{ autoJoin()
  66. /**
  67. * join the tables given, using the column names, to find out how to join
  68. * the tables this is, if table1 has a column names table2_id this method
  69. * will join WHERE table1.table2_id=table2.id
  70. * all joins made here are only concatenated via AND
  71. */
  72. function autoJoin($tables)
  73. {
  74. // FIXXME if $tables is empty, autoJoin all available tables that have a relation
  75. // to $this->table, starting to search in $this->table
  76. settype($tables, 'array');
  77. // add this->table to the tables array, so we go thru the current table first
  78. $tables = array_merge(array($this->table), $tables);
  79. $shortNameIndexed = $this->getTableSpec(true, $tables);
  80. $nameIndexed = $this->getTableSpec(false, $tables);
  81. //print_r($shortNameIndexed);
  82. //print_r($tables); print '<br /> <br />';
  83. if (sizeof($shortNameIndexed) != sizeof($tables)) {
  84. $this->_errorLog('autoJoin-ERROR: not all the tables are in the tableSpec!<br />');
  85. }
  86. $joinTables = array();
  87. $joinConditions = array();
  88. foreach ($tables as $aTable) {
  89. // go through $this->table and all the given tables
  90. if ($metadata = $this->metadata($aTable)) {
  91. foreach ($metadata as $aCol => $x) {
  92. // go through each row to check which might be related to $aTable
  93. $possibleTableShortName = preg_replace($this->_tableNamePreg, '', $aCol);
  94. $possibleColumnName = preg_replace($this->_columnNamePreg, '', $aCol);
  95. //print "$aTable.$aCol .... possibleTableShortName=$possibleTableShortName .... possibleColumnName=$possibleColumnName<br />";
  96. if (!empty($shortNameIndexed[$possibleTableShortName])) {
  97. // are the tables given in the tableSpec?
  98. if (!$shortNameIndexed[$possibleTableShortName]['name'] ||
  99. !$nameIndexed[$aTable]['name'])
  100. {
  101. // its an error of the developer, so log the error, dont show it to the end user
  102. $this->_errorLog("autoJoin-ERROR: '$aTable' is not given in the tableSpec!<br />");
  103. } else {
  104. // do only join different table.col combination,
  105. // we should not join stuff like 'question.question=question.question' this would be quite stupid, but it used to be :-(
  106. if ($shortNameIndexed[$possibleTableShortName]['name'] != $aTable ||
  107. $possibleColumnName != $aCol
  108. ) {
  109. $where = $shortNameIndexed[$possibleTableShortName]['name'].".$possibleColumnName=$aTable.$aCol";
  110. $this->addJoin($nameIndexed[$aTable]['name'], $where);
  111. $this->addJoin($shortNameIndexed[$possibleTableShortName]['name'], $where);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. // }}}
  120. }
  121. ?>