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 DBALStorage extends AbstractStorage { |
17
|
|
|
|
18
|
|
|
protected $connection; |
19
|
|
|
|
20
|
|
|
protected $table; |
21
|
|
|
|
22
|
|
|
protected $statementKeyExists; |
23
|
|
|
|
24
|
|
|
protected $statementKeyInsert; |
25
|
|
|
|
26
|
|
|
protected $statementKeyUpdate; |
27
|
|
|
|
28
|
|
|
protected function buildStatements() { |
29
|
|
|
$queryBuilder = $this->connection->createQueryBuilder(); |
30
|
|
|
$queryBuilder |
31
|
|
|
->select('COUNT(`key`) AS amount') |
32
|
|
|
->from('`'.$this->table.'`') |
33
|
|
|
->where('`key` = ?') |
34
|
|
|
; |
35
|
|
|
$this->statementKeyExists = $this->connection->prepare($queryBuilder->getSQL()); |
36
|
|
|
|
37
|
|
|
$queryBuilder = $this->connection->createQueryBuilder(); |
38
|
|
|
$queryBuilder |
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
|
|
|
$queryBuilder |
47
|
|
|
->update('`'.$this->table.'`') |
48
|
|
|
->set('`value`', '?') |
49
|
|
|
->where('`key` = ?') |
50
|
|
|
; |
51
|
|
|
$this->statementKeyUpdate = $this->connection->prepare($queryBuilder->getSQL()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function __construct(Connection $connection, $table = 'phpprom') { |
55
|
|
|
$this->connection = $connection; |
56
|
|
|
$this->table = $table; |
57
|
|
|
$this->buildStatements(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function storeMeasurement($prefix, $key, $value) { |
61
|
|
|
$prefixedKey = $prefix.':'.$key; |
62
|
|
|
$this->statementKeyExists->bindValue(1, $prefixedKey); |
63
|
|
|
$this->statementKeyExists->execute(); |
64
|
|
|
$exists = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC); |
65
|
|
|
$statementStore = $exists['amount'] > 0 ? $this->statementKeyUpdate : $this->statementKeyInsert; |
66
|
|
|
$statementStore->bindValue(1, $value); |
67
|
|
|
$statementStore->bindValue(2, $prefixedKey); |
68
|
|
|
$statementStore->execute(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getMeasurements($prefix, array $keys) { |
72
|
|
|
$prefixedKeys = array_map(function($key) use ($prefix) { |
73
|
|
|
return $prefix.':'.$key; |
74
|
|
|
}, $keys); |
75
|
|
|
$queryBuilder = $this->connection->createQueryBuilder(); |
76
|
|
|
$queryBuilder |
77
|
|
|
->select('`key`', '`value`') |
78
|
|
|
->from('`'.$this->table.'`') |
79
|
|
|
->where('`key` IN (?)') |
80
|
|
|
->setParameter(1, $prefixedKeys, Connection::PARAM_STR_ARRAY) |
81
|
|
|
; |
82
|
|
|
$measurements = []; |
83
|
|
|
foreach ($keys as $key) { |
84
|
|
|
$measurements[$key] = 'Nan'; |
85
|
|
|
} |
86
|
|
|
$rows = $queryBuilder->execute()->fetchAll(\PDO::FETCH_ASSOC); |
87
|
|
|
foreach ($rows as $row) { |
88
|
|
|
$unprefixedKey = substr($row['key'], strlen($prefix) + 1); |
89
|
|
|
$measurements[$unprefixedKey] = (float)$row['value']; |
90
|
|
|
} |
91
|
|
|
return $measurements; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|