1
|
|
|
<?php |
2
|
|
|
namespace Aoe\AoeDbSequenzer\Xclass; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 AOE GmbH ([email protected]) |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use Aoe\AoeDbSequenzer\Sequenzer; |
28
|
|
|
use Aoe\AoeDbSequenzer\Service\Typo3Service; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @package Aoe\AoeDbSequenzer\Xclass |
32
|
|
|
*/ |
33
|
|
|
class DatabaseConnection extends \TYPO3\CMS\Core\Database\DatabaseConnection |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var boolean |
37
|
|
|
*/ |
38
|
|
|
private $isEnabled = true; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Typo3Service |
42
|
|
|
*/ |
43
|
|
|
private $typo3Service; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Enables the sequencer. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function enable() |
51
|
|
|
{ |
52
|
|
|
$this->isEnabled = true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Disables the sequencer. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function disable() |
61
|
|
|
{ |
62
|
|
|
$this->isEnabled = false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates and executes an INSERT SQL-statement for $table from the array with field/value pairs $fields_values. |
67
|
|
|
* Using this function specifically allows us to handle BLOB and CLOB fields depending on DB |
68
|
|
|
* |
69
|
|
|
* @param string $table Table name |
70
|
|
|
* @param array $fields_values Field values as key=>value pairs. Values will be escaped internally. Typically you would fill an array like "$insertFields" with 'fieldname'=>'value' and pass it to this function as argument. |
71
|
|
|
* @param string /array $no_quote_fields See fullQuoteArray() |
72
|
|
|
* @return string|NULL Full SQL query for INSERT, NULL if $fields_values is empty |
73
|
|
|
*/ |
74
|
|
|
function INSERTquery($table, $fields_values, $no_quote_fields = false) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if ($this->isEnabled) { |
77
|
|
|
$fields_values = $this->getTypo3Service()->modifyInsertFields($table, $fields_values); |
78
|
|
|
} |
79
|
|
|
return parent::INSERTquery($table, $fields_values, $no_quote_fields); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates an INSERT SQL-statement for $table with multiple rows. |
84
|
|
|
* |
85
|
|
|
* @param string $table Table name |
86
|
|
|
* @param array $fields Field names |
87
|
|
|
* @param array $rows Table rows. Each row should be an array with field values mapping to $fields |
88
|
|
|
* @param bool|array|string $no_quote_fields See fullQuoteArray() |
89
|
|
|
* @return string Full SQL query for INSERT (unless $rows does not contain any elements in which case it will be false) |
90
|
|
|
*/ |
91
|
|
|
public function INSERTmultipleRows($table, array $fields, array $rows, $no_quote_fields = false) |
92
|
|
|
{ |
93
|
|
|
if ($this->isEnabled) { |
94
|
|
|
foreach ($rows as &$row) { |
95
|
|
|
$row = $this->getTypo3Service()->modifyInsertFields($table, $row); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return parent::INSERTmultipleRows($table, $fields, $rows, $no_quote_fields); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Creates an UPDATE SQL-statement for $table where $where-clause (typ. 'uid=...') from the array with field/value pairs $fields_values. |
103
|
|
|
* |
104
|
|
|
* @param string $table See exec_UPDATEquery() |
105
|
|
|
* @param string $where See exec_UPDATEquery() |
106
|
|
|
* @param array $fields_values See exec_UPDATEquery() |
107
|
|
|
* @param boolean $no_quote_fields See fullQuoteArray() |
108
|
|
|
* @return string Full SQL query for UPDATE (unless $fields_values does not contain any elements in which case it will be false) |
109
|
|
|
*/ |
110
|
|
|
public function UPDATEquery($table, $where, $fields_values, $no_quote_fields = false) |
111
|
|
|
{ |
112
|
|
|
if ($this->getTypo3Service()->needsSequenzer($table) && isset($fields_values['uid'])) { |
113
|
|
|
throw new \InvalidArgumentException('no uid allowed in update statement!', 1512378277); |
114
|
|
|
} |
115
|
|
|
return parent::UPDATEquery($table, $where, $fields_values, $no_quote_fields); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Creates and executes a DELETE SQL-statement for $table where $where-clause |
120
|
|
|
* |
121
|
|
|
* @param string $table Database tablename |
122
|
|
|
* @param string $where WHERE clause, eg. "uid=1". NOTICE: You must escape values in this argument with $this->fullQuoteStr() yourself! |
123
|
|
|
* @return boolean|\mysqli_result|object MySQLi result object / DBAL object |
124
|
|
|
*/ |
125
|
|
|
public function exec_DELETEquery($table, $where) |
126
|
|
|
{ |
127
|
|
|
//TODO: log deletes |
128
|
|
|
return parent::exec_DELETEquery($table, $where); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Open a (persistent) connection to a MySQL server |
133
|
|
|
* |
134
|
|
|
* @param string $host Deprecated since 6.1, will be removed in two versions. Database host IP/domain[:port] |
135
|
|
|
* @param string $username Deprecated since 6.1, will be removed in two versions. Username to connect with. |
136
|
|
|
* @param string $password Deprecated since 6.1, will be removed in two versions. Password to connect with. |
137
|
|
|
* @return \mysqli|NULL Returns current database handle |
138
|
|
|
*/ |
139
|
|
|
function sql_pconnect($host = null, $username = null, $password = null) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
parent::sql_pconnect(); |
142
|
|
|
$this->getTypo3Service()->setDbLink($this->getDatabaseHandle()); |
143
|
|
|
return $this->getDatabaseHandle(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* create instance of Typo3Service by lazy-loading |
148
|
|
|
* |
149
|
|
|
* Why we do this? |
150
|
|
|
* Because some unittests backup the variable $GLOBALS (and so, also the variable $GLOBALS['TYPO3_DB']), which means, that this |
151
|
|
|
* object/class will be serialized/unserialized, so the instance of Typo3Service will be null after unserialization! |
152
|
|
|
* |
153
|
|
|
* @return Typo3Service |
154
|
|
|
*/ |
155
|
|
|
protected function getTypo3Service() |
156
|
|
|
{ |
157
|
|
|
if (false === isset($this->typo3Service)) { |
158
|
|
|
$this->typo3Service = new Typo3Service(new Sequenzer()); |
159
|
|
|
} |
160
|
|
|
return $this->typo3Service; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.