<commit_before><?php
/**
 * User: coderd
 * Date: 2017/4/9
 * Time: 上午9:25
 */

namespace Rapkg\Model;


use Rapkg\Sql\DB;
use Rapkg\Sql\Query;
use Rapkg\Sql\RawQuery;
use Rapkg\Sql\QueryInterface;
use Rapkg\Config\ConfigInterface;

abstract class Table
{
    /**
     * @var DB
     */
    protected $db;

    /**
     * @var string
     */
    private $tableName;

    public function __construct($tableName)
    {
        $this->tableName = $tableName;
        $this->db = Manager::db($this->dbConfig());
    }

    /**
     * @return ConfigInterface
     */
    abstract protected function dbConfig();

    /**
     * @return string
     */
    public function getTableName()
    {
        return $this->tableName;
    }

    /**
     * @param array|string $columns
     * @param array $wheres
     * @param array $orders
     * @param array $limit
     * @return array|bool
     */
    public function select(
        $columns,
        array $wheres = [],
        array $orders = [],
        array $limit = []
    )
    {
        $offset = isset($limit[0]) ? $limit[0] : 0;
        $rowCount = isset($limit[1]) ? $limit[1] : 0;

        $q = (new Query())
            ->table($this->tableName)
            ->select($columns)
            ->where($wheres)
            ->orderBy($orders)
            ->limit($offset, $rowCount);

        return $this->db->execReturningRows($q);
    }

    /**
     * @param array|string $columns
     * @param array $wheres
     * @param array $orders
     * @return array|bool|mixed|null
     */
    public function first(
        $columns,
        array $wheres = [],
        array $orders = []
    )
    {
        $result = $this->select($columns, $wheres, $orders, [0, 1]);

        // Failure
        if ($result === false) {
            return $result;
        }

        return isset($result[0]) ? $result[0] : null;
    }

    public function count(array $wheres)
    {
        $result = $this->select('count(*) AS `count`', $wheres);
        if ($result === false) {
            return false;
        }

        if (isset($result[0]['count'])) {
            return $result[0]['count'];
        }

        return false;
    }

    public function insert(array $values)
    {
        $q = (new Query())
            ->table($this->tableName)
            ->insert($values);

        $result = $this->db->execWithoutReturningRows($q);
        if (empty($result)) {
            return $result;
        }

        return $result['last_insert_id'];
    }

    public function bulkInsert(array $columns, array $values)
    {
        $q = (new Query())
            ->table($this->tableName)
            ->bulkInsert($columns, $values);

        return $this->db->execWithoutReturningRows($q);
    }

    public function update(
        array $values,
        array $wheres = [],
        array $orders = [],
        array $limit = []
    )
    {
        $offset = isset($limit[0]) ? $limit[0] : 0;
        $rowCount = isset($limit[1]) ? $limit[1] : 0;

        $q = (new Query())
            ->table($this->tableName)
            ->where($wheres)
            ->orderBy($orders)
            ->limit($offset, $rowCount)
            ->update($values);

        $result = $this->db->execWithoutReturningRows($q);
        if (empty($result)) {
            return $result;
        }

        return $result['row_count'];
    }

    public function delete(array $wheres, array $orders = [], array $limit = [])
    {
        $offset = isset($limit[0]) ? $limit[0] : 0;
        $rowCount = isset($limit[1]) ? $limit[1] : 0;

        $q = (new Query())
            ->table($this->tableName)
            ->where($wheres)
            ->orderBy($orders)
            ->limit($offset, $rowCount)
            ->delete();

        $result = $this->db->execWithoutReturningRows($q);
        if (empty($result)) {
            return $result;
        }

        return $result['row_count'];
    }

    protected function execReturningRows(QueryInterface $query)
    {
        return $this->db->execReturningRows($query);
    }

    protected function execRawReturningRows($queryString, array $args = [])
    {
        return $this->execReturningRows(new RawQuery($queryString, $args));
    }
<commit_msg>Modified: model  supports string value<commit_after><?php
/**
 * User: coderd
 * Date: 2017/4/9
 * Time: 上午9:25
 */

namespace Rapkg\Model;


use Rapkg\Sql\DB;
use Rapkg\Sql\Query;
use Rapkg\Sql\RawQuery;
use Rapkg\Sql\QueryInterface;
use Rapkg\Config\ConfigInterface;

abstract class Table
{
    /**
     * @var DB
     */
    protected $db;

    /**
     * @var string
     */
    private $tableName;

    public function __construct($tableName)
    {
        $this->tableName = $tableName;
        $this->db = Manager::db($this->dbConfig());
    }

    /**
     * @return ConfigInterface
     */
    abstract protected function dbConfig();

    /**
     * @return string
     */
    public function getTableName()
    {
        return $this->tableName;
    }

    /**
     * @param array|string $columns
     * @param array|string $wheres
     * @param array $orders
     * @param array $limit
     * @return array|bool
     */
    public function select(
        $columns,
        $wheres = [],
        array $orders = [],
        array $limit = []
    )
    {
        $offset = isset($limit[0]) ? $limit[0] : 0;
        $rowCount = isset($limit[1]) ? $limit[1] : 0;

        $q = (new Query())
            ->table($this->tableName)
            ->select($columns)
            ->where($wheres)
            ->orderBy($orders)
            ->limit($offset, $rowCount);

        return $this->db->execReturningRows($q);
    }

    /**
     * @param array|string $columns
     * @param array|string $wheres
     * @param array $orders
     * @return array|bool|mixed|null
     */
    public function first(
        $columns,
        $wheres = [],
        array $orders = []
    )
    {
        $result = $this->select($columns, $wheres, $orders, [0, 1]);

        // Failure
        if ($result === false) {
            return $result;
        }

        return isset($result[0]) ? $result[0] : null;
    }

    /**
     * @param array|string $wheres
     * @return bool
     */
    public function count($wheres)
    {
        $result = $this->select('count(*) AS `count`', $wheres);
        if ($result === false) {
            return false;
        }

        if (isset($result[0]['count'])) {
            return $result[0]['count'];
        }

        return false;
    }

    public function insert(array $values)
    {
        $q = (new Query())
            ->table($this->tableName)
            ->insert($values);

        $result = $this->db->execWithoutReturningRows($q);
        if (empty($result)) {
            return $result;
        }

        return $result['last_insert_id'];
    }

    public function bulkInsert(array $columns, array $values)
    {
        $q = (new Query())
            ->table($this->tableName)
            ->bulkInsert($columns, $values);

        return $this->db->execWithoutReturningRows($q);
    }

    /**
     * @param array $values
     * @param array|string $wheres
     * @param array $orders
     * @param array $limit
     * @return array|bool
     */
    public function update(
        array $values,
        $wheres = [],
        array $orders = [],
        array $limit = []
    )
    {
        $offset = isset($limit[0]) ? $limit[0] : 0;
        $rowCount = isset($limit[1]) ? $limit[1] : 0;

        $q = (new Query())
            ->table($this->tableName)
            ->where($wheres)
            ->orderBy($orders)
            ->limit($offset, $rowCount)
            ->update($values);

        $result = $this->db->execWithoutReturningRows($q);
        if (empty($result)) {
            return $result;
        }

        return $result['row_count'];
    }

    /**
     * @param array|string $wheres
     * @param array $orders
     * @param array $limit
     * @return array|bool
     */
    public function delete($wheres, array $orders = [], array $limit = [])
    {
        $offset = isset($limit[0]) ? $limit[0] : 0;
        $rowCount = isset($limit[1]) ? $limit[1] : 0;

        $q = (new Query())
            ->table($this->tableName)
            ->where($wheres)
            ->orderBy($orders)
            ->limit($offset, $rowCount)
            ->delete();

        $result = $this->db->execWithoutReturningRows($q);
        if (empty($result)) {
            return $result;
        }

        return $result['row_count'];
    }

    protected function execReturningRows(QueryInterface $query)
    {
        return $this->db->execReturningRows($query);
    }

    protected function execRawReturningRows($queryString, array $args = [])
    {
        return $this->execReturningRows(new RawQuery($queryString, $args));
    }

{'fraction_non_alphanumeric': 0.15974114441416895, 'fraction_numerical': 0.007493188010899182, 'mean_word_length': 1.832475884244373, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3276403', 'n_tokens_mistral': 2748, 'n_tokens_neox': 2655, 'n_words': 555}