<commit_before><?php
/**
 * Created by PhpStorm.
 * User: wuwentao
 * Date: 2016/4/6
 * Time: 10:25
 */

namespace Flight2wwu\Component\Database;


abstract class OrmModel
{

    /**
     * @var string
     */
    protected $tableName = '';

    /**
     * @var string|array
     */
    protected $tableKey = '';

    /**
     * @param string|array $key
     * @param string|array $select
     * @return array
     * @throws \Exception
     */
    public function show($key, $select = null)
    {
        $where = $this->formatKey($key);
        if (is_null($select)) {
            $select = '*';
        }
        $db = getDB();
        $re = $db->getConnection()->get($this->getTableName(), $select, $where);
        if ($re) {
            return $re;
        }
        return [];
    }

    /**
     * @param string|array $select
     * @param array|null $where
     * @return array
     * @throws \Exception
     */
    public function lists($select = null, $where = null)
    {
        $db = getDB();
        if (is_null($select)) {
            $select = '*';
        }
        $re = $db->getConnection()->select($this->getTableName(), $select, $where);
        if ($re) {
            return $re;
        }
        return [];
    }

    /**
     * @param array $data
     * @param array $where
     * @return bool|int
     * @throws \Exception
     */
    public function update(array $data, array $where)
    {
        $db = getDB();
        $re = $db->getConnection()->update($this->getTableName(), $data, $where);
        return $re;
    }

    /**
     * @param array $data
     * @return array
     * @throws \Exception
     */
    public function insert(array $data)
    {
        $db = getDB();
        $re = $db->getConnection()->insert($this->getTableName(), $data);
        return $re;
    }

    /**
     * @param string|array $key
     * @return bool|int
     * @throws \Exception
     */
    public function delete($key)
    {
        $where = $this->formatKey($key);
        $db = getDB();
        $re = $db->getConnection()->delete($this->getTableName(), $where);
        return $re;
    }

    /**
     * @return string
     */
    protected function getTableName()
    {<commit_msg>- Add count, search in OrmModel - Add pagination in lists<commit_after><?php
/**
 * Created by PhpStorm.
 * User: wuwentao
 * Date: 2016/4/6
 * Time: 10:25
 */

namespace Flight2wwu\Component\Database;


abstract class OrmModel
{

    /**
     * @var string
     */
    protected $tableName = '';

    /**
     * @var string|array
     */
    protected $tableKey = '';

    /**
     * @param string|array $key
     * @param string|array $select
     * @return array
     * @throws \Exception
     */
    public function show($key, $select = null)
    {
        $where = $this->formatKey($key);
        if (is_null($select)) {
            $select = '*';
        }
        $db = getDB();
        $re = $db->getConnection()->get($this->getTableName(), $select, $where);
        if ($re) {
            return $re;
        }
        return [];
    }

    /**
     * @param string|array $select
     * @param array|null $where
     * @param bool|Pagination $page: true for auto page
     * @return array
     * @throws \Exception
     */
    public function lists($select = null, $where = null, $page = null)
    {
        $db = getDB();
        if (is_null($select)) {
            $select = '*';
        }
        if ($page === true) {
            $page = Pagination::getAutoPage($this->getTableName());
        }
        if ($page instanceof Pagination) {
            $where['LIMIT'] = [$page->getOffset(), $page->getLimit()];
        }
        $re = $db->getConnection()->select($this->getTableName(), $select, $where);
        if ($re) {
            return $re;
        }
        return [];
    }

    /**
     * @param array|null $where
     * @return int
     * @throws \Exception
     */
    public function count($where = null)
    {
        $db = getDB();
        $re = $db->getConnection()->count($this->getTableName(), $where);
        return $re;
    }

    /**
     * @param array $data
     * @param array $where
     * @return bool|int
     * @throws \Exception
     */
    public function update(array $data, array $where)
    {
        $db = getDB();
        $re = $db->getConnection()->update($this->getTableName(), $data, $where);
        return $re;
    }

    /**
     * @param array $data
     * @return array
     * @throws \Exception
     */
    public function insert(array $data)
    {
        $db = getDB();
        $re = $db->getConnection()->insert($this->getTableName(), $data);
        return $re;
    }

    /**
     * @param string|array $key
     * @return bool|int
     * @throws \Exception
     */
    public function delete($key)
    {
        $where = $this->formatKey($key);
        $db = getDB();
        $re = $db->getConnection()->delete($this->getTableName(), $where);
        return $re;
    }

    /**
     * @param string|array $text
     * @param string|array $fields
     * @param string|array $select
     * @return array|bool
     * @throws \Exception
     */
    public function search($text, $fields = [], $select = null)
    {
        if (is_null($select)) {
            $select = '*';
        }
        if (!$fields) {
            $fields = $this->getTableKey();
        }
        if (!$fields) {
            return [];
        }
        if (is_array($fields)) {
            $where = ['OR'=>[]];
            foreach ($fields as $field) {
                $where['OR'][$field . '[~]'] = $text;
            }
        } else {
            $f = $fields . '[~]';
            $where = [$f => $text];
        }
        $db = getDB();
        $re = $db->getConnection()->select($this->getTableName(), $select, $where);
        if ($re) {
            return $re;
        }
        return [];
    }

    /**
     * @return string
     */
    protected function getTableName()
    {
{'fraction_non_alphanumeric': 0.171225937183384, 'fraction_numerical': 0.0037149611617696724, 'mean_word_length': 1.704566210045662, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '5705792', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1812, 'n_words': 404}