Skip to content

Commit b0bacb3

Browse files
committed
support mariadb driver
1 parent bfe061f commit b0bacb3

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

Diff for: src/Connectors/ConnectionFactory.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Hoyvoy\CrossDatabase\Connectors;
44

5+
use Hoyvoy\CrossDatabase\MariaDbConnection;
56
use Hoyvoy\CrossDatabase\MySqlConnection;
67
use Hoyvoy\CrossDatabase\PostgresConnection;
78
use Hoyvoy\CrossDatabase\SqlServerConnection;
89
use Illuminate\Database\Connection;
9-
use Illuminate\Database\Connectors\ConnectionFactory as IlluminateConnectionFactory;
10-
use PDO;
1110

12-
class ConnectionFactory extends IlluminateConnectionFactory
11+
class ConnectionFactory extends \Illuminate\Database\Connectors\ConnectionFactory
1312
{
1413
/**
1514
* @param string $driver
16-
* @param \Closure|PDO $connection
15+
* @param \Closure|\PDO $connection
1716
* @param string $database
1817
* @param string $prefix
1918
* @param array $config
@@ -26,14 +25,12 @@ protected function createConnection($driver, $connection, $database, $prefix = '
2625
return $resolver($connection, $database, $prefix, $config);
2726
}
2827

29-
switch ($driver) {
30-
case 'mysql':
31-
return new MySqlConnection($connection, $database, $prefix, $config);
32-
case 'pgsql':
33-
return new PostgresConnection($connection, $database, $prefix, $config);
34-
case 'sqlsrv':
35-
return new SqlServerConnection($connection, $database, $prefix, $config);
36-
}
28+
return match ($driver) {
29+
'mysql' => new MySqlConnection($connection, $database, $prefix, $config),
30+
'mariadb' => new MariaDbConnection($connection, $database, $prefix, $config),
31+
'pgsql' => new PostgresConnection($connection, $database, $prefix, $config),
32+
'sqlsrv' => new SqlServerConnection($connection, $database, $prefix, $config),
33+
};
3734

3835
return parent::createConnection($driver, $connection, $database, $prefix, $config);
3936
}

Diff for: src/MariaDbConnection.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Hoyvoy\CrossDatabase;
4+
5+
class MariaDbConnection extends \Illuminate\Database\MariaDbConnection implements CanCrossDatabaseShazaamInterface
6+
{
7+
/**
8+
* Get the default query grammar instance.
9+
*
10+
* @return \Illuminate\Database\Query\Grammars\MySqlGrammar
11+
*/
12+
protected function getDefaultQueryGrammar()
13+
{
14+
return $this->withTablePrefix(new \Hoyvoy\CrossDatabase\Query\Grammars\MariaDbGrammar());
15+
}
16+
}

Diff for: src/Query/Grammars/MariaDbGrammar.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Hoyvoy\CrossDatabase\Query\Grammars;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
class MariaDbGrammar extends \Illuminate\Database\Query\Grammars\MariaDbGrammar
8+
{
9+
/**
10+
* Compile the "from" portion of the query.
11+
*
12+
* @param \Illuminate\Database\Query\Builder $query
13+
* @param string $table
14+
*
15+
* @return string
16+
*/
17+
protected function compileFrom(Builder $query, $table)
18+
{
19+
// Check for cross database query to attach database name
20+
if (strpos($table, '<-->') !== false) {
21+
list($prefix, $table, $database) = explode('<-->', $table);
22+
$wrappedTable = $this->wrapTable($table, true);
23+
$wrappedTablePrefixed = $this->wrap($prefix.$table, true);
24+
if ($wrappedTable != $wrappedTablePrefixed) {
25+
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed.' as '.$wrappedTable;
26+
}
27+
28+
return 'from '.$this->wrap($database).'.'.$wrappedTablePrefixed;
29+
}
30+
31+
return 'from '.$this->wrapTable($table);
32+
}
33+
}

0 commit comments

Comments
 (0)