<commit_before><?php

/**
 * Haefko - your php5 framework
 *
 * @author      <NAME>
 * @copyright   Copyright (c) 2007 - 2009, <NAME>
 * @link        http://haefko.skrasek.com
 * @license     http://www.opensource.org/licenses/mit-license.html
 * @version     0.8.5 - $Id$
 * @package     Haefko_Application
 * @subpackage  Database
 */


require_once dirname(__FILE__) . '/tools.php';
require_once dirname(__FILE__) . '/db-structure.php';


abstract class DbTable extends Object
{


	/** @var DbStructure */
	public static $structure;

	/**
	 * Creates table class and returns class name
	 * @param   string  table name
	 * @return  string
	 */
	public static function init($name)
	{
		$class = Tools::camelize($name) . 'Table';
		if (!class_exists($class, false))
			eval("class $class extends DbTable {}");

		return $class;
	}


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

	/** @var mixed */
	protected $primaryKey;

	/** @var mixed */
	protected $primaryKeyValue;

	/** @var array */
	protected $fields = array();

	/** @var array */
	protected $fieldsModificators = array();


	/**
	 * Constructor
	 * @param   string    primary key value
	 * @throws  Exception
	 * @return  void
	 */
	public function __construct($primaryKeyValue = null)
	{
		if (empty($this->table)) {
			$table = Tools::rTrim($this->getClass(), 'Table');
			$table = Tools::underscore($table);
			$this->table = $table;
		}

		if (!self::$structure->tableExists($this->table))
			throw new Exception("Db table \"{$this->table}\" doesn't exists.");


		$this->primaryKey = self::$structure->getPrimaryKey($this->table);
		$this->primaryKeyValue = $primaryKeyValue;
	}


	/**
	 * Selects $cols from table
	 * @param   string|array  selected columns
	 * @return  DbResultNode
	 */
	public function get($cols = '*')
	{
		$cols = $this->toSqlCols($cols);
		$mod = self::$structure->getModificator($this->table, $this->primaryKey);
		return Db::fetch("SELECT $cols FROM %c WHERE %c = $mod LIMIT 1", $this->table, $this->primaryKey, $this->primaryKeyValue);
	}


	/**
	 * Selects $cols from table by $col
	 * @param   string        column
	 * @param   string|array  selected columns
	 * @return  DbResultNode
	 */
	public function getBy($col, $cols = '*')
	{
		$cols = $this->toSqlCols($cols);
		$mod = self::$structure->getModificator($this->table, $col);
		return Db::fetch("SELECT $cols FROM %c WHERE %c = $mod LIMIT 1", $this->table, $col, $this->primaryKeyValue);
	}


	/**
	 * Returns table form
	 * @param   array    cols for edit
	 * @param   array    cols labels
	 * @throws  Exception
	 * @return  Form
	 */
	public function getForm($editCols = array(), $labels = array())
	{
		$form = new Form();
		$cols = self::$structure->getCols($this->table);

		foreach ($cols as $name => $data) {
			if (!empty($editCols) && !in_array($name, $editCols))
				continue;

			if ($data["primary"])
				continue;<commit_msg>- fixed DbTable bug in method ::getBy()<commit_after><?php

/**
 * Haefko - your php5 framework
 *
 * @author      <NAME>
 * @copyright   Copyright (c) 2007 - 2009, <NAME>
 * @link        http://haefko.skrasek.com
 * @license     http://www.opensource.org/licenses/mit-license.html
 * @version     0.8.5 - $Id$
 * @package     Haefko_Application
 * @subpackage  Database
 */


require_once dirname(__FILE__) . '/tools.php';
require_once dirname(__FILE__) . '/db-structure.php';


abstract class DbTable extends Object
{


	/** @var DbStructure */
	public static $structure;

	/**
	 * Creates table class and returns class name
	 * @param   string  table name
	 * @return  string
	 */
	public static function init($name)
	{
		$class = Tools::camelize($name) . 'Table';
		if (!class_exists($class, false))
			eval("class $class extends DbTable {}");

		return $class;
	}


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

	/** @var mixed */
	protected $primaryKey;

	/** @var mixed */
	protected $primaryKeyValue;

	/** @var array */
	protected $fields = array();

	/** @var array */
	protected $fieldsModificators = array();


	/**
	 * Constructor
	 * @param   string    primary key value
	 * @throws  Exception
	 * @return  void
	 */
	public function __construct($primaryKeyValue = null)
	{
		if (empty($this->table)) {
			$table = Tools::rTrim($this->getClass(), 'Table');
			$table = Tools::underscore($table);
			$this->table = $table;
		}

		if (!self::$structure->tableExists($this->table))
			throw new Exception("Db table \"{$this->table}\" doesn't exists.");


		$this->primaryKey = self::$structure->getPrimaryKey($this->table);
		$this->primaryKeyValue = $primaryKeyValue;
	}


	/**
	 * Selects $cols from table
	 * @param   string|array  selected columns
	 * @return  DbResultNode
	 */
	public function get($cols = '*')
	{
		$cols = $this->toSqlCols($cols);
		$mod = self::$structure->getModificator($this->table, $this->primaryKey);
		return Db::fetch("SELECT $cols FROM %c WHERE %c = $mod LIMIT 1", $this->table, $this->primaryKey, $this->primaryKeyValue);
	}


	/**
	 * Selects $cols from table by $col
	 * @param   string        column
	 * @param   string|array  selected columns
	 * @return  DbResultNode
	 */
	public function getBy($col, $cols = '*')
	{
		$cols = $this->toSqlCols($cols);
		$mod = self::$structure->getModificator($this->table, $col);
		return Db::fetch("SELECT $cols FROM %c WHERE %c = $mod LIMIT 1", $this->table, $col, $this->fields[$col]);
	}


	/**
	 * Returns table form
	 * @param   array    cols for edit
	 * @param   array    cols labels
	 * @throws  Exception
	 * @return  Form
	 */
	public function getForm($editCols = array(), $labels = array())
	{
		$form = new Form();
		$cols = self::$structure->getCols($this->table);

		foreach ($cols as $name => $data) {
			if (!empty($editCols) && !in_array($name, $editCols))
				continue;

			if ($data["primary"])
				continue;
{'fraction_non_alphanumeric': 0.17158639737614362, 'fraction_numerical': 0.004833419644398412, 'mean_word_length': 3.4027355623100304, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4705162', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1858, 'n_words': 475}