/** * * Connect to the database. * * @param array $data the data to connect the database. Can be a string (in this case, it is the DSN) * @param string $login the login (if not provided in the $data array) * @param string $password the password (if not provided in the $data array) * @throws SQLException */ protected function connect($data, $login = null, $password = null) { if (!is_array($data)) { // Apply as an array $data = ['dsn' => $data]; } $dsn = $data['dsn']; if (!$login) { $login = @$data['login']; } if (!$password) { $password = @$data['password']; } $options = []; if (@is_array($data['options'])) { $options = $data['options']; } $log = @$data['log']; if ($log) { $this->log = new \Concerto\Logger($log); } if (@$data['canLogQueries']) { if (!SQL::$showLogQueryWarning) { SQL::$showLogQueryWarning = true; $this->log->debug("The SQL queries will be displayed."); $this->canLogQueries = true; } } // echo "** $dsn **\n"; // echo "** $login **\n"; // echo "** $password **\n"; // echo "** $options **\n"; try { $this->conn = new \PDO($dsn, $login, $password, $options); if (!$this->conn) { throw new SQLException("Can not connect to database."); } } catch (\PDOException $e) { throw new SQLException("Can not connect to database.", SQL::CONNECT_ERROR, $e); } // Begin a transaction in ALL cases. $this->beginTransaction(); }
/** * * Connect to the database. * * @param array $data the data to connect the database. * @param string $login the login (if not provided in the $data array) * @param string $password the password (if not provided in the $data array) * @throws DAOException a DAOException (or a SQLException). */ protected function connect($data, $login = null, $password = null) { if (!is_array($data)) { // Apply as an array $data = ['dsn' => $data]; } try { $dsn = $data['dsn']; $this->db = parent::connect($data, $login, $password); if (Concerto\std::beginsWith($dsn, "mysql:")) { $this->converter = new MySQLConverter(); } else { $this->log("Unknown database converter -- using basic converter instead"); $this->converter = new BasicConverter(); } } catch (SQLException $e) { throw new DAOException("Can not connect to database.", SQL::CONNECT_ERROR, $e); } }