1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\TDBM; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventManager; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\DriverManager; |
8
|
|
|
use Doctrine\DBAL\Event\Listeners\OracleSessionInit; |
9
|
|
|
|
10
|
|
|
class ConnectionFactory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Resets the database and returns a (root) connection |
14
|
|
|
*/ |
15
|
|
|
public static function resetDatabase( |
16
|
|
|
string $dbDriver, |
17
|
|
|
?string $dbHost = null, |
18
|
|
|
?string $dbPort = null, |
19
|
|
|
?string $dbUserName = null, |
20
|
|
|
?string $dbAdminUserName = null, |
21
|
|
|
?string $dbPassword = null, |
22
|
|
|
?string $dbName = null |
23
|
|
|
): Connection { |
24
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
25
|
|
|
|
26
|
|
|
if ($dbDriver === 'pdo_sqlite') { |
27
|
|
|
$dbConnection = self::getConnection(); |
|
|
|
|
28
|
|
|
$dbConnection->exec('PRAGMA foreign_keys = ON;'); |
29
|
|
|
} elseif ($dbDriver === 'oci8') { |
30
|
|
|
$adminConn = self::createConnection($dbDriver, $dbHost, $dbPort, $dbAdminUserName, $dbPassword, $dbAdminUserName); |
31
|
|
|
|
32
|
|
|
// When dropAndCreateDatabase is run several times, Oracle can have some issues releasing the TDBM user. |
33
|
|
|
// Let's forcefully delete the connection! |
34
|
|
|
//$adminConn->exec("select 'alter system kill session ''' || sid || ',' || serial# || ''';' from v\$session where username = '".strtoupper($dbName)."'"); |
35
|
|
|
|
36
|
|
|
$adminConn->getSchemaManager()->dropAndCreateDatabase($dbName); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$dbConnection = self::createConnection($dbDriver, $dbHost, $dbPort, $dbName, $dbPassword, $dbName); |
39
|
|
|
} else { |
40
|
|
|
$connectionParams = array( |
41
|
|
|
'user' => $dbUserName, |
42
|
|
|
'password' => $dbPassword, |
43
|
|
|
'host' => $dbHost, |
44
|
|
|
'port' => $dbPort, |
45
|
|
|
'driver' => $dbDriver, |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$adminConn = DriverManager::getConnection($connectionParams, $config); |
49
|
|
|
|
50
|
|
|
$adminConn->getSchemaManager()->dropAndCreateDatabase($dbName); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$connectionParams['dbname'] = $dbName; |
53
|
|
|
|
54
|
|
|
$dbConnection = DriverManager::getConnection($connectionParams, $config); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $dbConnection; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function createConnection( |
61
|
|
|
string $dbDriver, |
62
|
|
|
?string $dbHost = null, |
63
|
|
|
?string $dbPort = null, |
64
|
|
|
?string $dbUserName = null, |
65
|
|
|
?string $dbPassword = null, |
66
|
|
|
?string $dbName = null |
67
|
|
|
): Connection { |
68
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
69
|
|
|
|
70
|
|
|
$dbDriver = $dbDriver; |
71
|
|
|
|
72
|
|
|
if ($dbDriver === 'pdo_sqlite') { |
73
|
|
|
$connectionParams = array( |
74
|
|
|
'memory' => true, |
75
|
|
|
'driver' => 'pdo_sqlite', |
76
|
|
|
); |
77
|
|
|
$dbConnection = DriverManager::getConnection($connectionParams, $config); |
78
|
|
|
} elseif ($dbDriver === 'oci8') { |
79
|
|
|
$evm = new EventManager(); |
80
|
|
|
$evm->addEventSubscriber(new OracleSessionInit(array( |
|
|
|
|
81
|
|
|
'NLS_TIME_FORMAT' => 'HH24:MI:SS', |
82
|
|
|
'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', |
83
|
|
|
'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', |
84
|
|
|
))); |
85
|
|
|
|
86
|
|
|
$connectionParams = array( |
87
|
|
|
'servicename' => 'XE', |
88
|
|
|
'user' => $dbUserName, |
89
|
|
|
'password' => $dbPassword, |
90
|
|
|
'host' => $dbHost, |
91
|
|
|
'port' => $dbPort, |
92
|
|
|
'driver' => $dbDriver, |
93
|
|
|
'dbname' => $dbName, |
94
|
|
|
'charset' => 'AL32UTF8', |
95
|
|
|
); |
96
|
|
|
$dbConnection = DriverManager::getConnection($connectionParams, $config, $evm); |
97
|
|
|
$dbConnection->setAutoCommit(true); |
98
|
|
|
} else { |
99
|
|
|
$connectionParams = array( |
100
|
|
|
'user' => $dbUserName, |
101
|
|
|
'password' => $dbPassword, |
102
|
|
|
'host' => $dbHost, |
103
|
|
|
'port' => $dbPort, |
104
|
|
|
'driver' => $dbDriver, |
105
|
|
|
'dbname' => $dbName, |
106
|
|
|
); |
107
|
|
|
$dbConnection = DriverManager::getConnection($connectionParams, $config); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $dbConnection; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.