Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelSelectQueryAdapter.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  {
47  private $clauses;
48 
54  private $distinct = false;
55 
60  private $clausesCount = 0;
61 
62  private $countClausePresent = false;
63 
64  private $idTableAliasesAndModelClassNames = array();
65 
66  public static function makeCountString($tableName, $columnName, $distinctPart = null)
67  {
68  assert('is_string($tableName)');
69  assert('is_string($columnName)');
70  assert('is_string($distinctPart) || $distinctPart == null');
72  $queryString = "{$distinctPart}{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
73  return "count({$queryString})";
74  }
75 
76  public static function makeSummationString($tableName, $columnName, $queryStringExtraPart = null)
77  {
78  assert('is_string($tableName)');
79  assert('is_string($columnName)');
80  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
82  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}" . $queryStringExtraPart;
83  return "sum({$queryString})";
84  }
85 
86  public static function makeAverageString($tableName, $columnName, $queryStringExtraPart = null)
87  {
88  assert('is_string($tableName)');
89  assert('is_string($columnName)');
90  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
92  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}" . $queryStringExtraPart;
93  return "avg({$queryString})";
94  }
95 
96  public static function makeMinimumString($tableName, $columnName, $queryStringExtraPart = null)
97  {
98  assert('is_string($tableName)');
99  assert('is_string($columnName)');
100  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
102  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}" . $queryStringExtraPart;
103  return "min({$queryString})";
104  }
105 
106  public static function makeMaximumString($tableName, $columnName, $queryStringExtraPart = null)
107  {
108  assert('is_string($tableName)');
109  assert('is_string($columnName)');
110  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
112  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}" . $queryStringExtraPart;
113  return "max({$queryString})";
114  }
115 
116  public static function makeDayModifierString($tableName, $columnName, $adjustForTimeZone = false)
117  {
118  assert('is_string($tableName)');
119  assert('is_string($columnName)');
120  assert('is_bool($adjustForTimeZone)');
122  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
123  if ($adjustForTimeZone)
124  {
125  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
126  }
127  return "day({$queryString})";
128  }
129 
130  public static function makeDayDateModifierString($tableName, $columnName, $adjustForTimeZone = false)
131  {
132  assert('is_string($tableName)');
133  assert('is_string($columnName)');
134  assert('is_bool($adjustForTimeZone)');
136  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
137  if ($adjustForTimeZone)
138  {
139  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
140  }
141  return "DATE_FORMAT({$queryString}, '%Y-%m-%d')";
142  }
143 
144  public static function makeWeekModifierString($tableName, $columnName, $adjustForTimeZone = false)
145  {
146  assert('is_string($tableName)');
147  assert('is_string($columnName)');
148  assert('is_bool($adjustForTimeZone)');
150  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
151  if ($adjustForTimeZone)
152  {
153  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
154  }
155  return "weekofyear({$queryString})";
156  }
157 
158  public static function makeFirstDayOfWeekDateModifierString($tableName, $columnName, $adjustForTimeZone = false)
159  {
160  assert('is_string($tableName)');
161  assert('is_string($columnName)');
162  assert('is_bool($adjustForTimeZone)');
164  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
165  if ($adjustForTimeZone)
166  {
167  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
168  }
169  return "DATE_FORMAT(DATE_SUB({$queryString}, INTERVAL(WEEKDAY(" .
170  "{$queryString})) day), '%Y-%m-%d')";
171  }
172 
173  public static function makeMonthModifierString($tableName, $columnName, $adjustForTimeZone = false)
174  {
175  assert('is_string($tableName)');
176  assert('is_string($columnName)');
177  assert('is_bool($adjustForTimeZone)');
179  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
180  if ($adjustForTimeZone)
181  {
182  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
183  }
184  return "month({$queryString})";
185  }
186 
187  public static function makeFirstDayOfMonthDateModifierString($tableName, $columnName, $adjustForTimeZone = false)
188  {
189  assert('is_string($tableName)');
190  assert('is_string($columnName)');
191  assert('is_bool($adjustForTimeZone)');
193  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
194  if ($adjustForTimeZone)
195  {
196  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
197  }
198  return $queryString = "DATE_FORMAT(DATE_ADD({$queryString}, INTERVAL(1-DAYOFMONTH(" .
199  "{$queryString})) day), '%Y-%m-%d')";
200  }
201 
202  public static function makeQuarterModifierString($tableName, $columnName, $adjustForTimeZone = false)
203  {
204  assert('is_string($tableName)');
205  assert('is_string($columnName)');
206  assert('is_bool($adjustForTimeZone)');
208  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
209  if ($adjustForTimeZone)
210  {
211  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
212  }
213  return "quarter({$queryString})";
214  }
215 
216  public static function makeYearModifierString($tableName, $columnName, $adjustForTimeZone = false)
217  {
218  assert('is_string($tableName)');
219  assert('is_string($columnName)');
220  assert('is_bool($adjustForTimeZone)');
222  $queryString = "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
223  if ($adjustForTimeZone)
224  {
225  $queryString .= DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
226  }
227  return "year({$queryString})";
228  }
229 
230  public function __construct($distinct = false)
231  {
232  $this->distinct = $distinct;
233  }
234 
235  protected function increaseClausesCountByOne()
236  {
237  $this->clausesCount++;
238  }
239 
240  public function isDistinct()
241  {
242  return $this->distinct;
243  }
244 
245  public function getClausesCount()
246  {
247  return $this->clausesCount;
248  }
249 
250  public function getClauses()
251  {
252  return $this->clauses;
253  }
254 
255  public function getIdTableAliasesAndModelClassNames()
256  {
257  return $this->idTableAliasesAndModelClassNames;
258  }
259 
260  public function getIdColumNameByTableAlias($tableAliasName)
261  {
262  assert('is_string($tableAliasName)');
263  return $tableAliasName . 'id';
264  }
265 
266  public function getSelect()
267  {
268  if ($this->getClausesCount() == 0)
269  {
270  throw new NotSupportedException();
271  }
272  $selectQuery = 'select ';
273  if ($this->distinct && !$this->countClausePresent)
274  {
275  $selectQuery .= 'distinct ';
276  }
277  foreach ($this->clauses as $clauseCount => $clause)
278  {
279  $selectQuery .= $clause;
280  if ($this->getClausesCount() > 1 && ($clauseCount + 1) < $this->getClausesCount())
281  {
282  $selectQuery .= ','; // Not Coding Standard
283  }
284  $selectQuery .= ' ';
285  }
286  return $selectQuery;
287  }
288 
289  public function addNonSpecificCountClause()
290  {
291  $this->clauses[] = "count(*)";
292  $this->countClausePresent = true;
293  $this->increaseClausesCountByOne();
294  }
295 
296  public function addCountClause($tableName, $columnName = 'id', $aliasName = null)
297  {
298  assert('is_string($tableName)');
299  assert('is_string($columnName)');
300  assert('is_string($aliasName) || $aliasName == null');
302  $distinctPart = null;
303  if ($this->distinct)
304  {
305  $distinctPart = 'distinct ';
306  }
307  $queryString = self::makeCountString($tableName, $columnName, $distinctPart);
308  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
309  $this->countClausePresent = true;
310  $this->increaseClausesCountByOne();
311  }
312 
313  public function addClause($tableName, $columnName, $aliasName = null)
314  {
315  assert('is_string($tableName)');
316  assert('is_string($columnName)');
317  assert('is_string($aliasName) || $aliasName == null');
319  $this->clauses[] = self::resolveForAliasName("{$quote}$tableName{$quote}.{$quote}$columnName{$quote}", $aliasName);
320  $this->increaseClausesCountByOne();
321  }
322 
323  public function addClauseByQueryString($queryString, $aliasName = null)
324  {
325  assert('is_string($queryString)');
326  assert('is_string($aliasName) || $aliasName == null');
327  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
328  $this->increaseClausesCountByOne();
329  }
330 
331  public function addClauseWithColumnNameOnlyAndNoEnclosure($columnName, $aliasName = null)
332  {
333  assert('is_string($columnName)');
334  assert('is_string($aliasName) || $aliasName == null');
335  $clause = "$columnName";
336  if ($aliasName != null)
337  {
338  $clause .= " $aliasName";
339  }
340  $this->clauses[] = self::resolveForAliasName("$columnName", $aliasName);
341  $this->increaseClausesCountByOne();
342  }
343 
344  public function addSummationClause($tableName, $columnName, $aliasName = null, $queryStringExtraPart = null)
345  {
346  assert('is_string($tableName)');
347  assert('is_string($columnName)');
348  assert('is_string($aliasName) || $aliasName == null');
349  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
350  $queryString = self::makeSummationString($tableName, $columnName, $queryStringExtraPart);
351  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
352  $this->increaseClausesCountByOne();
353  }
354 
355  public function addAverageClause($tableName, $columnName, $aliasName = null, $queryStringExtraPart = null)
356  {
357  assert('is_string($tableName)');
358  assert('is_string($columnName)');
359  assert('is_string($aliasName) || $aliasName == null');
360  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
361  $queryString = self::makeAverageString($tableName, $columnName, $queryStringExtraPart);
362  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
363  $this->increaseClausesCountByOne();
364  }
365 
366  public function addMinimumClause($tableName, $columnName, $aliasName = null, $queryStringExtraPart = null)
367  {
368  assert('is_string($tableName)');
369  assert('is_string($columnName)');
370  assert('is_string($aliasName) || $aliasName == null');
371  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
372  $queryString = self::makeMinimumString($tableName, $columnName, $queryStringExtraPart);
373  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
374  $this->increaseClausesCountByOne();
375  }
376 
377  public function addMaximumClause($tableName, $columnName, $aliasName = null, $queryStringExtraPart = null)
378  {
379  assert('is_string($tableName)');
380  assert('is_string($columnName)');
381  assert('is_string($aliasName) || $aliasName == null');
382  assert('is_string($queryStringExtraPart) || $queryStringExtraPart == null');
383  $queryString = self::makeMaximumString($tableName, $columnName, $queryStringExtraPart);
384  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
385  $this->increaseClausesCountByOne();
386  }
387 
388  public function addDayClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
389  {
390  assert('is_string($tableName)');
391  assert('is_string($columnName)');
392  assert('is_string($aliasName) || $aliasName == null');
393  assert('is_bool($adjustForTimeZone)');
394  $queryString = self::makeDayModifierString($tableName, $columnName, $adjustForTimeZone);
395  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
396  $this->increaseClausesCountByOne();
397  }
398 
399  public function addDayDateClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
400  {
401  assert('is_string($tableName)');
402  assert('is_string($columnName)');
403  assert('is_string($aliasName) || $aliasName == null');
404  assert('is_bool($adjustForTimeZone)');
405  $queryString = self::makeDayDateModifierString($tableName, $columnName, $adjustForTimeZone);
406  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
407  $this->increaseClausesCountByOne();
408  }
409 
410  public function addWeekClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
411  {
412  assert('is_string($tableName)');
413  assert('is_string($columnName)');
414  assert('is_string($aliasName) || $aliasName == null');
415  assert('is_bool($adjustForTimeZone)');
416  $queryString = self::makeWeekModifierString($tableName, $columnName, $adjustForTimeZone);
417  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
418  $this->increaseClausesCountByOne();
419  }
420 
421  public function addFirstDayOfWeekDateClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
422  {
423  assert('is_string($tableName)');
424  assert('is_string($columnName)');
425  assert('is_string($aliasName) || $aliasName == null');
426  assert('is_bool($adjustForTimeZone)');
427  $queryString = self::makeFirstDayOfWeekDateModifierString($tableName, $columnName, $adjustForTimeZone);
428  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
429  $this->increaseClausesCountByOne();
430  }
431 
432  public function addMonthClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
433  {
434  assert('is_string($tableName)');
435  assert('is_string($columnName)');
436  assert('is_string($aliasName) || $aliasName == null');
437  assert('is_bool($adjustForTimeZone)');
438  $queryString = self::makeMonthModifierString($tableName, $columnName, $adjustForTimeZone);
439  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
440  $this->increaseClausesCountByOne();
441  }
442 
443  public function addFirstDayOfMonthDateClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
444  {
445  assert('is_string($tableName)');
446  assert('is_string($columnName)');
447  assert('is_string($aliasName) || $aliasName == null');
448  assert('is_bool($adjustForTimeZone)');
449  $queryString = self::makeFirstDayOfMonthDateModifierString($tableName, $columnName, $adjustForTimeZone);
450  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
451  $this->increaseClausesCountByOne();
452  }
453 
454  public function addQuarterClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
455  {
456  assert('is_string($tableName)');
457  assert('is_string($columnName)');
458  assert('is_string($aliasName) || $aliasName == null');
459  assert('is_bool($adjustForTimeZone)');
460  $queryString = self::makeQuarterModifierString($tableName, $columnName, $adjustForTimeZone);
461  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
462  $this->increaseClausesCountByOne();
463  }
464 
465  public function addYearClause($tableName, $columnName, $aliasName = null, $adjustForTimeZone = false)
466  {
467  assert('is_string($tableName)');
468  assert('is_string($columnName)');
469  assert('is_string($aliasName) || $aliasName == null');
470  assert('is_bool($adjustForTimeZone)');
471  $queryString = self::makeYearModifierString($tableName, $columnName, $adjustForTimeZone);
472  $this->clauses[] = self::resolveForAliasName($queryString, $aliasName);
473  $this->increaseClausesCountByOne();
474  }
475 
476  public function resolveIdClause($modelClassName, $tableAliasName)
477  {
478  assert('is_string($modelClassName)');
479  assert('is_string($tableAliasName)');
480  if (!isset($this->idTableAliasesAndModelClassNames[$tableAliasName]))
481  {
482  $this->idTableAliasesAndModelClassNames[$tableAliasName] = $modelClassName;
483  $this->addClause($tableAliasName, 'id', $tableAliasName . 'id');
484  }
485  }
486 
487  public static function resolveForAliasName($clause, $aliasName = null)
488  {
489  assert('is_string($clause)');
490  assert('is_string($aliasName) || $aliasName == null');
491  if ($aliasName != null)
492  {
493  $clause .= " $aliasName";
494  }
495  return $clause;
496  }
497  }
498 ?>
Generated on Sun Jul 12 2020 07:10:26
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.