diagram = $diagram; $this->db = $db; $this->pageNumber = $pageNumber; $this->tableName = $tableName; $this->showKeys = $showKeys; $this->tableDimension = $tableDimension; $this->offline = $offline; $this->relation = new Relation($dbi); $this->font = new Font(); // checks whether the table exists // and loads fields $this->validateTableAndLoadFields(); // load table coordinates $this->loadCoordinates(); // loads display field $this->loadDisplayField(); // loads primary keys $this->loadPrimaryKey(); } /** * Validate whether the table exists. */ protected function validateTableAndLoadFields(): void { global $dbi; $sql = 'DESCRIBE ' . Util::backquote($this->tableName); $result = $dbi->tryQuery($sql); if (! $result || ! $result->numRows()) { $this->showMissingTableError(); exit; } if ($this->showKeys) { $indexes = Index::getFromTable($this->tableName, $this->db); $all_columns = []; foreach ($indexes as $index) { $all_columns = array_merge( $all_columns, array_flip(array_keys($index->getColumns())) ); } $this->fields = array_keys($all_columns); } else { $this->fields = $result->fetchAllColumn(); } } /** * Displays an error when the table cannot be found. * * @abstract */ abstract protected function showMissingTableError(): void; /** * Loads coordinates of a table */ protected function loadCoordinates(): void { if (! isset($_POST['t_h']) || ! is_array($_POST['t_h'])) { return; } foreach (array_keys($_POST['t_h']) as $key) { $db = rawurldecode($_POST['t_db'][$key]); $tbl = rawurldecode($_POST['t_tbl'][$key]); if ($this->db . '.' . $this->tableName === $db . '.' . $tbl) { $this->x = (float) $_POST['t_x'][$key]; $this->y = (float) $_POST['t_y'][$key]; break; } } } /** * Loads the table's display field */ protected function loadDisplayField(): void { $this->displayfield = $this->relation->getDisplayField($this->db, $this->tableName); } /** * Loads the PRIMARY key. */ protected function loadPrimaryKey(): void { global $dbi; $result = $dbi->query('SHOW INDEX FROM ' . Util::backquote($this->tableName) . ';'); if ($result->numRows() <= 0) { return; } while ($row = $result->fetchAssoc()) { if ($row['Key_name'] !== 'PRIMARY') { continue; } $this->primary[] = $row['Column_name']; } } /** * Returns title of the current table, * title can have the dimensions/co-ordinates of the table * * @return string title of the current table */ protected function getTitle() { return ($this->tableDimension ? sprintf('%.0fx%0.f', $this->width, $this->heightCell) : '' ) . ' ' . $this->tableName; } }