1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2022-2023 Rafael San José Tovar <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Database; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Core\Singletons\Config; |
22
|
|
|
use Alxarafe\Core\Singletons\Debug; |
23
|
|
|
use Alxarafe\Core\Singletons\Translator; |
24
|
|
|
use DebugBar\DebugBarException; |
25
|
|
|
use Exception; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class DB |
29
|
|
|
* |
30
|
|
|
* Esta clase proporciona acceso directo a la base de datos. |
31
|
|
|
* |
32
|
|
|
* @author Rafael San José Tovar <[email protected]> |
33
|
|
|
* @version 2022.0721 |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
|
|
abstract class DB |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Motor utilizado, puede ser 'MySql', 'MariaDB', 'PostgreSql' o cualquier otro PDO |
40
|
|
|
* |
41
|
|
|
* @var Engine |
42
|
|
|
*/ |
43
|
|
|
public static Engine $engine; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Instancia de la clase con el código SQL específico del motor. |
47
|
|
|
* |
48
|
|
|
* @var SqlHelper |
49
|
|
|
*/ |
50
|
|
|
public static SqlHelper $helper; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Prefijo de la base de datos en uso |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public static string $dbPrefix; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Nombre de la base de datos en uso |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
public static string $dbName; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Establece conexión con una base de datos. |
68
|
|
|
* |
69
|
|
|
* TODO: La idea en un futuro, es que se pueda establecer conexión con múltiples |
70
|
|
|
* bases de datos pasándole el nombre de la conexión. |
71
|
|
|
* El problema es, cómo invocar de forma fácil qué conexión queremos. |
72
|
|
|
* Si se mantiene como clase abstracta es complicado, lo más fácil sería |
73
|
|
|
* creando una instancia para cada conexión, pero no se podría utilizar como |
74
|
|
|
* clase abstracta (o ahora mismo, no caigo en cómo hacerlo). |
75
|
|
|
* |
76
|
|
|
* @param string $db |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public static function connectToDatabase(string $db = 'main'): bool |
81
|
|
|
{ |
82
|
|
|
// TODO: En un futuro, desearemos poder tener conexión con más de una base de datos. |
83
|
|
|
if (isset(self::$engine)) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$dbInfo = Config::getModuleVar('database'); |
88
|
|
|
if (!isset($dbInfo[$db])) { |
89
|
|
|
Debug::sqlMessage(Translator::trans('empty-database-config', ['%name%' => $db])); |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
self::$dbPrefix = strtolower($dbInfo[$db]['dbPrefix'] ?? ''); |
94
|
|
|
self::$dbName = strtolower($dbInfo[$db]['dbName']); |
95
|
|
|
|
96
|
|
|
$engineName = $dbInfo[$db]['dbEngineName'] ?? 'PdoMySql'; |
97
|
|
|
$helperName = 'Sql' . substr($engineName, 3); |
98
|
|
|
|
99
|
|
|
Debug::sqlMessage("Using '$engineName' engine."); |
100
|
|
|
Debug::sqlMessage("Using '$helperName' SQL helper engine."); |
101
|
|
|
|
102
|
|
|
$sqlEngine = '\\Alxarafe\\Database\\SqlHelpers\\' . $helperName; |
103
|
|
|
$engine = '\\Alxarafe\\Database\\Engines\\' . $engineName; |
104
|
|
|
try { |
105
|
|
|
self::$helper = new $sqlEngine(); |
106
|
|
|
self::$engine = new $engine([ |
107
|
|
|
'dbUser' => $dbInfo[$db]['dbUser'], |
108
|
|
|
'dbPass' => $dbInfo[$db]['dbPass'], |
109
|
|
|
'dbName' => self::$dbName, |
110
|
|
|
'dbHost' => $dbInfo[$db]['dbHost'], |
111
|
|
|
'dbPort' => $dbInfo[$db]['dbPort'], |
112
|
|
|
]); |
113
|
|
|
return isset(self::$engine) && self::$engine->connect() && self::$engine->checkConnection(); |
114
|
|
|
} catch (Exception $e) { |
115
|
|
|
Debug::addException($e); |
116
|
|
|
} |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Ejecuta una sentencia SQL retornando TRUE si ha tenido éxito |
122
|
|
|
* |
123
|
|
|
* @author Rafael San José Tovar <[email protected]> |
124
|
|
|
* @version 2022.0721 |
125
|
|
|
* |
126
|
|
|
* @param string $query |
127
|
|
|
* @param array $vars |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public static function exec(string $query, array $vars = []): bool |
132
|
|
|
{ |
133
|
|
|
return self::$engine->exec($query, $vars); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Ejecuta una sentencia SELECT retornando 'false' si hay error, o un array |
138
|
|
|
* con el resultado de la consulta. |
139
|
|
|
* |
140
|
|
|
* @author Rafael San José Tovar <[email protected]> |
141
|
|
|
* @version 2022.0721 |
142
|
|
|
* |
143
|
|
|
* @param string $query |
144
|
|
|
* @param array $vars |
145
|
|
|
* |
146
|
|
|
* @return array|false |
147
|
|
|
*/ |
148
|
|
|
public static function select(string $query, array $vars = []) |
149
|
|
|
{ |
150
|
|
|
return self::$engine->select($query, $vars); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Retorna 'true' si la tabla existe en la base de datos. |
155
|
|
|
* |
156
|
|
|
* @author Rafael San José Tovar <[email protected]> |
157
|
|
|
* |
158
|
|
|
* @param string $tableName |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public static function tableExists(string $tableName): bool |
163
|
|
|
{ |
164
|
|
|
return self::$helper->tableExists(self::$dbPrefix . $tableName); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Retorna un array asociativo con los campos de la tabla |
169
|
|
|
* |
170
|
|
|
* @author Rafael San José Tovar <[email protected]> |
171
|
|
|
* |
172
|
|
|
* @param string $tableName |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
public static function getColumns(string $tableName): array |
177
|
|
|
{ |
178
|
|
|
return self::$helper->getColumns(self::$dbPrefix . $tableName); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Retorna un array asociativo con los índices de la tabla |
183
|
|
|
* |
184
|
|
|
* @author Rafael San José Tovar <[email protected]> |
185
|
|
|
* |
186
|
|
|
* @param string $tableName |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
* @throws DebugBarException |
190
|
|
|
*/ |
191
|
|
|
public static function getIndexes(string $tableName): array |
192
|
|
|
{ |
193
|
|
|
return self::$helper::getIndexes(DB::$dbPrefix . $tableName); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Retorna la sentencia SQL para la creación de un índice |
198
|
|
|
* |
199
|
|
|
* @author Rafael San José Tovar <[email protected]> |
200
|
|
|
* |
201
|
|
|
* @param string $tableName |
202
|
|
|
* @param string $index |
203
|
|
|
* @param array $data |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public static function createIndex(string $tableName, string $index, array $data): string |
208
|
|
|
{ |
209
|
|
|
return self::$helper->createIndex(self::$dbPrefix . $tableName, $index, $data); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Retorna la sentencia SQL para cambiar un índice o constraint |
214
|
|
|
* |
215
|
|
|
* @author Rafael San José Tovar <[email protected]> |
216
|
|
|
* |
217
|
|
|
* @param string $tableName |
218
|
|
|
* @param string $index |
219
|
|
|
* @param array $oldData |
220
|
|
|
* @param array $newData |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public static function changeIndex(string $tableName, string $index, array $oldData, array $newData): string |
225
|
|
|
{ |
226
|
|
|
return self::$helper->changeIndex(self::$dbPrefix . $tableName, $index, $oldData, $newData); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Retorna la sentencia SQL para la eliminación de un índice |
231
|
|
|
* |
232
|
|
|
* @author Rafael San José Tovar <[email protected]> |
233
|
|
|
* |
234
|
|
|
* @param string $tableName |
235
|
|
|
* @param string $index |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public static function removeIndex(string $tableName, string $index): string |
240
|
|
|
{ |
241
|
|
|
return self::$helper->removeIndex(self::$dbPrefix . $tableName, $index); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Retorna la secuencia SQL para modificar un campo de la tabla |
246
|
|
|
* |
247
|
|
|
* @author Rafael San José Tovar <[email protected]> |
248
|
|
|
* |
249
|
|
|
* @param string $tableName |
250
|
|
|
* @param array $oldField |
251
|
|
|
* @param array $newField |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public static function modify(string $tableName, array $oldField, array $newField): string |
256
|
|
|
{ |
257
|
|
|
return self::$helper->modify(self::$dbPrefix . $tableName, $oldField, $newField); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|