@@ -54,10 +54,10 @@ abstract class Connection
54
54
*/
55
55
private $ logger ;
56
56
/**
57
- * The name of the protocol that is used.
57
+ * The name of the adapter that is used.
58
58
* @var string
59
59
*/
60
- public $ protocol ;
60
+ public $ adapter ;
61
61
/**
62
62
* Database's date format
63
63
* @var string
@@ -91,41 +91,49 @@ abstract class Connection
91
91
/**
92
92
* Retrieve a database connection.
93
93
*
94
- * @param string $connection_string_or_connection_name A database connection string (ex. mysql://user:pass@host[:port]/dbname)
95
- * Everything after the protocol :// part is specific to the connection adapter.
94
+ * @param string $connection_info_or_name A database connection string (ex. mysql://user:pass@host[:port]/dbname)
95
+ * Everything after the adapter :// part is specific to the connection adapter.
96
96
* OR
97
97
* A connection name that is set in ActiveRecord\Config
98
98
* If null it will use the default connection specified by ActiveRecord\Config->set_default_connection
99
99
* @return Connection
100
- * @see parse_connection_url
100
+ * @see ConnectionInfo::from_connection_url
101
101
*/
102
- public static function instance ($ connection_string_or_connection_name =null )
102
+ public static function instance ($ connection_info =null )
103
103
{
104
104
$ config = Config::instance ();
105
105
106
- if (strpos ( $ connection_string_or_connection_name , ' :// ' ) === false )
106
+ if (!( $ connection_info instanceof ConnectionInfo) )
107
107
{
108
- $ connection_string = $ connection_string_or_connection_name ?
109
- $ config ->get_connection ($ connection_string_or_connection_name ) :
110
- $ config ->get_default_connection_string ();
108
+ // Connection instantiation using a connection array
109
+ if (is_array ($ connection_info ))
110
+ {
111
+ $ connection_info = new ConnectionInfo ($ connection_info );
112
+ }
113
+ // Connection instantiation using a connection url
114
+ else if (is_string ($ connection_info ) && strpos ($ connection_info , ':// ' ) !== false )
115
+ {
116
+ $ connection_info = ConnectionInfo::from_connection_url ($ connection_info );
117
+ }
118
+ // Connection instantiation using a connection name
119
+ else
120
+ {
121
+ $ connection_info = $ connection_info ?
122
+ $ config ->get_connection_info ($ connection_info ) :
123
+ $ config ->get_default_connection_info ();
124
+ }
111
125
}
112
- else
113
- $ connection_string = $ connection_string_or_connection_name ;
114
-
115
- if (!$ connection_string )
116
- throw new DatabaseException ("Empty connection string " );
117
126
118
- $ info = static ::parse_connection_url ($ connection_string );
119
- $ fqclass = static ::load_adapter_class ($ info ->protocol );
127
+ $ fqclass = static ::load_adapter_class ($ connection_info ->adapter );
120
128
121
129
try {
122
- $ connection = new $ fqclass ($ info );
123
- $ connection ->protocol = $ info -> protocol ;
130
+ $ connection = new $ fqclass ($ connection_info );
131
+ $ connection ->adapter = $ connection_info -> adapter ;
124
132
$ connection ->logging = $ config ->get_logging ();
125
133
$ connection ->logger = $ connection ->logging ? $ config ->get_logger () : null ;
126
134
127
- if (isset ($ info ->charset ))
128
- $ connection ->set_encoding ($ info ->charset );
135
+ if (isset ($ connection_info ->charset ))
136
+ $ connection ->set_encoding ($ connection_info ->charset );
129
137
} catch (PDOException $ e ) {
130
138
throw new DatabaseException ($ e );
131
139
}
@@ -151,92 +159,6 @@ private static function load_adapter_class($adapter)
151
159
return $ fqclass ;
152
160
}
153
161
154
- /**
155
- * Use this for any adapters that can take connection info in the form below
156
- * to set the adapters connection info.
157
- *
158
- * <code>
159
- * protocol://username:password@host[:port]/dbname
160
- * protocol://urlencoded%20username:urlencoded%20password@host[:port]/dbname?decode=true
161
- * protocol://username:password@unix(/some/file/path)/dbname
162
- * </code>
163
- *
164
- * Sqlite has a special syntax, as it does not need a database name or user authentication:
165
- *
166
- * <code>
167
- * sqlite://file.db
168
- * sqlite://../relative/path/to/file.db
169
- * sqlite://unix(/absolute/path/to/file.db)
170
- * sqlite://windows(c%2A/absolute/path/to/file.db)
171
- * </code>
172
- *
173
- * @param string $connection_url A connection URL
174
- * @return object the parsed URL as an object.
175
- */
176
- public static function parse_connection_url ($ connection_url )
177
- {
178
- $ url = @parse_url ($ connection_url );
179
-
180
- if (!isset ($ url ['host ' ]))
181
- throw new DatabaseException ('Database host must be specified in the connection string. If you want to specify an absolute filename, use e.g. sqlite://unix(/path/to/file) ' );
182
-
183
- $ info = new \stdClass ();
184
- $ info ->protocol = $ url ['scheme ' ];
185
- $ info ->host = $ url ['host ' ];
186
- $ info ->db = isset ($ url ['path ' ]) ? substr ($ url ['path ' ], 1 ) : null ;
187
- $ info ->user = isset ($ url ['user ' ]) ? $ url ['user ' ] : null ;
188
- $ info ->pass = isset ($ url ['pass ' ]) ? $ url ['pass ' ] : null ;
189
-
190
- $ allow_blank_db = ($ info ->protocol == 'sqlite ' );
191
-
192
- if ($ info ->host == 'unix( ' )
193
- {
194
- $ socket_database = $ info ->host . '/ ' . $ info ->db ;
195
-
196
- if ($ allow_blank_db )
197
- $ unix_regex = '/^unix\((.+)\)\/?().*$/ ' ;
198
- else
199
- $ unix_regex = '/^unix\((.+)\)\/(.+)$/ ' ;
200
-
201
- if (preg_match_all ($ unix_regex , $ socket_database , $ matches ) > 0 )
202
- {
203
- $ info ->host = $ matches [1 ][0 ];
204
- $ info ->db = $ matches [2 ][0 ];
205
- }
206
- } elseif (substr ($ info ->host , 0 , 8 ) == 'windows( ' )
207
- {
208
- $ info ->host = urldecode (substr ($ info ->host , 8 ) . '/ ' . substr ($ info ->db , 0 , -1 ));
209
- $ info ->db = null ;
210
- }
211
-
212
- if ($ allow_blank_db && $ info ->db )
213
- $ info ->host .= '/ ' . $ info ->db ;
214
-
215
- if (isset ($ url ['port ' ]))
216
- $ info ->port = $ url ['port ' ];
217
-
218
- if (strpos ($ connection_url , 'decode=true ' ) !== false )
219
- {
220
- if ($ info ->user )
221
- $ info ->user = urldecode ($ info ->user );
222
-
223
- if ($ info ->pass )
224
- $ info ->pass = urldecode ($ info ->pass );
225
- }
226
-
227
- if (isset ($ url ['query ' ]))
228
- {
229
- foreach (explode ('/&/ ' , $ url ['query ' ]) as $ pair ) {
230
- list ($ name , $ value ) = explode ('= ' , $ pair );
231
-
232
- if ($ name == 'charset ' )
233
- $ info ->charset = $ value ;
234
- }
235
- }
236
-
237
- return $ info ;
238
- }
239
-
240
162
/**
241
163
* Class Connection is a singleton. Access it via instance().
242
164
*
@@ -257,7 +179,7 @@ protected function __construct($info)
257
179
else
258
180
$ host = "unix_socket= $ info ->host " ;
259
181
260
- $ this ->connection = new PDO ("$ info ->protocol : $ host;dbname= $ info ->db " , $ info ->user , $ info ->pass , static ::$ PDO_OPTIONS );
182
+ $ this ->connection = new PDO ("$ info ->adapter : $ host;dbname= $ info ->database " , $ info ->username , $ info ->password , static ::$ PDO_OPTIONS );
261
183
} catch (PDOException $ e ) {
262
184
throw new DatabaseException ($ e );
263
185
}
0 commit comments