|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Db\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
|
6
|
|
|
|
|
7
|
|
|
trait QueryUtilTrait |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param TableFieldEntity $field |
|
11
|
|
|
* @param array $values First entries |
|
12
|
|
|
* @param bool $update |
|
13
|
|
|
* |
|
14
|
|
|
* @return string[] |
|
15
|
|
|
*/ |
|
16
|
|
|
private function getEditFunctionNames(TableFieldEntity $field, array $values, bool $update): array |
|
17
|
|
|
{ |
|
18
|
|
|
$names = $values; |
|
19
|
|
|
foreach ($this->driver->editFunctions() as $key => $functions) { |
|
20
|
|
|
if (!$key || (!isset($this->input->values['call']) && $update)) { // relative functions |
|
21
|
|
|
foreach ($functions as $pattern => $value) { |
|
22
|
|
|
if (!$pattern || preg_match("~$pattern~", $field->type)) { |
|
23
|
|
|
$names[] = $value; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
if ($key && !preg_match('~set|blob|bytea|raw|file|bool~', $field->type)) { |
|
28
|
|
|
$names[] = 'SQL'; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
return $names; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Functions displayed in edit form |
|
36
|
|
|
* |
|
37
|
|
|
* @param TableFieldEntity $field Single field from fields() |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function editFunctions(TableFieldEntity $field): array |
|
42
|
|
|
{ |
|
43
|
|
|
$update = isset($this->input->values['select']); // || $this->where([]); |
|
44
|
|
|
if ($field->autoIncrement && !$update) { |
|
45
|
|
|
return [$this->trans->lang('Auto Increment')]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$names = ($field->null ? ['NULL', ''] : ['']); |
|
49
|
|
|
return $this->getEditFunctionNames($field, $names, $update); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create SQL string from field type |
|
54
|
|
|
* |
|
55
|
|
|
* @param TableFieldEntity $field |
|
56
|
|
|
* @param string $collate |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
private function processType(TableFieldEntity $field, string $collate = 'COLLATE'): string |
|
61
|
|
|
{ |
|
62
|
|
|
$collation = ''; |
|
63
|
|
|
if (preg_match('~char|text|enum|set~', $field->type) && $field->collation) { |
|
64
|
|
|
$collation = " $collate " . $this->driver->quote($field->collation); |
|
65
|
|
|
} |
|
66
|
|
|
$sign = ''; |
|
67
|
|
|
if (preg_match($this->driver->numberRegex(), $field->type) && |
|
68
|
|
|
in_array($field->unsigned, $this->driver->unsigned())) { |
|
69
|
|
|
$sign = ' ' . $field->unsigned; |
|
70
|
|
|
} |
|
71
|
|
|
return ' ' . $field->type . $this->processLength($field->length) . $sign . $collation; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create SQL string from field |
|
76
|
|
|
* |
|
77
|
|
|
* @param TableFieldEntity $field Basic field information |
|
78
|
|
|
* @param TableFieldEntity $typeField Information about field type |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function processField(TableFieldEntity $field, TableFieldEntity $typeField): array |
|
83
|
|
|
{ |
|
84
|
|
|
$onUpdate = ''; |
|
85
|
|
|
if (preg_match('~timestamp|datetime~', $field->type) && $field->onUpdate) { |
|
86
|
|
|
$onUpdate = ' ON UPDATE ' . $field->onUpdate; |
|
87
|
|
|
} |
|
88
|
|
|
$comment = ''; |
|
89
|
|
|
if ($this->driver->support('comment') && $field->comment !== '') { |
|
90
|
|
|
$comment = ' COMMENT ' . $this->driver->quote($field->comment); |
|
91
|
|
|
} |
|
92
|
|
|
$null = $field->null ? ' NULL' : ' NOT NULL'; // NULL for timestamp |
|
93
|
|
|
$autoIncrement = $field->autoIncrement ? $this->driver->autoIncrement() : null; |
|
94
|
|
|
return [$this->driver->escapeId(trim($field->name)), $this->processType($typeField), |
|
95
|
|
|
$null, $this->driver->defaultValue($field), $onUpdate, $comment, $autoIncrement]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|