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