|
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 - 2016 Kumbia Team (http://www.kumbiaphp.com) |
|
19
|
|
|
* @license http://wiki.kumbiaphp.com/Licencia New BSD License |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Kumbia\ActiveRecord\Metadata; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Metadata de tabla. |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class Metadata |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Singleton de metadata. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $instances = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Descripción de los campos. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fields = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Lista de campos. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $fieldsList = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Clave primaria. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $pk; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Campos con valor predeterminado. |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $withDefault = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Campos con valor autogenerado. |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $autoFields = []; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Metadata de la tabla. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $type tipo de controlador |
|
74
|
|
|
* @param string $database |
|
75
|
|
|
* @param string $table |
|
76
|
|
|
* @param string $schema |
|
77
|
|
|
* |
|
78
|
|
|
* @return Metadata |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function get($type, $database, $table, $schema = null) |
|
81
|
|
|
{ |
|
82
|
|
|
if (isset(self::$instances["$database.$table.$schema"])) { |
|
83
|
|
|
return self::$instances["$database.$table.$schema"]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return self::getMetadata($type, $database, $table, $schema); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Obtiene la metadata de la tabla |
|
91
|
|
|
* Y la cachea si esta en producción. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $type tipo de controlador |
|
94
|
|
|
* @param string $database |
|
95
|
|
|
* @param string $table |
|
96
|
|
|
* @param string $schema |
|
97
|
|
|
* |
|
98
|
|
|
* @return Metadata |
|
99
|
|
|
*/ |
|
100
|
|
|
private static function getMetadata($type, $database, $table, $schema) |
|
101
|
|
|
{ |
|
102
|
|
|
if (PRODUCTION && !(self::$instances["$database.$table.$schema"] = \Cache::driver()->get("$database.$table.$schema", 'ActiveRecord.Metadata'))) { |
|
103
|
|
|
return self::$instances["$database.$table.$schema"]; |
|
104
|
|
|
} |
|
105
|
|
|
$class = ucwords($type).'Metadata'; |
|
106
|
|
|
|
|
107
|
|
|
$class = __NAMESPACE__."\\$class"; |
|
108
|
|
|
|
|
109
|
|
|
self::$instances["$database.$table.$schema"] = new $class($database, $table, $schema); |
|
110
|
|
|
|
|
111
|
|
|
// Cachea los metadatos |
|
112
|
|
|
if (PRODUCTION) { |
|
113
|
|
|
\Cache::driver()->save( |
|
114
|
|
|
self::$instances["$database.$table.$schema"], |
|
115
|
|
|
\Config::get('config.application.metadata_lifetime'), |
|
116
|
|
|
"$database.$table.$schema", |
|
117
|
|
|
'ActiveRecord.Metadata' |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return self::$instances["$database.$table.$schema"]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Constructor. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $database base de datos |
|
128
|
|
|
* @param string $table tabla |
|
129
|
|
|
* @param string $schema squema |
|
130
|
|
|
*/ |
|
131
|
|
|
private function __construct($database, $table, $schema = null) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->fields = $this->queryFields($database, $table, $schema); |
|
134
|
|
|
$this->fieldsList = \array_keys($this->fields); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Permite el filtrado de columna en PK, por Defecto y Autogenerado. |
|
139
|
|
|
* |
|
140
|
|
|
* @param $m información de la columna |
|
141
|
|
|
* @param $field nombre de la columna |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function filterCol($m, $field) |
|
144
|
|
|
{ |
|
145
|
|
|
if ($m['Key'] == 'PRI') { |
|
146
|
|
|
$this->pk = $field; |
|
147
|
|
|
} elseif ($m['Default']) { |
|
148
|
|
|
$this->withDefault[] = $field; |
|
149
|
|
|
} elseif ($m['Auto']) { |
|
150
|
|
|
$this->autoFields[] = $field; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Consultar los campos de la tabla en la base de datos. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $database base de datos |
|
158
|
|
|
* @param string $table tabla |
|
159
|
|
|
* @param string $schema squema |
|
160
|
|
|
* |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
abstract protected function queryFields($database, $table, $schema = null); |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Obtiene la descripción de los campos. |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getFields() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->fields; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Obtiene la lista de campos. |
|
177
|
|
|
* |
|
178
|
|
|
* @return array |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getFieldsList() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->fieldsList; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Obtiene la clave primaria. |
|
187
|
|
|
* |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getPK() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->pk; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Obtiene los campos con valor predeterminado. |
|
197
|
|
|
* |
|
198
|
|
|
* @return array |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getWithDefault() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->withDefault; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Obtiene los campos con valor generado automatico. |
|
207
|
|
|
* |
|
208
|
|
|
* @return array |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getAutoFields() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->autoFields; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|