@@ -380,7 +380,8 @@ public function __construct($params)
380380 /**
381381 * Initialize Database Settings
382382 *
383- * @return bool
383+ * @return void
384+ * @throws RuntimeException In case of failure
384385 */
385386 public function initialize ()
386387 {
@@ -392,7 +393,7 @@ public function initialize()
392393 */
393394 if ($ this ->conn_id )
394395 {
395- return TRUE ;
396+ return ;
396397 }
397398
398399 // ----------------------------------------------------------------
@@ -429,19 +430,9 @@ public function initialize()
429430 // We still don't have a connection?
430431 if ( ! $ this ->conn_id )
431432 {
432- log_message ('error ' , 'Unable to connect to the database ' );
433-
434- if ($ this ->db_debug )
435- {
436- $ this ->display_error ('db_unable_to_connect ' );
437- }
438-
439- return FALSE ;
433+ throw new RuntimeException ('Unable to connect to the database. ' );
440434 }
441435 }
442-
443- // Now we set the character set and that's all
444- return $ this ->db_set_charset ($ this ->char_set );
445436 }
446437
447438 // --------------------------------------------------------------------
@@ -516,31 +507,6 @@ public function error()
516507
517508 // --------------------------------------------------------------------
518509
519- /**
520- * Set client character set
521- *
522- * @param string
523- * @return bool
524- */
525- public function db_set_charset ($ charset )
526- {
527- if (method_exists ($ this , '_db_set_charset ' ) && ! $ this ->_db_set_charset ($ charset ))
528- {
529- log_message ('error ' , 'Unable to set database connection charset: ' .$ charset );
530-
531- if ($ this ->db_debug )
532- {
533- $ this ->display_error ('db_unable_to_set_charset ' , $ charset );
534- }
535-
536- return FALSE ;
537- }
538-
539- return TRUE ;
540- }
541-
542- // --------------------------------------------------------------------
543-
544510 /**
545511 * The name of the platform in use (mysql, mssql, etc...)
546512 *
@@ -634,7 +600,6 @@ public function query($sql, $binds = FALSE, $return_object = NULL)
634600 // cached query if it exists
635601 if ($ this ->cache_on === TRUE && $ return_object === TRUE && $ this ->_cache_init ())
636602 {
637- $ this ->load_rdriver ();
638603 if (FALSE !== ($ cache = $ this ->CACHE ->read ($ sql )))
639604 {
640605 return $ cache ;
@@ -718,9 +683,9 @@ public function query($sql, $binds = FALSE, $return_object = NULL)
718683 return TRUE ;
719684 }
720685
721- // Load and instantiate the result driver
722- $ driver = $ this ->load_rdriver () ;
723- $ RES = new $ driver ($ this );
686+ // Instantiate the driver-specific result class
687+ $ driver = ' CI_DB_ ' . $ this ->dbdriver . ' _result ' ;
688+ $ RES = new $ driver ($ this );
724689
725690 // Is query caching enabled? If so, we'll serialize the
726691 // result object and save it to a cache file.
@@ -749,26 +714,6 @@ public function query($sql, $binds = FALSE, $return_object = NULL)
749714
750715 // --------------------------------------------------------------------
751716
752- /**
753- * Load the result drivers
754- *
755- * @return string the name of the result class
756- */
757- public function load_rdriver ()
758- {
759- $ driver = 'CI_DB_ ' .$ this ->dbdriver .'_result ' ;
760-
761- if ( ! class_exists ($ driver , FALSE ))
762- {
763- require_once (BASEPATH .'database/DB_result.php ' );
764- require_once (BASEPATH .'database/drivers/ ' .$ this ->dbdriver .'/ ' .$ this ->dbdriver .'_result.php ' );
765- }
766-
767- return $ driver ;
768- }
769-
770- // --------------------------------------------------------------------
771-
772717 /**
773718 * Simple Query
774719 * This is a simplified version of the query() function. Internally
@@ -780,14 +725,7 @@ public function load_rdriver()
780725 */
781726 public function simple_query ($ sql )
782727 {
783- if ( ! $ this ->conn_id )
784- {
785- if ( ! $ this ->initialize ())
786- {
787- return FALSE ;
788- }
789- }
790-
728+ empty ($ this ->conn_id ) && $ this ->initialize ();
791729 return $ this ->_execute ($ sql );
792730 }
793731
@@ -887,7 +825,7 @@ public function trans_status()
887825 {
888826 return $ this ->_trans_status ;
889827 }
890-
828+
891829 // --------------------------------------------------------------------
892830
893831 /**
@@ -1389,10 +1327,11 @@ public function field_data($table)
13891327 *
13901328 * This function escapes column and table names
13911329 *
1392- * @param mixed
1330+ * @param mixed $item Identifier to escape
1331+ * @param bool $split Whether to split identifiers when a dot is encountered
13931332 * @return mixed
13941333 */
1395- public function escape_identifiers ($ item )
1334+ public function escape_identifiers ($ item, $ split = TRUE )
13961335 {
13971336 if ($ this ->_escape_char === '' OR empty ($ item ) OR in_array ($ item , $ this ->_reserved_identifiers ))
13981337 {
@@ -1413,22 +1352,22 @@ public function escape_identifiers($item)
14131352 return $ item ;
14141353 }
14151354
1416- static $ preg_ec = array () ;
1355+ static $ preg_ec ;
14171356
14181357 if (empty ($ preg_ec ))
14191358 {
14201359 if (is_array ($ this ->_escape_char ))
14211360 {
14221361 $ preg_ec = array (
1423- preg_quote ($ this ->_escape_char [0 ], ' / ' ),
1424- preg_quote ($ this ->_escape_char [1 ], ' / ' ),
1362+ preg_quote ($ this ->_escape_char [0 ]),
1363+ preg_quote ($ this ->_escape_char [1 ]),
14251364 $ this ->_escape_char [0 ],
14261365 $ this ->_escape_char [1 ]
14271366 );
14281367 }
14291368 else
14301369 {
1431- $ preg_ec [0 ] = $ preg_ec [1 ] = preg_quote ($ this ->_escape_char , ' / ' );
1370+ $ preg_ec [0 ] = $ preg_ec [1 ] = preg_quote ($ this ->_escape_char );
14321371 $ preg_ec [2 ] = $ preg_ec [3 ] = $ this ->_escape_char ;
14331372 }
14341373 }
@@ -1437,11 +1376,13 @@ public function escape_identifiers($item)
14371376 {
14381377 if (strpos ($ item , '. ' .$ id ) !== FALSE )
14391378 {
1440- return preg_replace ('/ ' .$ preg_ec [0 ].'?([^ ' .$ preg_ec [1 ].'\.]+) ' .$ preg_ec [1 ].'?\./ i ' , $ preg_ec [2 ].'$1 ' .$ preg_ec [3 ].'. ' , $ item );
1379+ return preg_replace ('# ' .$ preg_ec [0 ].'?([^ ' .$ preg_ec [1 ].'\.]+) ' .$ preg_ec [1 ].'?\.# i ' , $ preg_ec [2 ].'$1 ' .$ preg_ec [3 ].'. ' , $ item );
14411380 }
14421381 }
14431382
1444- return preg_replace ('/ ' .$ preg_ec [0 ].'?([^ ' .$ preg_ec [1 ].'\.]+) ' .$ preg_ec [1 ].'?(\.)?/i ' , $ preg_ec [2 ].'$1 ' .$ preg_ec [3 ].'$2 ' , $ item );
1383+ $ dot = ($ split !== FALSE ) ? '\. ' : '' ;
1384+
1385+ return preg_replace ('# ' .$ preg_ec [0 ].'?([^ ' .$ preg_ec [1 ].$ dot .']+) ' .$ preg_ec [1 ].'?(\.)?#i ' , $ preg_ec [2 ].'$1 ' .$ preg_ec [3 ].'$2 ' , $ item );
14451386 }
14461387
14471388 // --------------------------------------------------------------------
@@ -1855,14 +1796,14 @@ public function protect_identifiers($item, $prefix_single = FALSE, $protect_iden
18551796 if ($ offset = strripos ($ item , ' AS ' ))
18561797 {
18571798 $ alias = ($ protect_identifiers )
1858- ? substr ($ item , $ offset , 4 ).$ this ->escape_identifiers (substr ($ item , $ offset + 4 ))
1799+ ? substr ($ item , $ offset , 4 ).$ this ->escape_identifiers (substr ($ item , $ offset + 4 ), FALSE )
18591800 : substr ($ item , $ offset );
18601801 $ item = substr ($ item , 0 , $ offset );
18611802 }
18621803 elseif ($ offset = strrpos ($ item , ' ' ))
18631804 {
18641805 $ alias = ($ protect_identifiers )
1865- ? ' ' .$ this ->escape_identifiers (substr ($ item , $ offset + 1 ))
1806+ ? ' ' .$ this ->escape_identifiers (substr ($ item , $ offset + 1 ), FALSE )
18661807 : substr ($ item , $ offset );
18671808 $ item = substr ($ item , 0 , $ offset );
18681809 }
0 commit comments