Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chainelement.php
1<?php
2
4
16
18{
20 protected $value;
21
22 protected $parameters;
23
24 protected $type;
25
27
28 protected $alias_fragment;
29
42 public function __construct($element, $parameters = array())
43 {
44 if ($element instanceof Reference)
45 {
46 $this->type = 2;
47 }
48 elseif (is_array($element)
49 && $element[0] instanceof Entity
50 && $element[1] instanceof Reference
51 )
52 {
53 $this->type = 3;
54 }
55 elseif ($element instanceof Entity)
56 {
57 $this->type = 4;
58 }
59 elseif ($element instanceof Field)
60 {
61 $this->type = 1;
62 }
63 else
64 {
65 throw new SystemException(sprintf('Invalid value for QueryChainElement: %s.', $element));
66 }
67
68 $this->value = $element;
69 $this->parameters = $parameters;
70 }
71
75 public function getValue()
76 {
77 return $this->value;
78 }
79
85 public function getParameter($name)
86 {
87 if (array_key_exists($name, $this->parameters))
88 {
89 return $this->parameters[$name];
90 }
91
92 return null;
93 }
94
95 public function setParameter($name, $value)
96 {
97 $this->parameters[$name] = $value;
98 }
99
105 public function getDefinitionFragment()
106 {
107 if (is_null($this->definition_fragment))
108 {
109 if ($this->type == 2)
110 {
111 // skip uts entity
112 if ($this->value->getRefEntity()->isUts())
113 {
114 $this->definition_fragment = '';
115 }
116 else
117 {
118 $this->definition_fragment = $this->value->getName();
119 }
120 }
121 elseif ($this->type == 3)
122 {
123 // skip utm entity
124 if ($this->value[0]->isUtm())
125 {
126 $this->definition_fragment = '';
127 }
128 else
129 {
130 $this->definition_fragment = $this->value[0]->getFullName() . ':' . $this->value[1]->getName();
131 }
132 }
133 elseif ($this->type == 4)
134 {
135 $this->definition_fragment = '*';
136 }
137 else
138 {
139 if (!empty($this->parameters['uField']))
140 {
141 $this->definition_fragment = $this->parameters['uField']->getName();
142 }
143 else
144 {
145 $this->definition_fragment = $this->value->getName();
146 }
147 }
148 }
149
151 }
152
158 public function getAliasFragment()
159 {
160 if (is_null($this->alias_fragment))
161 {
162 if ($this->type == 2)
163 {
164 // skip uts entity
165 if ($this->value->getRefEntity()->isUts())
166 {
167 $this->alias_fragment = '';
168 }
169 else
170 {
171 $this->alias_fragment = $this->value->getName();
172 }
173 }
174 elseif ($this->type == 3)
175 {
176 // skip utm entity
177 if ($this->value[0]->isUtm())
178 {
179 $this->alias_fragment = '';
180 }
181 else
182 {
183 $this->alias_fragment = $this->value[0]->getCode() . '_' . $this->value[1]->getName();
184 }
185 }
186 elseif ($this->type == 4)
187 {
188 $this->alias_fragment = $this->value->getCode();
189 }
190 else
191 {
192 if (!empty($this->parameters['ufield']))
193 {
194 $this->alias_fragment = $this->parameters['ufield']->getName();
195 }
196 else
197 {
198 $this->alias_fragment = $this->value->getName();
199 }
200 }
201 }
202
204 }
205
211 public function getSqlDefinition()
212 {
213 if (is_array($this->value) || $this->value instanceof Reference || $this->value instanceof Entity)
214 {
215 throw new SystemException(sprintf(
216 'There is no SQL definition for Entity `%s`, please use a scalar field',
217 $this->getAliasFragment()
218 ));
219 }
220
221 $helper = $this->value->getEntity()->getConnection()->getSqlHelper();
222
223 if ($this->value instanceof ExpressionField)
224 {
225 $SQLBuildFrom = [];
226 $buildFromChains = $this->value->getBuildFromChains();
227
228 foreach ($this->value->getBuildFrom() as $element)
229 {
230 if ($element instanceof \Closure)
231 {
233 $sqlExpression = $element();
234
235 if (!($sqlExpression instanceof SqlExpression))
236 {
237 throw new ArgumentException(sprintf(
238 'Expected instance of %s, got %s instead.',
239 SqlExpression::class, gettype($sqlExpression)
240 ));
241 }
242
243 $SQLBuildFrom[] = $sqlExpression->compile();
244 }
245 else
246 {
247 $chain = array_shift($buildFromChains);
248 $SQLBuildFrom[] = $chain->getSQLDefinition();
249 }
250 }
251
252 $expr = $this->value->getExpression();
253
254 // insert talias
255 if (strpos($expr, '%%TABLE_ALIAS') !== false)
256 {
257 $expr = str_replace('%%TABLE_ALIAS', $helper->quote($this->getParameter('talias')), $expr);
258 }
259
260 // join
261 $sql = call_user_func_array('sprintf', array_merge([$expr], $SQLBuildFrom));
262 }
263 else
264 {
265 $sql = $helper->quote($this->getParameter('talias')) . '.';
266 $sql .= $helper->quote($this->value->getColumnName());
267 }
268
269 return $sql;
270 }
271
277 public function isBackReference()
278 {
279 if ($this->type === 3 || $this->value instanceof OneToMany || $this->value instanceof ManyToMany)
280 {
281 return true;
282 }
283
284 if ($this->value instanceof ExpressionField)
285 {
286 foreach ($this->value->getBuildFromChains() as $bfChain)
287 {
288 if ($bfChain->hasBackReference())
289 {
290 return true;
291 }
292 }
293 }
294
295 return false;
296 }
297
298 public function dump()
299 {
300 echo gettype($this->value).' ';
301
302 if ($this->value instanceof Field)
303 {
304 echo get_class($this->value).' '.$this->value->getName();
305 }
306 elseif ($this->value instanceof Entity)
307 {
308 echo get_class($this->value).' '.$this->value->getFullName();
309 }
310 elseif (is_array($this->value))
311 {
312 echo '('.get_class($this->value[0]).', '.get_class($this->value[1]).' '.$this->value[1]->getName().')';
313 }
314
315 echo ' '.json_encode($this->parameters);
316 }
317}
__construct($element, $parameters=array())