/** * * 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); } }
/** * Display all the stuff based on pure SQL * request. * * @param string $sql the query. */ public function showDataFromSQL($query) { $tbl = new Concerto\DataTable(); if (std::beginsWith($query, '?')) { $sql = "SELECT * FROM " . substr($query, 1); } else { $sql = $query; } $rs = $this->dao->query($sql); if ($rs) { $tableColDef = []; $nb_cols = $rs->columnCount(); for ($i = 0; $i < $nb_cols; $i++) { $info = $rs->getColumnMeta($i); $tableColDef["col_{$i}"] = $info['name']; } $tbl->setColumns($tableColDef); echo $tbl->getHeader(); // Show data foreach ($rs as $row) { $data = []; for ($i = 0; $i < $nb_cols; $i++) { $data["col_{$i}"] = $row[$i]; } echo $tbl->getRow($data); } echo $tbl->getFooter(); } else { $tbl->setColumns(['state' => 'SQL State', 'code' => 'SQL Error Code', 'message' => 'SQL Error message']); echo $tbl->getHeader(); echo $tbl->getRow($this->dao->getLastError()); echo $tbl->getFooter(); } }