|
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\Database\SqlHelpers\SqlMySql; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class DB |
|
27
|
|
|
* |
|
28
|
|
|
* Esta clase proporciona acceso directo a la base de datos. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
31
|
|
|
* @version 2022.0721 |
|
32
|
|
|
* |
|
33
|
|
|
*/ |
|
34
|
|
|
class DB |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Motor utilizado, puede ser MySql, MariaDB, PostgreSql o cualquier otro PDO |
|
38
|
|
|
* |
|
39
|
|
|
* @var Engine |
|
40
|
|
|
*/ |
|
41
|
|
|
public static $engine; |
|
42
|
|
|
|
|
43
|
|
|
public static $sqlHelper; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct() |
|
46
|
|
|
{ |
|
47
|
|
|
self::$engine = Config::getEngine(); |
|
48
|
|
|
self::$sqlHelper = Config::getSqlHelper(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function connect() |
|
52
|
|
|
{ |
|
53
|
|
|
return self::$engine->connect(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function disconnect() |
|
57
|
|
|
{ |
|
58
|
|
|
return self::$engine->disconnect(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function connected() |
|
62
|
|
|
{ |
|
63
|
|
|
return self::$engine->connected(); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function getDataTypes() |
|
67
|
|
|
{ |
|
68
|
|
|
return self::$sqlHelper->getDataTypes(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Ejecuta una sentencia SQL retornando TRUE si ha tenido éxito |
|
73
|
|
|
* |
|
74
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
75
|
|
|
* @version 2022.0721 |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $query |
|
78
|
|
|
* @param array $vars |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function exec(string $query, array $vars = []): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return self::$engine->exec($query, $vars); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Ejecuta una sentencia SELECT, retornando false si hay error, o un array |
|
89
|
|
|
* con el resultado de la consulta. |
|
90
|
|
|
* |
|
91
|
|
|
* @author Rafael San José Tovar <[email protected]> |
|
92
|
|
|
* @version 2022.0721 |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $query |
|
95
|
|
|
* @param array $vars |
|
96
|
|
|
* |
|
97
|
|
|
* @return array|false |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function select(string $query, array $vars = []) |
|
100
|
|
|
{ |
|
101
|
|
|
return self::$engine->select($query, $vars); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function getErrors() |
|
105
|
|
|
{ |
|
106
|
|
|
return self::$engine->getErrors(); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public static function beginTransaction() |
|
110
|
|
|
{ |
|
111
|
|
|
return self::$engine->beginTransaction(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function commit() |
|
115
|
|
|
{ |
|
116
|
|
|
return self::$engine->commit(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public static function close() |
|
120
|
|
|
{ |
|
121
|
|
|
return self::$engine->close(); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public static function rollback() |
|
125
|
|
|
{ |
|
126
|
|
|
return self::$engine->rollback(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public static function version() |
|
130
|
|
|
{ |
|
131
|
|
|
return self::$engine->server_info(); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public static function tableExists(string $tableName) |
|
135
|
|
|
{ |
|
136
|
|
|
return self::$sqlHelper->tableExists(Config::$dbPrefix . $tableName); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public static function getColumns(string $tableName) |
|
140
|
|
|
{ |
|
141
|
|
|
return self::$sqlHelper->getColumns(Config::$dbPrefix . $tableName); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public static function yamlFieldToDb(array $data): array |
|
145
|
|
|
{ |
|
146
|
|
|
return self::$sqlHelper::yamlFieldToDb($data); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public static function yamlFieldToSchema(array $data): array |
|
150
|
|
|
{ |
|
151
|
|
|
return self::$sqlHelper::yamlFieldToSchema($data); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public static function dbFieldToSchema(array $data): array |
|
155
|
|
|
{ |
|
156
|
|
|
return self::$sqlHelper::dbFieldToSchema($data); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public static function dbFieldToYaml(array $data): array |
|
160
|
|
|
{ |
|
161
|
|
|
return self::$sqlHelper::dbFieldToYaml($data); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public static function normalizeFromDb(array $data) |
|
165
|
|
|
{ |
|
166
|
|
|
$result = self::$sqlHelper::normalizeDbField($data); |
|
167
|
|
|
dump([ |
|
168
|
|
|
'normalizeFromDb', |
|
169
|
|
|
'data' => $data, |
|
170
|
|
|
'result' => $result, |
|
171
|
|
|
]); |
|
172
|
|
|
return $result; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public static function normalizeFromYaml(array $yamlFields) |
|
176
|
|
|
{ |
|
177
|
|
|
$result = []; |
|
178
|
|
|
foreach ($yamlFields as $field => $yamlField) { |
|
179
|
|
|
$result[$field] = self::$sqlHelper::normalizeYamlField($yamlField); |
|
180
|
|
|
} |
|
181
|
|
|
dump([ |
|
182
|
|
|
'normalizeFromYaml', |
|
183
|
|
|
'data' => $yamlFields, |
|
184
|
|
|
'result' => $result, |
|
185
|
|
|
]); |
|
186
|
|
|
return $result; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public static function normalize(array $data) |
|
190
|
|
|
{ |
|
191
|
|
|
return self::$sqlHelper->normalizeField($data); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public static function getIndexType(): string |
|
195
|
|
|
{ |
|
196
|
|
|
return self::$sqlHelper->getIndexType(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public static function modify(string $tableName, array $oldField, array $newField):string |
|
200
|
|
|
{ |
|
201
|
|
|
return self::$sqlHelper->modify(Config::$dbPrefix . $tableName, $oldField, $newField); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.