-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDsnGenerator.php
171 lines (161 loc) · 5.46 KB
/
DsnGenerator.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Qubus\Dbal
*
* @link https://github.com/QubusPHP/dbal
* @copyright 2020
* @author Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Dbal;
class DsnGenerator
{
/**
* Generates Sql Server DSN.
*
* @param Connection $conn
* @return string
*/
public function getSqlsrvDNS(Connection $conn): string
{
$dsn = 'sqlsrv:server=';
if (isset($conn->getConfigurations()['host'])) {
$dsn .= $conn->getConfigurations()['host'];
}
if (isset($conn->getConfigurations()['port']) && ! empty($conn->getConfigurations()['port'])) {
$dsn .= ',' . $conn->getConfigurations()['port'];
}
if (isset($conn->getConfigurations()['dbname'])) {
$dsn .= ';Database=' . $conn->getConfigurations()['dbname'];
}
if (isset($conn->getConfigurations()['MultipleActiveResultSets'])) {
$dsn .= '; MultipleActiveResultSets=' . ($conn->getConfigurations()['MultipleActiveResultSets'] ? 'true' : 'false');
}
return $dsn;
}
/**
* Generates Dblib DSN.
*
* @param Connection $conn
* @return string
*/
public function getDblibDNS(Connection $conn): string
{
$dsn = 'dblib:host=';
if (isset($conn->getConfigurations()['host'])) {
$dsn .= $conn->getConfigurations()['host'];
}
if (isset($conn->getConfigurations()['port']) && ! empty($conn->getConfigurations()['port'])) {
$dsn .= ':' . $conn->getConfigurations()['port'];
}
if (isset($conn->getConfigurations()['dbname'])) {
$dsn .= ';dbname=' . $conn->getConfigurations()['dbname'];
}
return $dsn;
}
/**
* Generates Sqlite DSN.
*
* @param Connection $conn
* @return string
*/
public function getSqliteDNS(Connection $conn): string
{
$dsn = 'sqlite:';
if (isset($conn->getConfigurations()['path'])) {
$dsn .= $conn->getConfigurations()['path'];
} elseif (isset($conn->getConfigurations()['memory'])) {
$dsn .= ':memory:';
}
return $dsn;
}
/**
* Generates Postgres DSN.
*
* @param Connection $conn
* @return string
*/
public function getPgsqlDNS(Connection $conn): string
{
$dsn = 'pgsql:';
if (isset($conn->getConfigurations()['host']) && ! empty($conn->getConfigurations()['host'])) {
$dsn .= 'host=' . $conn->getConfigurations()['host'] . ' ';
}
if (isset($conn->getConfigurations()['port']) && ! empty($conn->getConfigurations()['port'])) {
$dsn .= 'port=' . $conn->getConfigurations()['port'] . ' ';
}
if (isset($conn->getConfigurations()['dbname'])) {
$dsn .= 'dbname=' . $conn->getConfigurations()['dbname'] . ' ';
} else {
// Used for temporary connections to allow operations like dropping the database currently connected to.
// Connecting without an explicit database does not work, therefore "template1" database is used
// as it is certainly present in every server setup.
$dsn .= 'dbname=template1' . ' ';
}
if (isset($conn->getConfigurations()['sslmode'])) {
$dsn .= 'sslmode=' . $conn->getConfigurations()['sslmode'] . ' ';
}
return $dsn;
}
/**
* Generates Oracle DSN.
*
* @param Connection $conn
* @return string
*/
public function getOracleDNS(Connection $conn): string
{
$dsn = 'pgsql:';
if (isset($conn->getConfigurations()['dbname'])) {
$dsn .= 'dbname=//' . $conn->getConfigurations()['dbname'];
}
}
/**
* Generates IBM DSN.
*
* @param Connection $conn
* @return string
*/
public function getIbmDNS(Connection $conn): string
{
$dsn = 'ibm:DRIVER={IBM DB2 ODBC DRIVER};';
if (isset($conn->getConfigurations()['host'])) {
$dsn .= 'HOSTNAME=' . $conn->getConfigurations()['host'] . ';';
}
if (isset($conn->getConfigurations()['port'])) {
$dsn .= 'PORT=' . $conn->getConfigurations()['port'] . ';';
}
$dsn .= 'PROTOCOL=TCPIP;';
if (isset($conn->getConfigurations()['dbname'])) {
$dsn .= 'DATABASE=' . $conn->getConfigurations()['dbname'] . ';';
}
return $dsn;
}
/**
* Generates Mysql DSN.
*
* @param Connection $conn
* @return string
*/
public function getMysqlDNS(Connection $conn): string
{
$dsn = 'mysql:';
if (isset($conn->getConfigurations()['host']) && ! empty($conn->getConfigurations()['host'])) {
$dsn .= 'host=' . $conn->getConfigurations()['host'] . ';';
}
if (isset($conn->getConfigurations()['port'])) {
$dsn .= 'port=' . $conn->getConfigurations()['port'] . ';';
}
if (isset($conn->getConfigurations()['dbname'])) {
$dsn .= 'dbname=' . $conn->getConfigurations()['dbname'] . ';';
}
if (isset($conn->getConfigurations()['unix_socket'])) {
$dsn .= 'unix_socket=' . $conn->getConfigurations()['unix_socket'] . ';';
}
if (isset($conn->getConfigurations()['charset'])) {
$dsn .= 'charset=' . $conn->getConfigurations()['charset'] . ';';
}
return $dsn;
}
}