Skip to content

Commit

Permalink
Merge pull request #1 from shipmonk-rnd/initialize
Browse files Browse the repository at this point in the history
Initial version
  • Loading branch information
janedbal authored Nov 29, 2023
2 parents 66660f4 + d0747e4 commit 2d8fe0b
Show file tree
Hide file tree
Showing 19 changed files with 5,743 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4

10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
* text=auto

/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
-
package-ecosystem: composer
directory: "/"
schedule:
interval: monthly
versioning-strategy: lockfile-only
103 changes: 103 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Checks
on:
pull_request:
push:
branches:
- "master"
- "v[0-9]"
jobs:
checks:
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
-
name: Checkout code
uses: actions/checkout@v4
-
name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
-
name: Install dependencies
run: composer install --no-progress --prefer-dist --no-interaction

-
name: Install extra tools
run: |
wget -q https://github.com/composer-unused/composer-unused/releases/latest/download/composer-unused.phar
wget -q https://github.com/maglnet/ComposerRequireChecker/releases/download/4.5.0/composer-require-checker.phar
-
name: Check composer normalization
run: composer check:composer

-
name: Check editorconfig
run: composer check:ec

-
name: Check coding style
run: composer check:cs

-
name: Check unused dependencies
run: php composer-unused.phar --output-format=default

-
name: Check shadow dependencies
run: php composer-require-checker.phar check composer.json

phpstan:
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
-
name: Checkout code
uses: actions/checkout@v4
-
name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
-
name: Install dependencies
run: composer install --no-progress --prefer-dist --no-interaction

-
name: Run PHPStan (latest deps)
run: composer check:types

-
name: Install lowest dependencies
run: composer update --no-progress --prefer-lowest --prefer-dist --no-interaction

-
name: Run PHPStan (lowest deps)
run: composer check:types

tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ]
dependency-version: [ prefer-lowest, prefer-stable ]
steps:
-
name: Checkout code
uses: actions/checkout@v4
-
name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
-
name: Update dependencies
run: composer update --no-progress --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

-
name: Run tests
run: composer check:tests
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/cache/*
!/cache/.gitkeep
/.idea
/phpstan.neon
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## SqlWalker for Doctrine allowing multiple handlers to modify resulting SQL

Since Doctrine's [SqlWalker](https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/cookbook/dql-custom-walkers.html#modify-the-output-walker-to-generate-vendor-specific-sql) serves as a translator from DQL AST to SQL,
it becomes problematic when you want to alter resulting SQL within multiple libraries by such approach.
There just can be only single SqlWalker.

This library solves this issue, by providing `HintHandler` base class which is designed for SQL modification
and can be used multiple times in `$queryBuilder->setHint()`.

```php
$queryBuilder
->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, HintDrivenSqlWalker::class)
->setHint(MaxExecutionTimeHintHandler::class, 1000)
```

Where `MaxExecutionTimeHintHandler` just extends our `HintHandler` and picks some `SqlNode` to hook to and alters appropriate SQL part:

```php
class MaxExecutionTimeSqlWalker extends HintHandler
{

public function getNodes(): array
{
return [SqlNode::SelectClause];
}

public function processNode(
SqlNode $sqlNode,
string $sql,
): string
{
// grab the 1000 passed to ->setHint()
$milliseconds = $this->getHintValue();

// edit SQL as needed
return preg_replace(
'~^SELECT (.*?)~',
"SELECT /*+ MAX_EXECUTION_TIME($milliseconds) */ \\1 ",
$sql
);
}
```

SqlNode is an enum of all walkXxx methods in Doctrine's SqlWalker, so you are able to intercept any part of AST processing the SqlWalker does.

### Implementors
- shipmonk/doctrine-mysql-optimizer-hints (since v2)
- shipmonk/doctrine-mysql-index-hints (since v3)
Empty file added cache/.gitkeep
Empty file.
54 changes: 54 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "shipmonk/doctrine-hint-driven-sql-walker",
"description": "Doctrine's SqlWalker that allows hooking multiple handlers via ->setHint() while each can edit produced SQL or its part.",
"license": [
"MIT"
],
"require": {
"php": "^7.2 || ^8.0",
"doctrine/orm": "^2.14.0"
},
"require-dev": {
"doctrine/annotations": "^1.14.3",
"editorconfig-checker/editorconfig-checker": "^10.4.0",
"ergebnis/composer-normalize": "^2.19.0",
"phpstan/phpstan": "^1.10.38",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-strict-rules": "^1.5.1",
"phpunit/phpunit": "^8.5.34",
"slevomat/coding-standard": "^8.14.1"
},
"autoload": {
"psr-4": {
"ShipMonk\\Doctrine\\Walker\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ShipMonk\\Doctrine\\Walker\\": "tests/"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false,
"ergebnis/composer-normalize": true,
"ocramius/package-versions": false
},
"sort-packages": true
},
"scripts": {
"check": [
"@check:composer",
"@check:ec",
"@check:cs",
"@check:types",
"@check:tests"
],
"check:composer": "composer normalize --dry-run --no-check-lock --no-update-lock",
"check:cs": "phpcs",
"check:ec": "ec src tests",
"check:tests": "phpunit -vvv tests",
"check:types": "phpstan analyse -vvv",
"fix:cs": "phpcbf"
}
}
Loading

0 comments on commit 2d8fe0b

Please sign in to comment.