1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* KumbiaPHP web & app Framework. |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file LICENSE.txt. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* http://wiki.kumbiaphp.com/Licencia |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @category Kumbia |
17
|
|
|
* |
18
|
|
|
* @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) |
19
|
|
|
* @license http://wiki.kumbiaphp.com/Licencia New BSD License |
20
|
|
|
*/ |
21
|
|
|
namespace Kumbia\ActiveRecord\Metadata; |
22
|
|
|
|
23
|
|
|
use Kumbia\ActiveRecord\Db; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Metadata de tabla. |
27
|
|
|
*/ |
28
|
|
|
abstract class Metadata |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Singleton de metadata. |
32
|
|
|
* |
33
|
|
|
* @var self[] |
34
|
|
|
*/ |
35
|
|
|
private static $instances = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Descripción de los campos. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $fields = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Lista de campos. |
46
|
|
|
* |
47
|
|
|
* @var string[] |
48
|
|
|
*/ |
49
|
|
|
protected $fieldsList = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Clave primaria. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $pk; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Campos con valor predeterminado. |
60
|
|
|
* |
61
|
|
|
* @var string[] |
62
|
|
|
*/ |
63
|
|
|
protected $withDefault = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Campos con valor autogenerado. |
67
|
|
|
* |
68
|
|
|
* @var string[] |
69
|
|
|
*/ |
70
|
|
|
protected $autoFields = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Metadata de la tabla. |
74
|
|
|
* |
75
|
|
|
* @param string $database |
76
|
|
|
* @param string $table |
77
|
|
|
* @param string $schema |
78
|
|
|
* |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
|
|
public static function get(string $database, string $table, string $schema = ''): self |
82
|
|
|
{ |
83
|
|
|
return self::$instances["$database.$table.$schema"] ?? self::getMetadata($database, $table, $schema); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Obtiene la metadata de la tabla |
88
|
|
|
* Y la cachea si esta en producción. |
89
|
|
|
* |
90
|
|
|
* @param string $database |
91
|
|
|
* @param string $table |
92
|
|
|
* @param string $schema |
93
|
|
|
* |
94
|
|
|
* @return self |
95
|
|
|
*/ |
96
|
|
|
private static function getMetadata(string $database, string $table, string $schema): self |
97
|
|
|
{ |
98
|
|
|
$key = "$database.$table.$schema"; |
99
|
|
|
//TODO añadir cache propia |
100
|
|
|
if (\PRODUCTION && ! (self::$instances[$key] = \Cache::driver()->get($key, 'ActiveRecord.Metadata'))) { |
101
|
|
|
return self::$instances[$key]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$pdo = Db::get($database); |
105
|
|
|
|
106
|
|
|
$driverClass = __NAMESPACE__."\\".\ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; |
107
|
|
|
|
108
|
|
|
self::$instances[$key] = new $driverClass($pdo, $table, $schema); |
109
|
|
|
|
110
|
|
|
// Cachea los metadatos |
111
|
|
|
if (\PRODUCTION) { |
112
|
|
|
\Cache::driver()->save( |
113
|
|
|
self::$instances[$key], |
114
|
|
|
\Config::get('config.application.metadata_lifetime'), |
115
|
|
|
$key, |
116
|
|
|
'ActiveRecord.Metadata' |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return self::$instances[$key]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Constructor. |
125
|
|
|
* |
126
|
|
|
* @param \PDO $pdo base de datos |
127
|
|
|
* @param string $table tabla |
128
|
|
|
* @param string $schema squema |
129
|
|
|
*/ |
130
|
|
|
private function __construct(\PDO $pdo, string $table, string $schema = '') |
131
|
|
|
{ |
132
|
|
|
$this->fields = $this->queryFields($pdo, $table, $schema); |
133
|
|
|
$this->fieldsList = \array_keys($this->fields); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Permite el filtrado de columna en PK, por Defecto y Autogenerado. |
138
|
|
|
* |
139
|
|
|
* @param array $meta información de la columna |
140
|
|
|
* @param string $field nombre de la columna |
141
|
|
|
*/ |
142
|
|
|
protected function filterColumn(array $meta, string $field): void |
143
|
|
|
{ |
144
|
|
|
if ($meta['Key'] === 'PRI') { |
145
|
|
|
$this->pk = $field; |
146
|
|
|
} |
147
|
|
|
if ($meta['Default']) { |
148
|
|
|
$this->withDefault[] = $field; |
149
|
|
|
} |
150
|
|
|
if ($meta['Auto']) { |
151
|
|
|
$this->autoFields[] = $field; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Consultar los campos de la tabla en la base de datos. |
157
|
|
|
* |
158
|
|
|
* @param \PDO $pdo base de datos |
159
|
|
|
* @param string $table tabla |
160
|
|
|
* @param string $schema squema |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
abstract protected function queryFields(\PDO $pdo, string $table, string $schema = ''): array; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Obtiene la descripción de los campos. |
168
|
|
|
* |
169
|
|
|
* @return string[] |
170
|
|
|
*/ |
171
|
|
|
public function getFields(): array |
172
|
|
|
{ |
173
|
|
|
return $this->fields; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Obtiene la lista de campos. |
178
|
|
|
* |
179
|
|
|
* @return string[] |
180
|
|
|
*/ |
181
|
|
|
public function getFieldsList(): array |
182
|
|
|
{ |
183
|
|
|
return $this->fieldsList; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Obtiene la clave primaria. |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function getPK(): string |
192
|
|
|
{ |
193
|
|
|
return $this->pk; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Obtiene los campos con valor predeterminado. |
198
|
|
|
* |
199
|
|
|
* @return string[] |
200
|
|
|
*/ |
201
|
|
|
public function getWithDefault(): array |
202
|
|
|
{ |
203
|
|
|
return $this->withDefault; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Obtiene los campos con valor generado automatico. |
208
|
|
|
* |
209
|
|
|
* @return string[] |
210
|
|
|
*/ |
211
|
|
|
public function getAutoFields(): array |
212
|
|
|
{ |
213
|
|
|
return $this->autoFields; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..