33
44use PDO ;
55use PDOStatement ;
6- use PDOException ;
76use RuntimeException ;
87use InvalidArgumentException ;
98
@@ -63,14 +62,22 @@ class PdoConnection
6362 * the given database info, or the default configured info set in the database
6463 * config file if no info is given
6564 *
66- * @param array $dbInfo Database information for this connection
65+ * @param array $dbInfo Database information for this connection:
66+ * - driver DB Driver
67+ * - host DB Host
68+ * - database DB name
69+ * - user DB User
70+ * - pass DB Password
71+ * - port DB Port
72+ * - options Array of PDO connection options
6773 */
68- public function __construct (array $ dbInfo = null ) {
74+ public function __construct (array $ dbInfo )
75+ {
6976 $ this ->dbInfo = $ dbInfo ;
7077 }
7178
7279 /**
73- * Attemps to initialize a connection to the database if one does not already exist.
80+ * Attemps to initialize a connection to the database if one does not already exist
7481 *
7582 * @return PDO The PDO connection
7683 */
@@ -80,16 +87,49 @@ public function connect()
8087 if ($ connection instanceof PDO ) {
8188 return $ connection ;
8289 }
83- return $ this ->makeConnection ($ this ->dbInfo );
90+
91+ // Attempt to reuse an existing connection if one exists that matches this connection
92+ if ($ this ->reuseConnection
93+ && ($ key = array_search ($ this ->dbInfo , self ::$ dbInfos )) !== false
94+ ) {
95+ $ connection = self ::$ connections [$ key ];
96+ $ this ->setConnection ($ connection );
97+ return $ connection ;
98+ }
99+
100+ $ dsn = $ this ->makeDsn ($ this ->dbInfo );
101+ $ username = isset ($ this ->dbInfo ['user ' ])
102+ ? $ this ->dbInfo ['user ' ]
103+ : null ;
104+ $ password = isset ($ this ->dbInfo ['pass ' ])
105+ ? $ this ->dbInfo ['pass ' ]
106+ : null ;
107+ $ options = (array ) (
108+ isset ($ this ->dbInfo ['options ' ])
109+ ? $ this ->dbInfo ['options ' ]
110+ : null
111+ )
112+ + $ this ->options ;
113+
114+ $ connection = $ this ->makeConnection ($ dsn , $ username , $ password , $ options );
115+
116+ self ::$ connections [] = $ connection ;
117+ self ::$ dbInfos [] = $ this ->dbInfo ;
118+ $ this ->setConnection ($ connection );
119+
120+ return $ connection ;
84121 }
85122
86123 /**
124+ * Set whether or not to reuse an existing connection
87125 *
88126 * @param boolean $enable True to reuse an existing matching connection if available
127+ * @return PdoConnection
89128 */
90129 public function reuseConnection ($ enable )
91130 {
92131 $ this ->reuseConnection = $ enable ;
132+ return $ this ;
93133 }
94134
95135 /**
@@ -120,10 +160,12 @@ public function lastInsertId($name = null)
120160 *
121161 * @param long $attribute The attribute to set
122162 * @param int $value The value to assign to the attribute
163+ * @return PdoConnection
123164 */
124165 public function setAttribute ($ attribute , $ value )
125166 {
126167 $ this ->connect ()->setAttribute ($ attribute , $ value );
168+ return $ this ;
127169 }
128170
129171 /**
@@ -223,20 +265,12 @@ public function getConnection()
223265 * Set the PDO connection to use
224266 *
225267 * @param PDO $connection
268+ * @return PdoConnection
226269 */
227270 public function setConnection (PDO $ connection )
228271 {
229272 $ this ->connection = $ connection ;
230- }
231-
232- /**
233- * Return all registered connections available
234- *
235- * @return array
236- */
237- public function getRegisteredConnections ()
238- {
239- return $ this ->connections ;
273+ return $ this ;
240274 }
241275
242276 /**
@@ -275,71 +309,23 @@ public function makeDsn(array $db)
275309 );
276310 }
277311
278- return $ db ['driver ' ] . ":dbname= " . $ db ['database ' ] . ";host= " . $ db ['host ' ]
279- . (
280- isset ($ db ['port ' ])
281- ? ";port= " . $ db ['port ' ]
282- : ""
283- );
312+ return isset ($ db ['port ' ])
313+ ? $ db ['driver ' ] . ':host= ' . $ db ['host ' ] . ';dbname= ' . $ db ['database ' ] . ';port= ' . $ db ['port ' ]
314+ : $ db ['driver ' ] . ':host= ' . $ db ['host ' ] . ';dbname= ' . $ db ['database ' ];
284315 }
285316
286317 /**
287318 * Establish a new PDO connection using the given array of information. If
288319 * a connection already exists, no new connection will be created.
289320 *
290- * @param array $dbInfo Database information for this connection
321+ * @param string $dsn
322+ * @param string $username
323+ * @param string $password
324+ * @param array $options
291325 * @return \PDO The connection
292- * @throws RuntimeException Throw when PDOException is encountered
293326 */
294- private function makeConnection (array $ dbInfo )
327+ private function makeConnection ($ dsn , $ username , $ password , $ options )
295328 {
296- // Attempt to reuse an existing connection if one exists that matches this connection
297- if ($ this ->reuseConnection
298- && ($ key = array_search ($ dbInfo , self ::$ dbInfos )) !== false
299- ) {
300- $ this ->setConnection (self ::$ connections [$ key ]);
301- return $ this ->getConnection ();
302- }
303-
304- // Override any default settings with those provided
305- $ options = (array ) (
306- isset ($ dbInfo ['options ' ])
307- ? $ dbInfo ['options ' ]
308- : null
309- )
310- + $ this ->options ;
311-
312- try {
313- $ this ->setConnection (new PDO (
314- $ this ->makeDsn ($ dbInfo ),
315- (
316- isset ($ dbInfo ['user ' ])
317- ? $ dbInfo ['user ' ]
318- : null
319- ),
320- (
321- isset ($ dbInfo ['pass ' ])
322- ? $ dbInfo ['pass ' ]
323- : null
324- ),
325- $ options
326- ));
327- $ connection = $ this ->getConnection ();
328-
329- // Record the connection
330- self ::$ connections [] = $ connection ;
331- self ::$ dbInfos [] = $ dbInfo ;
332-
333- // Run a character set query to override the database server's default character set
334- if (!empty ($ dbInfo ['charset_query ' ])) {
335- $ this ->query ($ dbInfo ['charset_query ' ]);
336- }
337- return $ connection ;
338-
339- } catch (PDOException $ e ) {
340- throw new RuntimeException ($ e ->getMessage ());
341- }
342-
343- throw new RuntimeException ('Connection could not be established. ' );
329+ return new PDO ($ dsn , $ username , $ password , $ options );
344330 }
345331}
0 commit comments