1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHPProm package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHPProm\Storage; |
13
|
|
|
|
14
|
|
|
use \Doctrine\DBAL\Connection; |
15
|
|
|
|
16
|
|
|
class DBAL implements StorageInterface { |
17
|
|
|
|
18
|
|
|
protected $connection; |
19
|
|
|
|
20
|
|
|
protected $table; |
21
|
|
|
|
22
|
|
|
protected $statementKeyExists; |
23
|
|
|
|
24
|
|
|
protected $statementKeyInsert; |
25
|
|
|
|
26
|
|
|
protected $statementKeyUpdate; |
27
|
|
|
|
28
|
|
|
protected $statementKeyIncrement; |
|
|
|
|
29
|
|
|
|
30
|
|
|
protected function buildStatements() { |
31
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
|
|
|
|
32
|
|
|
->select('COUNT(`key`) AS amount') |
33
|
|
|
->from('`'.$this->table.'`') |
34
|
|
|
->where('`key` = ?') |
35
|
|
|
; |
36
|
|
|
$this->statementKeyExists = $this->connection->prepare($queryBuilder->getSQL()); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
|
|
|
|
39
|
|
|
->insert('`'.$this->table.'`') |
40
|
|
|
->setValue('`value`', '?') |
41
|
|
|
->setValue('`key`', '?') |
42
|
|
|
; |
43
|
|
|
$this->statementKeyInsert = $this->connection->prepare($queryBuilder->getSQL()); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
|
|
|
|
46
|
|
|
->update('`'.$this->table.'`') |
47
|
|
|
->set('`value`', '?') |
48
|
|
|
->where('`key` = ?') |
49
|
|
|
; |
50
|
|
|
$this->statementKeyUpdate = $this->connection->prepare($queryBuilder->getSQL()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
53
|
|
|
->update('`'.$this->table.'`') |
54
|
|
|
->set('`value`', '`value` + 1') |
55
|
|
|
->where('`key` = ?') |
56
|
|
|
; |
57
|
|
|
$this->statementKeyIncrement = $this->connection->prepare($queryBuilder->getSQL()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function __construct(Connection $connection, $table = 'phpprom') { |
61
|
|
|
$this->connection = $connection; |
62
|
|
|
$this->table = $table; |
63
|
|
|
$this->buildStatements(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function storeMeasurement($prefix, $key, $value) { |
67
|
|
|
$prefixedKey = $prefix.':'.$key; |
68
|
|
|
$this->statementKeyExists->bindValue(1, $prefixedKey); |
69
|
|
|
$this->statementKeyExists->execute(); |
70
|
|
|
$exists = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC); |
71
|
|
|
$statementStore = $exists['amount'] > 0 ? $this->statementKeyUpdate : $this->statementKeyInsert; |
72
|
|
|
$statementStore->bindValue(1, $value); |
73
|
|
|
$statementStore->bindValue(2, $prefixedKey); |
74
|
|
|
$statementStore->execute(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function incrementMeasurement($prefix, $key) { |
78
|
|
|
$prefixedKey = $prefix.':'.$key; |
79
|
|
|
$this->statementKeyExists->bindValue(1, $prefixedKey); |
80
|
|
|
$this->statementKeyExists->execute(); |
81
|
|
|
$exists = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC); |
82
|
|
|
$statementIncrement = $exists['amount'] > 0 ? $this->statementKeyIncrement : $this->statementKeyInsert; |
83
|
|
|
if ($exists['amount'] > 0) { |
84
|
|
|
$statementIncrement->bindValue(1, $prefixedKey); |
85
|
|
|
} else { |
86
|
|
|
$statementIncrement->bindValue(1, 1); |
87
|
|
|
$statementIncrement->bindValue(2, $prefixedKey); |
88
|
|
|
} |
89
|
|
|
$statementIncrement->execute(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getMeasurements($prefix, array $keys, $defaultValue = 'Nan') { |
93
|
|
|
$prefixedKeys = array_map(function($key) use ($prefix) { |
94
|
|
|
return $prefix.':'.$key; |
95
|
|
|
}, $keys); |
96
|
|
|
$queryBuilder = $this->connection->createQueryBuilder(); |
97
|
|
|
$queryBuilder |
98
|
|
|
->select('`key`', '`value`') |
99
|
|
|
->from('`'.$this->table.'`') |
100
|
|
|
->where('`key` IN (?)') |
101
|
|
|
->setParameter(1, $prefixedKeys, Connection::PARAM_STR_ARRAY) |
102
|
|
|
; |
103
|
|
|
$measurements = []; |
104
|
|
|
foreach ($keys as $key) { |
105
|
|
|
$measurements[$key] = $defaultValue; |
106
|
|
|
} |
107
|
|
|
$rows = $queryBuilder->execute()->fetchAll(\PDO::FETCH_ASSOC); |
108
|
|
|
foreach ($rows as $row) { |
109
|
|
|
$unprefixedKey = substr($row['key'], strlen($prefix) + 1); |
110
|
|
|
$measurements[$unprefixedKey] = (float)$row['value']; |
111
|
|
|
} |
112
|
|
|
return $measurements; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.