1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Datamanager; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
abstract class Datamanager |
8
|
|
|
{ |
9
|
|
|
use CrudTrait, DataTrait; |
10
|
|
|
|
11
|
|
|
private array $result = []; |
12
|
|
|
protected array $data = []; |
13
|
|
|
|
14
|
|
|
private bool $full = false; |
15
|
|
|
private ?string $clause = null; |
16
|
|
|
|
17
|
|
|
private array $where = [''=> ["1",'=',"1"] ]; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
private function mountTable_Field(string $field, $value = null) |
21
|
|
|
{ |
22
|
|
|
$this->$field = null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function mountTable_Type(string $field, $value = null) |
26
|
|
|
{ |
27
|
|
|
$type = $value; |
28
|
|
|
$maxlength = null; |
29
|
|
|
|
30
|
|
|
if(strpos($value,'(')){ |
31
|
|
|
$type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type; |
32
|
|
|
$type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type; |
33
|
|
|
$type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$maxlength = (array_key_exists( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength; |
37
|
|
|
$maxlength = (array_key_exists( $type , ['date'])) ? 10 : $maxlength; |
38
|
|
|
$maxlength = (array_key_exists( $type , ['datetime'])) ? 19 : $maxlength; |
39
|
|
|
$maxlength = (array_key_exists( $type , ['boolean'])) ? 1 : $maxlength; |
40
|
|
|
|
41
|
|
|
$this->$field = ['maxlength' => $maxlength]; |
42
|
|
|
$this->$field = ['type' => $type]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function mountTable_Null(string $field, $value = null) |
46
|
|
|
{ |
47
|
|
|
$this->$field = ['null' => ($value === 'YES') ? 1 : 0]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function mountTable_Key(string $field, $value = null) |
51
|
|
|
{ |
52
|
|
|
$this->$field = ['key' => $value]; |
53
|
|
|
$this->$field = ['upgradeable' => ($value == 'PRI') ? 0 : 1]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function mountTable_Extra(string $field, $value = null) |
57
|
|
|
{ |
58
|
|
|
$this->$field = ['extra' => $value]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function mountTable_Default(string $field, $value = null) |
62
|
|
|
{ |
63
|
|
|
$this->$field = ['default' => $value]; |
64
|
|
|
$this->$field = ['value' => null]; |
65
|
|
|
$this->$field = ['changed' => false]; |
66
|
|
|
$this->select[$field] = true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function mountData(array $table): Datamanager |
70
|
|
|
{ |
71
|
|
|
foreach ($table as $column) { |
72
|
|
|
foreach ($column as $propriety => $value) { |
73
|
|
|
$method = "mountTable_{$propriety}"; |
74
|
|
|
$this->$method($column['Field'], $value); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function check_where_array(array $where) |
81
|
|
|
{ |
82
|
|
|
if(count($where) != 3){ |
83
|
|
|
throw new Exception("Condition where set incorrectly: ".implode(' ',$where)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if(!array_key_exists($where[0],$this->data) && $this->full){ |
87
|
|
|
throw new Exception("{$where[0]} field does not exist in the table {$this->table}."); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function mountRemove(): array |
92
|
|
|
{ |
93
|
|
|
$return = ['data' => [], 'where' => []]; |
94
|
|
|
foreach($this->where as $clause => $condition){ |
95
|
|
|
if(strlen($clause) === 0){ |
96
|
|
|
$return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
97
|
|
|
$return['data'] .= "q_{$condition[0]}={$condition[2]}&"; |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach($condition as $value){ |
102
|
|
|
$return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} "; |
103
|
|
|
$return['data'] .= "q_{$value[0]}={$value[2]}&"; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return $return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function mountSave(): array |
110
|
|
|
{ |
111
|
|
|
$return = ['data' => []]; |
112
|
|
|
|
113
|
|
|
foreach ($this->data as $key => $value) { |
114
|
|
|
if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){ |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){ |
119
|
|
|
$return['data'][$key] = $this->data[$key]['value']; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function save(): Datamanager |
127
|
|
|
{ |
128
|
|
|
$this->transaction('begin'); |
129
|
|
|
|
130
|
|
|
try{ |
131
|
|
|
$this->update( |
132
|
|
|
$this->mountSave()['data'], |
133
|
|
|
"{$this->primary}=:{$this->primary}", |
134
|
|
|
$this->primary.'='.$this->getData()[$this->primary]['value'] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->check_fail(); |
138
|
|
|
|
139
|
|
|
$this->transaction('commit'); |
140
|
|
|
}catch(Exception $er){ |
141
|
|
|
$this->transaction('rollback'); |
142
|
|
|
throw $er; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function mountWhereExec(): array |
149
|
|
|
{ |
150
|
|
|
$return = ['where' => [], 'data' => []]; |
151
|
|
|
|
152
|
|
|
foreach ($this->where as $key => $value) { |
153
|
|
|
|
154
|
|
|
$key = (!$key) ? '' : " {$key} "; |
155
|
|
|
|
156
|
|
|
if(is_array($value[0])){ |
157
|
|
|
|
158
|
|
|
foreach ($value as $k => $v) { |
159
|
|
|
$return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
160
|
|
|
$return['data']["q_{$v[0]}"] = $v[2]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
continue; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$return['where'] .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} "; |
167
|
|
|
$return['data']["q_{$value[0]}"] = $value[2]; |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
return $return; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function mountSelect() |
174
|
|
|
{ |
175
|
|
|
$select = implode(',',array_keys($this->select)); |
176
|
|
|
|
177
|
|
|
$this->query = str_replace('*', $select,$this->query); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function mountLimit() |
181
|
|
|
{ |
182
|
|
|
if(!is_null($this->limit)){ |
183
|
|
|
$this->query .= " LIMIT {$this->limit}"; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function mountOffset() |
188
|
|
|
{ |
189
|
|
|
if(!is_null($this->offset)){ |
190
|
|
|
$this->query .= " OFFSET {$this->offset}"; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|