<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016
 *
 * @see      https://www.github.com/janhuang
 * @see      http://www.fast-d.cn/
 */

namespace FastD\Migration;

use PDO;

/**
 * Class Migration
 * @package FastD\Migration
 */
class TableBuilder
{
    /**
     * @var string
     */
    protected $cachePath = __DIR__ . '/.cache/tables';

    /**
     * @var PDO
     */
    protected $pdo;

    /**
     * @var string
     */
    protected $sql;

    /**
     * @var Table
     */
    protected $table;

    /**
     * Migration constructor.
     * @param PDO|null $pdo
     */
    public function __construct(PDO $pdo = null)
    {
        $this->pdo = $pdo;
    }

    /**
     * @param Table $table
     * @return bool|mixed
     */
    final protected function getCache(Table $table)
    {
        if (!$this->hasCache($table)) {
            return false;
        }
        $cache = file_get_contents($this->cachePath . '/' . $table->getTableName());

        return unserialize($cache);
    }

    /**
     * @param Table $table
     * @return bool|int
     */
    final protected function saveCache(Table $table)
    {
        $cacheFile = $this->cachePath . '/' . $table->getTableName();

        return file_put_contents($cacheFile, serialize($table->getColumns()));
    }

    /**
     * @param Table $table
     * @return bool
     */
    final protected function hasCache(Table $table)
    {
        return file_exists($this->cachePath . '/' . $table->getTableName());
    }

    /**
     * @param Table $table
     */
    final protected function clearCache(Table $table)
    {
        $cacheFile = $this->cachePath . '/' . $table->getTableName();

        if (file_exists($cacheFile)) {
            unlink($cacheFile);
        }
    }

    /**
     * @param $table
     * @return Table[]
     */
    public function extract($table = null)
    {
        $sql = 'SHOW TABLES';
        if (!empty($table)) {
            $sql .= ' LIKE "' . $table . '"';
        }

        $tables = $this->pdo
            ->query($sql)
            ->fetchAll();
        $result = [];
        foreach ($tables as $key => $table) {
            $table = array_values($table)[0];
            $result[$table] = $this->parseTable($table);
        }

        return $result;
    }

    /**
     * @param $name
     * @return Table
     */
    protected function parseTable($name)
    {
        $table = new Table($name);

        if (!empty(($columns = $this->showTableSchema($table->getTableName())))) {
            foreach ($columns as $column) {
                $column = $this->parseColumn($column);<commit_msg>fixed migrate bug<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016
 *
 * @see      https://www.github.com/janhuang
 * @see      http://www.fast-d.cn/
 */

namespace FastD\Migration;

use PDO;

/**
 * Class Migration
 * @package FastD\Migration
 */
class TableBuilder
{
    /**
     * @var string
     */
    protected $cachePath = __DIR__ . '/.cache/tables';

    /**
     * @var PDO
     */
    protected $pdo;

    /**
     * @var string
     */
    protected $sql;

    /**
     * @var Table
     */
    protected $table;

    /**
     * Migration constructor.
     * @param PDO|null $pdo
     */
    public function __construct(PDO $pdo = null)
    {
        $this->pdo = $pdo;
    }

    /**
     * @param Table $table
     * @return bool|mixed
     */
    final protected function getCache(Table $table)
    {
        if (!$this->hasCache($table)) {
            return false;
        }
        $cache = file_get_contents($this->cachePath . '/' . $table->getTableName());

        return unserialize($cache);
    }

    /**
     * @param Table $table
     * @return bool|int
     */
    final protected function saveCache(Table $table)
    {
        $cacheFile = $this->cachePath . '/' . $table->getTableName();

        return file_put_contents($cacheFile, serialize($table->getColumns()));
    }

    /**
     * @param Table $table
     * @return bool
     */
    final protected function hasCache(Table $table)
    {
        return file_exists($this->cachePath . '/' . $table->getTableName());
    }

    /**
     * @param Table $table
     */
    final protected function clearCache(Table $table)
    {
        $cacheFile = $this->cachePath . '/' . $table->getTableName();

        if (file_exists($cacheFile)) {
            unlink($cacheFile);
        }
    }

    /**
     * @param $table
     * @return Table[]
     */
    public function extract($table = null)
    {
        $sql = 'SHOW TABLES';
        if (!empty($table)) {
            $sql .= ' LIKE "' . $table . '"';
        }

        $tables = $this->pdo
            ->query($sql)
            ->fetchAll();
        $result = [];
        foreach ($tables as $table) {
            $table = array_values($table)[0];
            $result[$table] = $this->parseTable($table);
            $this->saveCache($result[$table]);
        }

        return $result;
    }

    /**
     * @param $name
     * @return Table
     */
    protected function parseTable($name)
    {
        $table = new Table($name);

        if (!empty(($columns = $this->showTableSchema($table->getTableName())))) {
            foreach ($columns as $column) {
                $column = $this->parseColumn($column);
{'fraction_non_alphanumeric': 0.16374599585453176, 'fraction_numerical': 0.001884303749764462, 'mean_word_length': 1.7718015665796345, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6052153', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1611, 'n_words': 327}