<commit_before><?php
/*
 * Avalon
 * Copyright (C) 2011-2012 <NAME>
 *
 * @author <NAME>. <<EMAIL>>
 * @copyright <NAME>.
 * @license New BSD License
 */

/**
 * Database Model class
 *
 * @package Avalon
 * @subpackage Database
 * @since 0.1
 * @author <NAME>. <<EMAIL>>
 * @copyright Copyright (c) <NAME>.
 */
class Model
{
	public static $db;
	protected static $_name; // Table name
	protected static $_primary_key = 'id'; // Primary key
	protected static $_has_many; // Has many relationship array
	protected static $_belongs_to; // Belongs to relationship array
	protected static $_after = array(); // After filters
	protected static $_properties = array(); // Table columns
	protected $_changed_properties = array(); // Properties that have been changed
	protected $_data = array();
	protected $_is_new = true; // Used to determine if this is a new row or not.
	
	/**
	 * Used to build to assign the row data to the class as variables.
	 *
	 * @param array $data The row data
	 */
	public function __construct($data = null, $is_new = true)
	{
		$this->_data = $data;
		$this->_is_new = $is_new;
		
		if (!isset(static::$_after['construct'])) {
			static::$_after['construct'] = array();
		}
		if (!in_array('_date_time_convert', static::$_after['construct'])) {
			static::$_after['construct'][] = '_date_time_convert';
		}
			
		if (isset(static::$_after['construct'])) {
			$filters = (is_array(static::$_after['construct']) ? static::$_after['construct'] : array(static::$_after['construct']));
			foreach ($filters as $filter) {
				$this->$filter();
			}
		}
	}
	
	/**
	 * Find the first matching row and returns it.
	 *
	 * @param string $find Either the value of the primary key, or the field name.
	 * @param value $value The value of the field to find if the $find param is the field name.
	 *
	 * @return Object
	 */
	public static function find($find, $value = null)
	{
		$select = Database::driver()->select()->from(static::$_name);
		if ($value == null) {
			$select = $select->where(static::$_primary_key, $find)->limit(1)->exec();
		} else {
			$select = $select->where($find, $value)->limit(1)->exec();
		}
		
		if ($select->row_count() == 0)
		{
			return false;
		}
		
		return new static($select->fetch(), false);
	}
	
	/**
	 * Creates a new row or saves the changed properties.
	 */
	public function save()
	{
		$primary_key = static::$_primary_key;
		
		// Make sure the data is valid..
		if (!$this->is_valid())
		{
			return false;
		}
		
		// Save
		if ($this->_is_new() === false)
		{
			// Loop over the properties
			$data = array();
			foreach (static::$_properties as $column) {
				// Check if column is updated, if so, save.
				if (in_array($column, $this->_changed_properties)) {
					$data[$column] = $this->_data[$column];
				}
			}
			unset($data[static::$_primary_key]);
			
			// Save the row..
			Database::driver()->update(static::$_name)->set($data)->where(static::$_primary_key, $this->_data[static::$_primary_key])->exec();
		}
		// Create
		else
		{
			// Before create filters
			if (isset(static::$_filters_before['create']) and is_array(static::$_filters_before['create']))
			{
				foreach (static::$_filters_before['create'] as $filter)
				{
					$this->$filter();
				}
			}
			
			// Loop over the properties
			$data = array();
			foreach (static::$_properties as $column) {
				$data[$column] = $this->_data[$column];
			}
			unset($data[static::$_primary_key]);
			
			// Insert the row..
			Database::driver()->insert($data)->into(static::$_name)->exec();
			
			// Set the primary key
			$this->$primary_key = Database::driver()->last_insert_id();
		}
	}
	
	/**
	 * Deletes the row.
	 */
	public function delete()
	{
		if ($this->_is_new() === false) {
			return Database::driver()->delete()->from('users')->where(static::$_primary_key, $this->_data[static::$_primary_key])->exec();
		}
	}
	
	/**
	 * Checks if the row is new or not.
	 *
	 * @return bool
	 */
	public function _is_new($is_new = null)
	{
		if ($is_new !== null) {
			$this->_is_new =  $is_new;
		}
		return $this->_is_new;
	}
	
	/**
	 * Sets the value of the column(s) to the value(s).
	 *
	 * @param mixed $col Either the column or an array to update multiple columns.
	 * @param mixed $val The value of the column if only updating one column.
	 *
	 * @example $model->set(array('col1'=>'val1', 'col2'=>'val2'));
	 *          $model->set('col1', 'val1');
	 */
	public function set($col, $val = null)
	{
		if (is_array($col)) {
			foreach ($col as $var => $val) {
				$this->set($var, $val);
			}
		} else {
			$this->_data[$col] = $val;
			$this->_set_changed($col);
			
			if (!isset(static::$_properties[$var]))
			{
				static::$_properties[] = $var;
			}
		}
	}
	
	private function _set_changed($property)
	{
		if (in_array($property, static::$_properties) and !in_array($property, $this->_changed_properties)) {
			$this->_changed_properties[] = $property;
		}
	}
	
	/**
	 * Shortcut of the select() function for the database.
	 *
	 * @param mixed $cols The columns to select.
	 *
	 * @return object<commit_msg>Fixed an issue with the $row->set() method and renamed the $_before filters array to $_filters_before.<commit_after><?php
/*
 * Avalon
 * Copyright (C) 2011-2012 <NAME>
 *
 * @author <NAME>. <<EMAIL>>
 * @copyright <NAME>.
 * @license New BSD License
 */

/**
 * Database Model class
 *
 * @package Avalon
 * @subpackage Database
 * @since 0.1
 * @author <NAME>. <<EMAIL>>
 * @copyright Copyright (c) <NAME>.
 */
class Model
{
	public static $db;
	protected static $_name; // Table name
	protected static $_primary_key = 'id'; // Primary key
	protected static $_has_many; // Has many relationship array
	protected static $_belongs_to; // Belongs to relationship array
	protected static $_filters_before = array(); // Before filters
	protected static $_filters_after = array(); // After filters
	protected static $_properties = array(); // Table columns
	protected $_changed_properties = array(); // Properties that have been changed
	protected $_data = array();
	protected $_is_new = true; // Used to determine if this is a new row or not.
	
	/**
	 * Used to build to assign the row data to the class as variables.
	 *
	 * @param array $data The row data
	 */
	public function __construct($data = null, $is_new = true)
	{
		$this->_data = $data;
		$this->_is_new = $is_new;
		
		if (!isset(static::$_filters_after['construct'])) {
			static::$_filters_after['construct'] = array();
		}
		if (!in_array('_date_time_convert', static::$_filters_after['construct'])) {
			static::$_filters_after['construct'][] = '_date_time_convert';
		}
			
		if (isset(static::$_filters_after['construct'])) {
			$filters = (is_array(static::$_filters_after['construct']) ? static::$_filters_after['construct'] : array(static::$_filters_after['construct']));
			foreach ($filters as $filter) {
				$this->$filter();
			}
		}
	}
	
	/**
	 * Find the first matching row and returns it.
	 *
	 * @param string $find Either the value of the primary key, or the field name.
	 * @param value $value The value of the field to find if the $find param is the field name.
	 *
	 * @return Object
	 */
	public static function find($find, $value = null)
	{
		$select = Database::driver()->select()->from(static::$_name);
		if ($value == null) {
			$select = $select->where(static::$_primary_key, $find)->limit(1)->exec();
		} else {
			$select = $select->where($find, $value)->limit(1)->exec();
		}
		
		if ($select->row_count() == 0)
		{
			return false;
		}
		
		return new static($select->fetch(), false);
	}
	
	/**
	 * Creates a new row or saves the changed properties.
	 */
	public function save()
	{
		$primary_key = static::$_primary_key;
		
		// Make sure the data is valid..
		if (!$this->is_valid())
		{
			return false;
		}
		
		// Save
		if ($this->_is_new() === false)
		{
			// Loop over the properties
			$data = array();
			foreach (static::$_properties as $column) {
				// Check if column is updated, if so, save.
				if (in_array($column, $this->_changed_properties)) {
					$data[$column] = $this->_data[$column];
				}
			}
			unset($data[static::$_primary_key]);
			
			// Save the row..
			Database::driver()->update(static::$_name)->set($data)->where(static::$_primary_key, $this->_data[static::$_primary_key])->exec();
		}
		// Create
		else
		{
			// Before create filters
			if (isset(static::$_filters_before['create']) and is_array(static::$_filters_before['create']))
			{
				foreach (static::$_filters_before['create'] as $filter)
				{
					$this->$filter();
				}
			}
			
			// Loop over the properties
			$data = array();
			foreach (static::$_properties as $column) {
				$data[$column] = $this->_data[$column];
			}
			unset($data[static::$_primary_key]);
			
			// Insert the row..
			Database::driver()->insert($data)->into(static::$_name)->exec();
			
			// Set the primary key
			$this->$primary_key = Database::driver()->last_insert_id();
		}
	}
	
	/**
	 * Deletes the row.
	 */
	public function delete()
	{
		if ($this->_is_new() === false) {
			return Database::driver()->delete()->from('users')->where(static::$_primary_key, $this->_data[static::$_primary_key])->exec();
		}
	}
	
	/**
	 * Checks if the row is new or not.
	 *
	 * @return bool
	 */
	public function _is_new($is_new = null)
	{
		if ($is_new !== null) {
			$this->_is_new =  $is_new;
		}
		return $this->_is_new;
	}
	
	/**
	 * Sets the value of the column(s) to the value(s).
	 *
	 * @param mixed $col Either the column or an array to update multiple columns.
	 * @param mixed $val The value of the column if only updating one column.
	 *
	 * @example $model->set(array('col1'=>'val1', 'col2'=>'val2'));
	 *          $model->set('col1', 'val1');
	 */
	public function set($col, $val = null)
	{
		if (is_array($col)) {
			foreach ($col as $var => $val) {
				$this->set($var, $val);
			}
		} else {
			$this->_data[$col] = $val;
			$this->_set_changed($col);
			
			if (!isset(static::$_properties[$val]))
			{
				static::$_properties[] = $val;
			}
		}
	}
	
	private function _set_changed($property)
	{
		if (in_array($property, static::$_properties) and !in_array($property, $this->_changed_properties))
		{
			$this->_changed_properties[] = $property;
		}
	}
	
	/**
	 * Shortcut of the select() function for the database.
	 *
	 * @param mixed $cols The columns to select.
	 *
	 * @return object
{'fraction_non_alphanumeric': 0.17759906985757193, 'fraction_numerical': 0.0036818137777347155, 'mean_word_length': 3.418664383561644, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 116, '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': '7346637', 'n_tokens_mistral': 3774, 'n_tokens_neox': 3284, 'n_words': 885}