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 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 $statementKeyIncr; |
29
|
|
|
|
30
|
|
|
protected function buildStatements() { |
31
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
32
|
|
|
->select('COUNT(`key`) AS amount')->from('`'.$this->table.'`')->where('`key` = ?'); |
33
|
|
|
$this->statementKeyExists = $this->connection->prepare($queryBuilder->getSQL()); |
34
|
|
|
|
35
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
36
|
|
|
->insert('`'.$this->table.'`')->setValue('`value`', '?')->setValue('`key`', '?'); |
37
|
|
|
$this->statementKeyInsert = $this->connection->prepare($queryBuilder->getSQL()); |
38
|
|
|
|
39
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
40
|
|
|
->update('`'.$this->table.'`')->set('`value`', '?')->where('`key` = ?'); |
41
|
|
|
$this->statementKeyUpdate = $this->connection->prepare($queryBuilder->getSQL()); |
42
|
|
|
|
43
|
|
|
$queryBuilder = $this->connection->createQueryBuilder() |
44
|
|
|
->update('`'.$this->table.'`')->set('`value`', '`value` + 1')->where('`key` = ?'); |
45
|
|
|
$this->statementKeyIncr = $this->connection->prepare($queryBuilder->getSQL()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function __construct(Connection $connection, $table = 'phpprom') { |
49
|
|
|
$this->connection = $connection; |
50
|
|
|
$this->table = $table; |
51
|
|
|
$this->buildStatements(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function storeMeasurement($prefix, $key, $value) { |
55
|
|
|
$prefixedKey = $prefix.':'.$key; |
56
|
|
|
$this->statementKeyExists->bindValue(1, $prefixedKey); |
57
|
|
|
$this->statementKeyExists->execute(); |
58
|
|
|
$exists = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC); |
59
|
|
|
$statementStore = $exists['amount'] > 0 ? $this->statementKeyUpdate : $this->statementKeyInsert; |
60
|
|
|
$statementStore->bindValue(1, $value); |
61
|
|
|
$statementStore->bindValue(2, $prefixedKey); |
62
|
|
|
$statementStore->execute(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function incrementMeasurement($prefix, $key) { |
66
|
|
|
$prefixedKey = $prefix.':'.$key; |
67
|
|
|
$this->statementKeyExists->bindValue(1, $prefixedKey); |
68
|
|
|
$this->statementKeyExists->execute(); |
69
|
|
|
$exists = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC); |
70
|
|
|
$statementIncrement = $exists['amount'] > 0 ? $this->statementKeyIncr : $this->statementKeyInsert; |
71
|
|
|
if ($exists['amount'] > 0) { |
72
|
|
|
$statementIncrement->bindValue(1, $prefixedKey); |
73
|
|
|
} else { |
74
|
|
|
$statementIncrement->bindValue(1, 1); |
75
|
|
|
$statementIncrement->bindValue(2, $prefixedKey); |
76
|
|
|
} |
77
|
|
|
$statementIncrement->execute(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getMeasurements($prefix, array $keys, $defaultValue = 'Nan') { |
81
|
|
|
$prefixedKeys = array_map(function($key) use ($prefix) { |
82
|
|
|
return $prefix.':'.$key; |
83
|
|
|
}, $keys); |
84
|
|
|
$queryBuilder = $this->connection->createQueryBuilder(); |
85
|
|
|
$queryBuilder |
86
|
|
|
->select('`key`', '`value`') |
87
|
|
|
->from('`'.$this->table.'`') |
88
|
|
|
->where('`key` IN (?)') |
89
|
|
|
->setParameter(1, $prefixedKeys, Connection::PARAM_STR_ARRAY) |
90
|
|
|
; |
91
|
|
|
$measurements = []; |
92
|
|
|
foreach ($keys as $key) { |
93
|
|
|
$measurements[$key] = $defaultValue; |
94
|
|
|
} |
95
|
|
|
$rows = $queryBuilder->execute()->fetchAll(\PDO::FETCH_ASSOC); |
96
|
|
|
foreach ($rows as $row) { |
97
|
|
|
$unprefixedKey = substr($row['key'], strlen($prefix) + 1); |
98
|
|
|
$measurements[$unprefixedKey] = (float)$row['value']; |
99
|
|
|
} |
100
|
|
|
return $measurements; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|