<commit_before><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/10/11
 * Time: 上午1:13
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 * WebSite: http://www.janhuang.me
 */

namespace FastD\Database\ORM\Generator;

use FastD\Database\Drivers\DriverInterface;
use FastD\Database\ORM\Parser\DBParser;
use FastD\Database\ORM\Parser\TableParser;

/**
 * Class Builder
 *
 * @package FastD\Database\ORM\Mapper
 */
class Mapping
{
    /**
     * @var DriverInterface
     */
    protected $driver;

    /**
     * @var DBParser
     */
    protected $parser;

    /**
     * Builder constructor.
     *
     * @param DriverInterface|null $driverInterface
     */
    public function __construct(DriverInterface $driverInterface = null)
    {
        $this->driver = $driverInterface;

        $this->parser = new DBParser($driverInterface);
    }

    /**
     * @return DBParser
     */
    public function getParser()
    {
        return $this->parser;
    }

    /**
     * @return TableParser[]
     */
    public function getTables()
    {
        return $this->parser->getTables();
    }

    /**
     * @param array $table
     * @return $this
     */
    public function addTable(array $table)
    {
        if (!isset($table['table'])) {
            throw new \RuntimeException('Table name is undefined.');
        }

        if ($this->parser->hasTable($table['table'])) {
            $this->parser->getTable($table['table'])->setNewFields($table);
        } else {
            $table = new TableParser(
                $this->driver,
                $table['table'],
                $table,
                $this->parser->hasTable($table['table'])
            );
            $this->parser->addTable($table);
        }

        return $this;
    }

    /**
     * @param array $tables
     * @return $this
     */
    public function setTables(array $tables)
    {
        foreach ($tables as $table) {
            $this->addTable($table);
        }

        return $this;
    }

    /**
     * Update table if table is exists.
     * Create table if table is not exists.
     *
     * @return bool
     */
    public function updateTablesFromEntity()
    {
        foreach ($this->getTables() as $table) {
            $sql = $table->makeSQL();
            if (!empty($sql)) {
                $this->driver->createQuery($sql)->getQuery()->getAll();
            }
        }

        return true;
    }

    /**
     * Generate entity and repository into exists tables.
     *
     * @param string $table<commit_msg>add dump sql<commit_after><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/10/11
 * Time: 上午1:13
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 * WebSite: http://www.janhuang.me
 */

namespace FastD\Database\ORM\Generator;

use FastD\Database\Drivers\DriverInterface;
use FastD\Database\ORM\Parser\DBParser;
use FastD\Database\ORM\Parser\TableParser;

/**
 * Class Builder
 *
 * @package FastD\Database\ORM\Mapper
 */
class Mapping
{
    /**
     * @var DriverInterface
     */
    protected $driver;

    /**
     * @var DBParser
     */
    protected $parser;

    /**
     * Builder constructor.
     *
     * @param DriverInterface|null $driverInterface
     */
    public function __construct(DriverInterface $driverInterface = null)
    {
        $this->driver = $driverInterface;

        $this->parser = new DBParser($driverInterface);
    }

    /**
     * @return DBParser
     */
    public function getParser()
    {
        return $this->parser;
    }

    /**
     * @return TableParser[]
     */
    public function getTables()
    {
        return $this->parser->getTables();
    }

    /**
     * @param array $table
     * @return $this
     */
    public function addTable(array $table)
    {
        if (!isset($table['table'])) {
            throw new \RuntimeException('Table name is undefined.');
        }

        if ($this->parser->hasTable($table['table'])) {
            $this->parser->getTable($table['table'])->setNewFields($table);
        } else {
            $table = new TableParser(
                $this->driver,
                $table['table'],
                $table,
                $this->parser->hasTable($table['table'])
            );
            $this->parser->addTable($table);
        }

        return $this;
    }

    /**
     * @param array $tables
     * @return $this
     */
    public function setTables(array $tables)
    {
        foreach ($tables as $table) {
            $this->addTable($table);
        }

        return $this;
    }

    /**
     * @return array
     */
    public function makeAllSql()
    {
        $query = [];
        foreach ($this->getTables() as $table) {
            $sql = $table->makeSQL();
            if (!empty($sql)) {
                $query[] = $sql;
            }
        }
        return $query;
    }

    /**
     * Update table if table is exists.
     * Create table if table is not exists.
     *
     * @return bool
     */
    public function updateTablesFromEntity()
    {
        foreach ($this->makeAllSql() as $sql) {
            $this->driver->createQuery($sql)->getQuery()->getAll();
        }
        return true;
    }

    /**
     * Generate entity and repository into exists tables.
     *
     * @param string $table
{'fraction_non_alphanumeric': 0.14633273703041144, 'fraction_numerical': 0.0032200357781753132, 'mean_word_length': 1.861310133060389, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 50, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, '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': '1877617', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1647, 'n_words': 333}