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