Skip to content

Commit

Permalink
Expression -> ExpressionInterface refactoring (#496)
Browse files Browse the repository at this point in the history
* Expression -> ExpressionInterface refactoring

* add test

* styleci
  • Loading branch information
darkdef committed Jan 29, 2023
1 parent 3effb73 commit 73eb925
Show file tree
Hide file tree
Showing 25 changed files with 253 additions and 105 deletions.
44 changes: 44 additions & 0 deletions .github/actions/subpackage_install/action.yml
@@ -0,0 +1,44 @@
name: Subpackage install

inputs:
CURRENT_PACKAGE:
required: true
type: string
BRANCH_NAME:
required: true
type: string
FULL_BRANCH_NAME:
required: true
type: string
COMPOSER_ROOT_VERSION:
required: true
type: string
WORK_PACKAGE_URL:
required: true
type: string

runs:
using: "composite"
steps:
- name: db-mysql / Check exist branch.
shell: bash
run: |
if git ls-remote --heads ${{ inputs.WORK_PACKAGE_URL }}${{ inputs.CURRENT_PACKAGE }} ${{ inputs.BRANCH_NAME }} | grep ${{ inputs.BRANCH_NAME }};
then echo "BRANCH_EXISTS=1" >> $GITHUB_ENV; else echo "BRANCH_EXISTS=0" >> $GITHUB_ENV; fi
- name: db-mysql / Install from fork.
shell: bash
if: ${{ github.event.pull_request.head.repo.fork && env.BRANCH_EXISTS == 1 }}
run: |
composer config repositories.yiisoft/${{ inputs.CURRENT_PACKAGE }} git ${{ inputs.WORK_PACKAGE_URL }}${{ inputs.CURRENT_PACKAGE }}
COMPOSER_ROOT_VERSION=${{ inputs.COMPOSER_ROOT_VERSION }} composer require "yiisoft/${{ inputs.CURRENT_PACKAGE }}:${{ inputs.FULL_BRANCH_NAME }} as ${{ inputs.COMPOSER_ROOT_VERSION }}" --no-interaction --no-progress --optimize-autoloader --ansi
- name: db-mysql / Install from branch.
shell: bash
if: ${{ ! github.event.pull_request.head.repo.fork && env.BRANCH_EXISTS == 1 }}
run: composer require yiisoft/${{ inputs.CURRENT_PACKAGE }}:${{ inputs.FULL_BRANCH_NAME }} --no-interaction --no-progress --optimize-autoloader --ansi

- name: db-mysql / Install from master.
shell: bash
if: ${{ github.event.pull_request.head.repo.full_name == 'yiisoft/db' && env.BRANCH_EXISTS != 1 }}
run: composer require yiisoft/${{ inputs.CURRENT_PACKAGE }}:${{ inputs.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
70 changes: 64 additions & 6 deletions .github/workflows/active-record.yml
Expand Up @@ -27,6 +27,10 @@ jobs:

env:
COMPOSER_ROOT_VERSION: dev-master
BRANCH_NAME: ${{ github.head_ref }}
FULL_BRANCH_NAME: dev-${{ github.head_ref }}
WORK_PACKAGE: active-record
WORK_PACKAGE_URL: https://github.com/${{ github.event.pull_request.head.repo.owner.login }}/
extensions: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlite, pdo_sqlsrv-5.10.1

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -102,22 +106,76 @@ jobs:
run: composer self-update

- name: Install db-mssql
run: composer require yiisoft/db-mssql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-mssql
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

# - name: Install db-mssql
# run: composer require yiisoft/db-mssql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install db-mysql
run: composer require yiisoft/db-mysql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-mysql
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

# - name: Install db-mysql
# run: composer require yiisoft/db-mysql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install db-pgsql
run: composer require yiisoft/db-pgsql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-pgsql
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

# - name: Install db-pgsql
# run: composer require yiisoft/db-pgsql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install db-oracle
run: composer require yiisoft/db-oracle:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-oracle
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

# - name: Install db-oracle
# run: composer require yiisoft/db-oracle:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install db-sqlite
run: composer require yiisoft/db-sqlite:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-sqlite
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

# - name: Install db-sqlite
# run: composer require yiisoft/db-sqlite:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install active-record
run: composer require yiisoft/active-record:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: active-record
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

# - name: Install active-record
# run: composer require yiisoft/active-record:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run tests with phpunit with code coverage
run: vendor/bin/phpunit --testsuite ActiveRecord --coverage-clover=coverage.xml --colors=always
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/db-mssql.yml
Expand Up @@ -27,6 +27,10 @@ jobs:

env:
COMPOSER_ROOT_VERSION: dev-master
BRANCH_NAME: ${{ github.head_ref }}
FULL_BRANCH_NAME: dev-${{ github.head_ref }}
WORK_PACKAGE: db-mssql
WORK_PACKAGE_URL: https://github.com/${{ github.event.pull_request.head.repo.owner.login }}/
extensions: pdo, pdo_sqlsrv-5.10.1

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -79,7 +83,13 @@ jobs:
run: composer self-update

- name: Install db-mssql.
run: composer require yiisoft/db-mssql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-mssql
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

- name: Run mssql tests with phpunit and code coverage.
run: vendor/bin/phpunit --testsuite Mssql --coverage-clover=coverage.xml --colors=always
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/db-mysql.yml
Expand Up @@ -27,6 +27,10 @@ jobs:

env:
COMPOSER_ROOT_VERSION: dev-master
BRANCH_NAME: ${{ github.head_ref }}
FULL_BRANCH_NAME: dev-${{ github.head_ref }}
WORK_PACKAGE: db-mysql
WORK_PACKAGE_URL: https://github.com/${{ github.event.pull_request.head.repo.owner.login }}/
extensions: pdo, pdo_mysql

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -72,7 +76,13 @@ jobs:
run: composer self-update

- name: Install db-mysql.
run: composer require yiisoft/db-mysql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-mysql
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

- name: Run Mssql tests with phpunit and code coverage.
run: vendor/bin/phpunit --testsuite Mysql --coverage-clover=coverage.xml --colors=always
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/db-oracle.yml
Expand Up @@ -27,6 +27,10 @@ jobs:

env:
COMPOSER_ROOT_VERSION: dev-master
BRANCH_NAME: ${{ github.head_ref }}
FULL_BRANCH_NAME: dev-${{ github.head_ref }}
WORK_PACKAGE: db-oracle
WORK_PACKAGE_URL: https://github.com/${{ github.event.pull_request.head.repo.owner.login }}/
extensions: pdo, pdo_oci

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -76,7 +80,13 @@ jobs:
run: composer self-update

- name: Install db-oracle.
run: composer require yiisoft/db-oracle:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-oracle
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

- name: Run oracle tests with phpunit and code coverage.
run: vendor/bin/phpunit --testsuite Oracle --coverage-clover=coverage.xml --colors=always
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/db-pgsql.yml
Expand Up @@ -27,6 +27,10 @@ jobs:

env:
COMPOSER_ROOT_VERSION: dev-master
BRANCH_NAME: ${{ github.head_ref }}
FULL_BRANCH_NAME: dev-${{ github.head_ref }}
WORK_PACKAGE: db-pgsql
WORK_PACKAGE_URL: https://github.com/${{ github.event.pull_request.head.repo.owner.login }}/
extensions: pdo, pdo_pgsql

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -77,7 +81,13 @@ jobs:
run: composer self-update

- name: Install db-pgsql.
run: composer require yiisoft/db-pgsql:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-pgsql
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

- name: Run pgsql tests with phpunit and code coverage.
run: vendor/bin/phpunit --testsuite Pgsql --coverage-clover=coverage.xml --colors=always
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/db-sqlite.yml
Expand Up @@ -27,6 +27,10 @@ jobs:

env:
COMPOSER_ROOT_VERSION: dev-master
BRANCH_NAME: ${{ github.head_ref }}
FULL_BRANCH_NAME: dev-${{ github.head_ref }}
WORK_PACKAGE: db-sqlite
WORK_PACKAGE_URL: https://github.com/${{ github.event.pull_request.head.repo.owner.login }}/
extensions: pdo, pdo_sqlite

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -59,7 +63,13 @@ jobs:
run: composer self-update

- name: Install db-sqlite.
run: composer require yiisoft/db-sqlite:${{ env.COMPOSER_ROOT_VERSION }} --no-interaction --no-progress --optimize-autoloader --ansi
uses: ./.github/actions/subpackage_install
with:
CURRENT_PACKAGE: db-sqlite
BRANCH_NAME: ${{ env.BRANCH_NAME }}
FULL_BRANCH_NAME: ${{ env.FULL_BRANCH_NAME }}
COMPOSER_ROOT_VERSION: ${{ env.COMPOSER_ROOT_VERSION }}
WORK_PACKAGE_URL: ${{ env.WORK_PACKAGE_URL }}

- name: Run sqlite tests with phpunit and code coverage.
run: vendor/bin/phpunit --testsuite Sqlite --coverage-clover=coverage.xml --colors=always
Expand Down
14 changes: 6 additions & 8 deletions src/Query/Helper/QueryHelper.php
Expand Up @@ -5,9 +5,7 @@
namespace Yiisoft\Db\Query\Helper;

use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Query\QueryInterface;
use Yiisoft\Db\Schema\QuoterInterface;

use function array_key_exists;
Expand Down Expand Up @@ -46,7 +44,7 @@ public function cleanUpTableNames(array $tableNames, QuoterInterface $quoter): a
~^\s*((?:['"`\[]|{{).*?(?:['"`\]]|}})|\(.*?\)|.*?)(?:(?:\s+(?:as\s+)?)((?:['"`\[]|{{).*?(?:['"`\]]|}})|.*?))?\s*$~iux
PATTERN;

/** @psalm-var array<array-key, Expression|string> $tableNames */
/** @psalm-var array<array-key, ExpressionInterface|string> $tableNames */
foreach ($tableNames as $alias => $tableName) {
if (is_string($tableName) && !is_string($alias)) {
if (preg_match($pattern, $tableName, $matches)) {
Expand All @@ -64,13 +62,13 @@ public function cleanUpTableNames(array $tableNames, QuoterInterface $quoter): a
);
}

if ($tableName instanceof Expression) {
$cleanedUpTableNames[$quoter->ensureNameQuoted($alias)] = $tableName;
} elseif ($tableName instanceof QueryInterface) {
if (is_string($tableName)) {
$cleanedUpTableNames[$quoter->ensureNameQuoted($alias)] = $quoter->ensureNameQuoted($tableName);
} elseif ($tableName instanceof ExpressionInterface) {
$cleanedUpTableNames[$quoter->ensureNameQuoted($alias)] = $tableName;
} else {
$cleanedUpTableNames[$quoter->ensureNameQuoted($alias)] = $quoter->ensureNameQuoted(
(string) $tableName
throw new InvalidArgumentException(
'Use ExpressionInterface without cast to string as object of tableName'
);
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/Query/Query.php
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Helper\ArrayHelper;
use Yiisoft\Db\Query\Helper\QueryHelper;
Expand Down Expand Up @@ -75,8 +74,8 @@ class Query implements QueryInterface
protected array $withQueries = [];
private bool $emulateExecution = false;
private Closure|string|null $indexBy = null;
private Expression|int|null $limit = null;
private Expression|int|null $offset = null;
private ExpressionInterface|int|null $limit = null;
private ExpressionInterface|int|null $offset = null;
private QueryHelper|null $queryHelper = null;
private array|string|ExpressionInterface|null $where = null;

Expand Down Expand Up @@ -409,12 +408,12 @@ public function getJoin(): array
return $this->join;
}

public function getLimit(): Expression|int|null
public function getLimit(): ExpressionInterface|int|null
{
return $this->limit;
}

public function getOffset(): Expression|int|null
public function getOffset(): ExpressionInterface|int|null
{
return $this->offset;
}
Expand Down Expand Up @@ -507,7 +506,7 @@ public function leftJoin(array|string $table, array|string $on = '', array $para
return $this->addParams($params);
}

public function limit(Expression|int|null $limit): static
public function limit(ExpressionInterface|int|null $limit): static
{
$this->limit = $limit;

Expand All @@ -528,7 +527,7 @@ public function min(string $q): int|float|null|string
return is_numeric($min) ? $min : null;
}

public function offset(Expression|int|null $offset): static
public function offset(ExpressionInterface|int|null $offset): static
{
$this->offset = $offset;

Expand Down
5 changes: 2 additions & 3 deletions src/Query/QueryInterface.php
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;

Expand Down Expand Up @@ -156,9 +155,9 @@ public function getIndexBy(): Closure|string|null;

public function getJoin(): array;

public function getLimit(): Expression|int|null;
public function getLimit(): ExpressionInterface|int|null;

public function getOffset(): Expression|int|null;
public function getOffset(): ExpressionInterface|int|null;

public function getOrderBy(): array;

Expand Down

0 comments on commit 73eb925

Please sign in to comment.