|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: shawe [email protected] |
|
5
|
|
|
* Date: 26/05/18 |
|
6
|
|
|
* Time: 22:11 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace FacturaScriptsUtils\Console\Command\Common; |
|
10
|
|
|
|
|
11
|
|
|
use FacturaScripts\Core\Base\DataBase; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Trait TableInformation. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Francesc Pineda Segarra <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
trait TableInformation |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Folder destiny path. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $dstFolder; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Name of the model. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $modelName; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Name of the table. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $tableName; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Set destiny folder. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $dstFolder |
|
46
|
|
|
* |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setDstFolder(string $dstFolder): self |
|
50
|
|
|
{ |
|
51
|
|
|
$this->dstFolder = $dstFolder; |
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set the model name to use. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $modelName |
|
59
|
|
|
* |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setModelName(string $modelName): self |
|
63
|
|
|
{ |
|
64
|
|
|
$this->modelName = $modelName; |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set table name to use. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $tableName |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setTableName(string $tableName): self |
|
76
|
|
|
{ |
|
77
|
|
|
$this->tableName = $tableName; |
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return the folder destiny path. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getDstFolder(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->dstFolder; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return the model name. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getModelName(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->modelName; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return the table name. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getTableName(): string |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->tableName; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns an array with tables. |
|
113
|
|
|
* |
|
114
|
|
|
* @param DataBase $dataBase |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getTables(DataBase $dataBase): array |
|
119
|
|
|
{ |
|
120
|
|
|
return $dataBase->getTables(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns an array with the columns of a given table. |
|
125
|
|
|
* |
|
126
|
|
|
* @param DataBase $dataBase |
|
127
|
|
|
* @param string $tableName |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getItemsFromTable(DataBase $dataBase, string $tableName): array |
|
132
|
|
|
{ |
|
133
|
|
|
return $dataBase->getColumns($tableName); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns an array with the constraints of a table. |
|
138
|
|
|
* |
|
139
|
|
|
* @param DataBase $dataBase |
|
140
|
|
|
* @param string $tableName |
|
141
|
|
|
* |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getConstraintFromTable(DataBase $dataBase, $tableName): array |
|
145
|
|
|
{ |
|
146
|
|
|
return $dataBase->getConstraints($tableName, true); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Return constraints grouped by name. |
|
151
|
|
|
* |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getConstraintsGroupByName(): array |
|
155
|
|
|
{ |
|
156
|
|
|
$constraint = []; |
|
157
|
|
|
foreach ($this->getConstraintFromTable($this->getDataBase(), $this->getTableName()) as $cons) { |
|
|
|
|
|
|
158
|
|
|
if (isset($constraint[$cons['name']])) { |
|
159
|
|
|
$constraint[$cons['name']]['column_name'] .= ', ' . $cons['column_name']; |
|
160
|
|
|
} else { |
|
161
|
|
|
$constraint[$cons['name']] = $cons; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
\ksort($constraint); |
|
165
|
|
|
return $constraint; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Return items from table by type. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $type |
|
172
|
|
|
* |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getItemsFromTableBy(string $type = ''): array |
|
176
|
|
|
{ |
|
177
|
|
|
$items = []; |
|
178
|
|
|
foreach ($this->getItemsFromTable($this->getDataBase(), $this->getTableName()) as $item) { |
|
179
|
|
|
$colType = $this->parseType($item['type'], 'view'); |
|
180
|
|
|
if (0 === \strpos($colType, $type)) { |
|
181
|
|
|
$items[] = $item; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
return $items; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Return items from table ordered, primary key first. |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getItemsFromTableOrdered(): array |
|
193
|
|
|
{ |
|
194
|
|
|
$items = []; |
|
195
|
|
|
$primaryKey = null; |
|
196
|
|
|
foreach ($this->getItemsFromTable($this->getDataBase(), $this->getTableName()) as $item) { |
|
197
|
|
|
$items[] = $item; |
|
198
|
|
|
if (0 === \strpos($item['default'], 'nextval')) { |
|
199
|
|
|
$primaryKey = array_pop($items); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
array_unshift($items, $primaryKey); |
|
203
|
|
|
return $items; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Return a list of tables. |
|
208
|
|
|
* |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getTablesMsg(): string |
|
212
|
|
|
{ |
|
213
|
|
|
return \implode(', ', $this->getTables($this->dataBase)); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Convert type to XML/PHP type. |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $type |
|
220
|
|
|
* @param string $usedOn |
|
221
|
|
|
* |
|
222
|
|
|
* @return string |
|
223
|
|
|
*/ |
|
224
|
|
|
public function parseType($type = '', string $usedOn = ''): string |
|
225
|
|
|
{ |
|
226
|
|
|
switch ($usedOn) { |
|
227
|
|
|
case 'table': |
|
228
|
|
|
$type = str_replace('varchar', 'character varying', $type); |
|
229
|
|
|
$type = preg_replace('/^double$/', 'double precision', $type); |
|
230
|
|
|
$type = preg_replace('/^int\(\d+\)/', 'integer', $type); |
|
231
|
|
|
$type = preg_replace('/tinyint\(1\)/', 'boolean', $type); |
|
232
|
|
|
$type = preg_replace('/^timestamp$/', 'timestamp without time zone', $type); |
|
233
|
|
|
break; |
|
234
|
|
|
case 'php': |
|
235
|
|
|
$type = preg_replace('/^varchar\(\d+\)/', 'string', $type); |
|
236
|
|
|
$type = preg_replace('/^character varying\(\d+\)/', 'string', $type); |
|
237
|
|
|
$type = str_replace('text', 'string', $type); |
|
238
|
|
|
$type = str_replace('double precision', 'float', $type); |
|
239
|
|
|
$type = preg_replace('/^int\(\d+\)/', 'int', $type); |
|
240
|
|
|
$type = preg_replace('/tinyint\(1\)/', 'bool', $type); |
|
241
|
|
|
$type = preg_replace('/^timestamp$/', 'string', $type); |
|
242
|
|
|
$type = str_replace('date', 'string', $type); |
|
243
|
|
|
break; |
|
244
|
|
|
case 'view': |
|
245
|
|
|
$type = preg_replace('/^character varying\(\d+\)/', 'text', $type); |
|
246
|
|
|
$type = str_replace('double precision', 'number-decimal', $type); |
|
247
|
|
|
$type = str_replace('integer', 'number', $type); |
|
248
|
|
|
$type = str_replace('boolean', 'checkbox', $type); |
|
249
|
|
|
$type = preg_replace('/^timestamp$/', 'text', $type); |
|
250
|
|
|
$type = str_replace('time without time zone', 'text', $type); |
|
251
|
|
|
$type = str_replace('date', 'datepicker', $type); |
|
252
|
|
|
break; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return $type; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths