From 22f683b5008423a3aad907453c26593fe56a2016 Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Sat, 5 Oct 2024 20:33:06 -0500 Subject: [PATCH] Deprecate unnecessary getter methods --- CHANGELOG.md | 6 ++- README.md | 83 +++++++++++++++--------------- lib/BaseStatement.php | 4 +- lib/BulkInsertResult.php | 26 ++++++++-- lib/InsertResult.php | 16 +++++- lib/Mysql.php | 4 +- lib/PeachySql.php | 13 +++-- lib/QueryBuilder/Delete.php | 4 +- lib/QueryBuilder/Selector.php | 4 +- lib/QueryBuilder/SqlParams.php | 17 ++++-- lib/QueryBuilder/Update.php | 4 +- lib/QueryableSelector.php | 2 +- lib/SqlException.php | 27 ++++++++-- lib/SqlServer.php | 2 +- test/BulkInsertResultTest.php | 6 +-- test/DbTest.php | 26 +++++----- test/InsertResultTest.php | 12 +---- test/QueryBuilder/DeleteTest.php | 4 +- test/QueryBuilder/InsertTest.php | 8 +-- test/QueryBuilder/SelectorTest.php | 18 +++---- test/QueryBuilder/UpdateTest.php | 4 +- 21 files changed, 170 insertions(+), 120 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72110f6..5d10988 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [6.3.0] - 2024-10-05 +### Deprecated +- Unnecessary getter methods (in `InsertResult`, `BulkInsertResult`, `SqlParams`, and `SqlException`). ## [6.2.0] - 2022-03-14 ### Added @@ -291,7 +293,7 @@ insert a single row. ## [1.0.0] - 2014-02-20 - Initial release -[Unreleased]: https://github.com/theodorejb/peachy-sql/compare/v6.2.0...HEAD +[6.3.0]: https://github.com/theodorejb/peachy-sql/compare/v6.2.0...v6.3.0 [6.2.0]: https://github.com/theodorejb/peachy-sql/compare/v6.1.0...v6.2.0 [6.1.0]: https://github.com/theodorejb/peachy-sql/compare/v6.0.3...v6.1.0 [6.0.3]: https://github.com/theodorejb/peachy-sql/compare/v6.0.2...v6.0.3 diff --git a/README.md b/README.md index a01f5eb..b5b6406 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ $peachySql = new PeachySQL\SqlServer($sqlSrvConn); ``` After instantiation, arbitrary statements can be prepared by passing a -SQL string and array of bound parameters to the `prepare` method: +SQL string and array of bound parameters to the `prepare()` method: ```php $sql = "UPDATE Users SET fname = ? WHERE user_id = ?"; @@ -46,7 +46,7 @@ $stmt->close(); ``` Most of the time prepared statements only need to be executed a single time. -To make this easier, PeachySQL provides a `query` method which automatically +To make this easier, PeachySQL provides a `query()` method which automatically prepares, executes, and closes a statement after results are retrieved: ```php @@ -55,20 +55,20 @@ $result = $peachySql->query($sql, ['theo%', 'b%']); echo json_encode($result->getAll()); ``` -Both `prepare` and `query` return a `Statement` object with the following methods: +Both `prepare()` and `query()` return a `Statement` object with the following methods: -| Method | Behavior | -|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `execute` | Executes the prepared statement (automatically called when using `query`). | -| `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. | -| `getAll` | Returns all selected rows as an array of associative arrays. | -| `getFirst` | Returns the first selected row as an associative array (or `null` if no rows were selected). | -| `getAffected` | Returns the number of rows affected by the query. | -| `close` | Closes the prepared statement and frees its resources (automatically called when using `query`). | +| Method | Behavior | +|-----------------|------------------------------------------------------------------------------------------------------------------| +| `execute()` | Executes the prepared statement (automatically called when using `query()`). | +| `getIterator()` | Returns a `Generator` object which can be used to iterate over large result sets without caching them in memory. | +| `getAll()` | Returns all selected rows as an array of associative arrays. | +| `getFirst()` | Returns the first selected row as an associative array (or `null` if no rows were selected). | +| `getAffected()` | Returns the number of rows affected by the query. | +| `close()` | Closes the prepared statement and frees its resources (automatically called when using `query()`). | -If using MySQL, the `Mysql\Statement` object additionally includes a `getInsertId` method. +If using MySQL, the `Mysql\Statement` object additionally includes a `getInsertId()` method. -Internally, `getAll` and `getFirst` are implemented using `getIterator`. +Internally, `getAll()` and `getFirst()` are implemented using `getIterator()`. As such they can only be called once for a given statement. ### Shorthand methods @@ -81,15 +81,15 @@ always use bound parameters for values, and column names are automatically escap #### select / selectFrom -The `selectFrom` method takes a single string argument containing a SQL SELECT query. +The `selectFrom()` method takes a single string argument containing a SQL SELECT query. It returns an object with three chainable methods: -1. `where` -2. `orderBy` -3. `offset` +1. `where()` +2. `orderBy()` +3. `offset()` -Additionally, the object has a `getSqlParams` method which builds the select query, -and a `query` method which executes the query and returns a `Statement` object. +Additionally, the object has a `getSqlParams()` method which builds the select query, +and a `query()` method which executes the query and returns a `Statement` object. ```php // 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 ->query()->getIterator(); ``` -The `select` method works the same as `selectFrom`, but takes a `SqlParams` object -rather than a string and supports bound params in the select query: +The `select()` method works the same as `selectFrom()`, but takes a `SqlParams` +object rather than a string and supports bound params in the select query: ```php use PeachySQL\QueryBuilder\SqlParams; @@ -123,15 +123,16 @@ $sql = " FROM Users u INNER JOIN UserVisits uv ON uv.user_id = u.user_id"; -$date = new DateTime('2 months ago'); -$rows = $peachySql->select(new SqlParams($sql, $date->format('Y-m-d'))) +$date = (new DateTime('2 months ago'))->format('Y-m-d'); + +$rows = $peachySql->select(new SqlParams($sql, [$date])) ->where(['u.status' => 'verified']) ->query()->getIterator(); ``` ##### Where clause generation -In addition to passing basic column => value arrays to the `where` method, you can +In addition to passing basic column => value arrays to the `where()` method, you can specify more complex conditions by using arrays as values. For example, passing `['col' => ['lt' => 15, 'gt' => 5]]` would generate the condition `WHERE col < 15 AND col > 5`. @@ -157,8 +158,8 @@ IN(...) or NOT IN(...) condition, respectively. Passing a list with the `lk`, `n #### insertRow -The `insertRow` method allows a single row to be inserted from an associative array. -It returns an `InsertResult` object with `getId` and `getAffected` methods. +The `insertRow()` method allows a single row to be inserted from an associative array. +It returns an `InsertResult` object with readonly `id` and `affected` properties. ```php $userData = [ @@ -166,13 +167,13 @@ $userData = [ 'lname' => 'Chamberlin' ]; -$id = $peachySql->insertRow('Users', $userData)->getId(); +$id = $peachySql->insertRow('Users', $userData)->id; ``` #### insertRows -The `insertRows` method makes it possible to bulk-insert multiple rows from an array. -It returns a `BulkInsertResult` object with `getIds`, `getAffected`, and `getQueryCount` methods. +The `insertRows()` method makes it possible to bulk-insert multiple rows from an array. +It returns a `BulkInsertResult` object with readonly `ids`, `affected`, and `queryCount` properties. ```php $userData = [ @@ -191,32 +192,32 @@ $userData = [ ]; $result = $peachySql->insertRows('Users', $userData); -$ids = $result->getIds(); // e.g. [64, 65, 66] -$affected = $result->getAffected(); // 3 -$queries = $result->getQueryCount(); // 1 +$ids = $result->ids; // e.g. [64, 65, 66] +$affected = $result->affected; // 3 +$queries = $result->queryCount; // 1 ``` -An optional third parameter can be passed to `insertRows` to override the default +An optional third parameter can be passed to `insertRows()` to override the default identity increment value: ```php $result = $peachySql->insertRows('Users', $userData, 2); -$ids = $result->getIds(); // e.g. [64, 66, 68] +$ids = $result->ids; // e.g. [64, 66, 68] ``` Note: SQL Server allows a maximum of 1,000 rows to be inserted at a time, and limits individual queries to 2,099 or fewer bound parameters. MySQL supports a maximum of 65,536 bound parameters per query. These limits can be easily reached when attempting to bulk-insert hundreds or thousands of rows at a time. To avoid these limits, the -`insertRows` method automatically splits large queries into batches to efficiently -handle any number of rows (`getQueryCount` returns the number of required batches). +`insertRows()` method automatically splits large queries into batches to efficiently +handle any number of rows (`queryCount` contains the number of required batches). #### updateRows and deleteFrom -The `updateRows` method takes three arguments: a table name, an associative array of +The `updateRows()` method takes three arguments: a table name, an associative array of columns/values to update, and a WHERE array to filter which rows are updated. -The `deleteFrom` method takes a table name and a WHERE array to filter the rows to delete. +The `deleteFrom()` method takes a table name and a WHERE array to filter the rows to delete. Both methods return the number of affected rows. @@ -231,14 +232,14 @@ $userTable->deleteFrom('Users', ['user_id' => [1, 2, 3]]); ### Transactions -Call the `begin` method to start a transaction. `prepare`, `execute`, `query` +Call the `begin()` method to start a transaction. `prepare()`, `execute()`, `query()` and any of the shorthand methods can then be called as needed, before committing -or rolling back the transaction with `commit` or `rollback`. +or rolling back the transaction with `commit()` or `rollback()`. ## Author Theodore Brown - + ## License diff --git a/lib/BaseStatement.php b/lib/BaseStatement.php index 887ba09..abf5d2e 100644 --- a/lib/BaseStatement.php +++ b/lib/BaseStatement.php @@ -10,11 +10,13 @@ abstract class BaseStatement { protected int $affected = 0; + /** + * True if the statement was created using `prepare()` + */ protected bool $usedPrepare; protected string $query; protected array $params; - // $usedPrepare should be true if the statement was created using `prepare` public function __construct(bool $usedPrepare, string $query, array $params) { $this->usedPrepare = $usedPrepare; diff --git a/lib/BulkInsertResult.php b/lib/BulkInsertResult.php index a6e765f..549745a 100644 --- a/lib/BulkInsertResult.php +++ b/lib/BulkInsertResult.php @@ -6,13 +6,25 @@ /** * Object returned when performing bulk insert queries + * @readonly */ class BulkInsertResult { - /** @var list */ - private array $ids; - private int $affected; - private int $queryCount; + /** + * The IDs of the inserted rows + * @var list + */ + public array $ids; + + /** + * The number of affected rows + */ + public int $affected; + + /** + * The number of individual queries used to perform the bulk insert + */ + public int $queryCount; /** * @param list $ids @@ -26,7 +38,9 @@ public function __construct(array $ids, int $affected, int $queryCount = 1) /** * Returns the IDs of the inserted rows + * @deprecated Use readonly property instead * @return list + * @api */ public function getIds(): array { @@ -35,6 +49,8 @@ public function getIds(): array /** * Returns the number of affected rows + * @deprecated Use readonly property instead + * @api */ public function getAffected(): int { @@ -43,6 +59,8 @@ public function getAffected(): int /** * Returns the number of individual queries used to perform the bulk insert + * @deprecated Use readonly property instead + * @api */ public function getQueryCount(): int { diff --git a/lib/InsertResult.php b/lib/InsertResult.php index 5b4d45b..554c476 100644 --- a/lib/InsertResult.php +++ b/lib/InsertResult.php @@ -6,11 +6,19 @@ /** * Object returned when inserting a single row + * @readonly */ class InsertResult { - private int $id; - private int $affected; + /** + * The ID of the inserted row (0 if the row doesn't have an auto-incremented ID) + */ + public int $id; + + /** + * The number of affected rows + */ + public int $affected; public function __construct(int $id, int $affected) { @@ -21,6 +29,8 @@ public function __construct(int $id, int $affected) /** * Returns the ID of the inserted row * @throws \Exception if the row doesn't have an auto-incremented ID + * @deprecated Use readonly property instead + * @api */ public function getId(): int { @@ -33,6 +43,8 @@ public function getId(): int /** * Returns the number of affected rows + * @deprecated Use readonly property instead + * @api */ public function getAffected(): int { diff --git a/lib/Mysql.php b/lib/Mysql.php index e07f551..b540423 100644 --- a/lib/Mysql.php +++ b/lib/Mysql.php @@ -123,7 +123,7 @@ public function query(string $sql, array $params = []): Statement protected function insertBatch(string $table, array $colVals, int $identityIncrement = 1): BulkInsertResult { $sqlParams = (new Insert($this->options))->buildQuery($table, $colVals); - $result = $this->query($sqlParams->getSql(), $sqlParams->getParams()); + $result = $this->query($sqlParams->sql, $sqlParams->params); $firstId = $result->getInsertId(); // ID of first inserted row, or zero if no insert ID if ($firstId) { @@ -138,7 +138,7 @@ protected function insertBatch(string $table, array $colVals, int $identityIncre /** * To bind parameters in mysqli, the type of each parameter must be specified. - * See http://php.net/manual/en/mysqli-stmt.bind-param.php. + * See https://www.php.net/manual/en/mysqli-stmt.bind-param.php. * Returns a string containing the type character for each parameter. */ private static function getMysqlParamTypes(array $params): string diff --git a/lib/PeachySql.php b/lib/PeachySql.php index 1d17515..6042fbe 100644 --- a/lib/PeachySql.php +++ b/lib/PeachySql.php @@ -72,9 +72,8 @@ public function select(SqlParams $query): QueryableSelector public function insertRow(string $table, array $colVals): InsertResult { $result = $this->insertBatch($table, [$colVals]); - $ids = $result->getIds(); - $id = empty($ids) ? 0 : $ids[0]; - return new InsertResult($id, $result->getAffected()); + $ids = $result->ids; + return new InsertResult($ids ? $ids[0] : 0, $result->affected); } /** @@ -90,8 +89,8 @@ public function insertRows(string $table, array $colVals, int $identityIncrement foreach ($batches as $batch) { $result = $this->insertBatch($table, $batch, $identityIncrement); - $ids = array_merge($ids, $result->getIds()); - $affected += $result->getAffected(); + $ids = array_merge($ids, $result->ids); + $affected += $result->affected; } return new BulkInsertResult($ids, $affected, count($batches)); @@ -106,7 +105,7 @@ public function updateRows(string $table, array $set, array $where): int { $update = new Update($this->options); $sqlParams = $update->buildQuery($table, $set, $where); - return $this->query($sqlParams->getSql(), $sqlParams->getParams())->getAffected(); + return $this->query($sqlParams->sql, $sqlParams->params)->getAffected(); } /** @@ -118,6 +117,6 @@ public function deleteFrom(string $table, array $where): int { $delete = new Delete($this->options); $sqlParams = $delete->buildQuery($table, $where); - return $this->query($sqlParams->getSql(), $sqlParams->getParams())->getAffected(); + return $this->query($sqlParams->sql, $sqlParams->params)->getAffected(); } } diff --git a/lib/QueryBuilder/Delete.php b/lib/QueryBuilder/Delete.php index ed33b1c..59a7ec3 100644 --- a/lib/QueryBuilder/Delete.php +++ b/lib/QueryBuilder/Delete.php @@ -17,7 +17,7 @@ class Delete extends Query public function buildQuery(string $table, array $where): SqlParams { $whereClause = $this->buildWhereClause($where); - $sql = "DELETE FROM {$table}" . $whereClause->getSql(); - return new SqlParams($sql, $whereClause->getParams()); + $sql = "DELETE FROM {$table}" . $whereClause->sql; + return new SqlParams($sql, $whereClause->params); } } diff --git a/lib/QueryBuilder/Selector.php b/lib/QueryBuilder/Selector.php index 9a88340..679887f 100644 --- a/lib/QueryBuilder/Selector.php +++ b/lib/QueryBuilder/Selector.php @@ -83,7 +83,7 @@ public function getSqlParams(): SqlParams $select = new Select($this->options); $where = $select->buildWhereClause($this->where); $orderBy = $select->buildOrderByClause($this->orderBy); - $sql = $this->query->getSql() . $where->getSql() . $orderBy; + $sql = $this->query->sql . $where->sql . $orderBy; if ($this->limit !== null && $this->offset !== null) { if ($this->orderBy === []) { @@ -93,6 +93,6 @@ public function getSqlParams(): SqlParams $sql .= ' ' . $select->buildPagination($this->limit, $this->offset); } - return new SqlParams($sql, [...$this->query->getParams(), ...$where->getParams()]); + return new SqlParams($sql, [...$this->query->params, ...$where->params]); } } diff --git a/lib/QueryBuilder/SqlParams.php b/lib/QueryBuilder/SqlParams.php index 29215ec..85d7a10 100644 --- a/lib/QueryBuilder/SqlParams.php +++ b/lib/QueryBuilder/SqlParams.php @@ -6,15 +6,16 @@ /** * Represents a SQL query and its corresponding parameters + * @readonly */ class SqlParams { - private string $sql; - /** @var list */ - private array $params; + public string $sql; + /** @var list */ + public array $params; /** - * @param list $params + * @param list $params */ public function __construct(string $sql, array $params) { @@ -22,13 +23,19 @@ public function __construct(string $sql, array $params) $this->params = $params; } + /** + * @deprecated Use readonly property instead + * @api + */ public function getSql(): string { return $this->sql; } /** - * @return list + * @return list + * @deprecated Use readonly property instead + * @api */ public function getParams(): array { diff --git a/lib/QueryBuilder/Update.php b/lib/QueryBuilder/Update.php index 0db909d..2b86224 100644 --- a/lib/QueryBuilder/Update.php +++ b/lib/QueryBuilder/Update.php @@ -33,8 +33,8 @@ public function buildQuery(string $table, array $set, array $where): SqlParams $sql = substr_replace($sql, '', -2); // remove trailing comma $whereClause = $this->buildWhereClause($where); - $sql .= $whereClause->getSql(); + $sql .= $whereClause->sql; - return new SqlParams($sql, array_merge($params, $whereClause->getParams())); + return new SqlParams($sql, array_merge($params, $whereClause->params)); } } diff --git a/lib/QueryableSelector.php b/lib/QueryableSelector.php index 72b26c4..d3856fe 100644 --- a/lib/QueryableSelector.php +++ b/lib/QueryableSelector.php @@ -19,6 +19,6 @@ public function __construct(SqlParams $query, PeachySql $peachySql) public function query(): BaseStatement { $sqlParams = $this->getSqlParams(); - return $this->peachySql->query($sqlParams->getSql(), $sqlParams->getParams()); + return $this->peachySql->query($sqlParams->sql, $sqlParams->params); } } diff --git a/lib/SqlException.php b/lib/SqlException.php index 1ee7e26..9af9de6 100644 --- a/lib/SqlException.php +++ b/lib/SqlException.php @@ -5,14 +5,27 @@ namespace PeachySQL; /** - * Has methods to retrieve the SQL query, bound parameters, and error array - * (returned by sqlsrv_errors() or mysqli::$error_list). + * Access the SQL query, bound parameters, and error list returned by the DB driver. */ class SqlException extends \RuntimeException { - private array $errors; - private string $query; - private array $params; + /** + * The error list returned by the database driver + * @readonly + */ + public array $errors; + + /** + * The failed SQL query + * @readonly + */ + public string $query; + + /** + * The failed query's bound parameters + * @readonly + */ + public array $params; /** * @param string $msg The error message @@ -53,6 +66,7 @@ public function getSqlState(): string /** * Returns the list of errors from sqlsrv_errors() or mysqli::$error_list + * @deprecated Use readonly property instead * @api */ public function getErrors(): array @@ -62,6 +76,8 @@ public function getErrors(): array /** * Returns the failed SQL query + * @deprecated Use readonly property instead + * @api */ public function getQuery(): string { @@ -70,6 +86,7 @@ public function getQuery(): string /** * Returns the array of bound parameters + * @deprecated Use readonly property instead * @api */ public function getParams(): array diff --git a/lib/SqlServer.php b/lib/SqlServer.php index 8c84574..517dc0e 100644 --- a/lib/SqlServer.php +++ b/lib/SqlServer.php @@ -119,7 +119,7 @@ public function query(string $sql, array $params = []): Statement protected function insertBatch(string $table, array $colVals, int $identityIncrement = 1): BulkInsertResult { $sqlParams = (new Insert($this->options))->buildQuery($table, $colVals); - $result = $this->query($sqlParams->getSql(), $sqlParams->getParams()); + $result = $this->query($sqlParams->sql, $sqlParams->params); $row = $result->getFirst(); if (isset($row['RowID'])) { diff --git a/test/BulkInsertResultTest.php b/test/BulkInsertResultTest.php index 24313f7..7138b70 100644 --- a/test/BulkInsertResultTest.php +++ b/test/BulkInsertResultTest.php @@ -15,8 +15,8 @@ class BulkInsertResultTest extends TestCase public function testCreateRetrieve(): void { $result = new BulkInsertResult([48, 49, 50], 6, 2); - $this->assertSame([48, 49, 50], $result->getIds()); - $this->assertSame(6, $result->getAffected()); - $this->assertSame(2, $result->getQueryCount()); + $this->assertSame([48, 49, 50], $result->ids); + $this->assertSame(6, $result->affected); + $this->assertSame(2, $result->queryCount); } } diff --git a/test/DbTest.php b/test/DbTest.php index 9f2b74d..fb1703d 100644 --- a/test/DbTest.php +++ b/test/DbTest.php @@ -61,8 +61,8 @@ public function testNoIdentityInsert(PeachySql $peachySql): void ]; $result = $peachySql->insertRows('Test', $colVals); - $this->assertSame(2, $result->getAffected()); - $this->assertEmpty($result->getIds()); + $this->assertSame(2, $result->affected); + $this->assertCount(0, $result->ids); $this->assertSame($colVals, $peachySql->selectFrom("SELECT * FROM Test")->query()->getAll()); $peachySql->query("DROP TABLE Test"); } @@ -82,7 +82,7 @@ public function testTransactions(PeachySql $peachySql): void 'uuid' => $peachySql->makeBinaryParam(Uuid::uuid4()->getBytes(), 16), ]; - $id = $peachySql->insertRow(self::TABLE_NAME, $colVals)->getId(); + $id = $peachySql->insertRow(self::TABLE_NAME, $colVals)->id; $sql = 'SELECT user_id, isDisabled FROM Users WHERE user_id = ?'; $result = $peachySql->query($sql, [$id]); @@ -94,7 +94,7 @@ public function testTransactions(PeachySql $peachySql): void $this->assertSame(null, $sameRow); // the row should no longer exist $peachySql->begin(); // start another transaction - $newId = $peachySql->insertRow(self::TABLE_NAME, $colVals)->getId(); + $newId = $peachySql->insertRow(self::TABLE_NAME, $colVals)->id; $peachySql->commit(); // complete the transaction $newRow = $peachySql->selectFrom("SELECT user_id FROM " . self::TABLE_NAME) ->where(['user_id' => $newId])->query()->getFirst(); @@ -112,7 +112,7 @@ public function testException(PeachySql $peachySql): void try { $peachySql->query($badQuery); // should throw exception } catch (SqlException $e) { - $this->assertSame($badQuery, $e->getQuery()); + $this->assertSame($badQuery, $e->query); $this->assertSame('42000', $e->getSqlState()); if ($peachySql instanceof SqlServer) { @@ -149,7 +149,7 @@ public function testIteratorQuery(PeachySql $peachySql): void $insertColVals[] = $row; } - $ids = $peachySql->insertRows(self::TABLE_NAME, $insertColVals)->getIds(); + $ids = $peachySql->insertRows(self::TABLE_NAME, $insertColVals)->ids; $iterator = $peachySql->selectFrom("SELECT * FROM Users") ->where(['user_id' => $ids])->query()->getIterator(); @@ -221,9 +221,9 @@ public function testInsertBulk(PeachySql $peachySql): void } $result = $peachySql->insertRows(self::TABLE_NAME, $insertColVals); - $this->assertSame($expectedQueries, $result->getQueryCount()); - $this->assertSame($rowCount, $result->getAffected()); - $ids = $result->getIds(); + $this->assertSame($expectedQueries, $result->queryCount); + $this->assertSame($rowCount, $result->affected); + $ids = $result->ids; $this->assertSame($rowCount, count($ids)); $columns = implode(', ', array_keys($colVals[0])); @@ -247,9 +247,9 @@ public function testInsertBulk(PeachySql $peachySql): void public function testEmptyBulkInsert(PeachySql $peachySql): void { $result = $peachySql->insertRows(self::TABLE_NAME, []); - $this->assertSame(0, $result->getAffected()); - $this->assertSame(0, $result->getQueryCount()); - $this->assertEmpty($result->getIds()); + $this->assertSame(0, $result->affected); + $this->assertSame(0, $result->queryCount); + $this->assertEmpty($result->ids); } /** @@ -258,7 +258,7 @@ public function testEmptyBulkInsert(PeachySql $peachySql): void public function testSelectFromBinding(PeachySql $peachySql): void { $row = ['name' => 'Test User', 'dob' => '2000-01-01', 'weight' => 123, 'isDisabled' => true]; - $id = $peachySql->insertRow(self::TABLE_NAME, $row)->getId(); + $id = $peachySql->insertRow(self::TABLE_NAME, $row)->id; $result = $peachySql->select(new SqlParams("SELECT name, ? AS bound FROM " . self::TABLE_NAME, ['value'])) ->where(['user_id' => $id])->query()->getFirst(); diff --git a/test/InsertResultTest.php b/test/InsertResultTest.php index 61fac3d..afa16e9 100644 --- a/test/InsertResultTest.php +++ b/test/InsertResultTest.php @@ -15,15 +15,7 @@ class InsertResultTest extends TestCase public function testCreateRetrieve(): void { $result = new InsertResult(24, 2); - $this->assertSame(24, $result->getId()); - $this->assertSame(2, $result->getAffected()); - } - - public function testNoInsertId(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Inserted row does not have an auto-incremented ID'); - $result = new InsertResult(0, 1); - $result->getId(); + $this->assertSame(24, $result->id); + $this->assertSame(2, $result->affected); } } diff --git a/test/QueryBuilder/DeleteTest.php b/test/QueryBuilder/DeleteTest.php index bcba18d..e81973b 100644 --- a/test/QueryBuilder/DeleteTest.php +++ b/test/QueryBuilder/DeleteTest.php @@ -19,7 +19,7 @@ public function testBuildQuery(): void $actual = $delete->buildQuery('TestTable', $where); $expected = 'DELETE FROM TestTable WHERE "id" = ? AND "username" IN(?,?)'; - $this->assertSame($expected, $actual->getSql()); - $this->assertSame([5, 'tester', 'tester2'], $actual->getParams()); + $this->assertSame($expected, $actual->sql); + $this->assertSame([5, 'tester', 'tester2'], $actual->params); } } diff --git a/test/QueryBuilder/InsertTest.php b/test/QueryBuilder/InsertTest.php index 32dd232..a074909 100644 --- a/test/QueryBuilder/InsertTest.php +++ b/test/QueryBuilder/InsertTest.php @@ -73,8 +73,8 @@ public function testBuildQuery(): void $actual = (new Insert(new \PeachySQL\Mysql\Options()))->buildQuery('TestTable', [$colVals]); $expected = 'INSERT INTO TestTable (`col1`, `col2`, `col3`) VALUES (?,?,?)'; - $this->assertSame($expected, $actual->getSql()); - $this->assertSame(['val1', 'val2', 'val3'], $actual->getParams()); + $this->assertSame($expected, $actual->sql); + $this->assertSame(['val1', 'val2', 'val3'], $actual->params); } /** @@ -97,7 +97,7 @@ public function testBuildQueryWithScopeIdentity(): void $expected = 'INSERT INTO TestTable ("col1", "col2")' . ' VALUES (?,?), (?,?);' . ' SELECT SCOPE_IDENTITY() AS RowID;'; - $this->assertSame($expected, $actual->getSql()); - $this->assertSame(['foo1', 'foo2', 'bar1', 'bar2'], $actual->getParams()); + $this->assertSame($expected, $actual->sql); + $this->assertSame(['foo1', 'foo2', 'bar1', 'bar2'], $actual->params); } } diff --git a/test/QueryBuilder/SelectorTest.php b/test/QueryBuilder/SelectorTest.php index 3d5299e..dd2c75a 100644 --- a/test/QueryBuilder/SelectorTest.php +++ b/test/QueryBuilder/SelectorTest.php @@ -19,8 +19,8 @@ public function testBoundSelect(): void $query = 'SELECT column, ? FROM TestTable'; $selector = new Selector(new SqlParams($query, ['value']), new MysqlOptions()); $actual = $selector->where(['column' => 10])->getSqlParams(); - $this->assertSame($query . ' WHERE `column` = ?', $actual->getSql()); - $this->assertSame(['value', 10], $actual->getParams()); + $this->assertSame($query . ' WHERE `column` = ?', $actual->sql); + $this->assertSame(['value', 10], $actual->params); } public function testWhere(): void @@ -51,11 +51,11 @@ public function testWhere(): void . ' AND "firstname" NOT LIKE ? AND "firstname" NOT LIKE ?' . ' AND "datecol" >= ? AND "datecol" < ?' . ' AND "numcol" > ? AND "numcol" <= ?'; - $this->assertSame($expected, $actual->getSql()); + $this->assertSame($expected, $actual->sql); $params = ['TestUser', 'Brown', 'TestPassword', 'Password123', 'Password123', 'Admin%', 'Low%', '%nnet%', '%zabet%', '%dore', '%rah', '2016-05-01', '2016-06-01', 10, 20]; - $this->assertSame($params, $actual->getParams()); + $this->assertSame($params, $actual->params); } public function testInvalidWhere(): void @@ -101,12 +101,12 @@ public function testOrderBy(): void $actual = $select->orderBy(['lastname', 'firstname'])->getSqlParams(); $expected = $query . ' ORDER BY "lastname", "firstname"'; - $this->assertSame($expected, $actual->getSql()); - $this->assertSame([], $actual->getParams()); + $this->assertSame($expected, $actual->sql); + $this->assertSame([], $actual->params); $orderBy = ['lastname' => 'asc', 'firstname' => 'asc', 'age' => 'desc']; $actual = (new Selector(new SqlParams($query, []), new Options()))->orderBy($orderBy)->getSqlParams(); - $this->assertSame($query . ' ORDER BY "lastname" ASC, "firstname" ASC, "age" DESC', $actual->getSql()); + $this->assertSame($query . ' ORDER BY "lastname" ASC, "firstname" ASC, "age" DESC', $actual->sql); } public function testInvalidOrderBy(): void @@ -146,8 +146,8 @@ public function testGetSqlParams(): void ->getSqlParams(); $expected = $query . ' WHERE "a"."id" = ? ORDER BY "a"."username" OFFSET 25 ROWS FETCH NEXT 25 ROWS ONLY'; - $this->assertSame($expected, $result->getSql()); - $this->assertSame([1], $result->getParams()); + $this->assertSame($expected, $result->sql); + $this->assertSame([1], $result->params); } public function testInvalidPagination(): void diff --git a/test/QueryBuilder/UpdateTest.php b/test/QueryBuilder/UpdateTest.php index 1dab123..1778e02 100644 --- a/test/QueryBuilder/UpdateTest.php +++ b/test/QueryBuilder/UpdateTest.php @@ -25,7 +25,7 @@ public function testBuildQuery(): void $actual = $update->buildQuery('TestTable', $set, $where); $expected = 'UPDATE TestTable SET "username" = ?, "othercol" = ? WHERE "id" = ?'; - $this->assertSame($expected, $actual->getSql()); - $this->assertSame(['TestUser', null, 21], $actual->getParams()); + $this->assertSame($expected, $actual->sql); + $this->assertSame(['TestUser', null, 21], $actual->params); } }