-
Notifications
You must be signed in to change notification settings - Fork 23
/
Doctrine.php
67 lines (52 loc) · 1.85 KB
/
Doctrine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace Flat3\Lodata\Drivers\SQL\PDO;
use Doctrine\DBAL\Connection as DoctrineConnection;
use Doctrine\DBAL\Driver;
use Exception;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\SqlServerConnection;
trait Doctrine
{
protected $doctrineConnection = null;
protected function getDoctrineSchemaManager()
{
$connection = $this->getDoctrineConnection();
return $connection->createSchemaManager();
}
/**
* Get the Doctrine DBAL database connection instance.
*
* @return DoctrineConnection
*/
protected function getDoctrineConnection()
{
if (is_null($this->doctrineConnection)) {
$driver = $this->getDoctrineDriver();
$connection = $this->getConnection();
$this->doctrineConnection = new DoctrineConnection(array_filter([
'pdo' => $connection->getPdo(),
'dbname' => $connection->getDatabaseName(),
'driver' => $driver->getName(),
'serverVersion' => $connection->getConfig('server_version'),
]), $driver);
}
return $this->doctrineConnection;
}
protected function getDoctrineDriver(): Driver
{
switch (true) {
case $this->getConnection() instanceof PostgresConnection:
return new PostgresDriver;
case $this->getConnection() instanceof MySqlConnection:
return new MySqlDriver;
case $this->getConnection() instanceof SQLiteConnection:
return new SQLiteDriver;
case $this->getConnection() instanceof SqlServerConnection:
return new SqlServerDriver;
}
throw new Exception('Connection not known');
}
}