Ticket #550: Database.php.diff

File Database.php.diff, 4.8 kB (added by JAAulde, 9 months ago)

Database library changes

  • Database.php

     
    7474                        // Load the default group 
    7575                        $config = Config::item('database.default'); 
    7676                } 
     77                elseif (is_array($config) && count($config) > 0) 
     78                { 
     79                        if ( ! array_key_exists('connection', $config)) 
     80                        { 
     81                                $config = array('connection' => $config); 
     82                        } 
     83                } 
    7784                elseif (is_string($config)) 
    7885                { 
    7986                        // The config is a DSN string 
     
    95102                // Merge the default config with the passed config 
    96103                $this->config = array_merge($this->config, $config); 
    97104 
    98                 // Make sure the connection is valid 
    99                 if (strpos($this->config['connection'], '://') === FALSE) 
    100                         throw new Kohana_Database_Exception('database.invalid_dsn', $this->config['connection']); 
     105                if(is_string($this->config['connection'])) 
     106                { 
     107                        // Make sure the connection is valid 
     108                        if (strpos($this->config['connection'], '://') === FALSE) 
     109                                throw new Kohana_Database_Exception('database.invalid_dsn', $this->config['connection']); 
    101110 
    102                 // Parse the DSN, creating an array to hold the connection parameters 
    103                 $db = array 
    104                 ( 
    105                         'type'     => FALSE, 
    106                         'user'     => FALSE, 
    107                         'pass'     => FALSE, 
    108                         'host'     => FALSE, 
    109                         'port'     => FALSE, 
    110                         'socket'   => FALSE, 
    111                         'database' => FALSE 
    112                 ); 
     111                        // Parse the DSN, creating an array to hold the connection parameters 
     112                        $db = array 
     113                        ( 
     114                                'type'     => FALSE, 
     115                                'user'     => FALSE, 
     116                                'pass'     => FALSE, 
     117                                'host'     => FALSE, 
     118                                'port'     => FALSE, 
     119                                'socket'   => FALSE, 
     120                                'database' => FALSE 
     121                        ); 
    113122 
    114                 // Get the protocol and arguments 
    115                 list ($db['type'], $connection) = explode('://', $this->config['connection'], 2); 
     123                        // Get the protocol and arguments 
     124                        list ($db['type'], $connection) = explode('://', $this->config['connection'], 2); 
    116125 
    117                 if (strpos($connection, '@') !== FALSE) 
    118                 { 
    119                         // Get the username and password 
    120                         list ($db['pass'], $connection) = explode('@', $connection, 2); 
    121                         // Check if a password is supplied 
    122                         $logindata = explode(':', $db['pass'], 2); 
    123                         $db['pass'] = (count($logindata) > 1) ? $logindata[1] : ''; 
    124                         $db['user'] = $logindata[0]; 
     126                        if (strpos($connection, '@') !== FALSE) 
     127                        { 
     128                                // Get the username and password 
     129                                list ($db['pass'], $connection) = explode('@', $connection, 2); 
     130                                // Check if a password is supplied 
     131                                $logindata = explode(':', $db['pass'], 2); 
     132                                $db['pass'] = (count($logindata) > 1) ? $logindata[1] : ''; 
     133                                $db['user'] = $logindata[0]; 
    125134 
    126                         // Prepare for finding the database 
    127                         $connection = explode('/', $connection); 
     135                                // Prepare for finding the database 
     136                                $connection = explode('/', $connection); 
    128137 
    129                         // Find the database name 
    130                         $db['database'] = array_pop($connection); 
     138                                // Find the database name 
     139                                $db['database'] = array_pop($connection); 
    131140 
    132                         // Reset connection string 
    133                         $connection = implode('/', $connection); 
     141                                // Reset connection string 
     142                                $connection = implode('/', $connection); 
    134143 
    135                         // Find the socket 
    136                         if (preg_match('/^unix\([^)]++\)/', $connection)) 
    137                         { 
    138                                 // This one is a little hairy: we explode based on the end of 
    139                                 // the socket, removing the 'unix(' from the connection string 
    140                                 list ($db['socket'], $connection) = explode(')', substr($connection, 5), 2); 
     144                                // Find the socket 
     145                                if (preg_match('/^unix\([^)]++\)/', $connection)) 
     146                                { 
     147                                        // This one is a little hairy: we explode based on the end of 
     148                                        // the socket, removing the 'unix(' from the connection string 
     149                                        list ($db['socket'], $connection) = explode(')', substr($connection, 5), 2); 
     150                                } 
     151                                elseif (strpos($connection, ':') !== FALSE) 
     152                                { 
     153                                        // Fetch the host and port name 
     154                                        list ($db['host'], $db['port']) = explode(':', $connection, 2); 
     155                                } 
     156                                else 
     157                                { 
     158                                        $db['host'] = $connection; 
     159                                } 
    141160                        } 
    142                         elseif (strpos($connection, ':') !== FALSE) 
    143                         { 
    144                                 // Fetch the host and port name 
    145                                 list ($db['host'], $db['port']) = explode(':', $connection, 2); 
    146                         } 
    147161                        else 
    148162                        { 
    149                                 $db['host'] = $connection; 
     163                                // File connection 
     164                                $connection = explode('/', $connection); 
     165 
     166                                // Find database file name 
     167                                $db['database'] = array_pop($connection); 
     168 
     169                                // Find database directory name 
     170                                $db['socket'] = implode('/', $connection).'/'; 
    150171                        } 
    151                 } 
    152                 else 
    153                 { 
    154                         // File connection 
    155                         $connection = explode('/', $connection); 
    156172 
    157                         // Find database file name 
    158                         $db['database'] = array_pop($connection); 
    159  
    160                         // Find database directory name 
    161                         $db['socket'] = implode('/', $connection).'/'; 
     173                        // Reset the connection array to the database config 
     174                        $this->config['connection'] = $db; 
    162175                } 
    163  
    164                 // Reset the connection array to the database config 
    165                 $this->config['connection'] = $db; 
    166  
    167176                // Set driver name 
    168177                $driver = 'Database_'.ucfirst($this->config['connection']['type']).'_Driver'; 
    169178