<commit_before><?php

class Model
{
	public $_data = array();
	public $_changed = array();
	public $_loaded = false;
	public $_relationalrows = array();
	public $_errors = array();
	
	public function relations()
	{
		return array();
	}
	
	public function rules()
	{
		return array();
	}
	
	public static function findPrimaryKey()
	{
		$tablename = static::tablename();
		$res = nf_sql_query('SHOW KEYS FROM `' . $tablename . '` WHERE Key_name = \'PRIMARY\'');
		$row = $res->fetch_assoc();
		if($row !== null) {
			return $row['Column_name'];
		}
		nf_error(9, $tablename);
		return false;
	}
	
	public static function findByPk($pk)
	{
		$class = get_called_class();
		return $class::findByQuery('SELECT * FROM `' . static::tablename() . '` WHERE `' . static::findPrimaryKey() . '`=' . nf_sql_encode($pk));
	}
	
	public static function findByAttributes($attributes)
	{
		$class = get_called_class();
		$query = 'SELECT * FROM `' . static::tablename() . '` ';
		if(count($attributes) > 0) {
			$query .= 'WHERE ';
			$firstAnd = false;
			foreach($attributes as $k => $v) {
				if($firstAnd) {
					$query .= 'AND ';
				}
				$firstAnd = true;
				$query .= '`' . $k . '`=' . nf_sql_encode($v) . ' ';
			}
		}
		$query .= 'LIMIT 0,1';
		return $class::findByQuery($query);
	}
	
	public static function findAllByAttributes($attributes)
	{
		$class = get_called_class();
		$query = 'SELECT * FROM `' . static::tablename() . '` ';
		if(count($attributes) > 0) {
			$query .= 'WHERE ';
			$firstAnd = false;
			foreach($attributes as $k => $v) {
				if($firstAnd) {
					$query .= 'AND ';
				}
				$firstAnd = true;
				$query .= '`' . $k . '`=' . nf_sql_encode($v) . ' ';
			}
		}
		return $class::findAllByQuery($query);
	}

	public static function countByAttributes($attributes)
	{
		$class = get_called_class();
		$query = 'SELECT COUNT(*) AS c FROM `' . static::tablename() . '` ';
		if(count($attributes) > 0) {
			$query .= 'WHERE ';
			$firstAnd = false;
			foreach($attributes as $k => $v) {
				if($firstAnd) {
					$query .= 'AND ';
				}
				$firstAnd = true;
				$query .= '`' . $k . '`=' . nf_sql_encode($v) . ' ';
			}
		}
		return $class::countByQuery($query);
	}
	
	public static function findAll()
	{
		$class = get_called_class();
		return $class::findAllByQuery('SELECT * FROM `' . static::tablename() . '`');
	}

	public static function findByQuery($query, $params = false)
	{
		$q = $query;
		if($params !== false) {
			foreach($params as $k => $v) {
				$q = str_replace($k, nf_sql_encode($v), $q);
			}
		}
		$class = get_called_class();
		$res = nf_sql_query($q);
		if($res !== false) {
			$row = $res->fetch_assoc();
			if($row) {
				$ret = new $class();
				$ret->loadRow($row);
				return $ret;
			}
		}
		return false;
	}

	public static function findAllByQuery($query, $params = false)
	{
		$q = $query;
		if($params !== false) {
			foreach($params as $k => $v) {
				$q = str_replace($k, nf_sql_encode($v), $q);
			}
		}
		$class = get_called_class();
		$res = nf_sql_query($q);
		$ret = array();
		if($res !== false) {
			while($row = $res->fetch_assoc()) {
				$obj = new $class();
				$obj->loadRow($row);
				$ret[] = $obj;
			}
		}
		return $ret;
	}

	public static function countByQuery($query, $params = false)
	{
		$q = $query;
		if($params !== false) {
			foreach($params as $k => $v) {
				$q = str_replace($k, nf_sql_encode($v), $q);
			}
		}
		$res = nf_sql_query($q);
		$row = $res->fetch_assoc();
		return intval($row['c']);
	}
	
	public function insert()
	{
		$tablename = static::tablename();
		$pk_col = static::findPrimaryKey();
		$query = 'INSERT INTO `' . $tablename . '` (';
		$values = '';
		foreach($this->_data as $k => $v) {
			$query .= '`' . $k . '`,';
			$values .= nf_sql_encode($v) . ',';
		}
		$query = rtrim($query, ',');
		$query .= ') VALUES(' . rtrim($values, ',') . ')';
		$res = nf_sql_query($query);
		if($res !== false) {
			$this->_data[$pk_col] = nf_sql_insertid();
			$this->_loaded = true;
			return true;
		}
		return false;
	}
	
	public function save()
	{
		if(!$this->_loaded) {
			return $this->insert();
		}
		$tablename = static::tablename();
		$query = 'UPDATE `' . $tablename . '` SET ';
		foreach($this->_changed as $k) {
			$query .= '`' . $k . '`=' . nf_sql_encode($this->_data[$k]) . ',';
		}
		$query = rtrim($query, ',');
		$pk_col = static::findPrimaryKey();
		$pk = $this->_data[$pk_col];
		$query .= ' WHERE `' . $pk_col . '`=' . nf_sql_encode($pk);
		return nf_sql_query($query) !== false;
	}
	
	public function remove()
	{
		if(!$this->_loaded) {
			return false;
		}
		$tablename = static::tablename();
		$pk_col = static::findPrimaryKey();
		$pk = $this->_data[$pk_col];
		$query = 'DELETE FROM `' . $tablename . '` WHERE `' . $pk_col . '`=' . nf_sql_encode($pk);
		return nf_sql_query($query) !== false;
	}
	
	protected function lookupRelation($k, $v)
	{
		if(isset($this->_relationalrows[$k])) {
			return $this->_relationalrows[$k];
		}<commit_msg>Fixed save() trying to save when there are no changed values<commit_after><?php

class Model
{
	public $_data = array();
	public $_changed = array();
	public $_loaded = false;
	public $_relationalrows = array();
	public $_errors = array();
	
	public function relations()
	{
		return array();
	}
	
	public function rules()
	{
		return array();
	}
	
	public static function findPrimaryKey()
	{
		$tablename = static::tablename();
		$res = nf_sql_query('SHOW KEYS FROM `' . $tablename . '` WHERE Key_name = \'PRIMARY\'');
		$row = $res->fetch_assoc();
		if($row !== null) {
			return $row['Column_name'];
		}
		nf_error(9, $tablename);
		return false;
	}
	
	public static function findByPk($pk)
	{
		$class = get_called_class();
		return $class::findByQuery('SELECT * FROM `' . static::tablename() . '` WHERE `' . static::findPrimaryKey() . '`=' . nf_sql_encode($pk));
	}
	
	public static function findByAttributes($attributes)
	{
		$class = get_called_class();
		$query = 'SELECT * FROM `' . static::tablename() . '` ';
		if(count($attributes) > 0) {
			$query .= 'WHERE ';
			$firstAnd = false;
			foreach($attributes as $k => $v) {
				if($firstAnd) {
					$query .= 'AND ';
				}
				$firstAnd = true;
				$query .= '`' . $k . '`=' . nf_sql_encode($v) . ' ';
			}
		}
		$query .= 'LIMIT 0,1';
		return $class::findByQuery($query);
	}
	
	public static function findAllByAttributes($attributes)
	{
		$class = get_called_class();
		$query = 'SELECT * FROM `' . static::tablename() . '` ';
		if(count($attributes) > 0) {
			$query .= 'WHERE ';
			$firstAnd = false;
			foreach($attributes as $k => $v) {
				if($firstAnd) {
					$query .= 'AND ';
				}
				$firstAnd = true;
				$query .= '`' . $k . '`=' . nf_sql_encode($v) . ' ';
			}
		}
		return $class::findAllByQuery($query);
	}

	public static function countByAttributes($attributes)
	{
		$class = get_called_class();
		$query = 'SELECT COUNT(*) AS c FROM `' . static::tablename() . '` ';
		if(count($attributes) > 0) {
			$query .= 'WHERE ';
			$firstAnd = false;
			foreach($attributes as $k => $v) {
				if($firstAnd) {
					$query .= 'AND ';
				}
				$firstAnd = true;
				$query .= '`' . $k . '`=' . nf_sql_encode($v) . ' ';
			}
		}
		return $class::countByQuery($query);
	}
	
	public static function findAll()
	{
		$class = get_called_class();
		return $class::findAllByQuery('SELECT * FROM `' . static::tablename() . '`');
	}

	public static function findByQuery($query, $params = false)
	{
		$q = $query;
		if($params !== false) {
			foreach($params as $k => $v) {
				$q = str_replace($k, nf_sql_encode($v), $q);
			}
		}
		$class = get_called_class();
		$res = nf_sql_query($q);
		if($res !== false) {
			$row = $res->fetch_assoc();
			if($row) {
				$ret = new $class();
				$ret->loadRow($row);
				return $ret;
			}
		}
		return false;
	}

	public static function findAllByQuery($query, $params = false)
	{
		$q = $query;
		if($params !== false) {
			foreach($params as $k => $v) {
				$q = str_replace($k, nf_sql_encode($v), $q);
			}
		}
		$class = get_called_class();
		$res = nf_sql_query($q);
		$ret = array();
		if($res !== false) {
			while($row = $res->fetch_assoc()) {
				$obj = new $class();
				$obj->loadRow($row);
				$ret[] = $obj;
			}
		}
		return $ret;
	}

	public static function countByQuery($query, $params = false)
	{
		$q = $query;
		if($params !== false) {
			foreach($params as $k => $v) {
				$q = str_replace($k, nf_sql_encode($v), $q);
			}
		}
		$res = nf_sql_query($q);
		$row = $res->fetch_assoc();
		return intval($row['c']);
	}
	
	public function insert()
	{
		$tablename = static::tablename();
		$pk_col = static::findPrimaryKey();
		$query = 'INSERT INTO `' . $tablename . '` (';
		$values = '';
		foreach($this->_data as $k => $v) {
			$query .= '`' . $k . '`,';
			$values .= nf_sql_encode($v) . ',';
		}
		$query = rtrim($query, ',');
		$query .= ') VALUES(' . rtrim($values, ',') . ')';
		$res = nf_sql_query($query);
		if($res !== false) {
			$this->_data[$pk_col] = nf_sql_insertid();
			$this->_loaded = true;
			return true;
		}
		return false;
	}
	
	public function save()
	{
		if(!$this->_loaded) {
			return $this->insert();
		}
		if(count($this->_changed) == 0) {
			return true;
		}
		$tablename = static::tablename();
		$query = 'UPDATE `' . $tablename . '` SET ';
		foreach($this->_changed as $k => $v) {
			$query .= '`' . $k . '`=' . nf_sql_encode($v) . ',';
		}
		$query = rtrim($query, ',');
		$pk_col = static::findPrimaryKey();
		$pk = $this->_data[$pk_col];
		$query .= ' WHERE `' . $pk_col . '`=' . nf_sql_encode($pk);
		return nf_sql_query($query) !== false;
	}
	
	public function remove()
	{
		if(!$this->_loaded) {
			return false;
		}
		$tablename = static::tablename();
		$pk_col = static::findPrimaryKey();
		$pk = $this->_data[$pk_col];
		$query = 'DELETE FROM `' . $tablename . '` WHERE `' . $pk_col . '`=' . nf_sql_encode($pk);
		return nf_sql_query($query) !== false;
	}
	
	protected function lookupRelation($k, $v)
	{
		if(isset($this->_relationalrows[$k])) {
			return $this->_relationalrows[$k];
		}
{'fraction_non_alphanumeric': 0.21376339777621958, 'fraction_numerical': 0.0013022137633977762, 'mean_word_length': 3.19672131147541, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 60, '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': '4777604', 'n_tokens_mistral': 4370, 'n_tokens_neox': 3851, 'n_words': 741}