-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistory.php
213 lines (167 loc) · 5.15 KB
/
History.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/orm/blob/master/LICENSE
*/
namespace Quid\Orm;
use Quid\Base;
use Quid\Main;
// history
// class used to store the history of requests made to the database object
class History extends Main\Map
{
// config
protected static array $config = [];
// dynamique
protected ?array $mapAllow = ['push','empty']; // méthodes permises
protected ?string $syntax = null; // garde une copie de la classe de syntaxe à utiliser
protected $mapIs = 'array'; // les valeurs doivent passés ce test de validation ou exception
// invoke
// retourne un index de l'historique
final public function __invoke(...$args)
{
return $this->index(...$args);
}
// toString
// affiche le dump du tableau des requêtes uni
final public function __toString():string
{
return Base\Debug::varGet($this->keyValue());
}
// cast
// cast de l'historique, retourne le count
final public function _cast()
{
return $this->count();
}
// getSyntax
// retourne la classe de syntaxe
final public function getSyntax():?string
{
return $this->syntax;
}
// setSyntax
// enregistre la classe de syntaxe
final protected function setSyntax(Pdo $pdo):void
{
$this->syntax = $pdo->getSyntax();
}
// add
// ajoute un statement dans l'historique db
final public function add(array $value,\PDOStatement $statement,Pdo $pdo):self
{
if(empty($this->getSyntax()))
$this->setSyntax($pdo);
if(!empty($value['type']))
{
if(array_key_exists('cast',$value))
unset($value['cast']);
if($pdo->isOutput($value['type'],'rowCount'))
$value['row'] = $statement->rowCount();
if($pdo->isOutput($value['type'],'columnCount'))
{
$value['column'] = $statement->columnCount();
$value['cell'] = $value['row'] * $value['column'];
}
$this->push($value);
}
return $this;
}
// all
// retourne des donnés de l'historique
// possibilité de filtrer par type
final public function all(?string $type=null,bool $reverse=false):array
{
$return = [];
$data = $this->arr();
if(is_string($type))
{
foreach ($data as $value)
{
if(is_array($value) && !empty($value['type']) && $value['type'] === $type)
$return[] = $value;
}
}
else
$return = $data;
if($reverse === true)
$return = array_reverse($return,false);
return $return;
}
// keyValue
// retourne un tableau unidimensionnel d'historique
// emule la requête si nécessaire
final public function keyValue(?string $type=null,bool $reverse=false):array
{
$return = [];
$syntax = $this->getSyntax();
if(!empty($syntax))
{
foreach ($this->all($type,$reverse) as $value)
{
if(is_array($value) && array_key_exists('sql',$value))
{
$sql = $value['sql'];
if(!empty($value['prepare']))
$sql = $syntax::emulate($sql,$value['prepare']);
$return[] = $sql;
}
}
}
return $return;
}
// typeCount
// retourne les données counts de l'historique
// le type est requis
final public function typeCount(string $type):array
{
$return = [];
foreach ($this->all($type) as $value)
{
if(is_array($value) && !empty($value))
{
if(!array_key_exists('query',$return))
$return['query'] = 1;
else
$return['query']++;
foreach (['row','column','cell'] as $v)
{
if(array_key_exists($v,$value) && is_int($value[$v]))
{
if(!array_key_exists($v,$return))
$return[$v] = 0;
$return[$v] += $value[$v];
}
}
}
}
return $return;
}
// typeIndex
// retourne un index de l'historique filtre par type ou null si non existant
// par défaut index est le dernier, plus récent
final public function typeIndex(string $type,int $index=-1):?array
{
return Base\Arr::index($index,$this->all($type));
}
// total
// retourne les données counts de l'historique pour tous les types
final public function total():array
{
$return = [];
$syntax = $this->getSyntax();
if(!empty($syntax))
{
foreach ($syntax::getQueryTypes() as $type)
{
$array = $this->typeCount($type);
if(!empty($array))
$return[$type] = $array;
}
}
return $return;
}
}
?>