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
|
|
|
use Alxarafe\Core\Singletons\Config; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The SqlHelper class provides support for creating SQL queries and commands. |
13
|
|
|
* The class will have to be extended by completing the particularities of each of them. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class SqlHelper |
18
|
|
|
* |
19
|
|
|
* Proporciona soporte para la creación de comandos y consultas SQL. |
20
|
|
|
* Esta clase deberá de extenderse para cada controlador de base de datos específico. |
21
|
|
|
* |
22
|
|
|
* @author Rafael San José Tovar <[email protected]> |
23
|
|
|
* @version 2023.0101 |
24
|
|
|
* |
25
|
|
|
* @package Alxarafe\Database |
26
|
|
|
*/ |
27
|
|
|
abstract class SqlHelper |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Retorna el carácter que se usa como separador de nombre de tabla en la consulta SQL. |
31
|
|
|
* |
32
|
|
|
* @author Rafael San José Tovar <[email protected]> |
33
|
|
|
* @version 2023.0101 |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
abstract public static function getTableQuote(): string; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retorna el carácter que se usa como separador de nombre de campo en la consulta SQL. |
41
|
|
|
* |
42
|
|
|
* @author Rafael San José Tovar <[email protected]> |
43
|
|
|
* @version 2023.0101 |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
abstract public static function getFieldQuote(): string; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Retorna el nombre de la tabla entre comillas. |
51
|
|
|
* |
52
|
|
|
* @author Rafael San José Tovar <[email protected]> |
53
|
|
|
* @version 2023.0101 |
54
|
|
|
* |
55
|
|
|
* @param string $tableName |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public static function quoteTableName(string $tableName): string |
60
|
|
|
{ |
61
|
|
|
return static::getTableQuote() . $tableName . static::getTableQuote(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Retorna el nombre de un campo entre comillas. |
66
|
|
|
* |
67
|
|
|
* @author Rafael San José Tovar <[email protected]> |
68
|
|
|
* @version 2023.0101 |
69
|
|
|
* |
70
|
|
|
* @param string $fieldName |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public static function quoteFieldName(string $fieldName): string |
75
|
|
|
{ |
76
|
|
|
return static::getFieldQuote() . $fieldName . static::getFieldQuote(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Retorna un array con los distintos tipos de datos del motor |
81
|
|
|
* |
82
|
|
|
* @author Rafael San José Tovar <[email protected]> |
83
|
|
|
* @version 2023.0101 |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
abstract public function getDataTypes(): array; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retorna un array con el nombre de todas las tablas de la base de datos. |
91
|
|
|
* |
92
|
|
|
* @author Rafael San José Tovar <[email protected]> |
93
|
|
|
* @version 2023.0101 |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
abstract public static function getTables(): array; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns an array with all the columns of a table |
101
|
|
|
* |
102
|
|
|
* TODO: Review the types. The variants will depend on type + length. |
103
|
|
|
* |
104
|
|
|
* 'name_of_the_field' => { |
105
|
|
|
* (Required type and length or bytes) |
106
|
|
|
* 'type' => (string/integer/float/decimal/boolean/date/datetime/text/blob) |
107
|
|
|
* 'length' => It is the number of characters that the field needs (optional if bytes exists) |
108
|
|
|
* 'bytes' => Number of bytes occupied by the data (optional if length exists) |
109
|
|
|
* (Optional) |
110
|
|
|
* 'default' => Default value |
111
|
|
|
* 'nullable' => True if it can be null |
112
|
|
|
* 'primary' => True if it is the primary key |
113
|
|
|
* 'autoincrement' => True if it is an autoincremental number |
114
|
|
|
* 'zerofilled' => True if it completes zeros on the left |
115
|
|
|
* } |
116
|
|
|
* |
117
|
|
|
* @param string $tableName |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getColumns(string $tableName): array |
122
|
|
|
{ |
123
|
|
|
$query = $this->getColumnsSql($tableName); |
124
|
|
|
$data = DB::select($query); |
125
|
|
|
$result = []; |
126
|
|
|
foreach ($data as $value) { |
127
|
|
|
$row = $this->normalizeFields($value); |
128
|
|
|
$result[$row['field']] = $row; |
129
|
|
|
} |
130
|
|
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* SQL statement that returns the fields in the table |
135
|
|
|
* |
136
|
|
|
* @param string $tableName |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
abstract public function getColumnsSql(string $tableName): string; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Modifies the structure returned by the query generated with |
144
|
|
|
* getColumnsSql to the normalized format that returns getColumns |
145
|
|
|
* |
146
|
|
|
* @param array $fields |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
abstract public function normalizeFields(array $fields): array; |
151
|
|
|
|
152
|
|
|
//abstract public function normalizeConstraints(array $fields): array; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Obtains an array of indexes for a table |
156
|
|
|
* |
157
|
|
|
* @param string $tableName |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
* @throws \DebugBar\DebugBarException |
161
|
|
|
*/ |
162
|
|
|
public function getIndexes(string $tableName): array |
163
|
|
|
{ |
164
|
|
|
$query = $this->getIndexesSql($tableName); |
165
|
|
|
$data = DB::select($query); |
166
|
|
|
$result = []; |
167
|
|
|
foreach ($data as $value) { |
168
|
|
|
$row = $this->normalizeIndexes($value); |
169
|
|
|
$result[$row['index']] = $row; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the SQL sentence for obtains the index list of a table. |
177
|
|
|
* |
178
|
|
|
* @param string $tableName |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
abstract public function getIndexesSql(string $tableName): string; |
183
|
|
|
|
184
|
|
|
abstract public function normalizeIndexes(array $fields): array; |
185
|
|
|
|
186
|
|
|
/* |
187
|
|
|
abstract public function getConstraintsSql(string $tableName): string; |
188
|
|
|
|
189
|
|
|
public function getConstraints(string $tableName): array |
190
|
|
|
{ |
191
|
|
|
$query = $this->getConstraintsSql($tableName); |
192
|
|
|
$data = DB::select($query); |
193
|
|
|
$result = []; |
194
|
|
|
foreach ($data as $value) { |
195
|
|
|
$row = $this->normalizeConstraints($value); |
196
|
|
|
$result[$row['constraint']] = $row; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
*/ |
202
|
|
|
} |
203
|
|
|
|