type = strtolower($params->type); $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; // Let the connection work with 'conn_db' or 'database' if (isset($params->database)) { $params->conn_db = $params->database; } // Add the driver type to the dsn $dsn = ($dbtype !== 'firebird' && $dbtype !== 'sqlite') ? strtolower($dbtype).':' : ''; // 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; } // -------------------------------------------------------------------------- // Attempt to connect // -------------------------------------------------------------------------- // Create the dsn for the database to connect to switch($dbtype) { default: $dsn .= "dbname={$params->conn_db}"; if ( ! empty($params->host)) { $dsn .= ";host={$params->host}"; } if ( ! empty($params->port)) { $dsn .= ";port={$params->port}"; } break; case "sqlite": $dsn .= $params->file; break; case "firebird": $dsn = "{$params->host}:{$params->file}"; break; } try { // Create the database connection $db = ( ! empty($params->user)) ? new $dbtype($dsn, $params->user, $params->pass, $options) : new $dbtype($dsn, '', '', $options); } catch(Exception $e) { throw new BadConnectionException('Connection failed, invalid arguments', 2); } // -------------------------------------------------------------------------- // 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)) { $connections[$params->alias] = $conn; } else { $connections[] = $conn; } // Return the Query Builder object return $conn; } // End of common.php