TYPO3 CMS  TYPO3_7-6
AbstractCompiler.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
22 abstract class AbstractCompiler
23 {
28 
33  {
34  $this->databaseConnection = $databaseConnection;
35  }
36 
44  public function compileSQL($components)
45  {
46  $query = '';
47  switch ($components['type']) {
48  case 'SELECT':
49  $query = $this->compileSELECT($components);
50  break;
51  case 'UPDATE':
52  $query = $this->compileUPDATE($components);
53  break;
54  case 'INSERT':
55  $query = $this->compileINSERT($components);
56  break;
57  case 'DELETE':
58  $query = $this->compileDELETE($components);
59  break;
60  case 'EXPLAIN':
61  $query = 'EXPLAIN ' . $this->compileSELECT($components);
62  break;
63  case 'DROPTABLE':
64  $query = 'DROP TABLE' . ($components['ifExists'] ? ' IF EXISTS' : '') . ' ' . $components['TABLE'];
65  break;
66  case 'CREATETABLE':
67  $query = $this->compileCREATETABLE($components);
68  break;
69  case 'ALTERTABLE':
70  $query = $this->compileALTERTABLE($components);
71  break;
72  case 'TRUNCATETABLE':
73  $query = $this->compileTRUNCATETABLE($components);
74  break;
75  }
76  return $query;
77  }
78 
86  protected function compileSELECT($components)
87  {
88  // Initialize:
89  $where = $this->compileWhereClause($components['WHERE']);
90  $groupBy = $this->compileFieldList($components['GROUPBY']);
91  $orderBy = $this->compileFieldList($components['ORDERBY']);
92  $limit = $components['LIMIT'];
93  // Make query:
94  $query = 'SELECT ' . ($components['STRAIGHT_JOIN'] ?: '') . ' ' .
95  $this->compileFieldList($components['SELECT']) .
96  ' FROM ' . $this->compileFromTables($components['FROM']) . ($where !== '' ?
97  ' WHERE ' . $where : '') . ($groupBy !== '' ?
98  ' GROUP BY ' . $groupBy : '') . ($orderBy !== '' ?
99  ' ORDER BY ' . $orderBy : '') . ((string)$limit !== '' ?
100  ' LIMIT ' . $limit : '');
101  return $query;
102  }
103 
111  protected function compileUPDATE($components)
112  {
113  // Where clause:
114  $where = $this->compileWhereClause($components['WHERE']);
115  // Fields
116  $fields = [];
117  foreach ($components['FIELDS'] as $fN => $fV) {
118  $fields[] = $fN . '=' . $fV[1] . $this->compileAddslashes($fV[0]) . $fV[1];
119  }
120  // Make query:
121  $query = 'UPDATE ' . $components['TABLE'] . ' SET ' . implode(',', $fields) .
122  ($where !== '' ? ' WHERE ' . $where : '');
123 
124  return $query;
125  }
126 
134  abstract protected function compileINSERT($components);
135 
143  protected function compileDELETE($components)
144  {
145  // Where clause:
146  $where = $this->compileWhereClause($components['WHERE']);
147  // Make query:
148  $query = 'DELETE FROM ' . $components['TABLE'] . ($where !== '' ? ' WHERE ' . $where : '');
149 
150  return $query;
151  }
152 
160  abstract protected function compileCREATETABLE($components);
161 
169  abstract protected function compileALTERTABLE($components);
170 
178  protected function compileTRUNCATETABLE(array $components)
179  {
180  // Make query:
181  $query = 'TRUNCATE TABLE ' . $components['TABLE'];
182  // Return query
183  return $query;
184  }
185 
196  abstract public function compileFieldList($selectFields, $compileComments = true, $functionMapping = true);
197 
214  abstract public function compileWhereClause($clauseArray, $functionMapping = true);
215 
224  abstract protected function compileAddslashes($str);
225 
234  protected function compileJoinIdentifier($identifierParts)
235  {
236  if ($identifierParts['type'] === 'cast') {
237  return sprintf('CAST(%s AS %s)',
238  $identifierParts['table'] ? $identifierParts['table'] . '.' . $identifierParts['field'] : $identifierParts['field'],
239  $identifierParts['datatype'][0]
240  );
241  } else {
242  return $identifierParts['table'] ? $identifierParts['table'] . '.' . $identifierParts['field'] : $identifierParts['field'];
243  }
244  }
245 
253  public function compileFromTables($tablesArray)
254  {
255  // Prepare buffer variable:
256  $outputParts = [];
257  // Traverse the table names:
258  if (is_array($tablesArray)) {
259  foreach ($tablesArray as $k => $v) {
260  // Set table name:
261  $outputParts[$k] = $v['table'];
262  // Add alias AS if there:
263  if ($v['as']) {
264  $outputParts[$k] .= ' ' . $v['as_keyword'] . ' ' . $v['as'];
265  }
266  if (is_array($v['JOIN'])) {
267  foreach ($v['JOIN'] as $join) {
268  $outputParts[$k] .= ' ' . $join['type'] . ' ' . $join['withTable'];
269  // Add alias AS if there:
270  if (isset($join['as']) && $join['as']) {
271  $outputParts[$k] .= ' ' . $join['as_keyword'] . ' ' . $join['as'];
272  }
273  $outputParts[$k] .= ' ON ';
274  foreach ($join['ON'] as $condition) {
275  if ($condition['operator'] !== '') {
276  $outputParts[$k] .= ' ' . $condition['operator'] . ' ';
277  }
278  $outputParts[$k] .= $this->compileJoinIdentifier($condition['left']);
279  $outputParts[$k] .= $condition['comparator'];
280  if (!empty($condition['right']['value'])) {
281  $value = $condition['right']['value'];
282  $outputParts[$k] .= $value[1] . $this->compileAddslashes($value[0]) . $value[1];
283  } else {
284  $outputParts[$k] .= $this->compileJoinIdentifier($condition['right']);
285  }
286  }
287  }
288  }
289  }
290  }
291  // Return imploded buffer:
292  return implode(', ', $outputParts);
293  }
294 
303  protected function compileCaseStatement(array $components, $functionMapping = true)
304  {
305  $statement = 'CASE';
306  if (isset($components['case_field'])) {
307  $statement .= ' ' . $components['case_field'];
308  } elseif (isset($components['case_value'])) {
309  $statement .= ' ' . $components['case_value'][1] . $components['case_value'][0] . $components['case_value'][1];
310  }
311  foreach ($components['when'] as $when) {
312  $statement .= ' WHEN ';
313  $statement .= $this->compileWhereClause($when['when_value'], $functionMapping);
314  $statement .= ' THEN ';
315  $statement .= $when['then_value'][1] . $when['then_value'][0] . $when['then_value'][1];
316  }
317  if (isset($components['else'])) {
318  $statement .= ' ELSE ';
319  $statement .= $components['else'][1] . $components['else'][0] . $components['else'][1];
320  }
321  $statement .= ' END';
322  return $statement;
323  }
324 }
__construct(DatabaseConnection $databaseConnection)
compileFieldList($selectFields, $compileComments=true, $functionMapping=true)
compileCaseStatement(array $components, $functionMapping=true)
compileWhereClause($clauseArray, $functionMapping=true)