Skip to content

Commit 61db497

Browse files
authored
Add MariaDB driver (#50146)
1 parent 09513ac commit 61db497

31 files changed

+2742
-17
lines changed

config/database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
],
6060

6161
'mariadb' => [
62-
'driver' => 'mysql',
62+
'driver' => 'mariadb',
6363
'url' => env('DB_URL'),
6464
'host' => env('DB_HOST', '127.0.0.1'),
6565
'port' => env('DB_PORT', '3306'),

src/Illuminate/Database/Connectors/ConnectionFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Container\Container;
66
use Illuminate\Database\Connection;
7+
use Illuminate\Database\MariaDbConnection;
78
use Illuminate\Database\MySqlConnection;
89
use Illuminate\Database\PostgresConnection;
910
use Illuminate\Database\SQLiteConnection;
@@ -241,6 +242,7 @@ public function createConnector(array $config)
241242

242243
return match ($config['driver']) {
243244
'mysql' => new MySqlConnector,
245+
'mariadb' => new MariaDbConnector,
244246
'pgsql' => new PostgresConnector,
245247
'sqlite' => new SQLiteConnector,
246248
'sqlsrv' => new SqlServerConnector,
@@ -268,6 +270,7 @@ protected function createConnection($driver, $connection, $database, $prefix = '
268270

269271
return match ($driver) {
270272
'mysql' => new MySqlConnection($connection, $database, $prefix, $config),
273+
'mariadb' => new MariaDbConnection($connection, $database, $prefix, $config),
271274
'pgsql' => new PostgresConnection($connection, $database, $prefix, $config),
272275
'sqlite' => new SQLiteConnection($connection, $database, $prefix, $config),
273276
'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Connectors;
4+
5+
use PDO;
6+
7+
class MariaDbConnector extends MySqlConnector implements ConnectorInterface
8+
{
9+
/**
10+
* Get the query to enable strict mode.
11+
*
12+
* @param \PDO $connection
13+
* @param array $config
14+
* @return string
15+
*/
16+
protected function strictMode(PDO $connection, $config)
17+
{
18+
return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
19+
}
20+
}

src/Illuminate/Database/Console/DatabaseInspectionCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Database\ConnectionInterface;
7+
use Illuminate\Database\MariaDbConnection;
78
use Illuminate\Database\MySqlConnection;
89
use Illuminate\Database\PostgresConnection;
910
use Illuminate\Database\SQLiteConnection;
@@ -24,6 +25,7 @@ protected function getConnectionName(ConnectionInterface $connection, $database)
2425
return match (true) {
2526
$connection instanceof MySqlConnection && $connection->isMaria() => 'MariaDB',
2627
$connection instanceof MySqlConnection => 'MySQL',
28+
$connection instanceof MariaDbConnection => 'MariaDB',
2729
$connection instanceof PostgresConnection => 'PostgreSQL',
2830
$connection instanceof SQLiteConnection => 'SQLite',
2931
$connection instanceof SqlServerConnection => 'SQL Server',

src/Illuminate/Database/Console/DbCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function commandEnvironment(array $connection)
130130
public function getCommand(array $connection)
131131
{
132132
return [
133-
'mysql' => 'mysql',
133+
'mysql', 'mariadb' => 'mysql',
134134
'pgsql' => 'psql',
135135
'sqlite' => 'sqlite3',
136136
'sqlsrv' => 'sqlcmd',
@@ -156,6 +156,17 @@ protected function getMysqlArguments(array $connection)
156156
], $connection), [$connection['database']]);
157157
}
158158

159+
/**
160+
* Get the arguments for the MariaDB CLI.
161+
*
162+
* @param array $connection
163+
* @return array
164+
*/
165+
protected function getMariaDbArguments(array $connection)
166+
{
167+
return $this->getMysqlArguments($connection);
168+
}
169+
159170
/**
160171
* Get the arguments for the Postgres CLI.
161172
*

src/Illuminate/Database/Console/Migrations/MigrateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function repositoryExists()
148148
if (
149149
$e->getPrevious() instanceof PDOException &&
150150
$e->getPrevious()->getCode() === 1049 &&
151-
$connection->getDriverName() === 'mysql') {
151+
in_array($connection->getDriverName(), ['mysql', 'mariadb'])) {
152152
return $this->createMissingMysqlDatabase($connection);
153153
}
154154

src/Illuminate/Database/DatabaseManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,13 @@ public function setDefaultConnection($name)
361361
}
362362

363363
/**
364-
* Get all of the support drivers.
364+
* Get all of the supported drivers.
365365
*
366366
* @return string[]
367367
*/
368368
public function supportedDrivers()
369369
{
370-
return ['mysql', 'pgsql', 'sqlite', 'sqlsrv'];
370+
return ['mysql', 'mariadb', 'pgsql', 'sqlite', 'sqlsrv'];
371371
}
372372

373373
/**
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Illuminate\Database;
4+
5+
use Illuminate\Database\Query\Grammars\MariaDbGrammar as QueryGrammar;
6+
use Illuminate\Database\Query\Processors\MariaDbProcessor;
7+
use Illuminate\Database\Schema\Grammars\MariaDbGrammar as SchemaGrammar;
8+
use Illuminate\Database\Schema\MariaDbBuilder;
9+
use Illuminate\Database\Schema\MariaDbSchemaState;
10+
use Illuminate\Filesystem\Filesystem;
11+
use Illuminate\Support\Str;
12+
13+
class MariaDbConnection extends MySqlConnection
14+
{
15+
/**
16+
* Determine if the connected database is a MariaDB database.
17+
*
18+
* @return bool
19+
*/
20+
public function isMaria()
21+
{
22+
return true;
23+
}
24+
25+
/**
26+
* Get the server version for the connection.
27+
*
28+
* @return string
29+
*/
30+
public function getServerVersion(): string
31+
{
32+
return Str::between(parent::getServerVersion(), '5.5.5-', '-MariaDB');
33+
}
34+
35+
/**
36+
* Get the default query grammar instance.
37+
*
38+
* @return \Illuminate\Database\Query\Grammars\MariaDbGrammar
39+
*/
40+
protected function getDefaultQueryGrammar()
41+
{
42+
($grammar = new QueryGrammar)->setConnection($this);
43+
44+
return $this->withTablePrefix($grammar);
45+
}
46+
47+
/**
48+
* Get a schema builder instance for the connection.
49+
*
50+
* @return \Illuminate\Database\Schema\MariaDbBuilder
51+
*/
52+
public function getSchemaBuilder()
53+
{
54+
if (is_null($this->schemaGrammar)) {
55+
$this->useDefaultSchemaGrammar();
56+
}
57+
58+
return new MariaDbBuilder($this);
59+
}
60+
61+
/**
62+
* Get the default schema grammar instance.
63+
*
64+
* @return \Illuminate\Database\Schema\Grammars\MariaDbGrammar
65+
*/
66+
protected function getDefaultSchemaGrammar()
67+
{
68+
($grammar = new SchemaGrammar)->setConnection($this);
69+
70+
return $this->withTablePrefix($grammar);
71+
}
72+
73+
/**
74+
* Get the schema state for the connection.
75+
*
76+
* @param \Illuminate\Filesystem\Filesystem|null $files
77+
* @param callable|null $processFactory
78+
* @return \Illuminate\Database\Schema\MariaDbSchemaState
79+
*/
80+
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
81+
{
82+
return new MariaDbSchemaState($this, $files, $processFactory);
83+
}
84+
85+
/**
86+
* Get the default post processor instance.
87+
*
88+
* @return \Illuminate\Database\Query\Processors\MariaDbProcessor
89+
*/
90+
protected function getDefaultPostProcessor()
91+
{
92+
return new MariaDbProcessor;
93+
}
94+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Query\Grammars;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
class MariaDbGrammar extends MySqlGrammar
8+
{
9+
/**
10+
* Determine whether to use a legacy group limit clause for MySQL < 8.0.
11+
*
12+
* @param \Illuminate\Database\Query\Builder $query
13+
* @return bool
14+
*/
15+
public function useLegacyGroupLimit(Builder $query)
16+
{
17+
return false;
18+
}
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Query\Processors;
4+
5+
class MariaDbProcessor extends MySqlProcessor
6+
{
7+
//
8+
}

0 commit comments

Comments
 (0)