Skip to content

Commit 218a444

Browse files
committed
feat: support new v2 api
1 parent a6a88b4 commit 218a444

8 files changed

+8
-89
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Three new methods have been added to the query builder for very easy submission
3434

3535

3636
```php
37-
// $url will be e.g. https://mysqlexplain.com/e/C0Omak70mLEXfok1a7Oo1n
37+
// $url will be e.g. https://mysqlexplain.com/explain/01j2gcrbsjet9r8rav114vgfsy
3838
$url = Film::where('description', 'like', '%astronaut%')
3939
->explainForHumans();
4040

@@ -45,8 +45,7 @@ $users = Film::where('description', 'like', '%astronaut%')
4545

4646
// URL to EXPLAIN will be printed to screen & execution is stopped
4747
$users = Film::where('description', 'like', '%astronaut%')
48-
->ddExplainForHumans()
49-
->get();
48+
->ddExplainForHumans();
5049
```
5150

5251
### Raw Queries
@@ -56,7 +55,7 @@ In some cases you are executing raw SQL queries and don't use the query builder.
5655
```php
5756
use Tpetry\MysqlExplain\Facades\MysqlExplain;
5857

59-
// $url will be e.g. https://mysqlexplain.com/e/H1pfKQ7FH3HnH87dS64Wk1
58+
// $url will be e.g. https://mysqlexplain.com/explain/01j2gctgtheyva7a7mhpv8azje
6059
$url = MysqlExplain::submitQuery(
6160
DB::connection('mysql'),
6261
'SELECT * FROM actor WHERE first_name = ?',

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,3 @@ parameters:
1414
message: "#^Parameter \\$version of class Tpetry\\\\MysqlExplain\\\\Values\\\\QueryMetrics constructor expects string, float\\|int\\|string\\|null given\\.$#"
1515
count: 1
1616
path: src/MysqlExplain.php
17-
18-
-
19-
message: "#^Property Tpetry\\\\MysqlExplain\\\\Values\\\\QueryMetrics\\:\\:\\$explainTraditional type has no value type specified in iterable type array\\.$#"
20-
count: 1
21-
path: src/Values/QueryMetrics.php
22-
23-
-
24-
message: "#^Property Tpetry\\\\MysqlExplain\\\\Values\\\\QueryMetrics\\:\\:\\$warnings type has no value type specified in iterable type array\\.$#"
25-
count: 1
26-
path: src/Values/QueryMetrics.php

src/Helpers/ApiHelper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ApiHelper
1818
private Client $client;
1919

2020
public function __construct(
21-
private ?string $domain = 'https://mysqlexplain.com',
21+
private ?string $domain = 'https://api.mysqlexplain.com',
2222
?Client $client = null,
2323
) {
2424
$this->client = $client ?? new Client();
@@ -29,18 +29,16 @@ public function submitPlan(QueryMetrics $metrics): string
2929
$version = MysqlExplain::$VERSION;
3030

3131
try {
32-
$response = $this->client->post('/api/v1/plans', [
32+
$response = $this->client->post('/v2/explains', [
3333
'base_uri' => $this->domain,
3434
'headers' => [
3535
'User-Agent' => "tpetry/laravel-mysql-explain@{$version}",
3636
],
3737
'json' => [
3838
'query' => $metrics->getQuery(),
3939
'version' => $metrics->getVersion(),
40-
'explain_traditional' => $metrics->getExplainTraditional(),
4140
'explain_json' => $metrics->getExplainJson(),
4241
'explain_tree' => $metrics->getExplainTree(),
43-
'warnings' => $metrics->getWarnings(),
4442
],
4543
]);
4644

src/Helpers/DatabaseHelper.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ public function driverName(Connection $db): string
4040
return $db->getDriverName();
4141
}
4242

43-
/**
44-
* @param mixed[] $bindings
45-
* @return array<int, array<string, int|float|string|null>>
46-
*/
47-
public function queryAssoc(Connection $db, string $sql, array $bindings = []): array
48-
{
49-
return $this->executeQuery($db, $sql, $bindings, fn (PDOStatement $statement) => $statement->fetchAll(PDO::FETCH_ASSOC));
50-
}
51-
5243
/**
5344
* @param mixed[] $bindings
5445
* @return int|float|string|null

src/MysqlExplain.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,14 @@ private function collectQueryMetrics(ConnectionInterface $connection, string $sq
6363

6464
$query = $db->buildRawSql($connection, $sql, $bindings);
6565
$version = $db->queryScalar($connection, 'SELECT VERSION()');
66-
$explainTraditional = $db->queryAssoc($connection, "EXPLAIN FORMAT=TRADITIONAL {$sql}", $bindings);
67-
$warnings = $db->queryAssoc($connection, 'SHOW WARNINGS');
6866
$explainJson = $db->queryScalar($connection, "EXPLAIN FORMAT=JSON {$sql}", $bindings);
6967
$explainTree = rescue(fn () => $db->queryScalar($connection, "EXPLAIN FORMAT=TREE {$sql}", $bindings), null, false);
7068

7169
return new QueryMetrics(
7270
query: $query,
7371
version: $version,
74-
explainTraditional: $explainTraditional,
7572
explainJson: $explainJson,
7673
explainTree: $explainTree,
77-
warnings: $warnings,
7874
);
7975
}
8076
}

src/Values/QueryMetrics.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
/**
88
* @internal
9-
*
10-
* @phpstan-type ExplainTraditional array<int, mixed>
11-
* @phpstan-type Warnings array<int, mixed>
129
*/
1310
class QueryMetrics
1411
{
@@ -17,13 +14,6 @@ class QueryMetrics
1714
*/
1815
private string $explainJSON;
1916

20-
/**
21-
* The EXPLAIN FORMAT=TRADITIONAL output.
22-
*
23-
* @type ExplainTraditional
24-
*/
25-
private array $explainTraditional;
26-
2717
/**
2818
* The EXPLAIN FORMAT=TREE output.
2919
*/
@@ -39,46 +29,23 @@ class QueryMetrics
3929
*/
4030
private string $version;
4131

42-
/**
43-
* The query metrics generated by SHOW WARNINGS.
44-
*
45-
* @type Warnings
46-
*/
47-
private ?array $warnings;
48-
49-
/**
50-
* @param ExplainTraditional $explainTraditional
51-
* @param Warnings $warnings
52-
*/
5332
public function __construct(
5433
string $query,
5534
string $version,
56-
array $explainTraditional,
5735
string $explainJson,
5836
?string $explainTree = null,
59-
?array $warnings = null,
6037
) {
6138
$this->query = $query;
6239
$this->version = $version;
63-
$this->explainTraditional = $explainTraditional;
6440
$this->explainJSON = $explainJson;
6541
$this->explainTree = $explainTree;
66-
$this->warnings = $warnings;
6742
}
6843

6944
public function getExplainJson(): string
7045
{
7146
return $this->explainJSON;
7247
}
7348

74-
/**
75-
* @return ExplainTraditional
76-
*/
77-
public function getExplainTraditional(): array
78-
{
79-
return $this->explainTraditional;
80-
}
81-
8249
public function getExplainTree(): ?string
8350
{
8451
return $this->explainTree;
@@ -93,12 +60,4 @@ public function getVersion(): string
9360
{
9461
return $this->version;
9562
}
96-
97-
/**
98-
* @return Warnings
99-
*/
100-
public function getWarnings(): ?array
101-
{
102-
return $this->warnings;
103-
}
10463
}

tests/Helpers/ApiHelperTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ public function testAaa(): void
2626
),
2727
history: $history,
2828
);
29-
$apiHelper = new ApiHelper('https://some-random-domain.local', $client);
29+
$apiHelper = new ApiHelper('https://api.some-random-domain.local', $client);
3030

3131
$queryMetrics = new QueryMetrics(
3232
query: '...query...',
3333
version: '...version...',
34-
explainTraditional: ['...explain traditional...'],
3534
explainJson: '...explain json...',
3635
explainTree: '...explain tree...',
37-
warnings: ['...warnings...'],
3836
);
3937
$url = $apiHelper->submitPlan($queryMetrics);
4038

4139
$this->assertEquals('https://dummy-url-W2lDgjGDl1.local/4XvzCcPWKW', $url);
4240
$this->assertCount(1, $history);
4341
$this->assertEquals('POST', $history[0]['request']->getMethod());
44-
$this->assertEquals('https://some-random-domain.local/api/v1/plans', (string) $history[0]['request']->getUri());
42+
$this->assertEquals('https://api.some-random-domain.local/v2/explains', (string) $history[0]['request']->getUri());
4543
$this->assertEquals(['application/json'], $history[0]['request']->getHeader('Content-Type'));
46-
$this->assertEquals('{"query":"...query...","version":"...version...","explain_traditional":["...explain traditional..."],"explain_json":"...explain json...","explain_tree":"...explain tree...","warnings":["...warnings..."]}', $history[0]['request']->getBody());
44+
$this->assertEquals('{"query":"...query...","version":"...version...","explain_json":"...explain json...","explain_tree":"...explain tree..."}', $history[0]['request']->getBody());
4745
}
4846

4947
private function createGuzzleMock(Response $response, array &$history): Client

tests/MysqlExplainTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ public function testSubmitQueryCollectsMetricsAndSubmitsThem(): void
5050
$mock->shouldReceive('queryScalar')
5151
->withArgs([$connection, 'EXPLAIN FORMAT=TREE SELECT * FROM customer WHERE last_name = ?', ['SMITH']])
5252
->andReturn('...explain tree...');
53-
$mock->shouldReceive('queryAssoc')
54-
->withArgs([$connection, 'EXPLAIN FORMAT=TRADITIONAL SELECT * FROM customer WHERE last_name = ?', ['SMITH']])
55-
->andReturn(['...explain traditional...']);
56-
$mock->shouldReceive('queryAssoc')
57-
->withArgs([$connection, 'SHOW WARNINGS'])
58-
->andReturn(['...warnings...']);
5953
});
6054
$this->mock(ApiHelper::class, function (MockInterface $mock): void {
6155
$mock->shouldReceive('submitPlan')
@@ -66,18 +60,12 @@ public function testSubmitQueryCollectsMetricsAndSubmitsThem(): void
6660
if ($arg->getVersion() !== '...version...') {
6761
return false;
6862
}
69-
if ($arg->getExplainTraditional() !== ['...explain traditional...']) {
70-
return false;
71-
}
7263
if ($arg->getExplainJson() !== '...explain json...') {
7364
return false;
7465
}
7566
if ($arg->getExplainTree() !== '...explain tree...') {
7667
return false;
7768
}
78-
if ($arg->getWarnings() !== ['...warnings...']) {
79-
return false;
80-
}
8169

8270
return true;
8371
})

0 commit comments

Comments
 (0)