1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Alxarafe. Development of PHP applications in a flash! |
4
|
|
|
* Copyright (C) 2018 Alxarafe <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Alxarafe\Database; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SqlHelper |
11
|
|
|
* |
12
|
|
|
* Proporciona soporte para la creación de comandos y consultas SQL. |
13
|
|
|
* Esta clase deberá de extenderse para cada controlador de base de datos específico. |
14
|
|
|
* Se usa desde la clase estática DB. |
15
|
|
|
* |
16
|
|
|
* @author Rafael San José Tovar <[email protected]> |
17
|
|
|
* @version 2023.0101 |
18
|
|
|
* |
19
|
|
|
* @package Alxarafe\Database |
20
|
|
|
*/ |
21
|
|
|
abstract class SqlHelper |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Retorna el nombre de la tabla entre comillas. |
25
|
|
|
* |
26
|
|
|
* @author Rafael San José Tovar <[email protected]> |
27
|
|
|
* @version 2023.0101 |
28
|
|
|
* |
29
|
|
|
* @param string $tableName |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public static function quoteTableName(string $tableName): string |
34
|
|
|
{ |
35
|
|
|
return static::getTableQuote() . $tableName . static::getTableQuote(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Retorna el nombre de un campo entre comillas. |
40
|
|
|
* |
41
|
|
|
* @author Rafael San José Tovar <[email protected]> |
42
|
|
|
* @version 2023.0101 |
43
|
|
|
* |
44
|
|
|
* @param string $fieldName |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public static function quoteFieldName(string $fieldName): string |
49
|
|
|
{ |
50
|
|
|
return static::getFieldQuote() . $fieldName . static::getFieldQuote(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retorna las comillas que encierran al nombre de la tabla en una consulta SQL. |
55
|
|
|
* |
56
|
|
|
* @author Rafael San José Tovar <[email protected]> |
57
|
|
|
* @version 2023.0101 |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
abstract public static function getTableQuote(): string; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retorna las comillas que encierran al nombre de un campo en una consulta SQL |
65
|
|
|
* |
66
|
|
|
* @author Rafael San José Tovar <[email protected]> |
67
|
|
|
* @version 2023.0101 |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
abstract public static function getFieldQuote(): string; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Permite saber si una tabla existe. |
75
|
|
|
* |
76
|
|
|
* @author Rafael San José Tovar <[email protected]> |
77
|
|
|
* @version 2023.0106 |
78
|
|
|
* |
79
|
|
|
* @param string $tableName |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
abstract public static function tableExists(string $tableName): bool; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retorna un array con la asociación de tipos del motor SQL para cada tipo definido |
87
|
|
|
* en el Schema. |
88
|
|
|
* |
89
|
|
|
* @author Rafael San José Tovar <[email protected]> |
90
|
|
|
* @version 2023.0101 |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
abstract public static function getDataTypes(): array; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Retorna un array con el nombre de todas las tablas de la base de datos. |
98
|
|
|
* |
99
|
|
|
* @author Rafael San José Tovar <[email protected]> |
100
|
|
|
* @version 2023.0101 |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
abstract public static function getTables(): array; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Retorna el tipo de dato que se utiliza para los índices autoincrementados |
108
|
|
|
* |
109
|
|
|
* @author Rafael San José Tovar <[email protected]> |
110
|
|
|
* @version 2023.0108 |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
abstract public static function getIndexType(): string; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Retorna un array asociativo con la información de cada columna de la tabla. |
118
|
|
|
* El resultado será dependiente del motor de base de datos. |
119
|
|
|
* |
120
|
|
|
* @author Rafael San José Tovar <[email protected]> |
121
|
|
|
* @version 2023.0108 |
122
|
|
|
* |
123
|
|
|
* @param string $tableName |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
abstract public static function getColumns(string $tableName): array; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Recibiendo un array con los datos de un campo tal y como lo retorna la base de |
131
|
|
|
* datos, devuelve la información normalizada para ser utilizada por Schema. |
132
|
|
|
* |
133
|
|
|
* @author Rafael San José Tovar <[email protected]> |
134
|
|
|
* @version 2023.0108 |
135
|
|
|
* |
136
|
|
|
* @param array $row |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
abstract public static function normalizeDbField(array $row): array; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Recibiendo un array con los datos de un campo tal y como están en el yaml de |
144
|
|
|
* definición, devuelve la información normalizada para ser utilizada por Schema. |
145
|
|
|
* |
146
|
|
|
* @author Rafael San José Tovar <[email protected]> |
147
|
|
|
* @version 2023.0108 |
148
|
|
|
* |
149
|
|
|
* @param array $row |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
abstract public static function normalizeYamlField(array $row): array; |
154
|
|
|
|
155
|
|
|
abstract public static function yamlFieldToDb(array $data):array; |
156
|
|
|
abstract public static function yamlFieldToSchema(array $data):array; |
157
|
|
|
abstract public static function dbFieldToSchema(array $data):array; |
158
|
|
|
abstract public static function dbFieldToYaml(array $data):array; |
159
|
|
|
|
160
|
|
|
//abstract public function normalizeConstraints(array $fields): array; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Obtains an array of indexes for a table |
164
|
|
|
* |
165
|
|
|
* @param string $tableName |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
* @throws \DebugBar\DebugBarException |
169
|
|
|
*/ |
170
|
|
|
public function getIndexes(string $tableName): array |
171
|
|
|
{ |
172
|
|
|
$query = $this->getIndexesSql($tableName); |
173
|
|
|
$data = DB::select($query); |
174
|
|
|
$result = []; |
175
|
|
|
foreach ($data as $value) { |
176
|
|
|
$row = $this->normalizeIndexes($value); |
177
|
|
|
$result[$row['index']] = $row; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $result; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get the SQL sentence for obtains the index list of a table. |
185
|
|
|
* |
186
|
|
|
* @param string $tableName |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
abstract public function getIndexesSql(string $tableName): string; |
191
|
|
|
|
192
|
|
|
abstract public function normalizeIndexes(array $fields): array; |
193
|
|
|
|
194
|
|
|
abstract public static function modify(string $tableName, array $oldField, array $newField):string; |
195
|
|
|
/* |
196
|
|
|
abstract public function getConstraintsSql(string $tableName): string; |
197
|
|
|
|
198
|
|
|
public function getConstraints(string $tableName): array |
199
|
|
|
{ |
200
|
|
|
$query = $this->getConstraintsSql($tableName); |
201
|
|
|
$data = DB::select($query); |
202
|
|
|
$result = []; |
203
|
|
|
foreach ($data as $value) { |
204
|
|
|
$row = $this->normalizeConstraints($value); |
205
|
|
|
$result[$row['constraint']] = $row; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $result; |
209
|
|
|
} |
210
|
|
|
*/ |
211
|
|
|
} |
212
|
|
|
|