From 826d6eab4b2c4d669424e6e4225e698118f91d81 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sat, 11 Nov 2023 22:01:34 +0700 Subject: [PATCH 1/9] Enable backwards compatibility test (#774) --- .github/workflows/bc.yml | 33 +++++++++++++++++++++++++++++++++ .github/workflows/bc.yml_ | 15 --------------- 2 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/bc.yml delete mode 100644 .github/workflows/bc.yml_ diff --git a/.github/workflows/bc.yml b/.github/workflows/bc.yml new file mode 100644 index 000000000..00041a9f4 --- /dev/null +++ b/.github/workflows/bc.yml @@ -0,0 +1,33 @@ +on: + pull_request: + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'phpunit.xml.dist' + - 'psalm.xml' + push: + branches: ['master'] + paths-ignore: + - 'docs/**' + - 'README.md' + - 'CHANGELOG.md' + - '.gitignore' + - '.gitattributes' + - 'infection.json.dist' + - 'phpunit.xml.dist' + - 'psalm.xml' + +name: backwards compatibility + +jobs: + roave_bc_check: + uses: yiisoft/actions/.github/workflows/bc.yml@master + with: + os: >- + ['ubuntu-latest'] + php: >- + ['8.1'] diff --git a/.github/workflows/bc.yml_ b/.github/workflows/bc.yml_ deleted file mode 100644 index 35b3a8624..000000000 --- a/.github/workflows/bc.yml_ +++ /dev/null @@ -1,15 +0,0 @@ -on: - - pull_request - - push - -name: backwards compatibility -jobs: - roave_bc_check: - name: Roave BC Check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - name: fetch tags - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - - name: Roave BC Check - uses: docker://nyholm/roave-bc-check-ga From 63f68c0337cc2a2754c3e58bcaf717f20e7c5eca Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sun, 12 Nov 2023 14:20:03 +0700 Subject: [PATCH 2/9] Fix `batchInsert()` with associative arrays (#769) * Refactor DMLQueryBuilder * Get uniques using `getTableIndexes()` and `getTableUniques()` * Fix @psalm-var * Fix #61 (point 2) * Fix #61 (point 2) add test * Improve test * Remove methods with `NotSupportedException` * Fix test issues * Fix test issues * Revert "Remove methods with `NotSupportedException`" * Add line to CHANGELOG.md * Change order of checks * Improve performance of quoting column names up to 10% using `array_map()` * Fix `batchInsert()` with associative arrays * Update after merge * Remove old comments * Fix psalm * Add line to CHANGELOG.md --------- Co-authored-by: Sergei Predvoditelev --- CHANGELOG.md | 1 + src/QueryBuilder/AbstractDMLQueryBuilder.php | 37 ++++++----- tests/Provider/CommandProvider.php | 70 +++++++++++++++++++- 3 files changed, 91 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e92f048da..e1ef4bfa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Bug #761: Quote aliases of CTE in `WITH` queries (@Tigrov) - Chg #765: Deprecate `SchemaInterface::TYPE_JSONB` (@Tigrov) - Enh #770: Move methods from concrete `Command` class to `AbstractPdoCommand` class (@Tigrov) +- Bug #769, #61: Fix `AbstractDMLQueryBuilder::batchInsert()` for values as associative arrays (@Tigrov) ## 1.1.1 August 16, 2023 diff --git a/src/QueryBuilder/AbstractDMLQueryBuilder.php b/src/QueryBuilder/AbstractDMLQueryBuilder.php index 1224e503f..5d57d6412 100644 --- a/src/QueryBuilder/AbstractDMLQueryBuilder.php +++ b/src/QueryBuilder/AbstractDMLQueryBuilder.php @@ -19,6 +19,7 @@ use function array_combine; use function array_diff; +use function array_fill_keys; use function array_filter; use function array_keys; use function array_map; @@ -57,25 +58,31 @@ public function batchInsert(string $table, array $columns, iterable $rows, array $values = []; $columns = $this->getNormalizeColumnNames('', $columns); + $columnNames = array_values($columns); + $columnKeys = array_fill_keys($columnNames, false); $columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; foreach ($rows as $row) { $i = 0; - $placeholders = []; - - foreach ($row as $value) { - if (isset($columns[$i], $columnSchemas[$columns[$i]])) { - $value = $columnSchemas[$columns[$i]]->dbTypecast($value); + $placeholders = $columnKeys; + + foreach ($row as $key => $value) { + /** @psalm-suppress MixedArrayTypeCoercion */ + $columnName = $columns[$key] ?? (isset($columnKeys[$key]) ? $key : $columnNames[$i] ?? $i); + /** @psalm-suppress MixedArrayTypeCoercion */ + if (isset($columnSchemas[$columnName])) { + $value = $columnSchemas[$columnName]->dbTypecast($value); } if ($value instanceof ExpressionInterface) { - $placeholders[] = $this->queryBuilder->buildExpression($value, $params); + $placeholders[$columnName] = $this->queryBuilder->buildExpression($value, $params); } else { - $placeholders[] = $this->queryBuilder->bindParam($value, $params); + $placeholders[$columnName] = $this->queryBuilder->bindParam($value, $params); } ++$i; } + $values[] = '(' . implode(', ', $placeholders) . ')'; } @@ -83,13 +90,13 @@ public function batchInsert(string $table, array $columns, iterable $rows, array return ''; } - $columns = array_map( + $columnNames = array_map( [$this->quoter, 'quoteColumnName'], - $columns, + $columnNames, ); return 'INSERT INTO ' . $this->quoter->quoteTableName($table) - . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); + . ' (' . implode(', ', $columnNames) . ') VALUES ' . implode(', ', $values); } public function delete(string $table, array|string $condition, array &$params): string @@ -430,13 +437,11 @@ protected function normalizeColumnNames(string $table, array $columns): array */ protected function getNormalizeColumnNames(string $table, array $columns): array { - $normalizedNames = []; - - foreach ($columns as $name) { - $normalizedName = $this->quoter->ensureColumnName($name); - $normalizedNames[] = $this->quoter->unquoteSimpleColumnName($normalizedName); + foreach ($columns as &$name) { + $name = $this->quoter->ensureColumnName($name); + $name = $this->quoter->unquoteSimpleColumnName($name); } - return $normalizedNames; + return $columns; } } diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 5d2ae6176..9f7408b73 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -339,7 +339,7 @@ public static function batchInsert(): array ':qp3' => false, ], ], - 'with associative values' => [ + 'with associative values with different keys' => [ 'type', ['int_col', 'float_col', 'char_col', 'bool_col'], 'values' => [['int' => '1.0', 'float' => '2', 'char' => 10, 'bool' => 1]], @@ -356,6 +356,74 @@ public static function batchInsert(): array ':qp3' => true, ], ], + 'with associative values with different keys and columns with keys' => [ + 'type', + ['a' => 'int_col', 'b' => 'float_col', 'c' => 'char_col', 'd' => 'bool_col'], + 'values' => [['int' => '1.0', 'float' => '2', 'char' => 10, 'bool' => 1]], + 'expected' => DbHelper::replaceQuotes( + << [ + ':qp0' => 1, + ':qp1' => 2.0, + ':qp2' => '10', + ':qp3' => true, + ], + ], + 'with associative values with keys of column names' => [ + 'type', + ['int_col', 'float_col', 'char_col', 'bool_col'], + 'values' => [['bool_col' => 1, 'char_col' => 10, 'int_col' => '1.0', 'float_col' => '2']], + 'expected' => DbHelper::replaceQuotes( + << [ + ':qp0' => true, + ':qp1' => '10', + ':qp2' => 1, + ':qp3' => 2.0, + ], + ], + 'with associative values with keys of column keys' => [ + 'type', + ['int' => 'int_col', 'float' => 'float_col', 'char' => 'char_col', 'bool' => 'bool_col'], + 'values' => [['bool' => 1, 'char' => 10, 'int' => '1.0', 'float' => '2']], + 'expected' => DbHelper::replaceQuotes( + << [ + ':qp0' => true, + ':qp1' => '10', + ':qp2' => 1, + ':qp3' => 2.0, + ], + ], + 'with shuffled indexes of values' => [ + 'type', + ['int_col', 'float_col', 'char_col', 'bool_col'], + 'values' => [[3 => 1, 2 => 10, 0 => '1.0', 1 => '2']], + 'expected' => DbHelper::replaceQuotes( + << [ + ':qp0' => true, + ':qp1' => '10', + ':qp2' => 1, + ':qp3' => 2.0, + ], + ], ]; } From 133d972646d9c26c4b120eb5c8e3e1d83880f7b6 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sun, 12 Nov 2023 17:29:54 +0700 Subject: [PATCH 3/9] Fix BC issues (#775) * Fix BC issues * Remove BC test * revert removing `Generator` --------- Co-authored-by: Sergei Predvoditelev --- .github/workflows/bc.yml | 26 ++++++++----------------- src/Query/Query.php | 2 +- src/Query/QueryPartsInterface.php | 5 ++--- tests/Provider/QueryBuilderProvider.php | 1 - 4 files changed, 11 insertions(+), 23 deletions(-) diff --git a/.github/workflows/bc.yml b/.github/workflows/bc.yml index 00041a9f4..5970206c9 100644 --- a/.github/workflows/bc.yml +++ b/.github/workflows/bc.yml @@ -1,25 +1,15 @@ on: pull_request: - paths-ignore: - - 'docs/**' - - 'README.md' - - 'CHANGELOG.md' - - '.gitignore' - - '.gitattributes' - - 'infection.json.dist' - - 'phpunit.xml.dist' - - 'psalm.xml' + paths: + - 'src/**' + - '.github/workflows/bc.yml' + - 'composer.json' push: branches: ['master'] - paths-ignore: - - 'docs/**' - - 'README.md' - - 'CHANGELOG.md' - - '.gitignore' - - '.gitattributes' - - 'infection.json.dist' - - 'phpunit.xml.dist' - - 'psalm.xml' + paths: + - 'src/**' + - '.github/workflows/bc.yml' + - 'composer.json' name: backwards compatibility diff --git a/src/Query/Query.php b/src/Query/Query.php index 4ba681e7b..98e69b962 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -662,7 +662,7 @@ public function where(array|string|ExpressionInterface|null $condition, array $p return $this; } - public function withQuery(QueryInterface|string $query, ExpressionInterface|string $alias, bool $recursive = false): static + public function withQuery(QueryInterface|string $query, string $alias, bool $recursive = false): static { $this->withQueries[] = ['query' => $query, 'alias' => $alias, 'recursive' => $recursive]; return $this; diff --git a/src/Query/QueryPartsInterface.php b/src/Query/QueryPartsInterface.php index 8c3ddfd84..f6b9ca974 100644 --- a/src/Query/QueryPartsInterface.php +++ b/src/Query/QueryPartsInterface.php @@ -645,11 +645,10 @@ public function where(array|string|ExpressionInterface|null $condition, array $p * Prepends an SQL statement using `WITH` syntax. * * @param QueryInterface|string $query The SQL statement to append using `UNION`. - * @param ExpressionInterface|string $alias The query alias in `WITH` construction. - * To specify the alias in plain SQL, you may pass an instance of {@see ExpressionInterface}. + * @param string $alias The query alias in `WITH` construction. * @param bool $recursive Its `true` if using `WITH RECURSIVE` and `false` if using `WITH`. */ - public function withQuery(QueryInterface|string $query, ExpressionInterface|string $alias, bool $recursive = false): static; + public function withQuery(QueryInterface|string $query, string $alias, bool $recursive = false): static; /** * Specifies the `WITH` query clause for the query. diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 8e04fc7b5..a70696668 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -1258,7 +1258,6 @@ public static function cteAliases(): array 'with one column' => ['a(b)', '[[a]]([[b]])'], 'with columns' => ['a(b,c,d)', '[[a]]([[b]], [[c]], [[d]])'], 'with extra space' => ['a(b,c,d) ', 'a(b,c,d) '], - 'expression' => [new Expression('a(b,c,d)'), 'a(b,c,d)'], ]; } } From 437c6295c5a0257f37045ece708be8ed7766389b Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 12 Nov 2023 15:51:17 +0000 Subject: [PATCH 4/9] Release version 1.2.0 --- CHANGELOG.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1ef4bfa5..4955cf0b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,21 +1,17 @@ # Yii Database Change Log -## 1.1.2 under development +## 1.2.0 November 12, 2023 -- Enh #746: Refactor `AbstractDMLQueryBuilder` (@Tigrov) -- Bug #746: Fix `AbstractDMLQueryBuilder::upsert()` when unique index is not at the first position of inserted values (@Tigrov) +- Chg #755: Deprecate `TableSchemaInterface::compositeForeignKey()` (@Tigrov) +- Chg #765: Deprecate `SchemaInterface::TYPE_JSONB` (@Tigrov) +- Enh #746: Enhanced documentation of `batchInsert()` and `update()` methods of `DMLQueryBuilderInterface` interface (@Tigrov) +- Enh #756: Refactor `Quoter` (@Tigrov) +- Enh #770: Move methods from concrete `Command` class to `AbstractPdoCommand` class (@Tigrov) - Bug #746: Typecast values in `AbstractDMLQueryBuilder::batchInsert()` if column names with table name and brackets (@Tigrov) - Bug #746, #61: Typecast values in `AbstractDMLQueryBuilder::batchInsert()` if values with string keys (@Tigrov) -- Enh #746: Enhanced documentation of `batchInsert()` and `update()` methods of `DMLQueryBuilderInterface` interface (@Tigrov) - Bug #751: Fix collected debug actions (@xepozz) -- Chg #755: Deprecate `TableSchemaInterface::compositeForeignKey()` (@Tigrov) -- Enh #756: Refactor `Quoter` (@Tigrov) -- Bug #756: Fix `Quoter::quoteSql()` for SQL containing table with prefix (@Tigrov) -- Bug #756: Fix `Quoter::getTableNameParts()` for cases when different quotes for tables and columns (@Tigrov) - Bug #756: Fix `Quoter::quoteTableName()` for sub-query with alias (@Tigrov) - Bug #761: Quote aliases of CTE in `WITH` queries (@Tigrov) -- Chg #765: Deprecate `SchemaInterface::TYPE_JSONB` (@Tigrov) -- Enh #770: Move methods from concrete `Command` class to `AbstractPdoCommand` class (@Tigrov) - Bug #769, #61: Fix `AbstractDMLQueryBuilder::batchInsert()` for values as associative arrays (@Tigrov) ## 1.1.1 August 16, 2023 From 4629a8c16bbfbf93f4b19ab54c174f1156175f8f Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 12 Nov 2023 15:51:20 +0000 Subject: [PATCH 5/9] Prepare for next release --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4955cf0b7..293e1f8df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Yii Database Change Log +## 1.2.1 under development + +- no changes in this release. + ## 1.2.0 November 12, 2023 - Chg #755: Deprecate `TableSchemaInterface::compositeForeignKey()` (@Tigrov) From 6845a40a4eada08cb9300294b60ce9c08f7d94d9 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Thu, 16 Nov 2023 17:42:43 +0700 Subject: [PATCH 6/9] Fix query count (#777) --- .github/workflows/active-record.yml | 2 +- .github/workflows/db-mariadb.yml | 2 +- .github/workflows/db-mssql.yml | 2 +- .github/workflows/db-mysql.yml | 2 +- .github/workflows/db-oracle.yml | 2 +- .github/workflows/db-pgsql.yml | 2 +- .github/workflows/db-sqlite.yml | 2 +- CHANGELOG.md | 3 ++- src/Query/Query.php | 12 ++++++++---- tests/AbstractQueryTest.php | 14 ++++++++++++++ 10 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.github/workflows/active-record.yml b/.github/workflows/active-record.yml index 6f7b9cb78..a894fad8a 100644 --- a/.github/workflows/active-record.yml +++ b/.github/workflows/active-record.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-active-record-${{ matrix.os }} env: - COMPOSER_ROOT_VERSION: 1.1.0 + COMPOSER_ROOT_VERSION: 1.2.0 EXTENSIONS: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlite, pdo_sqlsrv-5.10.1 runs-on: ${{ matrix.os }} diff --git a/.github/workflows/db-mariadb.yml b/.github/workflows/db-mariadb.yml index 1ecc6bc67..7f69f4074 100644 --- a/.github/workflows/db-mariadb.yml +++ b/.github/workflows/db-mariadb.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-mariadb-${{ matrix.mariadb }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-mysql EXTENSIONS: pdo, pdo_mysql diff --git a/.github/workflows/db-mssql.yml b/.github/workflows/db-mssql.yml index a3000c876..8fab1e591 100644 --- a/.github/workflows/db-mssql.yml +++ b/.github/workflows/db-mssql.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-mssql-${{ matrix.mssql }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-mssql EXTENSIONS: pdo, pdo_sqlsrv-5.10.1 diff --git a/.github/workflows/db-mysql.yml b/.github/workflows/db-mysql.yml index 6e6b51baa..a56b3f2fb 100644 --- a/.github/workflows/db-mysql.yml +++ b/.github/workflows/db-mysql.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-mysql-${{ matrix.mysql }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-mysql EXTENSIONS: pdo, pdo_mysql diff --git a/.github/workflows/db-oracle.yml b/.github/workflows/db-oracle.yml index 75cdc548e..a37592db0 100644 --- a/.github/workflows/db-oracle.yml +++ b/.github/workflows/db-oracle.yml @@ -27,7 +27,7 @@ jobs: env: CURRENT_PACKAGE: db-oracle - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 EXTENSIONS: pdo, pdo_oci runs-on: ${{ matrix.os }} diff --git a/.github/workflows/db-pgsql.yml b/.github/workflows/db-pgsql.yml index d2db8a700..0a809bdda 100644 --- a/.github/workflows/db-pgsql.yml +++ b/.github/workflows/db-pgsql.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-pgsql-${{ matrix.pgsql }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-pgsql EXTENSIONS: pdo, pdo_pgsql diff --git a/.github/workflows/db-sqlite.yml b/.github/workflows/db-sqlite.yml index cf7c014a6..230ee0989 100644 --- a/.github/workflows/db-sqlite.yml +++ b/.github/workflows/db-sqlite.yml @@ -26,7 +26,7 @@ jobs: name: PHP ${{ matrix.php }}-sqlite-${{ matrix.os }} env: - COMPOSER_ROOT_VERSION: 1.0.0 + COMPOSER_ROOT_VERSION: 1.2.0 CURRENT_PACKAGE: db-sqlite EXTENSIONS: pdo, pdo_sqlite diff --git a/CHANGELOG.md b/CHANGELOG.md index 293e1f8df..44d5f549f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 1.2.1 under development -- no changes in this release. +- Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater + than `PHP_INT_MAX` (@Tigrov) ## 1.2.0 November 12, 2023 diff --git a/src/Query/Query.php b/src/Query/Query.php index 98e69b962..8aa9cc517 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -293,10 +293,14 @@ public function column(): array public function count(string $sql = '*'): int|string { - return match ($this->emulateExecution) { - true => 0, - false => is_numeric($count = $this->queryScalar("COUNT($sql)")) ? (int) $count : 0, - }; + /** @var int|string|null $count */ + $count = $this->queryScalar("COUNT($sql)"); + + if ($count === null) { + return 0; + } + + return $count <= PHP_INT_MAX ? (int) $count : $count; } public function createCommand(): CommandInterface diff --git a/tests/AbstractQueryTest.php b/tests/AbstractQueryTest.php index 63334ea9a..754cab5a3 100644 --- a/tests/AbstractQueryTest.php +++ b/tests/AbstractQueryTest.php @@ -787,4 +787,18 @@ public function testNormalizeSelect(array|string|Expression $columns, array|stri $query->select($columns); $this->assertEquals($expected, $query->getSelect()); } + + public function testCountGreaterThanPhpIntMax(): void + { + $query = $this->getMockBuilder(Query::class) + ->setConstructorArgs([$this->getConnection()]) + ->onlyMethods(['queryScalar']) + ->getMock(); + + $query->expects($this->once()) + ->method('queryScalar') + ->willReturn('12345678901234567890'); + + $this->assertSame('12345678901234567890', $query->count()); + } } From 102ee3a5aad4766db3ca88ee21c0b9c32d1a107a Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Tue, 21 Nov 2023 20:52:22 +0700 Subject: [PATCH 7/9] Deprecate unnecessary argument `$rawSql` of `AbstractCommand::internalExecute()` (#778) --- CHANGELOG.md | 4 ++-- src/Command/AbstractCommand.php | 4 ++-- src/Driver/Pdo/AbstractPdoCommand.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44d5f549f..52c6a39a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ## 1.2.1 under development -- Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater - than `PHP_INT_MAX` (@Tigrov) +- Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater than `PHP_INT_MAX` (@Tigrov) +- Enh #778: Deprecate unnecessary argument `$rawSql` of `AbstractCommand::internalExecute()` (@Tigrov) ## 1.2.0 November 12, 2023 diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index 479c1052e..0561e3bd1 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -548,7 +548,7 @@ abstract protected function internalGetQueryResult(int $queryMode): mixed; /** * Executes a prepared statement. * - * @param string|null $rawSql The rawSql if it has been created. + * @param string|null $rawSql Deprecated. Use `null` value. Will be removed in version 2.0.0. * * @throws Exception * @throws Throwable @@ -581,7 +581,7 @@ protected function queryInternal(int $queryMode): mixed $isReadMode = $this->isReadMode($queryMode); $this->prepare($isReadMode); - $this->internalExecute($this->getRawSql()); + $this->internalExecute(null); /** @psalm-var mixed $result */ $result = $this->internalGetQueryResult($queryMode); diff --git a/src/Driver/Pdo/AbstractPdoCommand.php b/src/Driver/Pdo/AbstractPdoCommand.php index c8348f832..bf3ae8252 100644 --- a/src/Driver/Pdo/AbstractPdoCommand.php +++ b/src/Driver/Pdo/AbstractPdoCommand.php @@ -186,7 +186,7 @@ protected function getQueryMode(int $queryMode): string * * It's a wrapper around {@see PDOStatement::execute()} to support transactions and retry handlers. * - * @param string|null $rawSql The rawSql if it has been created. + * @param string|null $rawSql Deprecated. Use `null` value. Will be removed in version 2.0.0. * * @throws Exception * @throws Throwable From 45a5ab62d3f21c00f612d63e29adbe3513010d56 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 21 Nov 2023 16:54:15 +0300 Subject: [PATCH 8/9] Improve type annotations (#779) --- CHANGELOG.md | 6 +++++- src/Command/CommandInterface.php | 2 +- src/Helper/DbArrayHelper.php | 5 ++--- src/Query/BatchQueryResultInterface.php | 5 +++++ src/Query/QueryInterface.php | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52c6a39a3..af82ceefc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,11 @@ ## 1.2.1 under development -- Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater than `PHP_INT_MAX` (@Tigrov) +- Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater + than `PHP_INT_MAX` (@Tigrov) +- Enh #779: Specify result type of `QueryInterface::all()`, `CommandInterface::queryAll()` and + `DbArrayHelper::populate()` methods to `array[]` (@vjik) +- Enh #779: Specify populate closure type in `BatchQueryResultInterface` (@vjik) - Enh #778: Deprecate unnecessary argument `$rawSql` of `AbstractCommand::internalExecute()` (@Tigrov) ## 1.2.0 November 12, 2023 diff --git a/src/Command/CommandInterface.php b/src/Command/CommandInterface.php index 9c3244201..3caad60d2 100644 --- a/src/Command/CommandInterface.php +++ b/src/Command/CommandInterface.php @@ -600,7 +600,7 @@ public function query(): DataReaderInterface; * @throws Exception * @throws Throwable If execution failed. * - * @return array All rows of the query result. Each array element is an array representing a row of data. + * @return array[] All rows of the query result. Each array element is an array representing a row of data. * Empty array if the query results in nothing. */ public function queryAll(): array; diff --git a/src/Helper/DbArrayHelper.php b/src/Helper/DbArrayHelper.php index e65e1509e..a5059a73b 100644 --- a/src/Helper/DbArrayHelper.php +++ b/src/Helper/DbArrayHelper.php @@ -333,9 +333,9 @@ public static function multisort( * This method is internally used to convert the data fetched from a database into the format as required by this * query. * - * @param array $rows The raw query result from a database. + * @param array[] $rows The raw query result from a database. * - * @psalm-suppress MixedArrayOffset + * @return array[] */ public static function populate(array $rows, Closure|string|null $indexBy = null): array { @@ -345,7 +345,6 @@ public static function populate(array $rows, Closure|string|null $indexBy = null $result = []; - /** @psalm-var array[][] $row */ foreach ($rows as $row) { /** @psalm-suppress MixedArrayOffset */ $result[self::getValueByPath($row, $indexBy)] = $row; diff --git a/src/Query/BatchQueryResultInterface.php b/src/Query/BatchQueryResultInterface.php index f9f20c16d..585ad50d9 100644 --- a/src/Query/BatchQueryResultInterface.php +++ b/src/Query/BatchQueryResultInterface.php @@ -35,6 +35,8 @@ * ``` * * @extends Iterator + * + * @psalm-type PopulateClosure=Closure(array[],Closure|string|null): mixed */ interface BatchQueryResultInterface extends Iterator { @@ -106,5 +108,8 @@ public function getBatchSize(): int; */ public function batchSize(int $value): self; + /** + * @psalm-param PopulateClosure|null $populateMethod + */ public function setPopulatedMethod(Closure|null $populateMethod = null): self; } diff --git a/src/Query/QueryInterface.php b/src/Query/QueryInterface.php index a13b0d5fd..0bbe54957 100644 --- a/src/Query/QueryInterface.php +++ b/src/Query/QueryInterface.php @@ -45,7 +45,7 @@ public function addParams(array $params): static; * @throws InvalidConfigException * @throws Throwable * - * @return array The query results. If the query results in nothing, it returns an empty array. + * @return array[] The query results. If the query results in nothing, it returns an empty array. */ public function all(): array; From 8a3d81f815a15e7e1cbff6e7b53a5c0fc9144ede Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Tue, 21 Nov 2023 21:02:03 +0700 Subject: [PATCH 9/9] Fix rector (#780) --- rector.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/rector.php b/rector.php index 5ad4b8e77..c80d86e9a 100644 --- a/rector.php +++ b/rector.php @@ -4,7 +4,6 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; -use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector; use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector; use Rector\Set\ValueObject\LevelSetList; @@ -25,7 +24,6 @@ $rectorConfig->skip([ ClosureToArrowFunctionRector::class, - AddDefaultValueForUndefinedVariableRector::class, JsonThrowOnErrorRector::class, ]); };