1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Aoe\AoeDbSequenzer\Xclass; |
5
|
|
|
|
6
|
|
|
/* |
7
|
|
|
* This file xclasses and extends a part of the TYPO3 CMS project. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use Aoe\AoeDbSequenzer\Sequenzer; |
11
|
|
|
use Aoe\AoeDbSequenzer\Service\Typo3Service; |
12
|
|
|
use TYPO3\CMS\Core\Database\Query\QueryBuilder as CoreQueryBuilder; |
13
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @package Aoe\AoeDbSequenzer\Xclass |
17
|
|
|
*/ |
18
|
|
|
class QueryBuilder extends CoreQueryBuilder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Typo3Service |
22
|
|
|
*/ |
23
|
|
|
private $typo3Service; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sets a new value for a column in a bulk update query. |
27
|
|
|
* |
28
|
|
|
* @param string $key The column to set. |
29
|
|
|
* @param string $value The value, expression, placeholder, etc. |
30
|
|
|
* @param bool $createNamedParameter Automatically create a named parameter for the value |
31
|
|
|
* |
32
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
33
|
|
|
*/ |
34
|
|
|
public function set(string $key, $value, bool $createNamedParameter = true): CoreQueryBuilder |
35
|
|
|
{ |
36
|
|
|
if ('uid' === $key && $this->shouldTableBeSequenced()) { |
37
|
|
|
throw new \InvalidArgumentException('no uid allowed in update statement!', 1564122229); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
parent::set($key, $value, $createNamedParameter); |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Specifies values for an insert query indexed by column names. |
47
|
|
|
* Replaces any previous values, if any. |
48
|
|
|
* |
49
|
|
|
* @param array $values The values to specify for the insert query indexed by column names. |
50
|
|
|
* @param bool $createNamedParameters Automatically create named parameters for all values |
51
|
|
|
* |
52
|
|
|
* @return QueryBuilder This QueryBuilder instance. |
53
|
|
|
*/ |
54
|
|
|
public function values(array $values, bool $createNamedParameters = true): CoreQueryBuilder |
55
|
|
|
{ |
56
|
|
|
parent::values( |
57
|
|
|
$this->getTypo3Service()->modifyInsertFields($this->sanitizeTableName($this->concreteQueryBuilder->getQueryPart('from')['table']), $values), |
58
|
|
|
$createNamedParameters |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* create instance of Typo3Service by lazy-loading |
66
|
|
|
* |
67
|
|
|
* Why we do this? |
68
|
|
|
* Because some unittests backup the variable $GLOBALS (and so, also the variable $GLOBALS['TYPO3_DB']), which means, that this |
69
|
|
|
* object/class will be serialized/unserialized, so the instance of Typo3Service will be null after unserialization! |
70
|
|
|
* |
71
|
|
|
* @return Typo3Service |
72
|
|
|
*/ |
73
|
|
|
protected function getTypo3Service() |
74
|
|
|
{ |
75
|
|
|
if (false === isset($this->typo3Service)) { |
76
|
|
|
$this->typo3Service = GeneralUtility::makeInstance(Typo3Service::class, new Sequenzer()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->typo3Service; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Determines the defined table name without quotation marks (`). |
84
|
|
|
* |
85
|
|
|
* @param string $tableName |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
3 |
|
protected function sanitizeTableName(string $tableName): string |
89
|
|
|
{ |
90
|
3 |
|
$mark = '`'; |
91
|
3 |
|
if (!empty($tableName) && $tableName[0] === $mark && $tableName[strlen($tableName) - 1] === $mark) { |
92
|
1 |
|
return str_replace($mark, '', $tableName); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
return $tableName; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
protected function shouldTableBeSequenced(): bool |
102
|
|
|
{ |
103
|
|
|
return $this->getTypo3Service()->needsSequenzer($this->sanitizeTableName($this->concreteQueryBuilder->getQueryPart('from')['table'])); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|