<commit_before><?php
/**
 * @package   Gears\Db
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011-2013 <NAME> <<EMAIL>>
 * @license   http://url/license
 */
namespace Gears\Db;

use Gears\Db\Adapter\AdapterAbstract;
use Gears\Db\Query\WhereAnd;

/**
 * Advanced implementation of Table Data Gateway pattern
 * @package    Gears\Db
 * @subpackage Database
 */
abstract class Table
{
    /**
     * Table name
     * @var string
     */
    protected $tableName;

    /**
     * Table field set
     * @var array
     */
    protected $tableFields = ['*'];

    /**
     * Table primary key
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Default ORDER BY clause field set
     * @var Query
     */
    protected $defaultOrderBy = [null];

    /**
     * Stores objects of all table relations to other tables
     * @see TableRelation
     * @var array
     */
    protected $relations = [];

    /**
     * Db adapter instance holder
     * @var AdapterAbstract
     */
    private $db;

    /**
     * Table query instance
     * @var Query
     */
    private $query;

    /**
     * Constructor
     */
    public function __construct(AdapterAbstract $db)
    {
        // init table db adapter
        $this->db = $db;

        if (is_null($this->tableName)) {
            throw new \Exception(get_called_class() . ' - table name is not specified');
        }

        // build default query
        $this->query = (new Query($db))
            ->select($this->getDefaultFields(), null, $this->getTableName())
            ->from($this->getTableName())
            ->where(new WhereAnd($db))
            ->order($this->defaultOrderBy);

        // do custom preparations
        $this->init();
    }

    /**
     * Add table own field to the selection query
     * @param string $field Table field name or alias
     * @param string (optional) $alias Alias name for selection
     */
    public function select($field, $alias = null)
    {
        $this->getQuery()->select($this->getFieldName($field), $alias, $this->getTableName());
    }

    /**
     * Fetch rows using current query configuration
     * @return array Result row set
     */
    public function fetchRows()
    {
        return $this->getQuery()->exec()->fetchAll();
    }

    /**
     * Fetch and return limited rows number. Uses current query configuration
     * @param integer $pageNumber
     * @param integer $rowsPerPage
     * @return array
     */
    public function fetchPage($pageNumber = 1, $rowsPerPage = 10)
    {
        $this->getQuery()->limit(--$pageNumber * $rowsPerPage, $rowsPerPage);
        $this->getQuery()->calcFoundRows();
        return $this->fetchRows();
    }

    /**
     * Fetch a single row data by a given row id. Returns a simple property-value fields object.
     * For each specific table field, a field alias (if defined in table metadata) is used as an object
     * property name under which to store the field value. Db field name itself used otherwise.
     * By default replaces table foreign key fields with actual row data from relative tables accessed by relation name
     * @param integer $rowId
     * @param array|string|bool $relations (optional) List by which to limit loaded relations. <i>false</i> to not load them at all
     * @return object Simple property-value object
     */
    public function fetchRow($rowId, $relations = true)
    {
        // yank db only if positive row id number given
        if (intval($rowId)) {
            // fetch all own table row data
            $q = $this->getQuery()->select($this->tableFields, null, $this->getTableName());
            $q->getWhere()->eq($this->primaryKey, $rowId);
            $row = $q->exec()->fetchRow();
        }

        // no row data fetched, fill all fields with NULL value
        if (empty($row)) {
            foreach ($this->tableFields as $fieldKey => $fieldValue) {
                $row[is_string($fieldKey) ? $fieldKey : $fieldValue] = null;
            }
            // prevent processing relations
            $relations = false;
        }

        // load relation table rows
        if ($relations && count($this->relations)) {
            // explode coma separated relations string
            if (is_string($relations)) {
                $relations = explode(',', $relations);
            }

            // no limitation array, use all relations
            if (!is_array($relations)) {<commit_msg>fetching table entity empty data row by default also fetches empty relations data<commit_after><?php
/**
 * @package   Gears\Db
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011-2013 <NAME> <<EMAIL>>
 * @license   http://url/license
 */
namespace Gears\Db;

use Gears\Db\Adapter\AdapterAbstract;
use Gears\Db\Query\WhereAnd;

/**
 * Advanced implementation of Table Data Gateway pattern
 * @package    Gears\Db
 * @subpackage Database
 */
abstract class Table
{
    /**
     * Table name
     * @var string
     */
    protected $tableName;

    /**
     * Table field set
     * @var array
     */
    protected $tableFields = ['*'];

    /**
     * Table primary key
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Default ORDER BY clause field set
     * @var Query
     */
    protected $defaultOrderBy = [null];

    /**
     * Stores objects of all table relations to other tables
     * @see TableRelation
     * @var array
     */
    protected $relations = [];

    /**
     * Db adapter instance holder
     * @var AdapterAbstract
     */
    private $db;

    /**
     * Table query instance
     * @var Query
     */
    private $query;

    /**
     * Constructor
     */
    public function __construct(AdapterAbstract $db)
    {
        // init table db adapter
        $this->db = $db;

        if (is_null($this->tableName)) {
            throw new \Exception(get_called_class() . ' - table name is not specified');
        }

        // build default query
        $this->query = (new Query($db))
            ->select($this->getDefaultFields(), null, $this->getTableName())
            ->from($this->getTableName())
            ->where(new WhereAnd($db))
            ->order($this->defaultOrderBy);

        // do custom preparations
        $this->init();
    }

    /**
     * Add table own field to the selection query
     * @param string $field Table field name or alias
     * @param string (optional) $alias Alias name for selection
     */
    public function select($field, $alias = null)
    {
        $this->getQuery()->select($this->getFieldName($field), $alias, $this->getTableName());
    }

    /**
     * Fetch rows using current query configuration
     * @return array Result row set
     */
    public function fetchRows()
    {
        return $this->getQuery()->exec()->fetchAll();
    }

    /**
     * Fetch and return limited rows number. Uses current query configuration
     * @param integer $pageNumber
     * @param integer $rowsPerPage
     * @return array
     */
    public function fetchPage($pageNumber = 1, $rowsPerPage = 10)
    {
        $this->getQuery()->limit(--$pageNumber * $rowsPerPage, $rowsPerPage);
        $this->getQuery()->calcFoundRows();
        return $this->fetchRows();
    }

    /**
     * Fetch a single row data by a given row id. Returns a simple property-value fields object.
     * For each specific table field, a field alias (if defined in table metadata) is used as an object
     * property name under which to store the field value. Db field name itself used otherwise.
     * By default replaces table foreign key fields with actual row data from relative tables accessed by relation name
     * @param integer $rowId
     * @param array|string|bool $relations (optional) List by which to limit loaded relations. <i>false</i> to not load them at all
     * @return object Simple property-value object
     */
    public function fetchRow($rowId, $relations = true)
    {
        // yank db only if positive row id number given
        if (intval($rowId)) {
            // fetch all own table row data
            $q = $this->getQuery()->select($this->tableFields, null, $this->getTableName());
            $q->getWhere()->eq($this->primaryKey, $rowId);
            $row = $q->exec()->fetchRow();
        }

        // no row data fetched, fill all fields with NULL value
        if (empty($row)) {
            foreach ($this->tableFields as $fieldKey => $fieldValue) {
                $row[is_string($fieldKey) ? $fieldKey : $fieldValue] = null;
            }
        }

        // load relation table rows
        if ($relations && count($this->relations)) {
            // explode coma separated relations string
            if (is_string($relations)) {
                $relations = explode(',', $relations);
            }

            // no limitation array, use all relations
            if (!is_array($relations)) {
{'fraction_non_alphanumeric': 0.12438202247191012, 'fraction_numerical': 0.0024719101123595504, 'mean_word_length': 2.1452296819787984, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 91, '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': '6243419', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2379, 'n_words': 779}