|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arrilot\DataAnonymization\Database; |
|
4
|
|
|
|
|
5
|
|
|
use PDO; |
|
6
|
|
|
|
|
7
|
|
|
class SqlDatabase implements DatabaseInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* PDO instance. |
|
11
|
|
|
* |
|
12
|
|
|
* @var PDO |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $pdo; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $dsn |
|
20
|
|
|
* @param string $user |
|
21
|
|
|
* @param string $password |
|
22
|
|
|
* @param null|array $options |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct($dsn, $user, $password, $options = null) |
|
25
|
|
|
{ |
|
26
|
|
|
if (is_null($options)) { |
|
27
|
|
|
$options = [ |
|
28
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
29
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$this->pdo = new PDO($dsn, $user, $password, $options); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get all rows from a $table specified by where. Only $columns are selected. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $table |
|
40
|
|
|
* @param string $columns |
|
41
|
|
|
* @param string|null $where |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getRows($table, $columns, $where) |
|
46
|
|
|
{ |
|
47
|
|
|
$columns = implode(',', $columns); |
|
48
|
|
|
$sql = "SELECT {$columns} FROM {$table}"; |
|
49
|
|
|
|
|
50
|
|
|
if ($where) { |
|
|
|
|
|
|
51
|
|
|
$sql .= " WHERE {$where}"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$stmt = $this->pdo->query($sql); |
|
55
|
|
|
while ($row = $stmt->fetch()) { |
|
56
|
|
|
yield $row; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Update $column value with a $newValue. |
|
62
|
|
|
* Update is performed on a row specified by $primaryValue of $table. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $table |
|
65
|
|
|
* @param array $primaryKeyValue |
|
66
|
|
|
* @param string $column |
|
67
|
|
|
* @param mixed $value |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function updateByPrimary($table, $primaryKeyValue, $column, $value) |
|
72
|
|
|
{ |
|
73
|
|
|
$where = $this->buildWhereForArray($primaryKeyValue); |
|
74
|
|
|
$quotedValue = $value === null ? 'null' : $this->pdo->quote($value); |
|
75
|
|
|
|
|
76
|
|
|
$sql = "UPDATE |
|
77
|
|
|
{$table} |
|
78
|
|
|
SET |
|
79
|
|
|
{$column} = {$quotedValue} |
|
80
|
|
|
WHERE |
|
81
|
|
|
{$where}"; |
|
82
|
|
|
|
|
83
|
|
|
$this->pdo->query($sql); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Build SQL where for key-value array. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $primaryKeyValue |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function buildWhereForArray($primaryKeyValue) |
|
94
|
|
|
{ |
|
95
|
|
|
$where = []; |
|
96
|
|
|
foreach ($primaryKeyValue as $key => $value) { |
|
97
|
|
|
$where[] = "{$key}='{$value}'"; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return implode(' AND ', $where); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: