connections[$name])) { return $this->connections[$name]; } elseif (empty($name) && ! empty($this->connections)) // Otherwise, return the last one { return end($this->connections); } else { throw new InvalidArgumentException("The specified connection does not exist"); } } // -------------------------------------------------------------------------- /** * Parse the passed parameters and return a connection * * @param array|object $params * @return Query_Builder * @throws BadConnectionException */ public function connect($params) { list($dsn, $dbtype, $params, $options) = $this->parse_params($params); // Create the database connection $db = ( ! empty($params->user)) ? new $dbtype($dsn, $params->user, $params->pass, $options) : new $dbtype($dsn, '', '', $options); // -------------------------------------------------------------------------- // Save connection // -------------------------------------------------------------------------- // Set the table prefix, if it exists if (isset($params->prefix)) { $db->table_prefix = $params->prefix; } // Create the Query Builder object $conn = new Query_Builder($db, $params); // Save it for later if (isset($params->alias)) { $this->connections[$params->alias] = $conn; } else { $this->connections[] = $conn; } return $conn; } // -------------------------------------------------------------------------- /** * Parses params into a dsn and option array * * @param ArrayObject $params * @throws BadDBDriverException */ private function parse_params($params) { // -------------------------------------------------------------------------- // Parse argument array / object // -------------------------------------------------------------------------- // Convert array to object if (is_array($params)) { $params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); } $params->type = strtolower($params->type); $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; // Make sure the class exists if ( ! class_exists($dbtype)) { throw new BadDBDriverException('Database driver does not exist, or is not supported'); } // Set additional PDO options $options = array(); if (isset($params->options)) { $options = (array) $params->options; } // Create the dsn for the database to connect to $dsn = $this->create_dsn($dbtype, $params); return array($dsn, $dbtype, $params, $options); } // -------------------------------------------------------------------------- /** * Create the dsn from the db type and params * * @param string $dbtype * @param array|object $params * @return string */ private function create_dsn($dbtype, $params) { // Add the driver type to the dsn $dsn = ($dbtype !== 'firebird' && $dbtype !== 'sqlite') ? strtolower($dbtype).':' : ''; if ($dbtype === 'firebird') $dsn = "{$params->host}:{$params->file}"; elseif ($dbtype === 'sqlite') $dsn = $params->file; else { if ( ! empty($params->database)) { $dsn .= "dbname={$params->database}"; } $skip = array( 'name' => 'name', 'pass' => 'pass', 'user' => 'user', 'file' => 'file', 'type' => 'type', 'prefix' => 'prefix', 'options' => 'options', 'database' => 'database' ); foreach($params as $key => $val) { if ( ! isset($skip[$key])) { $dsn .= ";{$key}={$val}"; } } } return $dsn; } }