|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BfwSql; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* All function to create pdo instance for all pdo drivers |
|
9
|
|
|
* @link http://php.net/manual/fr/pdo.drivers.php |
|
10
|
|
|
*/ |
|
11
|
|
|
class CreatePdoDsn |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @const ERR_UNKNOWN_FORMAT Exception code if the format is unknown. |
|
15
|
|
|
*/ |
|
16
|
|
|
const ERR_UNKNOWN_FORMAT = 2102001; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @throws \Exception Unknown DSN format |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static function unknownDsn() |
|
22
|
|
|
{ |
|
23
|
|
|
throw new Exception( |
|
24
|
|
|
'Sorry, the DSN drivers string is not declared in bfw-sql module.' |
|
25
|
|
|
.'The main raison is the author don\'t know dsn format.' |
|
26
|
|
|
.'You can create an issue on github and give the correct format or' |
|
27
|
|
|
.', better, create a pull-request.', |
|
28
|
|
|
self::ERR_UNKNOWN_FORMAT |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create the PDO instance for mysql driver |
|
34
|
|
|
* |
|
35
|
|
|
* @param object $connectionInfos All informations about the connection |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function mysql($connectionInfos): string |
|
40
|
|
|
{ |
|
41
|
|
|
$host = $connectionInfos->host; |
|
42
|
|
|
$port = $connectionInfos->port; |
|
43
|
|
|
$baseName = $connectionInfos->baseName; |
|
44
|
|
|
|
|
45
|
|
|
return 'mysql:host='.$host.';port='.$port.';dbname='.$baseName; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create the PDO instance for sqlite driver |
|
50
|
|
|
* |
|
51
|
|
|
* @param object $connectionInfos All informations about the connection |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function sqlite($connectionInfos): string |
|
56
|
|
|
{ |
|
57
|
|
|
return 'sqlite:'.$connectionInfos->filePath; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Create the PDO instance for pgsql driver |
|
62
|
|
|
* |
|
63
|
|
|
* @param object $connectionInfos All informations about the connection |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function pgsql($connectionInfos): string |
|
68
|
|
|
{ |
|
69
|
|
|
$host = $connectionInfos->host; |
|
70
|
|
|
$port = $connectionInfos->port; |
|
71
|
|
|
$baseName = $connectionInfos->baseName; |
|
72
|
|
|
|
|
73
|
|
|
return 'pgsql:host='.$host.';port='.$port.';dbname='.$baseName; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create the PDO instance for cubrid driver |
|
78
|
|
|
* |
|
79
|
|
|
* DSN find on http://php.net/manual/en/ref.pdo-cubrid.php |
|
80
|
|
|
* If is not correct, please, create a github issue. |
|
81
|
|
|
* |
|
82
|
|
|
* @param object $connectionInfos All informations about the connection |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function cubrid($connectionInfos): string |
|
87
|
|
|
{ |
|
88
|
|
|
$host = $connectionInfos->host; |
|
89
|
|
|
$port = $connectionInfos->port; |
|
90
|
|
|
$baseName = $connectionInfos->baseName; |
|
91
|
|
|
|
|
92
|
|
|
return 'cubrid:dbname='.$baseName.';host='.$host.';port='.$port; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Create the PDO instance for dblib driver |
|
97
|
|
|
* |
|
98
|
|
|
* DSN find on http://php.net/manual/fr/ref.pdo-dblib.php#118093 |
|
99
|
|
|
* If is not correct, please, create a github issue. |
|
100
|
|
|
* |
|
101
|
|
|
* @param object $connectionInfos All informations about the connection |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function dblib($connectionInfos): string |
|
106
|
|
|
{ |
|
107
|
|
|
$host = $connectionInfos->host; |
|
108
|
|
|
$port = $connectionInfos->port; |
|
109
|
|
|
$baseName = $connectionInfos->baseName; |
|
110
|
|
|
|
|
111
|
|
|
return 'dblib:host='.$host.':'.$port.';dbname='.$baseName; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Create the PDO instance for firebird driver |
|
116
|
|
|
* |
|
117
|
|
|
* @param object $connectionInfos All informations about the connection |
|
118
|
|
|
* |
|
119
|
|
|
* @throws \Exception Unknown DSN format |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function firebird($connectionInfos) |
|
122
|
|
|
{ |
|
123
|
|
|
self::unknownDsn(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Create the PDO instance for ibm driver |
|
128
|
|
|
* |
|
129
|
|
|
* @param object $connectionInfos All informations about the connection |
|
130
|
|
|
* |
|
131
|
|
|
* @throws \Exception Unknown DSN format |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function ibm($connectionInfos) |
|
134
|
|
|
{ |
|
135
|
|
|
self::unknownDsn(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Create the PDO instance for informix driver |
|
140
|
|
|
* |
|
141
|
|
|
* @param object $connectionInfos All informations about the connection |
|
142
|
|
|
* |
|
143
|
|
|
* @throws \Exception Unknown DSN format |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function informix($connectionInfos) |
|
146
|
|
|
{ |
|
147
|
|
|
self::unknownDsn(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Create the PDO instance for sqlsrv driver |
|
152
|
|
|
* |
|
153
|
|
|
* @param object $connectionInfos All informations about the connection |
|
154
|
|
|
* |
|
155
|
|
|
* @throws \Exception Unknown DSN format |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function sqlsrv($connectionInfos) |
|
158
|
|
|
{ |
|
159
|
|
|
self::unknownDsn(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Create the PDO instance for oci driver |
|
164
|
|
|
* |
|
165
|
|
|
* @param object $connectionInfos All informations about the connection |
|
166
|
|
|
* |
|
167
|
|
|
* @throws \Exception Unknown DSN format |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function oci($connectionInfos) |
|
170
|
|
|
{ |
|
171
|
|
|
self::unknownDsn(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Create the PDO instance for odbc driver |
|
176
|
|
|
* |
|
177
|
|
|
* @param object $connectionInfos All informations about the connection |
|
178
|
|
|
* |
|
179
|
|
|
* @throws \Exception Unknown DSN format |
|
180
|
|
|
*/ |
|
181
|
|
|
public static function odbc($connectionInfos) |
|
182
|
|
|
{ |
|
183
|
|
|
self::unknownDsn(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Create the PDO instance for 4d driver |
|
188
|
|
|
* |
|
189
|
|
|
* @param object $connectionInfos All informations about the connection |
|
190
|
|
|
* |
|
191
|
|
|
* @throws \Exception Unknown DSN format |
|
192
|
|
|
* |
|
193
|
|
|
//Error, function name couldn't start with a number |
|
194
|
|
|
public static function 4d($connectionInfos) |
|
195
|
|
|
{ |
|
196
|
|
|
self::unknownDsn(); |
|
197
|
|
|
} |
|
198
|
|
|
*/ |
|
199
|
|
|
} |
|
200
|
|
|
|