XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
QueryTag.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
11 class QueryTag
12 {
13 
18  var $action;
19 
24  var $query_id;
25 
30  var $priority;
31 
37 
42  var $query;
43 
48  var $columns;
49 
54  var $tables;
55 
60  var $subquery;
61 
67 
72  var $groups;
73 
79 
85 
90  var $preBuff;
91 
96  var $buff;
97 
103 
109 
114  var $alias;
115 
122  function __construct($query, $isSubQuery = FALSE)
123  {
124  $this->action = $query->attrs->action;
125  $this->query_id = $query->attrs->id;
126  $this->priority = $query->attrs->priority;
127  $this->query = $query;
128  $this->isSubQuery = $isSubQuery;
129  if($this->isSubQuery)
130  {
131  $this->action = 'select';
132  }
133  if($query->attrs->alias)
134  {
135  $dbParser = DB::getParser();
136  $this->alias = $dbParser->escape($query->attrs->alias);
137  }
138  $this->join_type = $query->attrs->join_type;
139 
140  $this->getColumns();
141  $tables = $this->getTables();
143  $this->getSubquery(); // Used for insert-select
144  $this->getConditions();
145  $this->getGroups();
146  $this->getNavigation();
147 
148  $this->getPrebuff();
149  $this->getBuff();
150  }
151 
152  function show()
153  {
154  return TRUE;
155  }
156 
157  function getQueryId()
158  {
159  return $this->query->attrs->query_id ? $this->query->attrs->query_id : $this->query->attrs->id;
160  }
161 
162  function getPriority()
163  {
164  return $this->query->attrs->priority;
165  }
166 
167  function getAction()
168  {
169  return $this->query->attrs->action;
170  }
171 
173  {
174  $query_id = $this->getQueryId();
175  if(!isset($this->column_type[$query_id]))
176  {
177  $table_tags = $tables->getTables();
178  $column_type = array();
179  foreach($table_tags as $table_tag)
180  {
181  if(is_a($table_tag, 'TableTag'))
182  {
183  $table_name = $table_tag->getTableName();
184  $table_alias = $table_tag->getTableAlias();
185  $tag_column_type = QueryParser::getTableInfo($query_id, $table_name);
186  $column_type[$table_alias] = $tag_column_type;
187  }
188  }
189  $this->column_type[$query_id] = $column_type;
190  }
191  }
192 
193  function getColumns()
194  {
195  if($this->action == 'select')
196  {
197  return $this->columns = new SelectColumnsTag($this->query->columns);
198  }
199  else if($this->action == 'insert' || $this->action == 'insert-select')
200  {
201  return $this->columns = new InsertColumnsTag($this->query->columns->column);
202  }
203  else if($this->action == 'update')
204  {
205  return $this->columns = new UpdateColumnsTag($this->query->columns->column);
206  }
207  else if($this->action == 'delete')
208  {
209  return $this->columns = null;
210  }
211  }
212 
213  function getPrebuff()
214  {
215  if($this->isSubQuery)
216  {
217  return;
218  }
219  // TODO Check if this work with arguments in join clause
220  $arguments = $this->getArguments();
221 
222  $prebuff = '';
223  foreach($arguments as $argument)
224  {
225  if(isset($argument))
226  {
227  $arg_name = $argument->getArgumentName();
228  if($arg_name)
229  {
230  unset($column_type);
231  $prebuff .= $argument->toString();
232 
233  $table_alias = $argument->getTableName();
234  if(isset($table_alias))
235  {
236  if(isset($this->column_type[$this->getQueryId()][$table_alias][$argument->getColumnName()]))
237  {
238  $column_type = $this->column_type[$this->getQueryId()][$table_alias][$argument->getColumnName()];
239  }
240  }
241  else
242  {
243  $current_tables = $this->column_type[$this->getQueryId()];
244  $column_name = $argument->getColumnName();
245  foreach($current_tables as $current_table)
246  {
247  if(isset($current_table[$column_name]))
248  {
249  $column_type = $current_table[$column_name];
250  }
251  }
252  }
253 
254  if(isset($column_type))
255  {
256  $prebuff .= sprintf('if(${\'%s_argument\'} !== null) ${\'%s_argument\'}->setColumnType(\'%s\');' . "\n"
257  , $arg_name
258  , $arg_name
259  , $column_type);
260  }
261  }
262  }
263  }
264  $prebuff .= "\n";
265 
266  return $this->preBuff = $prebuff;
267  }
268 
269  function getBuff()
270  {
271  $buff = '';
272  if($this->isSubQuery)
273  {
274  $buff = 'new Subquery(';
275  $buff .= "'" . $this->alias . '\', ';
276  $buff .= ($this->columns ? $this->columns->toString() : 'null' ) . ', ' . PHP_EOL;
277  $buff .= $this->tables->toString() . ',' . PHP_EOL;
278  $buff .= $this->conditions->toString() . ',' . PHP_EOL;
279  $buff .= $this->groups->toString() . ',' . PHP_EOL;
280  $buff .= $this->navigation->getOrderByString() . ',' . PHP_EOL;
281  $limit = $this->navigation->getLimitString();
282  $buff .= $limit ? $limit : 'null' . PHP_EOL;
283  $buff .= $this->join_type ? "'" . $this->join_type . "'" : '';
284  $buff .= ')';
285 
286  $this->buff = $buff;
287  return $this->buff;
288  }
289 
290  $buff .= '$query = new Query();' . PHP_EOL;
291  $buff .= sprintf('$query->setQueryId("%s");%s', $this->query_id, "\n");
292  $buff .= sprintf('$query->setAction("%s");%s', $this->action, "\n");
293  $buff .= sprintf('$query->setPriority("%s");%s', $this->priority, "\n");
294  $buff .= $this->preBuff;
295  if($this->columns)
296  {
297  $buff .= '$query->setColumns(' . $this->columns->toString() . ');' . PHP_EOL;
298  }
299 
300  $buff .= '$query->setTables(' . $this->tables->toString() . ');' . PHP_EOL;
301  if($this->action == 'insert-select')
302  {
303  $buff .= '$query->setSubquery(' . $this->subquery->toString() . ');' . PHP_EOL;
304  }
305  $buff .= '$query->setConditions(' . $this->conditions->toString() . ');' . PHP_EOL;
306  $buff .= '$query->setGroups(' . $this->groups->toString() . ');' . PHP_EOL;
307  $buff .= '$query->setOrder(' . $this->navigation->getOrderByString() . ');' . PHP_EOL;
308  $buff .= '$query->setLimit(' . $this->navigation->getLimitString() . ');' . PHP_EOL;
309 
310  $this->buff = $buff;
311  return $this->buff;
312  }
313 
314  function getTables()
315  {
316  if($this->query->index_hint && ($this->query->index_hint->attrs->for == 'ALL' || Context::getDBType() == strtolower($this->query->index_hint->attrs->for)))
317  {
318  return $this->tables = new TablesTag($this->query->tables, $this->query->index_hint);
319  }
320  else
321  {
322  return $this->tables = new TablesTag($this->query->tables);
323  }
324  }
325 
326  function getSubquery()
327  {
328  if($this->query->query)
329  {
330  $this->subquery = new QueryTag($this->query->query, true);
331  }
332  }
333 
334  function getConditions()
335  {
336  return $this->conditions = new ConditionsTag($this->query->conditions);
337  }
338 
339  function getGroups()
340  {
341  if($this->query->groups)
342  {
343  return $this->groups = new GroupsTag($this->query->groups->group);
344  }
345  else
346  {
347  return $this->groups = new GroupsTag(NULL);
348  }
349  }
350 
351  function getNavigation()
352  {
353  return $this->navigation = new NavigationTag($this->query->navigation);
354  }
355 
356  function toString()
357  {
358  return $this->buff;
359  }
360 
361  function getTableString()
362  {
363  return $this->buff;
364  }
365 
366  function getConditionString()
367  {
368  return $this->buff;
369  }
370 
371  function getExpressionString()
372  {
373  return $this->buff;
374  }
375 
376  function getArguments()
377  {
378  $arguments = array();
379  if($this->columns)
380  {
381  $arguments = array_merge($arguments, $this->columns->getArguments());
382  }
383  if($this->action == 'insert-select')
384  {
385  $arguments = array_merge($arguments, $this->subquery->getArguments());
386  }
387  $arguments = array_merge($arguments, $this->tables->getArguments());
388  $arguments = array_merge($arguments, $this->conditions->getArguments());
389  $arguments = array_merge($arguments, $this->navigation->getArguments());
390  return $arguments;
391  }
392 
393 }
394 /* End of file QueryTag.class.php */
395 /* Location: ./classes/xml/xmlquery/tags/navigation/QueryTag.class.php */
getTableInfo($query_id, $table_name)
getParser($force=FALSE)
Definition: DB.class.php:1345
setTableColumnTypes($tables)
__construct($query, $isSubQuery=FALSE)