Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelJoinTablesQueryAdapter.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
42  {
43  private $baseFromTableName;
44 
45  private $fromTablesAndAliases = array();
46 
47  private $leftTablesAndAliases = array();
48 
49  private $tableCounts = array();
50 
56  private $selectDistinct = false;
57 
61  public function __construct($modelClassName)
62  {
63  assert('is_string($modelClassName)');
64  $tableName = $modelClassName::getTableName();
66  $this->baseFromTableName = $tableName;
67  $this->addTableCount($tableName);
68  }
69 
75  public function addFromTableAndGetAliasName($tableName, $onTableJoinIdName, $onTableAliasName = null,
76  $tableJoinIdName = 'id')
77  {
78  assert('is_string($tableName)');
79  assert('is_string($onTableJoinIdName)');
80  assert('$onTableAliasName == null || is_string($onTableAliasName)');
81  assert('is_string($tableJoinIdName)');
82  if ($onTableAliasName == null)
83  {
84  $onTableAliasName = $this->baseFromTableName;
85  }
86  $tableAliasName = $tableName . $this->getAliasByTableName($tableName);
87  $this->fromTablesAndAliases[] = array(
88  'tableName' => $tableName,
89  'tableAliasName' => $tableAliasName,
90  'tableJoinIdName' => $tableJoinIdName,
91  'onTableAliasName' => $onTableAliasName,
92  'onTableJoinIdName' => $onTableJoinIdName,
93  );
94  $this->addTableCount($tableName);
95  return $tableAliasName;
96  }
97 
104  public function getAlreadyFromJoinedTableAliasName($tableName)
105  {
106  foreach ($this->fromTablesAndAliases as $information)
107  {
108  if ( $information['tableName'] == $tableName)
109  {
110  return $information['tableAliasName'];
111  }
112  }
113  return null;
114  }
115 
122  public function addLeftTableAndGetAliasName($tableName, $onTableJoinIdName,
123  $onTableAliasName = null, $tableJoinIdName = 'id',
124  $extraOnQueryPart = null)
125  {
126  assert('is_string($tableName)');
127  assert('is_string($onTableJoinIdName)');
128  assert('$onTableAliasName == null || is_string($onTableAliasName)');
129  assert('$extraOnQueryPart == null || is_string($extraOnQueryPart)');
130  if ($onTableAliasName == null)
131  {
132  $onTableAliasName = $this->baseFromTableName;
133  }
134  if (null !== $existingTableAliasName = $this->getAlreadyLeftJoinedTableAliasName(
135  $tableName, $onTableAliasName, $onTableJoinIdName))
136  {
137  return $existingTableAliasName;
138  }
139 
140  $tableAliasName = $tableName . $this->getAliasByTableName($tableName);
141  $this->leftTablesAndAliases[] = array(
142  'tableName' => $tableName,
143  'tableAliasName' => $tableAliasName,
144  'tableJoinIdName' => $tableJoinIdName,
145  'onTableAliasName' => $onTableAliasName,
146  'onTableJoinIdName' => $onTableJoinIdName,
147  'extraOnQueryPart' => $extraOnQueryPart,
148  );
149  $this->addTableCount($tableName);
150  return $tableAliasName;
151  }
152 
158  protected function getAlreadyLeftJoinedTableAliasName($tableName, $onTableAliasName, $onTableJoinIdName)
159  {
160  foreach ($this->leftTablesAndAliases as $information)
161  {
162  if ( $information['tableName'] == $tableName &&
163  $information['onTableAliasName'] == $onTableAliasName &&
164  $information['onTableJoinIdName'] == $onTableJoinIdName)
165  {
166  return $information['tableAliasName'];
167  }
168  }
169  return null;
170  }
171 
175  public function getJoinFromQueryPart()
176  {
177  if (count($this->fromTablesAndAliases) > 0)
178  {
180  $joinTableStrings = array();
181  foreach ($this->fromTablesAndAliases as $information)
182  {
183  //tbd if we should add quotes around the alias as well.
184  if ( $information['tableName'] == $information['tableAliasName'])
185  {
186  $joinTableStrings[] = $quote . $information['tableName'] . $quote;
187  }
188  else
189  {
190  $joinTableStrings[] = $quote . $information['tableName'] . $quote . ' ' .
191  $information['tableAliasName'];
192  }
193  }
194  return implode(', ', $joinTableStrings);
195  }
196  return null;
197  }
198 
202  public function getJoinQueryPart()
203  {
204  if (count($this->leftTablesAndAliases) > 0)
205  {
207  $queryPart = null;
208  foreach ($this->leftTablesAndAliases as $information)
209  {
210  if ($queryPart != null)
211  {
212  $queryPart .= ' ';
213  }
214  if ($information['tableName'] == $information['tableAliasName'])
215  {
216  $queryPart .= 'left join ' . $quote . $information['tableName'] . $quote . ' ';
217  }
218  else
219  {
220  $queryPart .= 'left join ' . $quote . $information['tableName'] . $quote . ' ' .
221  $information['tableAliasName'] . ' ';
222  }
223  $queryPart .= 'on ' . $quote . $information['tableAliasName'] . $quote . '.' . $quote .
224  $information['tableJoinIdName'] . $quote .' = ';
225  $queryPart .= $quote . $information['onTableAliasName'] . $quote . '.' . $quote .
226  $information['onTableJoinIdName'] . $quote;
227  $queryPart .= $information['extraOnQueryPart'];
228  }
229  return $queryPart . ' ';
230  }
231  return null;
232  }
233 
237  public function getJoinWhereQueryPart()
238  {
239  if (count($this->fromTablesAndAliases) > 0)
240  {
242  $queryPart = null;
243  foreach ($this->fromTablesAndAliases as $information)
244  {
245  if ($queryPart != null)
246  {
247  $queryPart .= ' and ';
248  }
249  $queryPart .= $quote . $information['tableAliasName'] . $quote . '.' . $quote .
250  $information['tableJoinIdName'] . $quote . ' = ';
251  $queryPart .= $quote . $information['onTableAliasName'] . $quote . '.' . $quote .
252  $information['onTableJoinIdName'] . $quote;
253  }
254  return $queryPart;
255  }
256  return null;
257  }
258 
262  public function getFromTableJoinCount()
263  {
264  return count($this->fromTablesAndAliases);
265  }
266 
270  public function getLeftTableJoinCount()
271  {
272  return count($this->leftTablesAndAliases);
273  }
274 
278  public function getFromTablesAndAliases()
279  {
280  return $this->fromTablesAndAliases;
281  }
282 
286  public function getLeftTablesAndAliases()
287  {
288  return $this->leftTablesAndAliases;
289  }
290 
294  public function isTableInFromTables($tableName)
295  {
296  assert('is_string($tableName)');
297  foreach ($this->fromTablesAndAliases as $information)
298  {
299  if ($information['tableName'] == $tableName)
300  {
301  return true;
302  }
303  }
304  return false;
305  }
306 
307  public function getAliasByTableName($tableName)
308  {
309  if (isset($this->tableCounts[$tableName]))
310  {
311  $alias = $this->tableCounts[$tableName];
312  }
313  else
314  {
315  $alias = null;
316  }
317  return $alias;
318  }
319 
320  private function addTableCount($tableName)
321  {
322  if (isset($this->tableCounts[$tableName]))
323  {
324  $this->tableCounts[$tableName]++;
325  }
326  else
327  {
328  $this->tableCounts[$tableName] = 1;
329  }
330  }
331 
332  public function getSelectDistinct()
333  {
334  return $this->selectDistinct;
335  }
336 
337  public function setSelectDistinctToTrue()
338  {
339  $this->selectDistinct = true;
340  }
341  }
342 ?>
addLeftTableAndGetAliasName($tableName, $onTableJoinIdName, $onTableAliasName=null, $tableJoinIdName= 'id', $extraOnQueryPart=null)
addFromTableAndGetAliasName($tableName, $onTableJoinIdName, $onTableAliasName=null, $tableJoinIdName= 'id')
getAlreadyLeftJoinedTableAliasName($tableName, $onTableAliasName, $onTableJoinIdName)
Generated on Mon Aug 3 2020 07:10:26
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.