5
5
use Simply \Database \Connection \Provider \ConnectionProvider ;
6
6
7
7
/**
8
- * MySqlConnection .
8
+ * Provides the basic functionality to operate on a MySQL database .
9
9
* @author Riikka Kalliomäki <[email protected] >
10
10
* @copyright Copyright (c) 2018 Riikka Kalliomäki
11
11
* @license http://opensource.org/licenses/mit-license.php MIT License
12
12
*/
13
13
class MySqlConnection implements Connection
14
14
{
15
- /** @var ConnectionProvider */
15
+ /** @var ConnectionProvider The provider for the PDO connection */
16
16
private $ provider ;
17
17
18
+ /**
19
+ * MySqlConnection constructor.
20
+ * @param ConnectionProvider $provider The provider for the PDO connection
21
+ */
18
22
public function __construct (ConnectionProvider $ provider )
19
23
{
20
24
$ this ->provider = $ provider ;
@@ -25,7 +29,7 @@ public function getConnection(): \PDO
25
29
return $ this ->provider ->getConnection ();
26
30
}
27
31
28
- public function insert (string $ table , array $ values , string & $ primaryKey = null ): \PDOStatement
32
+ public function insert (string $ table , array $ values , & $ primaryKey = null ): \PDOStatement
29
33
{
30
34
$ parameters = [];
31
35
$ result = $ this ->query ($ this ->formatQuery ([
@@ -40,8 +44,13 @@ public function insert(string $table, array $values, string & $primaryKey = null
40
44
return $ result ;
41
45
}
42
46
43
- public function select (array $ fields , string $ table , array $ where , array $ orderBy = [], int $ limit = null ): \PDOStatement
44
- {
47
+ public function select (
48
+ array $ fields ,
49
+ string $ table ,
50
+ array $ where ,
51
+ array $ orderBy = [],
52
+ int $ limit = null
53
+ ): \PDOStatement {
45
54
$ parameters = [];
46
55
47
56
return $ this ->query ($ this ->formatQuery ([
@@ -113,6 +122,12 @@ public function formatTable(string $table, string $alias = ''): string
113
122
return $ this ->escapeIdentifier ($ table );
114
123
}
115
124
125
+ /**
126
+ * Formats the conditions for the SQL query.
127
+ * @param array $conditions The conditions for the SQL query
128
+ * @param array $parameters Array of parameters to fill
129
+ * @return string Formatted conditions for the query
130
+ */
116
131
private function formatConditions (array $ conditions , array & $ parameters ): string
117
132
{
118
133
if (!$ conditions ) {
@@ -129,10 +144,11 @@ private function formatConditions(array $conditions, array & $parameters): strin
129
144
}
130
145
131
146
/**
132
- * @param string $field
133
- * @param mixed $value
134
- * @param array $parameters
135
- * @return string
147
+ * Formats a single condition for the SQL query.
148
+ * @param string $field The name of the field
149
+ * @param mixed $value Expected value for the fields
150
+ * @param array $parameters Array of parameters to fill
151
+ * @return string The formatted condition for the query
136
152
*/
137
153
private function formatClause (string $ field , $ value , array & $ parameters ): string
138
154
{
@@ -164,12 +180,23 @@ private function formatClause(string $field, $value, array & $parameters): strin
164
180
return "$ escaped = ? " ;
165
181
}
166
182
183
+ /**
184
+ * Formats a list of parenthesis enclosed parameters for an SQL query.
185
+ * @param array $values List of values
186
+ * @param array $parameters Array of parameters to fill
187
+ * @return string The formatted parenthesis enclosed list of parameter placeholders
188
+ */
167
189
private function formatParameters (array $ values , array & $ parameters ): string
168
190
{
169
191
array_push ($ parameters , ... array_values ($ values ));
170
192
return sprintf ('(%s) ' , implode (', ' , array_fill (0 , \count ($ values ), '? ' )));
171
193
}
172
194
195
+ /**
196
+ * Formats the order clause for a select query.
197
+ * @param array $order The order for the select query
198
+ * @return string The formatted ORDER BY clause for the query
199
+ */
173
200
private function formatOrder (array $ order ): string
174
201
{
175
202
$ clauses = [];
@@ -181,6 +208,11 @@ private function formatOrder(array $order): string
181
208
return implode (', ' , $ clauses );
182
209
}
183
210
211
+ /**
212
+ * Returns the appropriate SQL sorting order for the sorting constant.
213
+ * @param int $order The sorting constant
214
+ * @return string The SQL representation for the given sorting order
215
+ */
184
216
private function formatDirection (int $ order ): string
185
217
{
186
218
if ($ order === self ::ORDER_ASCENDING ) {
@@ -194,6 +226,12 @@ private function formatDirection(int $order): string
194
226
throw new \InvalidArgumentException ('Invalid sorting direction ' );
195
227
}
196
228
229
+ /**
230
+ * Formats the LIMIT clause for a select query.
231
+ * @param int|null $limit The maximum number of rows to return or null for no limit
232
+ * @param array $parameters Array of parameters to fill
233
+ * @return string The formatted LIMIT clause for the select query
234
+ */
197
235
private function formatLimit (?int $ limit , array & $ parameters ): string
198
236
{
199
237
if ($ limit === null ) {
@@ -204,6 +242,12 @@ private function formatLimit(?int $limit, array & $parameters): string
204
242
return '? ' ;
205
243
}
206
244
245
+ /**
246
+ * Formats value assignments in UPDATE query.
247
+ * @param array $values Associate array of columns and values
248
+ * @param array $parameters Array of parameters to fill
249
+ * @return string The formatted list of assignments for the UPDATE query
250
+ */
207
251
private function formatAssignments (array $ values , array & $ parameters ): string
208
252
{
209
253
if (!$ values ) {
@@ -220,11 +264,21 @@ private function formatAssignments(array $values, array & $parameters): string
220
264
return implode (', ' , $ assignments );
221
265
}
222
266
267
+ /**
268
+ * Escapes a MySQL query identifier.
269
+ * @param string $identifier The identifier to escape
270
+ * @return string The escaped identifier
271
+ */
223
272
private function escapeIdentifier (string $ identifier ): string
224
273
{
225
274
return "` $ identifier` " ;
226
275
}
227
276
277
+ /**
278
+ * Formats an arbitrary SQL query based on given clauses.
279
+ * @param array $clauses Associative array of SQL clauses and their contents
280
+ * @return string The complete formatted SQL query
281
+ */
228
282
private function formatQuery (array $ clauses ): string
229
283
{
230
284
$ parts = [];
@@ -254,10 +308,11 @@ public function query(string $sql, array $parameters = []): \PDOStatement
254
308
}
255
309
256
310
/**
257
- * @param \PDOStatement $query
258
- * @param int|string $name
259
- * @param mixed $value
260
- * @return bool
311
+ * Binds an SQL query parameter to the PDO statement fo the query
312
+ * @param \PDOStatement $query The PDO statement for binding the value
313
+ * @param int|string $name The name of the placeholder
314
+ * @param mixed $value The value to bind
315
+ * @return bool True if the binding was successful, false if not
261
316
*/
262
317
private function bindQueryParameter (\PDOStatement $ query , $ name , $ value ): bool
263
318
{
0 commit comments