| 97 | | // Make sure the connection is valid |
| 98 | | if (strpos($this->config['connection'], '://') === FALSE) |
| 99 | | throw new Kohana_Database_Exception('database.invalid_dsn', $this->config['connection']); |
| 100 | | |
| 101 | | // Parse the DSN, creating an array to hold the connection parameters |
| 102 | | $db = array |
| 103 | | ( |
| 104 | | 'type' => FALSE, |
| 105 | | 'user' => FALSE, |
| 106 | | 'pass' => FALSE, |
| 107 | | 'host' => FALSE, |
| 108 | | 'port' => FALSE, |
| 109 | | 'socket' => FALSE, |
| 110 | | 'database' => FALSE |
| 111 | | ); |
| 112 | | |
| 113 | | // Get the protocol and arguments |
| 114 | | list ($db['type'], $connection) = explode('://', $this->config['connection'], 2); |
| 115 | | |
| 116 | | if (strpos($connection, '@') !== FALSE) |
| 117 | | { |
| 118 | | // Get the username and password |
| 119 | | list ($db['pass'], $connection) = explode('@', $connection, 2); |
| 120 | | // Check if a password is supplied |
| 121 | | $logindata = explode(':', $db['pass'], 2); |
| 122 | | $db['pass'] = (count($logindata) > 1) ? $logindata[1] : ''; |
| 123 | | $db['user'] = $logindata[0]; |
| 124 | | |
| 125 | | // Prepare for finding the database |
| 126 | | $connection = explode('/', $connection); |
| 127 | | |
| 128 | | // Find the database name |
| 129 | | $db['database'] = array_pop($connection); |
| 130 | | |
| 131 | | // Reset connection string |
| 132 | | $connection = implode('/', $connection); |
| 133 | | |
| 134 | | // Find the socket |
| 135 | | if (preg_match('/^unix\([^)]++\)/', $connection)) |
| | 104 | if (is_string($this->config['connection'])) |
| | 105 | { |
| | 106 | // Make sure the connection is valid |
| | 107 | if (strpos($this->config['connection'], '://') === FALSE) |
| | 108 | throw new Kohana_Database_Exception('database.invalid_dsn', $this->config['connection']); |
| | 109 | |
| | 110 | // Parse the DSN, creating an array to hold the connection parameters |
| | 111 | $db = array |
| | 112 | ( |
| | 113 | 'type' => FALSE, |
| | 114 | 'user' => FALSE, |
| | 115 | 'pass' => FALSE, |
| | 116 | 'host' => FALSE, |
| | 117 | 'port' => FALSE, |
| | 118 | 'socket' => FALSE, |
| | 119 | 'database' => FALSE |
| | 120 | ); |
| | 121 | |
| | 122 | // Get the protocol and arguments |
| | 123 | list ($db['type'], $connection) = explode('://', $this->config['connection'], 2); |
| | 124 | |
| | 125 | if (strpos($connection, '@') !== FALSE) |
| 137 | | // This one is a little hairy: we explode based on the end of |
| 138 | | // the socket, removing the 'unix(' from the connection string |
| 139 | | list ($db['socket'], $connection) = explode(')', substr($connection, 5), 2); |
| 140 | | } |
| 141 | | elseif (strpos($connection, ':') !== FALSE) |
| 142 | | { |
| 143 | | // Fetch the host and port name |
| 144 | | list ($db['host'], $db['port']) = explode(':', $connection, 2); |
| | 127 | // Get the username and password |
| | 128 | list ($db['pass'], $connection) = explode('@', $connection, 2); |
| | 129 | // Check if a password is supplied |
| | 130 | $logindata = explode(':', $db['pass'], 2); |
| | 131 | $db['pass'] = (count($logindata) > 1) ? $logindata[1] : ''; |
| | 132 | $db['user'] = $logindata[0]; |
| | 133 | |
| | 134 | // Prepare for finding the database |
| | 135 | $connection = explode('/', $connection); |
| | 136 | |
| | 137 | // Find the database name |
| | 138 | $db['database'] = array_pop($connection); |
| | 139 | |
| | 140 | // Reset connection string |
| | 141 | $connection = implode('/', $connection); |
| | 142 | |
| | 143 | // Find the socket |
| | 144 | if (preg_match('/^unix\([^)]++\)/', $connection)) |
| | 145 | { |
| | 146 | // This one is a little hairy: we explode based on the end of |
| | 147 | // the socket, removing the 'unix(' from the connection string |
| | 148 | list ($db['socket'], $connection) = explode(')', substr($connection, 5), 2); |
| | 149 | } |
| | 150 | elseif (strpos($connection, ':') !== FALSE) |
| | 151 | { |
| | 152 | // Fetch the host and port name |
| | 153 | list ($db['host'], $db['port']) = explode(':', $connection, 2); |
| | 154 | } |
| | 155 | else |
| | 156 | { |
| | 157 | $db['host'] = $connection; |
| | 158 | } |
| 150 | | } |
| 151 | | else |
| 152 | | { |
| 153 | | // File connection |
| 154 | | $connection = explode('/', $connection); |
| 155 | | |
| 156 | | // Find database file name |
| 157 | | $db['database'] = array_pop($connection); |
| 158 | | |
| 159 | | // Find database directory name |
| 160 | | $db['socket'] = implode('/', $connection).'/'; |
| 161 | | } |
| 162 | | |
| 163 | | // Reset the connection array to the database config |
| 164 | | $this->config['connection'] = $db; |
| 165 | | |
| | 171 | |
| | 172 | // Reset the connection array to the database config |
| | 173 | $this->config['connection'] = $db; |
| | 174 | } |