Skip to content

Commit 1cb7861

Browse files
committed
Improve documentation
1 parent b598f8b commit 1cb7861

25 files changed

+684
-109
lines changed

src/Connection/Connection.php

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,100 @@
33
namespace Simply\Database\Connection;
44

55
/**
6-
* Connection.
6+
* Interface for database connections that provide basic query functionality to an SQL database.
77
* @author Riikka Kalliomäki <[email protected]>
88
* @copyright Copyright (c) 2018 Riikka Kalliomäki
99
* @license http://opensource.org/licenses/mit-license.php MIT License
1010
*/
1111
interface Connection
1212
{
13+
/** Ascending sorting order for select queries. */
1314
public const ORDER_ASCENDING = 1;
15+
16+
/** Descending sorting order for select queries. */
1417
public const ORDER_DESCENDING = 2;
1518

19+
/**
20+
* Returns the underlying PDO connection to the database.
21+
* @return \PDO The underlying PDO connection to the database
22+
*/
1623
public function getConnection(): \PDO;
17-
public function insert(string $table, array $values, string & $primaryKey = null): \PDOStatement;
18-
public function select(array $fields, string $table, array $where, array $orderBy = [], int $limit = null): \PDOStatement;
24+
25+
/**
26+
* Inserts a row the database with given column values.
27+
* @param string $table Name of the table to insert
28+
* @param array $values Associative array of column values for the insert
29+
* @param string|null $primaryKey Name of automatic primary key column that will be overwritten with its value.
30+
* @return \PDOStatement The resulting PDO statement after executing the query
31+
*/
32+
public function insert(string $table, array $values, & $primaryKey = null): \PDOStatement;
33+
34+
/**
35+
* Selects rows from the database.
36+
*
37+
* The conditions provided to the query must be an associative array of columns and expected values. Any scalar
38+
* value and null are supported for equal comparison. If an array is provided as an value for the column, then
39+
* the value must by any of the provided values in the array, i.e. an "IN" SQL comparison. Null is also supported
40+
* as one of the values in the array.
41+
*
42+
* The sorting order provides list of columns to sort by in order of importance with the appropriate sorting
43+
* direction. Note that the limit argument is ignore if no sorting columns have been provided, as the order of
44+
* rows is undefined.
45+
*
46+
* @param string[] $fields List of fields to select from the database
47+
* @param string $table The name of the table to select
48+
* @param array $where Conditions for the select query
49+
* @param int[] $orderBy Associative array of columns to sort by and the sorting direction for each column
50+
* @param int|null $limit Maximum number of rows to return on sorted statement
51+
* @return \PDOStatement The resulting PDO statement after executing the query
52+
*/
53+
public function select(
54+
array $fields,
55+
string $table,
56+
array $where,
57+
array $orderBy = [],
58+
int $limit = null
59+
): \PDOStatement;
60+
61+
/**
62+
* Updates rows in the database.
63+
* @param string $table The table to update
64+
* @param array $values Associative array of columns and values to update
65+
* @param array $where Conditions for the updated row, similar to select queries
66+
* @return \PDOStatement The resulting PDO statement after executing the query
67+
*/
1968
public function update(string $table, array $values, array $where): \PDOStatement;
69+
70+
/**
71+
* Deletes rows from the database.
72+
* @param string $table The table where to delete rows
73+
* @param array $where Conditions for the delete query, similar to select query
74+
* @return \PDOStatement The resulting PDO statement after executing the query
75+
*/
2076
public function delete(string $table, array $where): \PDOStatement;
77+
78+
/**
79+
* Executes an arbitrary SQL query in the database.
80+
* @param string $sql The SQL query to execute
81+
* @param array $parameters Array of parameters to pass to the query
82+
* @return \PDOStatement The resulting PDO statement after executing the query
83+
*/
2184
public function query(string $sql, array $parameters = []): \PDOStatement;
85+
86+
/**
87+
* Returns an escaped table name aliased to the given alias.
88+
* @param string $table The name of the table to escape
89+
* @param string $alias The alias for the table or empty string for no alias
90+
* @return string The escaped table name for an SQL query
91+
*/
2292
public function formatTable(string $table, string $alias = ''): string;
93+
94+
/**
95+
* Returns a comma separated list of escaped field names from given table aliased with given prefix.
96+
* @param string[] $fields List of fields to escape
97+
* @param string $table The name or alias of the table for the fields
98+
* @param string $prefix Prefix to use for each field in the alias
99+
* @return string Comma separated list of escaped fields
100+
*/
23101
public function formatFields(array $fields, string $table = '', string $prefix = ''): string;
24102
}

src/Connection/MySqlConnection.php

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
use Simply\Database\Connection\Provider\ConnectionProvider;
66

77
/**
8-
* MySqlConnection.
8+
* Provides the basic functionality to operate on a MySQL database.
99
* @author Riikka Kalliomäki <[email protected]>
1010
* @copyright Copyright (c) 2018 Riikka Kalliomäki
1111
* @license http://opensource.org/licenses/mit-license.php MIT License
1212
*/
1313
class MySqlConnection implements Connection
1414
{
15-
/** @var ConnectionProvider */
15+
/** @var ConnectionProvider The provider for the PDO connection */
1616
private $provider;
1717

18+
/**
19+
* MySqlConnection constructor.
20+
* @param ConnectionProvider $provider The provider for the PDO connection
21+
*/
1822
public function __construct(ConnectionProvider $provider)
1923
{
2024
$this->provider = $provider;
@@ -25,7 +29,7 @@ public function getConnection(): \PDO
2529
return $this->provider->getConnection();
2630
}
2731

28-
public function insert(string $table, array $values, string & $primaryKey = null): \PDOStatement
32+
public function insert(string $table, array $values, & $primaryKey = null): \PDOStatement
2933
{
3034
$parameters = [];
3135
$result = $this->query($this->formatQuery([
@@ -40,8 +44,13 @@ public function insert(string $table, array $values, string & $primaryKey = null
4044
return $result;
4145
}
4246

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 {
4554
$parameters = [];
4655

4756
return $this->query($this->formatQuery([
@@ -113,6 +122,12 @@ public function formatTable(string $table, string $alias = ''): string
113122
return $this->escapeIdentifier($table);
114123
}
115124

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+
*/
116131
private function formatConditions(array $conditions, array & $parameters): string
117132
{
118133
if (!$conditions) {
@@ -129,10 +144,11 @@ private function formatConditions(array $conditions, array & $parameters): strin
129144
}
130145

131146
/**
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
136152
*/
137153
private function formatClause(string $field, $value, array & $parameters): string
138154
{
@@ -164,12 +180,23 @@ private function formatClause(string $field, $value, array & $parameters): strin
164180
return "$escaped = ?";
165181
}
166182

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+
*/
167189
private function formatParameters(array $values, array & $parameters): string
168190
{
169191
array_push($parameters, ... array_values($values));
170192
return sprintf('(%s)', implode(', ', array_fill(0, \count($values), '?')));
171193
}
172194

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+
*/
173200
private function formatOrder(array $order): string
174201
{
175202
$clauses = [];
@@ -181,6 +208,11 @@ private function formatOrder(array $order): string
181208
return implode(', ', $clauses);
182209
}
183210

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+
*/
184216
private function formatDirection(int $order): string
185217
{
186218
if ($order === self::ORDER_ASCENDING) {
@@ -194,6 +226,12 @@ private function formatDirection(int $order): string
194226
throw new \InvalidArgumentException('Invalid sorting direction');
195227
}
196228

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+
*/
197235
private function formatLimit(?int $limit, array & $parameters): string
198236
{
199237
if ($limit === null) {
@@ -204,6 +242,12 @@ private function formatLimit(?int $limit, array & $parameters): string
204242
return '?';
205243
}
206244

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+
*/
207251
private function formatAssignments(array $values, array & $parameters): string
208252
{
209253
if (!$values) {
@@ -220,11 +264,21 @@ private function formatAssignments(array $values, array & $parameters): string
220264
return implode(', ', $assignments);
221265
}
222266

267+
/**
268+
* Escapes a MySQL query identifier.
269+
* @param string $identifier The identifier to escape
270+
* @return string The escaped identifier
271+
*/
223272
private function escapeIdentifier(string $identifier): string
224273
{
225274
return "`$identifier`";
226275
}
227276

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+
*/
228282
private function formatQuery(array $clauses): string
229283
{
230284
$parts = [];
@@ -254,10 +308,11 @@ public function query(string $sql, array $parameters = []): \PDOStatement
254308
}
255309

256310
/**
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
261316
*/
262317
private function bindQueryParameter(\PDOStatement $query, $name, $value): bool
263318
{

src/Connection/Provider/ConnectionProvider.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
namespace Simply\Database\Connection\Provider;
44

55
/**
6-
* ConnectionProvider.
6+
* Interface for objects that can initialize and provide the PDO instance for database connectors.
77
* @author Riikka Kalliomäki <[email protected]>
88
* @copyright Copyright (c) 2018 Riikka Kalliomäki
99
* @license http://opensource.org/licenses/mit-license.php MIT License
1010
*/
1111
interface ConnectionProvider
1212
{
13+
/**
14+
* Provides the active PDO connection and initializes it if necessary.
15+
* @return \PDO An active PDO connection
16+
*/
1317
public function getConnection(): \PDO;
14-
}
18+
}

src/Connection/Provider/GenericConnectionProvider.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,35 @@
33
namespace Simply\Database\Connection\Provider;
44

55
/**
6-
* LazyConnectionProvider.
6+
* A generic lazy loading connector that can take any kind of PDO data source name.
77
* @author Riikka Kalliomäki <[email protected]>
88
* @copyright Copyright (c) 2018 Riikka Kalliomäki
99
* @license http://opensource.org/licenses/mit-license.php MIT License
1010
*/
1111
class GenericConnectionProvider implements ConnectionProvider
1212
{
13+
/** @var string The data source name for the PDO connection */
1314
private $dsn;
15+
16+
/** @var string The username for the PDO connection */
1417
private $username;
18+
19+
/** @var string The password for the PDO connection */
1520
private $password;
21+
22+
/** @var array Initial PDO options provided when initializing the connection */
1623
private $options;
24+
25+
/** @var \PDO The active PDO connection */
1726
private $pdo;
1827

28+
/**
29+
* GenericConnectionProvider constructor.
30+
* @param string $dsn The data source name for the PDO connection
31+
* @param string $username The username for the PDO connection
32+
* @param string $password The password for the PDO connection
33+
* @param array $options Initial PDO options provided when initializing the connection
34+
*/
1935
public function __construct(string $dsn, string $username, string $password, array $options)
2036
{
2137
$this->dsn = $dsn;
@@ -26,13 +42,17 @@ public function __construct(string $dsn, string $username, string $password, arr
2642

2743
public function getConnection(): \PDO
2844
{
29-
if (empty($this->pdo)) {
45+
if (!isset($this->pdo)) {
3046
$this->pdo = $this->initializeConnection();
3147
}
3248

3349
return $this->pdo;
3450
}
3551

52+
/**
53+
* Initializes the PDO connection when first requested.
54+
* @return \PDO The initialized PDO connection.
55+
*/
3656
protected function initializeConnection(): \PDO
3757
{
3858
return new \PDO($this->dsn, $this->username, $this->password, $this->options);

0 commit comments

Comments
 (0)