1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\MicroOrm; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Db\DbFunctionsInterface; |
6
|
|
|
use ByJG\MicroOrm\Exception\InvalidArgumentException; |
7
|
|
|
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException; |
8
|
|
|
|
9
|
|
|
class Updatable |
10
|
|
|
{ |
11
|
|
|
protected $fields = []; |
12
|
|
|
protected $table = ""; |
13
|
|
|
protected $where = []; |
14
|
|
|
|
15
|
13 |
|
public static function getInstance() |
16
|
|
|
{ |
17
|
13 |
|
return new Updatable(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Example: |
22
|
|
|
* $query->fields(['name', 'price']); |
23
|
|
|
* |
24
|
|
|
* @param array $fields |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
13 |
|
public function fields(array $fields) |
28
|
|
|
{ |
29
|
13 |
|
$this->fields = array_merge($this->fields, (array)$fields); |
30
|
|
|
|
31
|
13 |
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Example |
36
|
|
|
* $query->table('product'); |
37
|
|
|
* |
38
|
|
|
* @param string $table |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
18 |
|
public function table($table) |
42
|
|
|
{ |
43
|
18 |
|
$this->table = $table; |
44
|
|
|
|
45
|
18 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Example: |
50
|
|
|
* $query->filter('price > [[amount]]', [ 'amount' => 1000] ); |
51
|
|
|
* |
52
|
|
|
* @param string $filter |
53
|
|
|
* @param array $params |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
9 |
|
public function where($filter, array $params = []) |
57
|
|
|
{ |
58
|
9 |
|
$this->where[] = [ 'filter' => $filter, 'params' => $params ]; |
59
|
9 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getFields() |
63
|
|
|
{ |
64
|
|
|
if (empty($this->fields)) { |
65
|
|
|
return ' * '; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return ' ' . implode(', ', $this->fields) . ' '; |
69
|
|
|
} |
70
|
|
|
|
71
|
11 |
|
protected function getWhere() |
72
|
|
|
{ |
73
|
11 |
|
$whereStr = []; |
74
|
11 |
|
$params = []; |
75
|
|
|
|
76
|
11 |
|
foreach ($this->where as $item) { |
77
|
9 |
|
$whereStr[] = $item['filter']; |
78
|
9 |
|
$params = array_merge($params, $item['params']); |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
if (empty($whereStr)) { |
82
|
2 |
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
return [ implode(' AND ', $whereStr), $params ]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $params |
91
|
|
|
* @param DbFunctionsInterface|null $dbHelper |
92
|
|
|
* @return null|string|string[] |
93
|
|
|
* @throws \ByJG\MicroOrm\Exception\OrmInvalidFieldsException |
94
|
|
|
*/ |
95
|
7 |
|
public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null) |
96
|
|
|
{ |
97
|
7 |
|
if (empty($this->fields)) { |
98
|
|
|
throw new OrmInvalidFieldsException('You must specifiy the fields for insert'); |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
$fieldsStr = $this->fields; |
102
|
7 |
|
if (!is_null($dbHelper)) { |
103
|
7 |
|
$fieldsStr = $dbHelper->delimiterField($fieldsStr); |
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
$tableStr = $this->table; |
107
|
7 |
|
if (!is_null($dbHelper)) { |
108
|
7 |
|
$tableStr = $dbHelper->delimiterTable($tableStr); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$sql = 'INSERT INTO ' |
112
|
7 |
|
. $tableStr |
113
|
7 |
|
. '( ' . implode(', ', $fieldsStr) . ' ) ' |
114
|
7 |
|
. ' values ' |
115
|
7 |
|
. '( [[' . implode(']], [[', $this->fields) . ']] ) '; |
116
|
|
|
|
117
|
7 |
|
return ORMHelper::processLiteral($sql, $params); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $params |
122
|
|
|
* @param DbFunctionsInterface|null $dbHelper |
123
|
|
|
* @return string |
124
|
|
|
* @throws InvalidArgumentException |
125
|
|
|
*/ |
126
|
6 |
|
public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null) |
127
|
|
|
{ |
128
|
6 |
|
if (empty($this->fields)) { |
129
|
|
|
throw new InvalidArgumentException('You must specifiy the fields for insert'); |
130
|
|
|
} |
131
|
|
|
|
132
|
6 |
|
$fieldsStr = []; |
133
|
6 |
|
foreach ($this->fields as $field) { |
134
|
6 |
|
$fieldName = $field; |
135
|
6 |
|
if (!is_null($dbHelper)) { |
136
|
5 |
|
$fieldName = $dbHelper->delimiterField($fieldName); |
137
|
|
|
} |
138
|
6 |
|
$fieldsStr[] = "$fieldName = [[$field]] "; |
139
|
|
|
} |
140
|
|
|
|
141
|
6 |
|
$whereStr = $this->getWhere(); |
142
|
6 |
|
if (is_null($whereStr)) { |
143
|
1 |
|
throw new InvalidArgumentException('You must specifiy a where clause'); |
144
|
|
|
} |
145
|
|
|
|
146
|
5 |
|
$tableName = $this->table; |
147
|
5 |
|
if (!is_null($dbHelper)) { |
148
|
5 |
|
$tableName = $dbHelper->delimiterTable($tableName); |
149
|
|
|
} |
150
|
|
|
|
151
|
5 |
|
$sql = 'UPDATE ' . $tableName . ' SET ' |
152
|
5 |
|
. implode(', ', $fieldsStr) |
153
|
5 |
|
. ' WHERE ' . $whereStr[0]; |
154
|
|
|
|
155
|
5 |
|
$params = array_merge($params, $whereStr[1]); |
156
|
|
|
|
157
|
5 |
|
return ORMHelper::processLiteral($sql, $params); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $params |
162
|
|
|
* @return string |
163
|
|
|
* @throws InvalidArgumentException |
164
|
|
|
*/ |
165
|
5 |
|
public function buildDelete(&$params) |
166
|
|
|
{ |
167
|
5 |
|
$whereStr = $this->getWhere(); |
168
|
5 |
|
if (is_null($whereStr)) { |
169
|
1 |
|
throw new InvalidArgumentException('You must specifiy a where clause'); |
170
|
|
|
} |
171
|
|
|
|
172
|
4 |
|
$sql = 'DELETE FROM ' . $this->table |
173
|
4 |
|
. ' WHERE ' . $whereStr[0]; |
174
|
|
|
|
175
|
4 |
|
$params = array_merge($params, $whereStr[1]); |
176
|
|
|
|
177
|
4 |
|
return ORMHelper::processLiteral($sql, $params); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|