Minor improvement to prior commit.
[openemr.git] / phpmyadmin / libraries / plugins / schema / TableStats.class.php
blob584874d72f0a27cef7eb65fe909dd3a13a9926fc
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Contains abstract class to hold table preferences/statistics
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Table preferences/statistics
15 * This class preserves the table co-ordinates,fields
16 * and helps in drawing/generating the tables.
18 * @package PhpMyAdmin
19 * @abstract
21 abstract class TableStats
23 protected $diagram;
24 protected $db;
25 protected $pageNumber;
26 protected $tableName;
28 protected $showKeys;
29 protected $tableDimension;
31 public $displayfield;
32 public $fields = array();
33 public $primary = array();
34 public $x, $y;
36 public $width = 0;
37 public $heightCell = 0;
39 protected $offline;
41 /**
42 * Constructor
44 * @param object $diagram schema diagram
45 * @param string $db current db name
46 * @param integer $pageNumber current page number (from the
47 * $cfg['Servers'][$i]['table_coords'] table)
48 * @param string $tableName table name
49 * @param boolean $showKeys whether to display keys or not
50 * @param boolean $tableDimension whether to display table position or not
51 * @param boolean $offline whether the coordinates are sent
52 * from the browser
54 public function __construct(
55 $diagram, $db, $pageNumber, $tableName, $showKeys, $tableDimension, $offline
56 ) {
57 $this->diagram = $diagram;
58 $this->db = $db;
59 $this->pageNumber = $pageNumber;
60 $this->tableName = $tableName;
62 $this->showKeys = $showKeys;
63 $this->tableDimension = $tableDimension;
65 $this->offline = $offline;
67 // checks whether the table exists
68 // and loads fields
69 $this->validateTableAndLoadFields();
70 // load table coordinates
71 $this->loadCoordinates();
72 // loads display field
73 $this->loadDisplayField();
74 // loads primary keys
75 $this->loadPrimaryKey();
78 /**
79 * Validate whether the table exists.
81 * @return void
83 protected function validateTableAndLoadFields()
85 $sql = 'DESCRIBE ' . PMA_Util::backquote($this->tableName);
86 $result = $GLOBALS['dbi']->tryQuery(
87 $sql, null, PMA_DatabaseInterface::QUERY_STORE
89 if (! $result || ! $GLOBALS['dbi']->numRows($result)) {
90 $this->showMissingTableError();
93 if ($this->showKeys) {
94 $indexes = PMA_Index::getFromTable($this->tableName, $this->db);
95 $all_columns = array();
96 foreach ($indexes as $index) {
97 $all_columns = array_merge(
98 $all_columns,
99 array_flip(array_keys($index->getColumns()))
102 $this->fields = array_keys($all_columns);
103 } else {
104 while ($row = $GLOBALS['dbi']->fetchRow($result)) {
105 $this->fields[] = $row[0];
111 * Displays an error when the table cannot be found.
113 * @return void
114 * @abstract
116 protected abstract function showMissingTableError();
119 * Loads coordinates of a table
121 * @return void
123 protected function loadCoordinates()
125 foreach ($_REQUEST['t_h'] as $key => $value) {
126 if ($this->db . '.' . $this->tableName == $key) {
127 $this->x = (double) $_REQUEST['t_x'][$key];
128 $this->y = (double) $_REQUEST['t_y'][$key];
129 break;
135 * Loads the table's display field
137 * @return void
139 protected function loadDisplayField()
141 $this->displayfield = PMA_getDisplayField($this->db, $this->tableName);
145 * Loads the PRIMARY key.
147 * @return void
149 protected function loadPrimaryKey()
151 $result = $GLOBALS['dbi']->query(
152 'SHOW INDEX FROM ' . PMA_Util::backquote($this->tableName) . ';',
153 null, PMA_DatabaseInterface::QUERY_STORE
155 if ($GLOBALS['dbi']->numRows($result) > 0) {
156 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
157 if ($row['Key_name'] == 'PRIMARY') {
158 $this->primary[] = $row['Column_name'];
165 * Returns title of the current table,
166 * title can have the dimensions/co-ordinates of the table
168 * @return string title of the current table
170 protected function getTitle()
172 return ($this->tableDimension
173 ? sprintf('%.0fx%0.f', $this->width, $this->heightCell)
174 : ''
176 . ' ' . $this->tableName;