N98\Util\Console\Helper\DatabaseHelper::getTables PHP Method

getTables() public method

Get list of database tables
public getTables ( boolean $withoutPrefix = null ) : array
$withoutPrefix boolean [optional] remove prefix from the returned table names. prefix is obtained from magento database configuration. defaults to false.
return array
    public function getTables($withoutPrefix = null)
    {
        $withoutPrefix = (bool) $withoutPrefix;
        $db = $this->getConnection();
        $prefix = $this->dbSettings['prefix'];
        $length = strlen($prefix);
        $columnName = 'table_name';
        $column = $columnName;
        $input = array();
        if ($withoutPrefix && $length) {
            $column = sprintf('SUBSTRING(%1$s FROM 1 + CHAR_LENGTH(:name)) %1$s', $columnName);
            $input[':name'] = $prefix;
        }
        $condition = 'table_schema = database()';
        if ($length) {
            $escape = '=';
            $condition .= sprintf(" AND %s LIKE :like ESCAPE '%s'", $columnName, $escape);
            $input[':like'] = $this->quoteLike($prefix, $escape) . '%';
        }
        $query = sprintf('SELECT %s FROM information_schema.tables WHERE %s;', $column, $condition);
        $statement = $db->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
        $result = $statement->execute($input);
        if (!$result) {
            // @codeCoverageIgnoreStart
            $this->throwRuntimeException($statement, sprintf('Failed to obtain tables from database: %s', var_export($query, true)));
        }
        // @codeCoverageIgnoreEnd
        $result = $statement->fetchAll(PDO::FETCH_COLUMN, 0);
        return $result;
    }

Usage Example

 /**
  * @param \Symfony\Component\Console\Input\InputInterface   $input
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  *
  * @throws \InvalidArgumentException
  * @return int|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->input = $input;
     $this->output = $output;
     $this->isTypeAllowed();
     $this->detectMagento($output);
     $this->dbHelper = $this->getHelper('database');
     $this->showProgress = $input->getOption('format') == null;
     if ($input->getOption('table')) {
         $resolvedTables = array($this->dbHelper->resolveTables(array('@check'), array('check' => array('tables' => $input->getOption('table')))));
         $tables = $resolvedTables[0];
     } else {
         $tables = $this->dbHelper->getTables();
     }
     $allTableStatus = $this->dbHelper->getTablesStatus();
     $tableOutput = array();
     /** @var \Symfony\Component\Console\Helper\ProgressHelper $progress */
     $progress = $this->getHelper('progress');
     if ($this->showProgress) {
         $progress->start($output, count($tables));
     }
     $methods = array('InnoDB' => 1, 'MEMORY' => 1, 'MyISAM' => 1);
     foreach ($tables as $tableName) {
         if (isset($allTableStatus[$tableName]) && isset($methods[$allTableStatus[$tableName]['Engine']])) {
             $m = '_check' . $allTableStatus[$tableName]['Engine'];
             $tableOutput = array_merge($tableOutput, $this->{$m}($tableName));
         } else {
             $tableOutput[] = array('table' => $tableName, 'operation' => 'not supported', 'type' => '', 'status' => '');
         }
         $this->progressAdvance();
     }
     if ($this->showProgress) {
         $progress->finish();
     }
     $this->getHelper('table')->setHeaders(array('Table', 'Operation', 'Type', 'Status'))->renderByFormat($this->output, $tableOutput, $this->input->getOption('format'));
 }