1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Database\Drivers; |
4
|
|
|
|
5
|
|
|
use DrMVC\Database\SQLException; |
6
|
|
|
|
7
|
|
|
abstract class SQL extends Driver |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Initiate connection to database |
11
|
|
|
* |
12
|
|
|
* @return DriverInterface |
13
|
|
|
*/ |
14
|
|
|
public function connect(): DriverInterface |
15
|
|
|
{ |
16
|
|
|
try { |
17
|
|
|
$connection = new \PDO( |
18
|
|
|
$this->getDsn(), |
19
|
|
|
$this->getParam('username'), |
20
|
|
|
$this->getParam('password') |
21
|
|
|
); |
22
|
|
|
$this->setConnection($connection); |
23
|
|
|
|
24
|
|
|
} catch (SQLException $e) { |
25
|
|
|
// __construct |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Run a select statement against the database |
33
|
|
|
* |
34
|
|
|
* @param string $query |
35
|
|
|
* @param array $data |
36
|
|
|
* @return array|object |
37
|
|
|
*/ |
38
|
|
|
public function select(string $query, array $data = []) |
39
|
|
|
{ |
40
|
|
|
// Set statement |
41
|
|
|
$statement = $this->getConnection()->prepare($query); |
42
|
|
|
|
43
|
|
|
// Parse parameters from array |
44
|
|
|
foreach ($data as $key => $value) { |
45
|
|
|
$value_type = \is_int($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR; |
46
|
|
|
$statement->bindValue($key, $value, $value_type); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Execute the statement |
50
|
|
|
$statement->execute(); |
51
|
|
|
|
52
|
|
|
// Return result |
53
|
|
|
return $statement->fetchAll(\PDO::FETCH_OBJ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Exec query without return, create table for example |
58
|
|
|
* |
59
|
|
|
* @param string $query |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function exec(string $query) |
63
|
|
|
{ |
64
|
|
|
return $this->getConnection()->exec($query); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Generate INSERT query by array |
69
|
|
|
* |
70
|
|
|
* @param array $data |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
private function genInsert(array $data): string |
74
|
|
|
{ |
75
|
|
|
// Current table |
76
|
|
|
$table = $this->getCollection(); |
77
|
|
|
|
78
|
|
|
// Generate array of fields for insert |
79
|
|
|
$fieldNames = implode(',', array_keys($data)); |
80
|
|
|
|
81
|
|
|
// Generate line with data for update |
82
|
|
|
$fieldDetails = ':' . implode(', :', array_keys($data)); |
83
|
|
|
|
84
|
|
|
return "INSERT INTO $table ($fieldNames) VALUES ($fieldDetails)"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Insert in database and return of inserted element |
89
|
|
|
* |
90
|
|
|
* @param array $data array of columns and values |
91
|
|
|
* @param string $id field name of ID which must be returned |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function insert(array $data, string $id = null) |
95
|
|
|
{ |
96
|
|
|
// Prepare query |
97
|
|
|
$query = $this->genInsert($data); |
98
|
|
|
$statement = $this->_connection->prepare($query); |
99
|
|
|
|
100
|
|
|
// Bind values |
101
|
|
|
foreach ($data as $key => $value) { |
102
|
|
|
$statement->bindValue(":$key", $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Execute operation |
106
|
|
|
$statement->execute(); |
107
|
|
|
|
108
|
|
|
return (null !== $id) |
109
|
|
|
// Return ID of inserted element |
110
|
|
|
? $this->getConnection()->lastInsertId($id) |
111
|
|
|
// Return count of affected rows |
112
|
|
|
: $statement->rowCount(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Generate the line by provided keys |
117
|
|
|
* |
118
|
|
|
* @param array $array array with data |
119
|
|
|
* @param string $glue by this glue need merge items |
120
|
|
|
* @param string $name name of field for PDO->bindValue |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
private function genLine(array $array, string $glue, string $name): string |
124
|
|
|
{ |
125
|
|
|
$line = ''; |
126
|
|
|
$i = 0; |
127
|
|
|
foreach ($array as $key => $value) { |
128
|
|
|
$line .= (($i !== 0) ? null : $glue) . "$key = :${name}_$key"; |
129
|
|
|
$i = 1; |
130
|
|
|
} |
131
|
|
|
return $line; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Generate set of fields |
136
|
|
|
* |
137
|
|
|
* @param array $array |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
private function genFields(array $array): string |
141
|
|
|
{ |
142
|
|
|
return $this->genLine($array, ', ', 'field'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generate WHERE line |
147
|
|
|
* |
148
|
|
|
* @param array $array |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
private function genWhere(array $array): string |
152
|
|
|
{ |
153
|
|
|
return $this->genLine($array, ' AND ', 'where'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Generate update query |
158
|
|
|
* |
159
|
|
|
* @param array $data |
160
|
|
|
* @param array $where |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
private function genUpdate(array $data, array $where): string |
164
|
|
|
{ |
165
|
|
|
// Current table |
166
|
|
|
$table = $this->getConnection(); |
167
|
|
|
|
168
|
|
|
// Generate line with data for update |
169
|
|
|
$fieldDetails = !empty($data) |
170
|
|
|
? $this->genFields($data) |
171
|
|
|
: ''; |
172
|
|
|
|
173
|
|
|
// Generate where line |
174
|
|
|
$whereDetails = !empty($where) |
175
|
|
|
? ' WHERE ' . $this->genWhere($where) |
176
|
|
|
: ''; |
177
|
|
|
|
178
|
|
|
return "UPDATE $table SET $fieldDetails $whereDetails"; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Update method |
183
|
|
|
* |
184
|
|
|
* @param array $data array of columns and values |
185
|
|
|
* @param array $where array of columns and values |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
public function update(array $data, array $where) |
189
|
|
|
{ |
190
|
|
|
// Prepare query |
191
|
|
|
$query = $this->genUpdate($data, $where); |
192
|
|
|
$statement = $this->getConnection()->prepare($query); |
193
|
|
|
|
194
|
|
|
// Bind field values |
195
|
|
|
foreach ($data as $key => $value) { |
196
|
|
|
$statement->bindValue(":field_$key", $value); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Bind where values |
200
|
|
|
foreach ($where as $key => $value) { |
201
|
|
|
$statement->bindValue(":where_$key", $value); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Execute operation |
205
|
|
|
$statement->execute(); |
206
|
|
|
|
207
|
|
|
// Return count of affected rows |
208
|
|
|
return $statement->rowCount(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Delete rows from database |
213
|
|
|
* |
214
|
|
|
* @param array $where |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
public function delete(array $where) |
218
|
|
|
{ |
219
|
|
|
// Current table |
220
|
|
|
$table = $this->getConnection(); |
221
|
|
|
|
222
|
|
|
// Generate where line |
223
|
|
|
$whereDetails = !empty($where) |
224
|
|
|
? ' WHERE ' . $this->genWhere($where) |
225
|
|
|
: ''; |
226
|
|
|
|
227
|
|
|
// Prepare query |
228
|
|
|
$statement = $this->getConnection()->prepare("DELETE FROM $table $whereDetails"); |
229
|
|
|
|
230
|
|
|
// Bind where values |
231
|
|
|
foreach ($where as $key => $value) { |
232
|
|
|
$statement->bindValue(":where_$key", $value); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// Execute operation |
236
|
|
|
$statement->execute(); |
237
|
|
|
|
238
|
|
|
// Return count of affected rows |
239
|
|
|
return $statement->rowCount(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Clean table function |
244
|
|
|
* |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
public function truncate() |
248
|
|
|
{ |
249
|
|
|
// Current table |
250
|
|
|
$table = $this->getConnection(); |
251
|
|
|
|
252
|
|
|
// Exec the truncate command |
253
|
|
|
return $this->exec("TRUNCATE TABLE $table"); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|