All Data Structures Functions Variables Pages
ModelWhereAndJoinBuilder.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 
41  {
42  protected $wherePartColumnModifierType;
43 
44  public function __construct(RedBeanModelAttributeToDataProviderAdapter
45  $modelAttributeToDataProviderAdapter,
47  $joinTablesAdapter,
48  $setDistinct = false,
49  $wherePartColumnModifierType = null)
50  {
51  assert('is_string($wherePartColumnModifierType) || $wherePartColumnModifierType == null');
52  parent::__construct($modelAttributeToDataProviderAdapter, $joinTablesAdapter, $setDistinct);
53  $this->wherePartColumnModifierType = $wherePartColumnModifierType;
54  }
55 
64  public function resolveJoinsAndBuildWhere($operatorType, $value, & $clausePosition, & $where,
65  $onTableAliasName = null, $resolveAsSubquery = false)
66  {
67  assert('is_string($operatorType)');
68  assert('is_array($where)');
69  assert('is_string($onTableAliasName) || $onTableAliasName == null');
70  assert('is_bool($resolveAsSubquery)');
71  if (!$this->modelAttributeToDataProviderAdapter->hasRelatedAttribute())
72  {
73  $tableAliasName = $this->resolveJoins($onTableAliasName,
75  $this->addWherePartByClauseInformation($operatorType, $value, $where, $clausePosition, $tableAliasName,
76  $this->modelAttributeToDataProviderAdapter->getColumnName());
77  }
78  elseif ($this->modelAttributeToDataProviderAdapter->isRelationTypeAHasOneVariant() &&
79  $this->modelAttributeToDataProviderAdapter->getRelatedAttribute() == 'id')
80  {
81  $this->addWherePartByClauseInformation($operatorType, $value, $where, $clausePosition,
82  $this->resolveJoinsForRelatedId($onTableAliasName),
83  $this->modelAttributeToDataProviderAdapter->getColumnName());
84  }
85  else
86  {
87  $this->buildJoinAndWhereForRelatedAttribute($operatorType, $value, $clausePosition, $where,
88  $onTableAliasName, $resolveAsSubquery);
89  }
90  }
91 
100  protected function buildJoinAndWhereForRelatedAttribute($operatorType, $value, $whereKey, &$where,
101  $onTableAliasName = null, $resolveAsSubquery = false)
102  {
103  assert('is_string($operatorType)');
104  assert('is_int($whereKey)');
105  assert('is_array($where)');
106  assert('is_string($onTableAliasName) || $onTableAliasName == null');
107  assert('is_bool($resolveAsSubquery)');
108  $relationWhere = array();
109  if ($this->modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::MANY_MANY)
110  {
111  $relationAttributeTableAliasName = $this->resolveJoins($onTableAliasName,
113  $this->addWherePartByClauseInformation($operatorType, $value, $relationWhere, 1,
114  $relationAttributeTableAliasName,
115  $this->modelAttributeToDataProviderAdapter->resolveManyToManyColumnName());
116  }
117  elseif (($this->modelAttributeToDataProviderAdapter->isRelatedAttributeRelation() &&
118  $this->modelAttributeToDataProviderAdapter->getRelatedAttributeRelationType() == RedBeanModel::HAS_MANY) ||
119  $resolveAsSubquery)
120  {
121  $relationAttributeTableAliasName = $this->resolveRelationAttributeTableAliasNameForResolveSubquery(
122  $onTableAliasName, $resolveAsSubquery);
124  $relationAttributeTableAliasName,
125  $operatorType,
126  $value,
127  $relationWhere,
128  1);
129  }
130  else
131  {
132  $relationAttributeTableAliasName = $this->resolveJoins($onTableAliasName,
134  $this->addWherePartByClauseInformation($operatorType,
135  $value,
136  $relationWhere,
137  1,
138  $relationAttributeTableAliasName,
139  $this->modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName());
140  }
141  $where[$whereKey] = strtr('1', $relationWhere);
142  }
143 
144  protected function resolveRelationAttributeTableAliasNameForResolveSubquery($onTableAliasName, $resolveAsSubquery = false)
145  {
146  assert('is_string($onTableAliasName) || $onTableAliasName == null');
147  assert('is_bool($resolveAsSubquery)');
148  if ($resolveAsSubquery)
149  {
150  return $this->resolveRelationAttributeTableAliasNameForResolveSubqueryAsTrue($onTableAliasName);
151  }
152  else
153  {
154  return $this->resolveOnlyAttributeJoins($onTableAliasName,
156  }
157  }
158 
159  protected function resolveRelationAttributeTableAliasNameForResolveSubqueryAsTrue($onTableAliasName)
160  {
161  assert('is_string($onTableAliasName) || $onTableAliasName == null');
162  if ($onTableAliasName == null)
163  {
164  return $this->modelAttributeToDataProviderAdapter->getModelTableName();
165  }
166  else
167  {
168  return $onTableAliasName;
169  }
170  }
171 
182  protected function buildWhereForRelatedAttributeThatIsItselfAHasManyRelation($onTableAliasName,
183  $operatorType,
184  $value,
185  & $where,
186  $whereKey
187  )
188  {
189  assert('is_string($onTableAliasName)');
190  assert('is_string($operatorType)');
191  assert('(is_array($value) && count($value) > 0) || is_string($value) || is_int($value)');
192  assert('is_array($where)');
193  assert('is_int($whereKey)');
194  if (!$this->modelAttributeToDataProviderAdapter->getRelatedAttributeRelationType() == RedBeanModel::HAS_MANY)
195  {
196  throw new NotSupportedException();
197  }
198  $relationAttributeModelClassName = $this->modelAttributeToDataProviderAdapter->getRelatedAttributeRelationModelClassName();
199  if ($relationAttributeModelClassName != 'CustomFieldValue' && $operatorType != 'allOf')
200  {
201  $modelClassName = $this->modelAttributeToDataProviderAdapter->getRelationModelClassName();
202  $relationAttributeTableName = $modelClassName::getTableName();
203  $joinColumnName = $modelClassName::getColumnNameByAttribute(
204  $this->modelAttributeToDataProviderAdapter->getRelatedAttribute());
205  $relationColumnModelClassName = $this->modelAttributeToDataProviderAdapter->getModelClassName();
206  $relationColumnName = self::resolveForeignKey($relationColumnModelClassName::getTableName());
207  }
208  else
209  {
210  $relationAttributeTableName = $relationAttributeModelClassName::getTableName();
211  $joinColumnName = 'value';
212  $relatedAttributeModelClassName = $this->modelAttributeToDataProviderAdapter->getRelatedAttributeModelClassName();
213  $relationColumnName = self::resolveForeignKey($relatedAttributeModelClassName::getTableName());
214  }
215  $tableAliasName = $relationAttributeTableName;
217  $where[$whereKey] = "(1 = (select 1 from $quote$relationAttributeTableName$quote $tableAliasName " . // Not Coding Standard
218  "where $quote$tableAliasName$quote.$quote$relationColumnName$quote = " . // Not Coding Standard
219  "$quote$onTableAliasName$quote.id " . // Not Coding Standard
220  "and $quote$tableAliasName$quote.$quote$joinColumnName$quote " . // Not Coding Standard
221  DatabaseCompatibilityUtil::getOperatorAndValueWherePart($operatorType, $value) . " limit 1))";
222  }
223 
228  protected function resolveJoinsForRelatedId($onTableAliasName = null)
229  {
230  assert('is_string($onTableAliasName) || $onTableAliasName == null');
231  if ($this->modelAttributeToDataProviderAdapter->isAttributeOnDifferentModel() && $onTableAliasName == null)
232  {
234  }
235  elseif ($this->modelAttributeToDataProviderAdapter->isAttributeOnDifferentModel() &&
236  $onTableAliasName != null)
237  {
238  return $this->addLeftJoinsForAttributeThatIsCastedUp($onTableAliasName);
239  }
240  else
241  {
242  return $this->resolveOnTableAliasName($onTableAliasName);
243  //return $this->modelAttributeToDataProviderAdapter->getModelTableName();
244  }
245  }
246 
256  protected function addWherePartByClauseInformation( $operatorType, $value, &$where,
257  $whereKey, $tableAliasName, $columnName)
258  {
259  assert('is_string($operatorType)');
260  assert('is_array($where)');
261  assert('is_int($whereKey)');
262  assert('is_string($tableAliasName)');
263  assert('is_string($columnName)');
264  if (is_string($value) || (is_array($value) && count($value) > 0) || $value !== null ||
265  ($value === null && SQLOperatorUtil::doesOperatorTypeAllowNullValues($operatorType)))
266  {
267  $where[$whereKey] = "(" . self::resolveWhereColumnContentForModifier($tableAliasName, $columnName) . " " .
269  $value) . ")";
270  }
271  }
272 
278  protected function resolveWhereColumnContentForModifier($tableAliasName, $columnName)
279  {
280  assert('is_string($tableAliasName)');
281  assert('is_string($columnName)');
282  $content = self::makeColumnNameWithTableAlias($tableAliasName, $columnName);
283  if ($this->wherePartColumnModifierType != null)
284  {
286  $content = strtolower($this->wherePartColumnModifierType) . '(' . $content . ')';
287  }
288  return $content;
289  }
290 
305  protected function resolveLeftJoinsForARelationAttribute($onTableAliasName)
306  {
307  assert('is_string($onTableAliasName)');
308  if ($this->modelAttributeToDataProviderAdapter->hasRelatedAttribute())
309  {
310  return $this->addLeftJoinsForARelationAttribute($onTableAliasName);
311  }
312  //elseif ($this->modelAttributeToDataProviderAdapter->isOwnedRelation())
313  //{
314  // return $this->addLeftJoinsForARelationAttribute($onTableAliasName);
315  //}
316  return $onTableAliasName;
317  }
318 
323  {
325  $this->modelAttributeToDataProviderAdapter->getModel(),
326  $this->modelAttributeToDataProviderAdapter->getAttribute());
327  if ($attributeType == 'DateTime')
328  {
329  return DatabaseCompatibilityUtil::makeTimeZoneAdjustmentContent();
330  }
331  }
332  }
333 ?>
resolveJoinsForRelatedId($onTableAliasName=null)
resolveLeftJoinsForARelationAttribute($onTableAliasName)
resolveOnTableAliasName($onTableAliasName=null)
static resolveCanUseFromJoins($onTableAliasName)
buildJoinAndWhereForRelatedAttribute($operatorType, $value, $whereKey, &$where, $onTableAliasName=null, $resolveAsSubquery=false)
addLeftJoinsForAttributeThatIsCastedUp($onTableAliasName)
const MANY_MANY
Definition: BeanModel.php:78
resolveWhereColumnContentForModifier($tableAliasName, $columnName)
const HAS_MANY
Definition: BeanModel.php:71
resolveJoins($onTableAliasName=null, $canUseFromJoins=true)
addLeftJoinsForARelationAttribute($onTableAliasName)
resolveJoinsAndBuildWhere($operatorType, $value, &$clausePosition, &$where, $onTableAliasName=null, $resolveAsSubquery=false)
resolveOnlyAttributeJoins($onTableAliasName=null, $canUseFromJoins=true)
addWherePartByClauseInformation($operatorType, $value, &$where, $whereKey, $tableAliasName, $columnName)
static getType($model, $attributeName)
buildWhereForRelatedAttributeThatIsItselfAHasManyRelation($onTableAliasName, $operatorType, $value, &$where, $whereKey)
static getOperatorAndValueWherePart($operatorType, $value)
Generated on Fri Jun 5 2020 07:10:28