Skip to content

Commit

Permalink
fix: improved detection for mariadb databases
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetry committed Sep 13, 2024
1 parent 71bf4ae commit 492d45c
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/MysqlExplain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Connection;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Tpetry\MysqlExplain\Exceptions\NotMysqlException;
use Tpetry\MysqlExplain\Helpers\ApiHelper;
Expand Down Expand Up @@ -60,6 +61,17 @@ private function collectQueryMetrics(ConnectionInterface $connection, string $sq
throw NotMysqlException::create($db->driverName($connection));
}

// Laravel 11 added a new MariaDB database driver but older Laravel versions handle MySQL and MariaDB with the
// same driver. This query uses a feature implemented in MariaDB 10.10 (the first one with a different EXPLAIN
// output) to detect MariaDB which is unsupported.
try {
$db->queryScalar($connection, 'SELECT * FROM seq_1_to_1');
throw NotMysqlException::create('mariadb');
} catch (QueryException) {
// This exception is expected when using MySQL as sequence tables are not available. So the exception gets
// silenced as the check for MySQL has succeeded.
}

$query = $db->buildRawSql($connection, $sql, $bindings);
$version = $db->queryScalar($connection, 'SELECT VERSION()');
$explainJson = $db->queryScalar($connection, "EXPLAIN FORMAT=JSON {$sql}", $bindings);
Expand Down

0 comments on commit 492d45c

Please sign in to comment.