Typo3DbBackend.php 21.3 KB
Newer Older
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
*  All rights reserved
*
*  This class is a backport of the corresponding class of FLOW3.
*  All credits go to the v5 team.
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * A Storage backend
 *
 * @package Extbase
 * @subpackage Persistence
 * @version $Id: $
 */
Jochen Rau's avatar
Jochen Rau committed
35
class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persistence_Storage_BackendInterface, t3lib_Singleton {
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
36

Jochen Rau's avatar
Jochen Rau committed
37
38
39
	const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull';
	const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull';

Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
40
41
42
43
44
45
46
47
48
49
50
51
52
	/**
	 * The TYPO3 database object
	 *
	 * @var t3lib_db
	 */
	protected $databaseHandle;

	/**
	 * The TYPO3 page select object. Used for language and workspace overlay
	 *
	 * @var t3lib_pageSelect
	 */
	protected $pageSelectObject;
Jochen Rau's avatar
Jochen Rau committed
53
54
55
56
57
58
59
60

	/**
	 * TRUE if the framework should add the "enable fields" (e.g. checking for hidden or deleted records)
	 *
	 * @var boolean
	 */
	protected $useEnableFields = TRUE;

Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
61
62
63
64
65
66
	/**
	 * TRUE if automatic cache clearing in TCEMAIN should be done on insert/update/delete, FALSE otherwise.
	 *  
	 * @var boolean
	 */
	protected $automaticCacheClearing = FALSE;
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
67
68
69

	/**
	 * Constructs this Storage Backend instance
Jochen Rau's avatar
Jochen Rau committed
70
71
	 *
	 * @param t3lib_db $databaseHandle The database handle
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
72
	 */
Jochen Rau's avatar
Jochen Rau committed
73
74
	public function __construct($databaseHandle) {
		$this->databaseHandle = $databaseHandle;
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
75
	}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
76
77
78
79
80
81
82
83
84
85
86
87
	
	/**
	 * Set the automatic cache clearing flag.
	 * If TRUE, then inserted/updated/deleted records trigger a TCEMAIN cache clearing.
	 * 
	 * @param $automaticCacheClearing boolean if TRUE, enables automatic cache clearing
	 * @return void
	 * @internal
	 */
	public function setAutomaticCacheClearing($automaticCacheClearing) {
		$this->automaticCacheClearing = (boolean)$automaticCacheClearing;
	}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
88
89
90
91
92
93

	/**
	 * Adds a row to the storage
	 *
	 * @param string $tableName The database table name
	 * @param array $row The row to be inserted
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
94
	 * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
95
96
	 * @return int The uid of the inserted row
	 */
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
97
	public function addRow($tableName, array $row, $isRelation = FALSE) {
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
		$fields = array();
		$values = array();
		$parameters = array();
		unset($row['uid']); // TODO Check if the offset exists
		foreach ($row as $columnName => $value) {
			$fields[] = $columnName;
			$values[] = '?';
			$parameters[] = $value;
		}

		$sqlString = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
		$this->replacePlaceholders($sqlString, $parameters);

		$this->databaseHandle->sql_query($sqlString);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
112
		$uid = $this->databaseHandle->sql_insert_id();
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
113
114
115
		if (!$isRelation) {
			$this->clearPageCache($tableName, $uid);
		}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
116
		return $uid;
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
117
118
119
120
121
122
123
	}

	/**
	 * Updates a row in the storage
	 *
	 * @param string $tableName The database table name
	 * @param array $row The row to be updated
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
124
	 * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
125
126
	 * @return void
	 */
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
127
	public function updateRow($tableName, array $row, $isRelation = FALSE) {
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
		if (!isset($row['uid'])) throw new InvalidArgumentException('The given row must contain a value for "uid".');
		$uid = (int)$row['uid'];
		unset($row['uid']);
		$fields = array();
		$parameters = array();
		foreach ($row as $columnName => $value) {
			$fields[] = $columnName . '=?';
			$parameters[] = $value;
		}
		$parameters[] = $uid;

		$sqlString = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $fields) . ' WHERE uid=?';
		$this->replacePlaceholders($sqlString, $parameters);

Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
142
		$returnValue = $this->databaseHandle->sql_query($sqlString);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
143
144
145
		if (!$isRelation) {
			$this->clearPageCache($tableName, $uid);
		}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
146
		return $returnValue;
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
147
148
149
150
151
152
153
	}

	/**
	 * Deletes a row in the storage
	 *
	 * @param string $tableName The database table name
	 * @param array $uid The uid of the row to be deleted
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
154
	 * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
155
156
	 * @return void
	 */
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
157
	public function removeRow($tableName, $uid, $isRelation = FALSE) {
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
158
159
		$sqlString = 'DELETE FROM ' . $tableName . ' WHERE uid=?';
		$this->replacePlaceholders($sqlString, array((int)$uid));
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
160
161
162
		if (!$isRelation) {
			$this->clearPageCache($tableName, $uid, $isRelation);
		}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
163
164
		$returnValue = $this->databaseHandle->sql_query($sqlString);
		return $returnValue;
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
165
166
167
168
169
170
171
172
173
174
	}

	/**
	 * Returns an array with tuples matching the query.
	 *
	 * @param Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query
	 * @return array The matching tuples
	 */
	public function getRows(Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query) {
		$sql = array();
Jochen Rau's avatar
Jochen Rau committed
175
176
177
178
179
		$sql['tables'] = array();
		$sql['fields'] = array();
		$sql['where'] = array();
		$sql['enableFields'] = array();
		$sql['orderings'] = array();
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
180
181
182
		$parameters = array();
		$tuples = array();

Jochen Rau's avatar
Jochen Rau committed
183
		$this->parseSource($query, $sql, $parameters);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
184
		$sqlString = 'SELECT ' . implode(',', $sql['fields']) . ' FROM ' . implode(' ', $sql['tables']);
Jochen Rau's avatar
Jochen Rau committed
185
186

		$this->parseConstraint($query->getConstraint(), $sql, $parameters, $query->getBoundVariableValues());
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
187
		if (!empty($sql['where'])) {
Jochen Rau's avatar
Jochen Rau committed
188
189
190
191
192
			$sqlString .= ' WHERE ' . implode('', $sql['where']);
			if (!empty($sql['enableFields'])) {
				$sqlString .= ' AND ' . implode(' AND ', $sql['enableFields']);
			}
		} elseif (!empty($sql['enableFields'])) {
Jochen Rau's avatar
Jochen Rau committed
193
			$sqlString .= ' WHERE ' . implode(' AND ', $sql['enableFields']);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
194
		}
Jochen Rau's avatar
Jochen Rau committed
195
196

		$this->parseOrderings($query->getOrderings(), $sql, $parameters, $query->getBoundVariableValues());
Jochen Rau's avatar
Jochen Rau committed
197
198
199
		if (!empty($sql['orderings'])) {
			$sqlString .= ' ORDER BY ' . implode(', ', $sql['orderings']);
		}
Jochen Rau's avatar
Jochen Rau committed
200

Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
201
		$this->replacePlaceholders($sqlString, $parameters);
Jochen Rau's avatar
Jochen Rau committed
202

Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
203
204
		$result = $this->databaseHandle->sql_query($sqlString);
		if ($result) {
Jochen Rau's avatar
Jochen Rau committed
205
			// TODO Check for selector name
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
206
207
			$tuples = $this->getRowsFromResult($query->getSelectorName(), $result);
		}
Jochen Rau's avatar
Jochen Rau committed
208
		
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
		return $tuples;
	}

	/**
	 * Checks if a Value Object equal to the given Object exists in the data base
	 *
	 * @param array $properties The properties of the Value Object
	 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The Data Map
	 * @return array The matching tuples
	 */
	public function hasValueObject(array $properties, Tx_Extbase_Persistence_Mapper_DataMap $dataMap) {
		$fields = array();
		$parameters = array();
		foreach ($properties as $propertyName => $propertyValue) {
			if ($dataMap->isPersistableProperty($propertyName) && ($propertyName !== 'uid')) {
				$fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() . '=?';
				$parameters[] = $dataMap->convertPropertyValueToFieldValue($propertyValue);
			}
		}

Jochen Rau's avatar
Jochen Rau committed
229
		$sqlString = 'SELECT * FROM ' . $dataMap->getTableName() .  ' WHERE ' . implode('', $fields);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
		$this->replacePlaceholders($sqlString, $parameters);
		$res = $this->databaseHandle->sql_query($sqlString);
		$row = $this->databaseHandle->sql_fetch_assoc($res);
		if ($row !== FALSE) {
			return $row['uid'];
		} else {
			return FALSE;
		}
	}

	/**
	 * Transforms a Query Source into SQL and parameter arrays
	 *
	 * @param Tx_Extbase_Persistence_QOM_QueryObjectModel $query
	 * @param array &$sql
	 * @param array &$parameters
	 * @return void
	 */
	protected function parseSource(Tx_Extbase_Persistence_QOM_QueryObjectModel $query, array &$sql, array &$parameters) {
		$source = $query->getSource();
		if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
			$selectorName = $source->getSelectorName();
			$sql['fields'][] = $selectorName . '.*';
			$sql['tables'][] = $selectorName;
Jochen Rau's avatar
Jochen Rau committed
254
			// TODO Should we make the usage of enableFields configurable? And how? Because the Query object and even the QOM should be abstracted from the storage backend.
Jochen Rau's avatar
Jochen Rau committed
255
256
257
			if ($this->useEnableFields === TRUE) {
				$this->addEnableFieldsStatement($selectorName, $sql);
			}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
		} elseif ($source instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
			$this->parseJoin($source, $sql, $parameters);
		}
	}

	/**
	 * Transforms a Join into SQL and parameter arrays
	 *
	 * @param Tx_Extbase_Persistence_QOM_JoinInterface $join
	 * @param array &$sql
	 * @param array &$parameters
	 * @return void
	 */
	protected function parseJoin(Tx_Extbase_Persistence_QOM_JoinInterface $join, array &$sql, array &$parameters) {
		$leftSelectorName = $join->getLeft()->getSelectorName();
		$rightSelectorName = $join->getRight()->getSelectorName();

		$sql['fields'][] = $leftSelectorName . '.*';
		$sql['fields'][] = $rightSelectorName . '.*';

		// TODO Implement support for different join types and nested joins
Jochen Rau's avatar
Jochen Rau committed
279
		$sql['tables'][] = $leftSelectorName . ' LEFT JOIN ' . $rightSelectorName;
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
280
281
282
283
284
285
286

		$joinCondition = $join->getJoinCondition();
		// TODO Check the parsing of the join
		if ($joinCondition instanceof Tx_Extbase_Persistence_QOM_EquiJoinCondition) {
			$sql['tables'][] = 'ON ' . $joinCondition->getSelector1Name() . '.' . $joinCondition->getProperty1Name() . ' = ' . $joinCondition->getSelector2Name() . '.' . $joinCondition->getProperty2Name();
		}
		// TODO Implement childtableWhere
Jochen Rau's avatar
Jochen Rau committed
287
288
289

		$this->addEnableFieldsStatement($leftSelectorName, $sql);
		$this->addEnableFieldsStatement($rightSelectorName, $sql);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	}

	/**
	 * Transforms a constraint into SQL and parameter arrays
	 *
	 * @param Tx_Extbase_Persistence_QOM_ConstraintInterface $constraint
	 * @param array &$sql
	 * @param array &$parameters
	 * @param array $boundVariableValues
	 * @return void
	 */
	protected function parseConstraint(Tx_Extbase_Persistence_QOM_ConstraintInterface $constraint = NULL, array &$sql, array &$parameters, array $boundVariableValues) {
		if ($constraint instanceof Tx_Extbase_Persistence_QOM_AndInterface) {
			$sql['where'][] = '(';
			$this->parseConstraint($constraint->getConstraint1(), $sql, $parameters, $boundVariableValues);
			$sql['where'][] = ' AND ';
			$this->parseConstraint($constraint->getConstraint2(), $sql, $parameters, $boundVariableValues);
Jochen Rau's avatar
Jochen Rau committed
307
			$sql['where'][] = ')';
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
308
309
310
311
312
		} elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_OrInterface) {
			$sql['where'][] = '(';
			$this->parseConstraint($constraint->getConstraint1(), $sql, $parameters, $boundVariableValues);
			$sql['where'][] = ' OR ';
			$this->parseConstraint($constraint->getConstraint2(), $sql, $parameters, $boundVariableValues);
Jochen Rau's avatar
Jochen Rau committed
313
			$sql['where'][] = ')';
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
314
		} elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_NotInterface) {
Jochen Rau's avatar
Jochen Rau committed
315
			$sql['where'][] = 'NOT (';
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
316
			$this->parseConstraint($constraint->getConstraint(), $sql, $parameters, $boundVariableValues);
Jochen Rau's avatar
Jochen Rau committed
317
			$sql['where'][] = ')';
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
		} elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_ComparisonInterface) {
			$this->parseComparison($constraint, $sql, $parameters, $boundVariableValues);
		} elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_RelatedInterface) {
			$this->parseRelated($constraint, $sql, $parameters, $boundVariableValues);
		}
	}

	/**
	 * Parse a Comparison into SQL and parameter arrays.
	 *
	 * @param Tx_Extbase_Persistence_QOM_ComparisonInterface $comparison The comparison to parse
	 * @param array &$sql SQL query parts to add to
	 * @param array &$parameters Parameters to bind to the SQL
	 * @param array $boundVariableValues The bound variables in the query and their values
	 * @return void
	 */
	protected function parseComparison(Tx_Extbase_Persistence_QOM_ComparisonInterface $comparison, array &$sql, array &$parameters, array $boundVariableValues) {
Jochen Rau's avatar
Jochen Rau committed
335
336
337
338
339
340
341
342
343
344
345
346
		if (!($comparison->getOperand2() instanceof Tx_Extbase_Persistence_QOM_BindVariableValueInterface)) throw new Tx_Extbase_Persistence_Exception('Type of operand is not supported', 1247581135);

		$value = $boundVariableValues[$comparison->getOperand2()->getBindVariableName()];
		$operator = $comparison->getOperator();
		if ($value === NULL) {
			if ($operator === Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO) {
				$operator = self::OPERATOR_EQUAL_TO_NULL;
			} elseif ($operator === Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_NOT_EQUAL_TO) {
				$operator = self::OPERATOR_NOT_EQUAL_TO_NULL;
			} else {
				// TODO Throw exception
			}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
347
		}
Jochen Rau's avatar
Jochen Rau committed
348
349
350
		$parameters[] = $value;

		$this->parseDynamicOperand($comparison->getOperand1(), $operator, $sql, $parameters);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	}

	/**
	 * Parse a DynamicOperand into SQL and parameter arrays.
	 *
	 * @param Tx_Extbase_Persistence_QOM_DynamicOperandInterface $operand
	 * @param string $operator One of the JCR_OPERATOR_* constants
	 * @param array $boundVariableValues
	 * @param array &$parameters
	 * @param string $valueFunction an aoptional SQL function to apply to the operand value
	 * @return void
	 */
	protected function parseDynamicOperand(Tx_Extbase_Persistence_QOM_DynamicOperandInterface $operand, $operator, array &$sql, array &$parameters, $valueFunction = NULL) {
		if ($operand instanceof Tx_Extbase_Persistence_QOM_LowerCaseInterface) {
			$this->parseDynamicOperand($operand->getOperand(), $operator, $sql, $parameters, 'LOWER');
		} elseif ($operand instanceof Tx_Extbase_Persistence_QOM_UpperCaseInterface) {
			$this->parseDynamicOperand($operand->getOperand(), $operator, $sql, $parameters, 'UPPER');
		} elseif ($operand instanceof Tx_Extbase_Persistence_QOM_PropertyValueInterface) {
			$selectorName = $operand->getSelectorName();
			$operator = $this->resolveOperator($operator);

			if ($valueFunction === NULL) {
				$constraintSQL .= (!empty($selectorName) ? $selectorName . '.' : '') . $operand->getPropertyName() .  ' ' . $operator . ' ?';
			} else {
				$constraintSQL .= $valueFunction . '(' . (!empty($selectorName) ? $selectorName . '.' : '') . $operand->getPropertyName() .  ' ' . $operator . ' ?';
			}

			$sql['where'][] = $constraintSQL;
		}
	}

	/**
	 * Returns the SQL operator for the given JCR operator type.
	 *
	 * @param string $operator One of the JCR_OPERATOR_* constants
	 * @return string an SQL operator
	 */
	protected function resolveOperator($operator) {
		switch ($operator) {
Jochen Rau's avatar
Jochen Rau committed
390
391
392
393
394
395
			case self::OPERATOR_EQUAL_TO_NULL:
				$operator = 'IS';
				break;
			case self::OPERATOR_NOT_EQUAL_TO_NULL:
				$operator = 'IS NOT';
				break;
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
			case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO:
				$operator = '=';
				break;
			case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_NOT_EQUAL_TO:
				$operator = '!=';
				break;
			case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN:
				$operator = '<';
				break;
			case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO:
				$operator = '<=';
				break;
			case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN:
				$operator = '>';
				break;
			case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO:
				$operator = '>=';
				break;
			case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LIKE:
				$operator = 'LIKE';
				break;
			default:
				throw new Tx_Extbase_Persistence_Exception('Unsupported operator encountered.', 1242816073);
		}

		return $operator;
	}

	/**
	 * Replace query placeholders in a query part by the given
	 * parameters.
	 *
	 * @param string $queryPart The query part with placeholders
	 * @param array $parameters The parameters
	 * @return string The query part with replaced placeholders
	 */
	protected function replacePlaceholders(&$sqlString, array $parameters) {
Jochen Rau's avatar
Jochen Rau committed
433
		if (substr_count($sqlString, '?') !== count($parameters)) throw new Tx_Extbase_Persistence_Exception('The number of question marks to replace must be equal to the number of parameters.', 1242816074);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
434
435
436
		foreach ($parameters as $parameter) {
			$markPosition = strpos($sqlString, '?');
			if ($markPosition !== FALSE) {
Jochen Rau's avatar
Jochen Rau committed
437
438
439
440
441
442
443
				// TODO This is a bit hacky; improve the handling of $parameter === NULL
				if ($parameter === NULL) {
					$parameter = 'NULL';
				} else {
					$parameter = '"' . $parameter . '"';
				}
				$sqlString = substr($sqlString, 0, $markPosition) . $parameter . substr($sqlString, $markPosition + 1);
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
444
445
446
447
448
			}
		}
	}

	/**
Jochen Rau's avatar
Jochen Rau committed
449
450
	 * Builds the enable fields statement
	 *
Jochen Rau's avatar
Jochen Rau committed
451
452
453
454
455
456
	 * @param string $selectorName The selector name (= database table name)
	 * @param array &$sql The query parts
	 * @return void
	 */
	protected function addEnableFieldsStatement($selectorName, array &$sql) {
		// TODO We have to call the appropriate API method if we are in TYPO3BE mode
Jochen Rau's avatar
Jochen Rau committed
457
458
459
460
461
		if (is_array($GLOBALS['TCA'][$selectorName]['ctrl'])) {
			$statement = substr($GLOBALS['TSFE']->sys_page->enableFields($selectorName), 4);
			if(!empty($statement)) {
				$sql['enableFields'][] = $statement;
			}
Jochen Rau's avatar
Jochen Rau committed
462
463
464
465
466
		}
	}

	/**
	 * Transforms orderings into SQL
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
467
	 *
Jochen Rau's avatar
Jochen Rau committed
468
469
470
471
472
	 * @param array $orderings
	 * @param array &$sql
	 * @param array &$parameters
	 * @param array $boundVariableValues
	 * @return void
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
473
	 */
Jochen Rau's avatar
Jochen Rau committed
474
475
476
477
478
479
	protected function parseOrderings(array $orderings = NULL, array &$sql, array &$parameters, array $boundVariableValues) {
		if (is_array($orderings)) {
			foreach ($orderings as $propertyName => $ordering) {
				// TODO Implement
			}
		}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
	}

	/**
	 * Transforms a Resource from a database query to an array of rows. Performs the language and
	 * workspace overlay before.
	 *
	 * @return array The result as an array of rows (tuples)
	 */
	protected function getRowsFromResult($tableName, $res) {
		$rows = array();
		while ($row = $this->databaseHandle->sql_fetch_assoc($res)) {
			$row = $this->doLanguageAndWorkspaceOverlay($tableName, $row);
			if (is_array($row)) {
				// TODO Check if this is necessary, maybe the last line is enough
				$arrayKeys = range(0,count($row));
				array_fill_keys($arrayKeys, $row);
				$rows[] = $row;
			}
		}
		return $rows;
	}

	/**
	 * Performs workspace and language overlay on the given row array. The language and workspace id is automatically
	 * detected (depending on FE or BE context). You can also explicitly set the language/workspace id.
	 *
	 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap
	 * @param array $row The row array (as reference)
	 * @param string $languageUid The language id
	 * @param string $workspaceUidUid The workspace id
	 * @return void
	 */
	protected function doLanguageAndWorkspaceOverlay($tableName, array $row, $languageUid = NULL, $workspaceUid = NULL) {
		if (!($this->pageSelectObject instanceof t3lib_pageSelect)) {
			if (TYPO3_MODE == 'FE') {
				if (is_object($GLOBALS['TSFE'])) {
					$this->pageSelectObject = $GLOBALS['TSFE']->sys_page;
					if ($languageUid === NULL) {
						$languageUid = $GLOBALS['TSFE']->sys_language_content;
					}
				} else {
					require_once(PATH_t3lib . 'class.t3lib_page.php');
					$this->pageSelectObject = t3lib_div::makeInstance('t3lib_pageSelect');
					if ($languageUid === NULL) {
						$languageUid = intval(t3lib_div::_GP('L'));
					}
				}
				if ($workspaceUid !== NULL) {
					$this->pageSelectObject->versioningWorkspaceId = $workspaceUid;
				}
			} else {
				require_once(PATH_t3lib . 'class.t3lib_page.php');
				$this->pageSelectObject = t3lib_div::makeInstance( 't3lib_pageSelect' );
				//$this->pageSelectObject->versioningPreview =  TRUE;
				if ($workspaceUid === NULL) {
					$workspaceUid = $GLOBALS['BE_USER']->workspace;
				}
				$this->pageSelectObject->versioningWorkspaceId = $workspaceUid;
			}
		}

		$this->pageSelectObject->versionOL($tableName, $row, TRUE);
		$row = $this->pageSelectObject->getRecordOverlay($tableName, $row, $languageUid, ''); //'hideNonTranslated'
		// TODO Skip if empty languageoverlay (languagevisibility)
		return $row;
	}

Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
	/**
	 * Clear the TYPO3 page cache for the given record.
	 * Much of this functionality is taken from t3lib_tcemain::clear_cache() which unfortunately only works with logged-in BE user.
	 * 
	 * @param $tableName Table name of the record
	 * @param $uid UID of the record
	 * @return void
	 */
	protected function clearPageCache($tableName, $uid) {
		if ($this->automaticCacheClearing !== TRUE) return;

		$pageCache = $GLOBALS['typo3CacheManager']->getCache('cache_pages');
		$pageSectionCache = $GLOBALS['typo3CacheManager']->getCache('cache_pagesection');
	
		$result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('pid', $tableName, 'uid='.intval($uid));
	
		$pageIdsToClear = array();
		if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result))	{
			$storagePage = $row['pid'];
			$pageIdsToClear[] = $storagePage;
		}
		if (!$storagePage) {
			return;
		}
		
		$pageTSConfig = t3lib_BEfunc::getPagesTSconfig($storagePage);
		
		if ($pageTSConfig['clearCacheCmd'])	{
			$clearCacheCommands = t3lib_div::trimExplode(',',strtolower($TSConfig['clearCacheCmd']),1);
				$clearCacheCommands = array_unique($clearCacheCommands);
				foreach ($clearCacheCommands as $clearCacheCommand)	{
					if (t3lib_div::testInt($clearCacheCommand))	{
						$pageIdsToClear[] = $clearCacheCommand;
					}
				}
			}
		
		foreach ($pageIdsToClear as $pageIdToClear) {
			$pageCache->flushByTag('pageId_' . $pageIdToClear);
			$pageSectionCache->flushByTag('pageId_' . $pageIdToClear);
		}
	}
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
589
590
}

Jochen Rau's avatar
Jochen Rau committed
591
?>