|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Contains query string build tools for data manipulation |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package SQL |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\sql; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Contains query string build tools for data manipulation |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package SQL |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
abstract class DML |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Generate sorting parts of queries |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $sort Sort by column names |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
**/ |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
private static function _sort(array $sort) |
|
40
|
|
|
{ |
|
41
|
|
|
$query = ' ORDER BY '; |
|
42
|
|
|
|
|
43
|
|
|
// Check if array dimensions are fine |
|
44
|
|
|
if (isset($sort[0]) && !is_array($sort[0])) { |
|
45
|
|
|
|
|
46
|
|
|
$sort = [$sort]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
foreach ($sort AS $order) { |
|
50
|
|
|
|
|
51
|
|
|
// Sorting order is ASC by default, so just add it for DESC |
|
52
|
|
|
if ($order[1] == true) { |
|
53
|
|
|
|
|
54
|
|
|
$query .= $order[0] . ' DESC, '; |
|
55
|
|
|
|
|
56
|
|
|
} else { |
|
57
|
|
|
|
|
58
|
|
|
$query .= $order[0] . ', '; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$query = substr($query, 0, -2); |
|
63
|
|
|
|
|
64
|
|
|
return $query; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Generate join parts of queries |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $joins Joins to other tables |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
**/ |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
private static function _joins(array $joins) |
|
76
|
|
|
{ |
|
77
|
|
|
$query = ''; |
|
78
|
|
|
|
|
79
|
|
|
// Check if array dimensions are fine |
|
80
|
|
|
if (isset($joins[0]) && !is_array($joins[0])) { |
|
81
|
|
|
|
|
82
|
|
|
$joins = [$joins]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
foreach ($joins AS $join) { |
|
86
|
|
|
|
|
87
|
|
|
// Empty fourth parameter means its same as third |
|
88
|
|
|
// Join 0=table, 1=foreign-table, 2=serial, 3=foreign-id |
|
89
|
|
|
if (!isset($join[3]) || $join[3] == '') { |
|
90
|
|
|
|
|
91
|
|
|
$join[3] = $join[2]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$query .= ' INNER JOIN {pre}' . $join[1] . ' ON ' |
|
95
|
|
|
. '{pre}' . $join[0] . '.' . $join[2] . ' = ' |
|
96
|
|
|
. '{pre}' . $join[1] . '.' . $join[3]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $query; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Selects database entries from tables |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $table Name of the database table |
|
106
|
|
|
* @param mixed $joins Joins to other tables as an array |
|
107
|
|
|
* @param mixed $columns Column names as string or array |
|
108
|
|
|
* @param array $conditions Conditions with column, operation and value |
|
109
|
|
|
* @param array $sort Sort by column names |
|
110
|
|
|
* @param array $group Group by column names as string or array |
|
111
|
|
|
* @param array $having Having clauses with conditions array structure |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
**/ |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
public static function select( |
|
117
|
|
|
$table, |
|
118
|
|
|
$joins = '', |
|
119
|
|
|
$columns = '*', |
|
120
|
|
|
array $conditions = [], |
|
121
|
|
|
array $sort = [], |
|
122
|
|
|
array $group = [], |
|
123
|
|
|
array $having = [] |
|
124
|
|
|
) { |
|
125
|
|
|
$assoc = []; |
|
126
|
|
|
|
|
127
|
|
|
// Add columns to query |
|
128
|
|
|
if (is_array($columns)) { |
|
129
|
|
|
|
|
130
|
|
|
$columns = implode(', ', $columns); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Add joins to query |
|
134
|
|
|
if (is_array($joins)) { |
|
135
|
|
|
|
|
136
|
|
|
$joins = self::_joins($joins); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// Build a matching select query |
|
140
|
|
|
$query = 'SELECT ' . $columns . ' FROM {pre}' . $table . $joins; |
|
141
|
|
|
|
|
142
|
|
|
// Add conditions to query |
|
143
|
|
|
if ($conditions != []) { |
|
144
|
|
|
|
|
145
|
|
|
$con = \csphere\core\sql\conditions::parse($conditions); |
|
146
|
|
|
|
|
147
|
|
|
$query .= ' WHERE ' . $con['query']; |
|
148
|
|
|
$assoc = $con['assoc']; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Add group to query |
|
152
|
|
|
if ($group != []) { |
|
153
|
|
|
|
|
154
|
|
|
$query .= ' GROUP BY ' . implode(', ', $group); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Add having to query |
|
158
|
|
|
if ($having != []) { |
|
159
|
|
|
|
|
160
|
|
|
$hav = \csphere\core\sql\conditions::parse($conditions, true); |
|
161
|
|
|
|
|
162
|
|
|
$query .= ' HAVING ' . $hav['query']; |
|
163
|
|
|
$assoc = array_merge($assoc, $hav['assoc']); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Add sorting to query |
|
167
|
|
|
if ($sort != []) { |
|
168
|
|
|
|
|
169
|
|
|
$query .= self::_sort($sort); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return ['statement' => $query, 'input' => $assoc]; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Removes database entries from a table |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $table Name of the database table |
|
179
|
|
|
* @param array $conditions Conditions with column, operation and value |
|
180
|
|
|
* @param mixed $joins Joins to other tables as an array |
|
181
|
|
|
* |
|
182
|
|
|
* @return array |
|
183
|
|
|
**/ |
|
184
|
|
|
public static function delete($table, array $conditions = [], $joins = '') |
|
185
|
|
|
{ |
|
186
|
|
|
$assoc = []; |
|
187
|
|
|
|
|
188
|
|
|
// Add joins to query |
|
189
|
|
|
if (is_array($joins)) { |
|
190
|
|
|
|
|
191
|
|
|
$joins = self::_joins($joins); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
// Build a matching delete query |
|
195
|
|
|
$query = 'DELETE FROM {pre}' . $table . $joins; |
|
196
|
|
|
|
|
197
|
|
|
// Add conditions to query |
|
198
|
|
|
if ($conditions != []) { |
|
199
|
|
|
|
|
200
|
|
|
$con = \csphere\core\sql\conditions::parse($conditions); |
|
201
|
|
|
|
|
202
|
|
|
$query .= ' WHERE ' . $con['query']; |
|
203
|
|
|
$assoc = $con['assoc']; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return ['statement' => $query, 'input' => $assoc]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Inserts new entries into a database table |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $table Name of the database table |
|
213
|
|
|
* @param array $assoc Array with columns and values |
|
214
|
|
|
* |
|
215
|
|
|
* @return array |
|
216
|
|
|
**/ |
|
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
public static function insert($table, array $assoc) |
|
219
|
|
|
{ |
|
220
|
|
|
// Build a matching insert query |
|
221
|
|
|
$insert = 'INSERT INTO {pre}' . $table . '('; |
|
222
|
|
|
$append = ') VALUES ('; |
|
223
|
|
|
|
|
224
|
|
|
foreach ($assoc AS $key => $value) { |
|
225
|
|
|
|
|
226
|
|
|
$insert .= $key . ', '; |
|
227
|
|
|
$append .= ':' . $key . ', '; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// Clear unused var |
|
231
|
|
|
unset($value); |
|
232
|
|
|
|
|
233
|
|
|
$query = substr($insert, 0, -2) . substr($append, 0, -2) . ')'; |
|
234
|
|
|
|
|
235
|
|
|
return ['statement' => $query, 'input' => $assoc]; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Updates entries inside a database table |
|
240
|
|
|
* |
|
241
|
|
|
* @param string $table Name of the database table |
|
242
|
|
|
* @param array $assoc Array with columns and values |
|
243
|
|
|
* @param string $where_column Name of the target database column |
|
244
|
|
|
* @param string $where_value Value of the target database column |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
**/ |
|
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
public static function update($table, array $assoc, $where_column, $where_value) |
|
250
|
|
|
{ |
|
251
|
|
|
// Build a matching update query |
|
252
|
|
|
$update = 'UPDATE {pre}' . $table . ' SET '; |
|
253
|
|
|
|
|
254
|
|
|
foreach ($assoc AS $key => $value) { |
|
255
|
|
|
|
|
256
|
|
|
$update .= $key . ' = :' . $key . ', '; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
// Clear unused var |
|
260
|
|
|
unset($value); |
|
261
|
|
|
|
|
262
|
|
|
$query = substr($update, 0, -2) |
|
263
|
|
|
. ' WHERE ' . $where_column . ' = :where_column'; |
|
264
|
|
|
|
|
265
|
|
|
$assoc['where_column'] = $where_value; |
|
266
|
|
|
|
|
267
|
|
|
return ['statement' => $query, 'input' => $assoc]; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|