PMA\libraries\Table::getUniqueColumns PHP Method

getUniqueColumns() public method

returns an array with all columns with unique content, in fact these are all columns being single indexed in PRIMARY or UNIQUE e.g. - PRIMARY(id) // id - UNIQUE(name) // name - PRIMARY(fk_id1, fk_id2) // NONE - UNIQUE(x,y) // NONE
public getUniqueColumns ( boolean $backquoted = true, boolean $fullName = true ) : array
$backquoted boolean whether to quote name with backticks ``
$fullName boolean whether to include full name of the table as a prefix
return array
    public function getUniqueColumns($backquoted = true, $fullName = true)
    {
        $sql = $this->_dbi->getTableIndexesSql($this->getDbName(), $this->getName(), 'Non_unique = 0');
        $uniques = $this->_dbi->fetchResult($sql, array('Key_name', null), 'Column_name');
        $return = array();
        foreach ($uniques as $index) {
            if (count($index) > 1) {
                continue;
            }
            if ($fullName) {
                $possible_column = $this->getFullName($backquoted) . '.';
            } else {
                $possible_column = '';
            }
            if ($backquoted) {
                $possible_column .= Util::backquote($index[0]);
            } else {
                $possible_column .= $index[0];
            }
            // a column might have a primary and an unique index on it
            if (!in_array($possible_column, $return)) {
                $return[] = $possible_column;
            }
        }
        return $return;
    }

Usage Example

Example #1
0
 /**
  * Test for getUniqueColumns
  *
  * @return void
  */
 public function testGetUniqueColumns()
 {
     $table = 'PMA_BookMark';
     $db = 'PMA';
     $table = new Table($table, $db);
     $return = $table->getUniqueColumns();
     $expect = array('`PMA`.`PMA_BookMark`.`index1`', '`PMA`.`PMA_BookMark`.`index3`', '`PMA`.`PMA_BookMark`.`index5`');
     $this->assertEquals($expect, $return);
 }