1 | <?php |
||
25 | class DB extends AbstractAnonymizer |
||
26 | { |
||
27 | /** |
||
28 | * The PDO connection |
||
29 | * |
||
30 | * @var \PDO |
||
31 | */ |
||
32 | private $pdo; |
||
33 | |||
34 | /** |
||
35 | * Prepared statement is stored for the update, to make the queries faster |
||
36 | * |
||
37 | * @var \PDOStatement |
||
38 | */ |
||
39 | private $preparedStmt; |
||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * @param \PDO $pdo |
||
45 | */ |
||
46 | 16 | public function __construct(\PDO $pdo) |
|
51 | |||
52 | /** |
||
53 | * Process an entity by reading / writing to the DB |
||
54 | * |
||
55 | * @param string $table |
||
56 | * @param callable|null $callback |
||
57 | * @param bool $pretend |
||
58 | * @param bool $returnResult |
||
59 | * |
||
60 | * @return void|array |
||
61 | */ |
||
62 | 14 | public function processEntity( |
|
109 | |||
110 | /** |
||
111 | * Identify the primary key for a table |
||
112 | * |
||
113 | * @param string $table |
||
114 | * |
||
115 | * @return string Field's name |
||
116 | */ |
||
117 | 10 | protected function getPrimaryKey(string $table) |
|
133 | |||
134 | /** |
||
135 | * Execute the Update with PDO |
||
136 | * |
||
137 | * @param string $table Name of the table |
||
138 | * @param array $data Array of fields => value to update the table |
||
139 | * @param string $primaryKey |
||
140 | * @param string $val Primary Key's Value |
||
141 | */ |
||
142 | 5 | private function runUpdate(string $table, array $data, string $primaryKey, $val) |
|
143 | { |
||
144 | 5 | if (is_null($this->preparedStmt)) { |
|
145 | 5 | $this->prepareStmt($table, $primaryKey, array_keys($data)); |
|
146 | } |
||
147 | |||
148 | 5 | $values = [":$primaryKey" => $val]; |
|
149 | 5 | foreach ($data as $field => $value) { |
|
150 | 5 | $values[":$field"] = $value; |
|
151 | } |
||
152 | |||
153 | try { |
||
154 | 5 | $this->preparedStmt->execute($values); |
|
155 | 1 | } catch (\PDOException $e) { |
|
156 | 1 | $this->pdo->rollback(); |
|
157 | 1 | throw new NeuralizerException("Problem anonymizing $table (" . $e->getMessage() . ')'); |
|
158 | } |
||
159 | 4 | } |
|
160 | |||
161 | /** |
||
162 | * Prepare the statement if asked |
||
163 | * |
||
164 | * @param string $table |
||
165 | * @param string $primaryKey |
||
166 | * @param array $fieldNames |
||
167 | * |
||
168 | * @return \PDOStatement |
||
169 | */ |
||
170 | 5 | private function prepareStmt(string $table, string $primaryKey, array $fieldNames) |
|
179 | |||
180 | /** |
||
181 | * Execute the Delete with PDO |
||
182 | * |
||
183 | * @param string $table |
||
184 | * @param string $where |
||
185 | * @param bool $pretend |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 4 | private function runDelete(string $table, string $where, bool $pretend): string |
|
206 | |||
207 | /** |
||
208 | * Build the SQL just for debug |
||
209 | * |
||
210 | * @param string $table |
||
211 | * @param array $data |
||
212 | * @param string $where |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | 6 | private function buildUpdateSQL(string $table, array $data, string $where): string |
|
227 | } |
||
228 |