1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use \DBAL\DbQuery; |
5
|
|
|
use \Entity\KeyedModel; |
6
|
|
|
use \Entity\EntityModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DbRowDirectOperator |
10
|
|
|
* |
11
|
|
|
* Handle Entity\EntityModel storing/loading operations |
12
|
|
|
*/ |
13
|
|
|
class DbRowDirectOperator { |
14
|
|
|
/** |
15
|
|
|
* @var db_mysql $db |
16
|
|
|
*/ |
17
|
|
|
protected $db; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* DbRowDirectOperator constructor. |
21
|
|
|
* |
22
|
|
|
* @param db_mysql $db |
23
|
|
|
*/ |
24
|
|
|
public function __construct($db) { |
25
|
|
|
$this->db = $db; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Builds SELECT DbQuery |
31
|
|
|
* |
32
|
|
|
* @param EntityModel $mEntity |
33
|
|
|
* @param array $where |
34
|
|
|
* @param array $whereDanger |
35
|
|
|
* |
36
|
|
|
* @return DbQuery |
37
|
|
|
*/ |
38
|
|
|
protected function buildSelectDbQuery($mEntity, $where, $whereDanger = array()) { |
39
|
|
|
$dbQuery = new DbQuery($this->db); |
40
|
|
|
$dbQuery |
41
|
|
|
->setFields(array('*'))// TODO - unused |
42
|
|
|
->setTable($mEntity->getTableName()) |
43
|
|
|
->setWhereArray($where) |
44
|
|
|
->setWhereArrayDanger($whereDanger); |
45
|
|
|
|
46
|
|
|
return $dbQuery; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets Iterator for fetching query results |
51
|
|
|
* |
52
|
|
|
* @param EntityModel $model |
53
|
|
|
* @param array $where |
54
|
|
|
* @param array $whereDanger |
55
|
|
|
* |
56
|
|
|
* @return DbResultIterator |
57
|
|
|
*/ |
58
|
|
|
public function loadIterator($model, $where, $whereDanger = array()) { |
59
|
|
|
$dbQuery = $this->buildSelectDbQuery($model, $where, $whereDanger); |
60
|
|
|
$strSql = $dbQuery->select(); |
61
|
|
|
|
62
|
|
|
return $this->doSelectIterator($strSql); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns iterator to iterate through mysqli_result |
67
|
|
|
* |
68
|
|
|
* @param string $strSql |
69
|
|
|
* |
70
|
|
|
* return DbResultIterator |
71
|
|
|
* |
72
|
|
|
* @return DbEmptyIterator|DbMysqliResultIterator |
73
|
|
|
*/ |
74
|
|
|
public function doSelectIterator($strSql) { |
75
|
|
|
$queryResult = $this->db->doSelect($strSql); |
76
|
|
|
|
77
|
|
|
if ($queryResult instanceof mysqli_result) { |
78
|
|
|
$result = new DbMysqliResultIterator($queryResult); |
79
|
|
|
} else { |
80
|
|
|
$result = new DbEmptyIterator(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $strSql |
88
|
|
|
* |
89
|
|
|
* @return array|null |
90
|
|
|
*/ |
91
|
|
|
public function doSelectFetchArray($strSql) { |
92
|
|
|
return $this->db->db_fetch($this->db->doSelect($strSql)); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $query |
98
|
|
|
* |
99
|
|
|
* @return mixed|null |
100
|
|
|
*/ |
101
|
|
|
public function doSelectFetchValue($query) { |
102
|
|
|
$row = $this->doSelectFetchArray($query); |
103
|
|
|
|
104
|
|
|
return is_array($row) ? reset($row) : null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Loads one record according to filter |
123
|
|
|
* |
124
|
|
|
* @param EntityModel $model |
125
|
|
|
* @param array $where |
126
|
|
|
* @param array $whereDanger |
127
|
|
|
* |
128
|
|
|
* @return array|null |
129
|
|
|
*/ |
130
|
|
|
public function loadRecord($model, $where, $whereDanger = array()) { |
131
|
|
|
$dbQuery = $this->buildSelectDbQuery($model, $where, $whereDanger); |
132
|
|
|
$dbQuery->setOneRow(DB_RECORD_ONE); |
133
|
|
|
$strSql = $dbQuery->select(); |
134
|
|
|
|
135
|
|
|
return $this->db->db_fetch($this->db->doSelect($strSql)); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Gets DB record array by dbId |
140
|
|
|
* |
141
|
|
|
* @param KeyedModel $model |
142
|
|
|
* @param int|string $dbId |
143
|
|
|
* |
144
|
|
|
* @return array|null |
145
|
|
|
*/ |
146
|
|
|
public function getById($model, $dbId) { |
147
|
|
|
return $this->loadRecord($model, array($model->getIdFieldName() => $dbId)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param KeyedModel $mKeyed |
153
|
|
|
* @param int|string $dbId |
154
|
|
|
* |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
public function deleteById($mKeyed, $dbId) { |
158
|
|
|
$db = $this->db; |
159
|
|
|
|
160
|
|
|
$db->doDeleteRow( |
161
|
|
|
$mKeyed->getTableName(), |
162
|
|
|
array( |
163
|
|
|
$mKeyed->getIdFieldName() => $dbId, |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
return $db->db_affected_rows(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param EntityModel $mEntity |
172
|
|
|
* @param array $row |
173
|
|
|
* |
174
|
|
|
* @return int|string |
175
|
|
|
*/ |
176
|
|
|
public function insert($mEntity, $row) { |
177
|
|
|
if (empty($row)) { |
178
|
|
|
// TODO Exception |
179
|
|
|
return 0; |
180
|
|
|
} |
181
|
|
|
$db = $this->db; |
182
|
|
|
$db->doInsertSet($mEntity->getTableName(), $row); |
183
|
|
|
|
184
|
|
|
// TODO Exception if db_insert_id() is empty |
185
|
|
|
return $db->db_insert_id(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
public function doUpdateRowSetAffected($table, $fieldsAndValues, $where) { |
190
|
|
|
$this->db->doUpdateRowSet($table, $fieldsAndValues, $where); |
191
|
|
|
|
192
|
|
|
return $this->db->db_affected_rows(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.