Skip to content

Commit d2df3b6

Browse files
committed
Deprecate unnecessary getter methods
1 parent 45c0805 commit d2df3b6

20 files changed

Lines changed: 166 additions & 118 deletions

README.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ $peachySql = new PeachySQL\SqlServer($sqlSrvConn);
2727
```
2828

2929
After instantiation, arbitrary statements can be prepared by passing a
30-
SQL string and array of bound parameters to the `prepare` method:
30+
SQL string and array of bound parameters to the `prepare()` method:
3131

3232
```php
3333
$sql = "UPDATE Users SET fname = ? WHERE user_id = ?";
@@ -46,7 +46,7 @@ $stmt->close();
4646
```
4747

4848
Most of the time prepared statements only need to be executed a single time.
49-
To make this easier, PeachySQL provides a `query` method which automatically
49+
To make this easier, PeachySQL provides a `query()` method which automatically
5050
prepares, executes, and closes a statement after results are retrieved:
5151

5252
```php
@@ -55,20 +55,20 @@ $result = $peachySql->query($sql, ['theo%', 'b%']);
5555
echo json_encode($result->getAll());
5656
```
5757

58-
Both `prepare` and `query` return a `Statement` object with the following methods:
58+
Both `prepare()` and `query()` return a `Statement` object with the following methods:
5959

60-
| Method | Behavior |
61-
|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
62-
| `execute` | Executes the prepared statement (automatically called when using `query`). |
63-
| `getIterator` | Returns a [Generator](http://php.net/manual/en/language.generators.overview.php) object which can be used to iterate over large result sets without caching them in memory. |
64-
| `getAll` | Returns all selected rows as an array of associative arrays. |
65-
| `getFirst` | Returns the first selected row as an associative array (or `null` if no rows were selected). |
66-
| `getAffected` | Returns the number of rows affected by the query. |
67-
| `close` | Closes the prepared statement and frees its resources (automatically called when using `query`). |
60+
| Method | Behavior |
61+
|-----------------|------------------------------------------------------------------------------------------------------------------|
62+
| `execute()` | Executes the prepared statement (automatically called when using `query()`). |
63+
| `getIterator()` | Returns a `Generator` object which can be used to iterate over large result sets without caching them in memory. |
64+
| `getAll()` | Returns all selected rows as an array of associative arrays. |
65+
| `getFirst()` | Returns the first selected row as an associative array (or `null` if no rows were selected). |
66+
| `getAffected()` | Returns the number of rows affected by the query. |
67+
| `close()` | Closes the prepared statement and frees its resources (automatically called when using `query()`). |
6868

69-
If using MySQL, the `Mysql\Statement` object additionally includes a `getInsertId` method.
69+
If using MySQL, the `Mysql\Statement` object additionally includes a `getInsertId()` method.
7070

71-
Internally, `getAll` and `getFirst` are implemented using `getIterator`.
71+
Internally, `getAll()` and `getFirst()` are implemented using `getIterator()`.
7272
As such they can only be called once for a given statement.
7373

7474
### Shorthand methods
@@ -81,15 +81,15 @@ always use bound parameters for values, and column names are automatically escap
8181

8282
#### select / selectFrom
8383

84-
The `selectFrom` method takes a single string argument containing a SQL SELECT query.
84+
The `selectFrom()` method takes a single string argument containing a SQL SELECT query.
8585
It returns an object with three chainable methods:
8686

87-
1. `where`
88-
2. `orderBy`
89-
3. `offset`
87+
1. `where()`
88+
2. `orderBy()`
89+
3. `offset()`
9090

91-
Additionally, the object has a `getSqlParams` method which builds the select query,
92-
and a `query` method which executes the query and returns a `Statement` object.
91+
Additionally, the object has a `getSqlParams()` method which builds the select query,
92+
and a `query()` method which executes the query and returns a `Statement` object.
9393

9494
```php
9595
// select all columns and rows in a table, ordered by last name and then first name
@@ -105,8 +105,8 @@ $rows = $peachySql->selectFrom("SELECT * FROM Users u INNER JOIN Customers c ON
105105
->query()->getIterator();
106106
```
107107

108-
The `select` method works the same as `selectFrom`, but takes a `SqlParams` object
109-
rather than a string and supports bound params in the select query:
108+
The `select()` method works the same as `selectFrom()`, but takes a `SqlParams`
109+
object rather than a string and supports bound params in the select query:
110110

111111
```php
112112
use PeachySQL\QueryBuilder\SqlParams;
@@ -123,15 +123,16 @@ $sql = "
123123
FROM Users u
124124
INNER JOIN UserVisits uv ON uv.user_id = u.user_id";
125125

126-
$date = new DateTime('2 months ago');
127-
$rows = $peachySql->select(new SqlParams($sql, $date->format('Y-m-d')))
126+
$date = (new DateTime('2 months ago'))->format('Y-m-d');
127+
128+
$rows = $peachySql->select(new SqlParams($sql, [$date]))
128129
->where(['u.status' => 'verified'])
129130
->query()->getIterator();
130131
```
131132

132133
##### Where clause generation
133134

134-
In addition to passing basic column => value arrays to the `where` method, you can
135+
In addition to passing basic column => value arrays to the `where()` method, you can
135136
specify more complex conditions by using arrays as values. For example, passing
136137
`['col' => ['lt' => 15, 'gt' => 5]]` would generate the condition `WHERE col < 15 AND col > 5`.
137138

@@ -157,22 +158,22 @@ IN(...) or NOT IN(...) condition, respectively. Passing a list with the `lk`, `n
157158

158159
#### insertRow
159160

160-
The `insertRow` method allows a single row to be inserted from an associative array.
161-
It returns an `InsertResult` object with `getId` and `getAffected` methods.
161+
The `insertRow()` method allows a single row to be inserted from an associative array.
162+
It returns an `InsertResult` object with readonly `id` and `affected` properties.
162163

163164
```php
164165
$userData = [
165166
'fname' => 'Donald',
166167
'lname' => 'Chamberlin'
167168
];
168169

169-
$id = $peachySql->insertRow('Users', $userData)->getId();
170+
$id = $peachySql->insertRow('Users', $userData)->id;
170171
```
171172

172173
#### insertRows
173174

174-
The `insertRows` method makes it possible to bulk-insert multiple rows from an array.
175-
It returns a `BulkInsertResult` object with `getIds`, `getAffected`, and `getQueryCount` methods.
175+
The `insertRows()` method makes it possible to bulk-insert multiple rows from an array.
176+
It returns a `BulkInsertResult` object with readonly `ids`, `affected`, and `queryCount` properties.
176177

177178
```php
178179
$userData = [
@@ -191,32 +192,32 @@ $userData = [
191192
];
192193

193194
$result = $peachySql->insertRows('Users', $userData);
194-
$ids = $result->getIds(); // e.g. [64, 65, 66]
195-
$affected = $result->getAffected(); // 3
196-
$queries = $result->getQueryCount(); // 1
195+
$ids = $result->ids; // e.g. [64, 65, 66]
196+
$affected = $result->affected; // 3
197+
$queries = $result->queryCount; // 1
197198
```
198199

199-
An optional third parameter can be passed to `insertRows` to override the default
200+
An optional third parameter can be passed to `insertRows()` to override the default
200201
identity increment value:
201202

202203
```php
203204
$result = $peachySql->insertRows('Users', $userData, 2);
204-
$ids = $result->getIds(); // e.g. [64, 66, 68]
205+
$ids = $result->ids; // e.g. [64, 66, 68]
205206
```
206207

207208
Note: SQL Server allows a maximum of 1,000 rows to be inserted at a time, and limits
208209
individual queries to 2,099 or fewer bound parameters. MySQL supports a maximum of
209210
65,536 bound parameters per query. These limits can be easily reached when attempting
210211
to bulk-insert hundreds or thousands of rows at a time. To avoid these limits, the
211-
`insertRows` method automatically splits large queries into batches to efficiently
212-
handle any number of rows (`getQueryCount` returns the number of required batches).
212+
`insertRows()` method automatically splits large queries into batches to efficiently
213+
handle any number of rows (`queryCount` contains the number of required batches).
213214

214215
#### updateRows and deleteFrom
215216

216-
The `updateRows` method takes three arguments: a table name, an associative array of
217+
The `updateRows()` method takes three arguments: a table name, an associative array of
217218
columns/values to update, and a WHERE array to filter which rows are updated.
218219

219-
The `deleteFrom` method takes a table name and a WHERE array to filter the rows to delete.
220+
The `deleteFrom()` method takes a table name and a WHERE array to filter the rows to delete.
220221

221222
Both methods return the number of affected rows.
222223

@@ -231,14 +232,14 @@ $userTable->deleteFrom('Users', ['user_id' => [1, 2, 3]]);
231232

232233
### Transactions
233234

234-
Call the `begin` method to start a transaction. `prepare`, `execute`, `query`
235+
Call the `begin()` method to start a transaction. `prepare()`, `execute()`, `query()`
235236
and any of the shorthand methods can then be called as needed, before committing
236-
or rolling back the transaction with `commit` or `rollback`.
237+
or rolling back the transaction with `commit()` or `rollback()`.
237238

238239
## Author
239240

240241
Theodore Brown
241-
<http://theodorejb.me>
242+
<https://theodorejb.me>
242243

243244
## License
244245

lib/BaseStatement.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
abstract class BaseStatement
1111
{
1212
protected int $affected = 0;
13+
/**
14+
* True if the statement was created using `prepare()`
15+
*/
1316
protected bool $usedPrepare;
1417
protected string $query;
1518
protected array $params;
1619

17-
// $usedPrepare should be true if the statement was created using `prepare`
1820
public function __construct(bool $usedPrepare, string $query, array $params)
1921
{
2022
$this->usedPrepare = $usedPrepare;

lib/BulkInsertResult.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@
66

77
/**
88
* Object returned when performing bulk insert queries
9+
* @readonly
910
*/
1011
class BulkInsertResult
1112
{
12-
/** @var list<int> */
13-
private array $ids;
14-
private int $affected;
15-
private int $queryCount;
13+
/**
14+
* The IDs of the inserted rows
15+
* @var list<int>
16+
*/
17+
public array $ids;
18+
19+
/**
20+
* The number of affected rows
21+
*/
22+
public int $affected;
23+
24+
/**
25+
* The number of individual queries used to perform the bulk insert
26+
*/
27+
public int $queryCount;
1628

1729
/**
1830
* @param list<int> $ids
@@ -26,7 +38,9 @@ public function __construct(array $ids, int $affected, int $queryCount = 1)
2638

2739
/**
2840
* Returns the IDs of the inserted rows
41+
* @deprecated Use readonly property instead
2942
* @return list<int>
43+
* @api
3044
*/
3145
public function getIds(): array
3246
{
@@ -35,6 +49,8 @@ public function getIds(): array
3549

3650
/**
3751
* Returns the number of affected rows
52+
* @deprecated Use readonly property instead
53+
* @api
3854
*/
3955
public function getAffected(): int
4056
{
@@ -43,6 +59,8 @@ public function getAffected(): int
4359

4460
/**
4561
* Returns the number of individual queries used to perform the bulk insert
62+
* @deprecated Use readonly property instead
63+
* @api
4664
*/
4765
public function getQueryCount(): int
4866
{

lib/InsertResult.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66

77
/**
88
* Object returned when inserting a single row
9+
* @readonly
910
*/
1011
class InsertResult
1112
{
12-
private int $id;
13-
private int $affected;
13+
/**
14+
* The ID of the inserted row (0 if the row doesn't have an auto-incremented ID)
15+
*/
16+
public int $id;
17+
18+
/**
19+
* The number of affected rows
20+
*/
21+
public int $affected;
1422

1523
public function __construct(int $id, int $affected)
1624
{
@@ -21,6 +29,8 @@ public function __construct(int $id, int $affected)
2129
/**
2230
* Returns the ID of the inserted row
2331
* @throws \Exception if the row doesn't have an auto-incremented ID
32+
* @deprecated Use readonly property instead
33+
* @api
2434
*/
2535
public function getId(): int
2636
{
@@ -33,6 +43,8 @@ public function getId(): int
3343

3444
/**
3545
* Returns the number of affected rows
46+
* @deprecated Use readonly property instead
47+
* @api
3648
*/
3749
public function getAffected(): int
3850
{

lib/Mysql.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function query(string $sql, array $params = []): Statement
123123
protected function insertBatch(string $table, array $colVals, int $identityIncrement = 1): BulkInsertResult
124124
{
125125
$sqlParams = (new Insert($this->options))->buildQuery($table, $colVals);
126-
$result = $this->query($sqlParams->getSql(), $sqlParams->getParams());
126+
$result = $this->query($sqlParams->sql, $sqlParams->params);
127127
$firstId = $result->getInsertId(); // ID of first inserted row, or zero if no insert ID
128128

129129
if ($firstId) {
@@ -138,7 +138,7 @@ protected function insertBatch(string $table, array $colVals, int $identityIncre
138138

139139
/**
140140
* To bind parameters in mysqli, the type of each parameter must be specified.
141-
* See http://php.net/manual/en/mysqli-stmt.bind-param.php.
141+
* See https://www.php.net/manual/en/mysqli-stmt.bind-param.php.
142142
* Returns a string containing the type character for each parameter.
143143
*/
144144
private static function getMysqlParamTypes(array $params): string

lib/PeachySql.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ public function select(SqlParams $query): QueryableSelector
7272
public function insertRow(string $table, array $colVals): InsertResult
7373
{
7474
$result = $this->insertBatch($table, [$colVals]);
75-
$ids = $result->getIds();
76-
$id = empty($ids) ? 0 : $ids[0];
77-
return new InsertResult($id, $result->getAffected());
75+
$ids = $result->ids;
76+
return new InsertResult($ids ? $ids[0] : 0, $result->affected);
7877
}
7978

8079
/**
@@ -90,8 +89,8 @@ public function insertRows(string $table, array $colVals, int $identityIncrement
9089

9190
foreach ($batches as $batch) {
9291
$result = $this->insertBatch($table, $batch, $identityIncrement);
93-
$ids = array_merge($ids, $result->getIds());
94-
$affected += $result->getAffected();
92+
$ids = array_merge($ids, $result->ids);
93+
$affected += $result->affected;
9594
}
9695

9796
return new BulkInsertResult($ids, $affected, count($batches));
@@ -106,7 +105,7 @@ public function updateRows(string $table, array $set, array $where): int
106105
{
107106
$update = new Update($this->options);
108107
$sqlParams = $update->buildQuery($table, $set, $where);
109-
return $this->query($sqlParams->getSql(), $sqlParams->getParams())->getAffected();
108+
return $this->query($sqlParams->sql, $sqlParams->params)->getAffected();
110109
}
111110

112111
/**
@@ -118,6 +117,6 @@ public function deleteFrom(string $table, array $where): int
118117
{
119118
$delete = new Delete($this->options);
120119
$sqlParams = $delete->buildQuery($table, $where);
121-
return $this->query($sqlParams->getSql(), $sqlParams->getParams())->getAffected();
120+
return $this->query($sqlParams->sql, $sqlParams->params)->getAffected();
122121
}
123122
}

lib/QueryBuilder/Delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Delete extends Query
1717
public function buildQuery(string $table, array $where): SqlParams
1818
{
1919
$whereClause = $this->buildWhereClause($where);
20-
$sql = "DELETE FROM {$table}" . $whereClause->getSql();
21-
return new SqlParams($sql, $whereClause->getParams());
20+
$sql = "DELETE FROM {$table}" . $whereClause->sql;
21+
return new SqlParams($sql, $whereClause->params);
2222
}
2323
}

lib/QueryBuilder/Selector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getSqlParams(): SqlParams
8383
$select = new Select($this->options);
8484
$where = $select->buildWhereClause($this->where);
8585
$orderBy = $select->buildOrderByClause($this->orderBy);
86-
$sql = $this->query->getSql() . $where->getSql() . $orderBy;
86+
$sql = $this->query->sql . $where->sql . $orderBy;
8787

8888
if ($this->limit !== null && $this->offset !== null) {
8989
if ($this->orderBy === []) {
@@ -93,6 +93,6 @@ public function getSqlParams(): SqlParams
9393
$sql .= ' ' . $select->buildPagination($this->limit, $this->offset);
9494
}
9595

96-
return new SqlParams($sql, [...$this->query->getParams(), ...$where->getParams()]);
96+
return new SqlParams($sql, [...$this->query->params, ...$where->params]);
9797
}
9898
}

0 commit comments

Comments
 (0)