Skip to content

Commit

Permalink
feat: new implementation based on tpetry/php-mysql-explain
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetry committed Sep 27, 2024
1 parent 1a00db4 commit 0b2c028
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 430 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
database:
image: container-registry.oracle.com/mysql/community-server:8.0
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
env:
MYSQL_DATABASE: testing
MYSQL_USER: testing
MYSQL_PASSWORD: testing
ports:
- 3306:3306
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -68,3 +78,7 @@ jobs:

- name: Execute tests
run: vendor/bin/phpunit
env:
DB_DATABASE: testing
DB_USERNAME: testing
DB_PASSWORD: testing
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ 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).

## [2.0.0] - 2024-09-27
### Changed
* New implementation based on tpetry/php-mysql-explain
* Switched namespace from `Tpetry\MysqlExplain` to `Tpetry\LaravelMysqlExplain`

## [1.3.2] - 2024-09-13
### Changed
* Improved the detection for MariaDB databases
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Film::where('description', 'like', '%astronaut%')
In some cases you are executing raw SQL queries and don't use the query builder. You can use the `MysqlExplain` facade to get the EXPLAIN url for them:

```php
use Tpetry\MysqlExplain\Facades\MysqlExplain;
use Tpetry\LaravelMysqlExplain\Facades\MysqlExplain;

// $url will be e.g. https://mysqlexplain.com/explain/01j2gctgtheyva7a7mhpv8azje
$url = MysqlExplain::submitQuery(
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"require": {
"php": "^8.0",
"ext-pdo": "*",
"guzzlehttp/guzzle": "^6.0|^7.0",
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0"
"guzzlehttp/guzzle": "^7.0",
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"tpetry/php-mysql-explain": "^1.0"
},
"require-dev": {
"mockery/mockery": "^1.6",
Expand All @@ -32,12 +33,12 @@
},
"autoload": {
"psr-4": {
"Tpetry\\MysqlExplain\\": "src/"
"Tpetry\\LaravelMysqlExplain\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tpetry\\MysqlExplain\\Tests\\": "tests/"
"Tpetry\\LaravelMysqlExplain\\Tests\\": "tests/"
}
},
"scripts": {
Expand All @@ -56,10 +57,10 @@
"extra": {
"laravel": {
"providers": [
"Tpetry\\MysqlExplain\\MysqlExplainServiceProvider"
"Tpetry\\LaravelMysqlExplain\\MysqlExplainServiceProvider"
],
"aliases": {
"MysqlExplain": "Tpetry\\MysqlExplain\\Facades\\MysqlExplain"
"MysqlExplain": "Tpetry\\LaravelMysqlExplain\\Facades\\MysqlExplain"
}
}
},
Expand Down
21 changes: 0 additions & 21 deletions phpstan-baseline.neon

This file was deleted.

3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
includes:
- phpstan-baseline.neon

parameters:
level: max
paths:
Expand Down
6 changes: 3 additions & 3 deletions src/Facades/MysqlExplain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

declare(strict_types=1);

namespace Tpetry\MysqlExplain\Facades;
namespace Tpetry\LaravelMysqlExplain\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @method static string submitBuilder(\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|\Illuminate\Contracts\Database\Query\Builder $builder)
* @method static string submitQuery(\Illuminate\Database\ConnectionInterface $connection, string $sql, mixed[] $bindings = [])
*
* @see \Tpetry\MysqlExplain\MysqlExplain
* @see \Tpetry\LaravelMysqlExplain\MysqlExplain
*/
class MysqlExplain extends Facade
{
protected static function getFacadeAccessor()
{
return \Tpetry\MysqlExplain\MysqlExplain::class;
return \Tpetry\LaravelMysqlExplain\MysqlExplain::class;
}
}
56 changes: 0 additions & 56 deletions src/Helpers/ApiHelper.php

This file was deleted.

59 changes: 0 additions & 59 deletions src/Helpers/DatabaseHelper.php

This file was deleted.

49 changes: 49 additions & 0 deletions src/LaravelQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tpetry\LaravelMysqlExplain;

use Illuminate\Database\ConnectionInterface;
use Tpetry\PhpMysqlExplain\Queries\QueryInterface;

class LaravelQuery implements QueryInterface
{
/**
* @param array<array-key,mixed> $parameters
*/
public function __construct(
private ConnectionInterface $connection,
private string $sql,
private array $parameters,
) {}

public function execute(string $sql, bool $useParams): array
{
$rows = match ($useParams) {
true => $this->connection->select($sql, $this->parameters),
false => $this->connection->select($sql),
};

// Laravel creates array<object> instead of array<arrays> as requested by interface.
$rows = array_map(fn ($row) => (array) $row, $rows);

return $rows;
}

public function getParameters(): array
{
// Transform special values like DateTimeInterface and bool to their string value.
return $this->connection->prepareBindings($this->parameters);
}

public function getSql(): string
{
return $this->sql;
}

public function name(): string
{
return 'laravel@'.MysqlExplain::$VERSION;
}
}
24 changes: 8 additions & 16 deletions src/Mixins/BuilderMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Tpetry\MysqlExplain\Mixins;
namespace Tpetry\LaravelMysqlExplain\Mixins;

use Closure;
use Tpetry\MysqlExplain\Facades\MysqlExplain;
use Tpetry\LaravelMysqlExplain\Facades\MysqlExplain;

/**
* @internal
Expand All @@ -17,8 +17,7 @@ public function ddExplainForHumans(): Closure
/** @return never-returns */
return function () {
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
$url = MysqlExplain::submitBuilder($this);
dd($url);
dd(MysqlExplain::submitBuilder($this));
};
}

Expand All @@ -27,17 +26,15 @@ public function ddVisualExplain(): Closure
/** @return never-returns */
return function () {
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
$url = MysqlExplain::submitBuilder($this);
dd($url);
dd(MysqlExplain::submitBuilder($this));
};
}

public function dumpExplainForHumans(): Closure
{
return function () {
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
$url = MysqlExplain::submitBuilder($this);
dump($url);
dump(MysqlExplain::submitBuilder($this));

return $this;
};
Expand All @@ -47,8 +44,7 @@ public function dumpVisualExplain(): Closure
{
return function () {
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
$url = MysqlExplain::submitBuilder($this);
dump($url);
dump(MysqlExplain::submitBuilder($this));

return $this;
};
Expand All @@ -58,19 +54,15 @@ public function explainForHumans(): Closure
{
return function (): string {
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
$url = MysqlExplain::submitBuilder($this);

return $url;
return MysqlExplain::submitBuilder($this);
};
}

public function visualExplain(): Closure
{
return function (): string {
/** @var \Illuminate\Contracts\Database\Query\Builder $this */
$url = MysqlExplain::submitBuilder($this);

return $url;
return MysqlExplain::submitBuilder($this);
};
}
}
Loading

0 comments on commit 0b2c028

Please sign in to comment.