From 916245600270615c246e6c8f249ad0141d1c1889 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 10 Mar 2022 08:53:30 +0100 Subject: [PATCH] Return numeric-string instead of just string to represent row count Constraint int to int<0, max> --- src/Connection.php | 15 +++++++++++++-- src/Driver/Connection.php | 5 ++++- src/Driver/IBMDB2/Connection.php | 5 ++++- .../Middleware/AbstractConnectionMiddleware.php | 5 ++++- src/Driver/Mysqli/Connection.php | 5 ++++- src/Driver/OCI8/Connection.php | 4 +++- src/Driver/PDO/Connection.php | 5 ++++- src/Driver/SQLSrv/Connection.php | 5 ++++- src/Logging/Connection.php | 5 ++++- src/Query/QueryBuilder.php | 6 ++++-- src/Tools/Console/Command/RunSqlCommand.php | 2 +- 11 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 60ae3aa4ea0..2661a1009ac 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -624,6 +624,7 @@ private function addCriteriaCondition( * @param array|array $types Parameter types * * @return int|string The number of affected rows. + * @psalm-return int<0,max>|numeric-string * * @throws Exception */ @@ -661,6 +662,7 @@ public function close() * @param int $level The level to set. * * @return int|string + * @psalm-return int<0,max>|numeric-string * * @throws Exception */ @@ -698,6 +700,7 @@ public function getTransactionIsolation() * @param array|array $types Parameter types * * @return int|string The number of affected rows. + * @psalm-return int<0,max>|numeric-string * * @throws Exception */ @@ -733,6 +736,7 @@ public function update($table, array $data, array $criteria, array $types = []) * @param array|array $types Parameter types * * @return int|string The number of affected rows. + * @psalm-return int<0,max>|numeric-string * * @throws Exception */ @@ -1126,6 +1130,7 @@ public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp) * @param array|array $types Parameter types * * @return int|string The number of affected rows. + * @psalm-return int<0,max>|numeric-string * * @throws Exception */ @@ -1826,8 +1831,11 @@ private function handleDriverException( * * @param array $params The query parameters * @param array $types The parameter types + * + * @return int|string + * @psalm-return int<0,max>|numeric-string */ - public function executeUpdate(string $sql, array $params = [], array $types = []): int + public function executeUpdate(string $sql, array $params = [], array $types = []) { return $this->executeStatement($sql, $params, $types); } @@ -1846,8 +1854,11 @@ public function query(string $sql): Result * BC layer for a wide-spread use-case of old DBAL APIs * * @deprecated This API is deprecated and will be removed after 2022 + * + * @return int|string + * @psalm-return int<0,max>|numeric-string */ - public function exec(string $sql): int + public function exec(string $sql) { return $this->executeStatement($sql); } diff --git a/src/Driver/Connection.php b/src/Driver/Connection.php index 2f460fd1066..9ff610b337f 100644 --- a/src/Driver/Connection.php +++ b/src/Driver/Connection.php @@ -42,9 +42,12 @@ public function quote($value, $type = ParameterType::STRING); /** * Executes an SQL statement and return the number of affected rows. * + * @return int|string + * @psalm-return int<0,max>|numeric-string + * * @throws Exception */ - public function exec(string $sql): int; + public function exec(string $sql); /** * Returns the ID of the last inserted row or sequence value. diff --git a/src/Driver/IBMDB2/Connection.php b/src/Driver/IBMDB2/Connection.php index 4f2a2926696..9f71ba7aca5 100644 --- a/src/Driver/IBMDB2/Connection.php +++ b/src/Driver/IBMDB2/Connection.php @@ -84,7 +84,10 @@ public function quote($value, $type = ParameterType::STRING) return "'" . $value . "'"; } - public function exec(string $sql): int + /** + * {@inheritdoc} + */ + public function exec(string $sql) { $stmt = @db2_exec($this->connection, $sql); diff --git a/src/Driver/Middleware/AbstractConnectionMiddleware.php b/src/Driver/Middleware/AbstractConnectionMiddleware.php index a0e69a5e779..66a656fd0b0 100644 --- a/src/Driver/Middleware/AbstractConnectionMiddleware.php +++ b/src/Driver/Middleware/AbstractConnectionMiddleware.php @@ -42,7 +42,10 @@ public function quote($value, $type = ParameterType::STRING) return $this->wrappedConnection->quote($value, $type); } - public function exec(string $sql): int + /** + * {@inheritdoc} + */ + public function exec(string $sql) { return $this->wrappedConnection->exec($sql); } diff --git a/src/Driver/Mysqli/Connection.php b/src/Driver/Mysqli/Connection.php index 8453f7406b2..7afcef314c1 100644 --- a/src/Driver/Mysqli/Connection.php +++ b/src/Driver/Mysqli/Connection.php @@ -81,7 +81,10 @@ public function quote($value, $type = ParameterType::STRING) return "'" . $this->connection->escape_string($value) . "'"; } - public function exec(string $sql): int + /** + * {@inheritdoc} + */ + public function exec(string $sql) { try { $result = $this->connection->query($sql); diff --git a/src/Driver/OCI8/Connection.php b/src/Driver/OCI8/Connection.php index 46ff4186691..6c573a09166 100644 --- a/src/Driver/OCI8/Connection.php +++ b/src/Driver/OCI8/Connection.php @@ -100,10 +100,12 @@ public function quote($value, $type = ParameterType::STRING) } /** + * {@inheritdoc} + * * @throws Exception * @throws Parser\Exception */ - public function exec(string $sql): int + public function exec(string $sql) { return $this->prepare($sql)->execute()->rowCount(); } diff --git a/src/Driver/PDO/Connection.php b/src/Driver/PDO/Connection.php index 505acba5f0b..c6b9f5dfeee 100644 --- a/src/Driver/PDO/Connection.php +++ b/src/Driver/PDO/Connection.php @@ -28,7 +28,10 @@ public function __construct(PDO $connection) $this->connection = $connection; } - public function exec(string $sql): int + /** + * {@inheritdoc} + */ + public function exec(string $sql) { try { $result = $this->connection->exec($sql); diff --git a/src/Driver/SQLSrv/Connection.php b/src/Driver/SQLSrv/Connection.php index 0295db7e5b0..ec6f4fb23a6 100644 --- a/src/Driver/SQLSrv/Connection.php +++ b/src/Driver/SQLSrv/Connection.php @@ -71,7 +71,10 @@ public function quote($value, $type = ParameterType::STRING) return "'" . str_replace("'", "''", $value) . "'"; } - public function exec(string $sql): int + /** + * {@inheritdoc} + */ + public function exec(string $sql) { $stmt = sqlsrv_query($this->connection, $sql); diff --git a/src/Logging/Connection.php b/src/Logging/Connection.php index 2175dc467df..43eb82b373d 100644 --- a/src/Logging/Connection.php +++ b/src/Logging/Connection.php @@ -46,7 +46,10 @@ public function query(string $sql): Result return parent::query($sql); } - public function exec(string $sql): int + /** + * {@inheritDoc} + */ + public function exec(string $sql) { $this->logger->debug('Executing statement: {sql}', ['sql' => $sql]); diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 7f30a1287ca..fcf8beb9847 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -315,11 +315,12 @@ public function executeQuery(): Result * * Should be used for INSERT, UPDATE and DELETE * - * @return int The number of affected rows. + * @return int|string The number of affected rows. + * @psalm-return int<0,max>|numeric-string * * @throws Exception */ - public function executeStatement(): int + public function executeStatement() { return $this->connection->executeStatement($this->getSQL(), $this->params, $this->paramTypes); } @@ -330,6 +331,7 @@ public function executeStatement(): int * @deprecated Use {@see executeQuery()} or {@see executeStatement()} instead. * * @return Result|int|string + * @psalm-return Result|int<0,max>|numeric-string * * @throws Exception */ diff --git a/src/Tools/Console/Command/RunSqlCommand.php b/src/Tools/Console/Command/RunSqlCommand.php index a9137206178..387dfa4c796 100644 --- a/src/Tools/Console/Command/RunSqlCommand.php +++ b/src/Tools/Console/Command/RunSqlCommand.php @@ -124,6 +124,6 @@ private function runQuery(SymfonyStyle $io, Connection $conn, string $sql): void */ private function runStatement(SymfonyStyle $io, Connection $conn, string $sql): void { - $io->success(sprintf('%d rows affected.', $conn->executeStatement($sql))); + $io->success(sprintf('%s rows affected.', $conn->executeStatement($sql))); } }